LangChain MCP 工具转换实用程序

LangChain MCP 工具转换实用程序

将MCP工具转换为LangChain兼容工具的TypeScript实用程序。

快速入门

安装

运行以下命令安装:

npm i @h1deya/langchain-mcp-tools

使用方法

  1. 配置MCP服务器,例如:

const mcpServers = { filesystem: { command: "npx", args: ["-y", "@modelcontextprotocol/server-filesystem", "."] } };

  1. 调用 convertMcpToLangchainTools 函数初始化并获取工具:

const { tools, cleanup } = await convertMcpToLangchainTools(mcpServers);

  1. 在LangChain中使用返回的工具,例如:

const llm = new ChatAnthropic({ model: "claude-3-7-sonnet-latest" }); const agent = createReactAgent({ llm, tools });

  1. 使用完成后调用清理函数:

await cleanup();

更多详细信息请参考示例代码和文档。