@h1deya/langchain-mcp-tools
Version:
MCP To LangChain Tools Conversion Utility
65 lines (64 loc) • 2.3 kB
TypeScript
import { IOType } from "node:child_process";
import { Stream } from "node:stream";
import { StructuredTool } from "@langchain/core/tools";
interface CommandBasedConfig {
url?: never;
command: string;
args?: string[];
env?: Record<string, string>;
stderr?: IOType | Stream | number;
cwd?: string;
}
interface UrlBasedConfig {
url: string;
command?: never;
args?: never;
env?: never;
stderr?: never;
cwd?: never;
}
type McpServerConfig = CommandBasedConfig | UrlBasedConfig;
export interface McpServersConfig {
[key: string]: McpServerConfig;
}
export interface McpToolsLogger {
debug(...args: unknown[]): void;
info(...args: unknown[]): void;
warn(...args: unknown[]): void;
error(...args: unknown[]): void;
}
interface LogOptions {
logLevel?: "fatal" | "error" | "warn" | "info" | "debug" | "trace";
}
export interface McpServerCleanupFn {
(): Promise<void>;
}
/**
* Initializes multiple MCP (Model Context Protocol) servers and converts them into LangChain tools.
* This function concurrently sets up all specified servers and aggregates their tools.
*
* @param configs - A mapping of server names to their respective configurations
* @param options - Optional configuration settings
* @param options.logLevel - Log verbosity level ("fatal" | "error" | "warn" | "info" | "debug" | "trace")
* @param options.logger - Custom logger implementation that follows the McpToolsLogger interface.
* If provided, overrides the default Logger instance.
*
* @returns A promise that resolves to:
* - tools: Array of StructuredTool instances ready for use with LangChain
* - cleanup: Function to properly terminate all server connections
*
* @throws McpInitializationError if any server fails to initialize
*
* @example
* const { tools, cleanup } = await convertMcpToLangchainTools({
* filesystem: { command: "npx", args: ["-y", "@modelcontextprotocol/server-filesystem", "."] },
* fetch: { command: "uvx", args: ["mcp-server-fetch"] }
* });
*/
export declare function convertMcpToLangchainTools(configs: McpServersConfig, options?: LogOptions & {
logger?: McpToolsLogger;
}): Promise<{
tools: StructuredTool[];
cleanup: McpServerCleanupFn;
}>;
export {};