langchain-mcp-adapters 库使用在 MCP 服务器上定义的工具。
安装
安装@langchain/mcp-adapters 库以在 LangGraph 中使用 MCP 工具:
传输类型
MCP 支持不同的客户端-服务器通信传输机制:- stdio – 客户端将服务器作为子进程启动并通过标准输入/输出进行通信。最适合本地工具和简单设置。
- 可流式 HTTP – 服务器作为处理 HTTP 请求的独立进程运行。支持远程连接和多个客户端。
- 服务器发送事件(SSE) – 针对实时流式通信优化的可流式 HTTP 变体。
使用 MCP 工具
@langchain/mcp-adapters enables agents to use tools defined across one or more MCP server.
Accessing multiple MCP servers
MultiServerMCPClient is stateless by default. Each tool invocation creates a fresh MCP ClientSession, executes the tool, and then cleans up.自定义 MCP 服务器
To create your own MCP servers, you can use the@modelcontextprotocol/sdk library. This library provides a simple way to define tools and run them as servers.
Math server (stdio transport)
Weather server (SSE transport)
有状态工具使用
对于在工具调用之间维护上下文的有状态服务器,使用client.session() 创建持久的 ClientSession。
Using MCP ClientSession for stateful tool usage