MCP客户端Langgraph集成

一个演示如何通过langchain_mcp_adapters在LangChain/LangGraph中集成MCP工具的微型项目,包含远程HTTP服务器和本地计算器服务器的示例。

  • by anjalbhattarai79
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 anjalbhattarai79/mcp-client-langgraph

使用说明

Mcp客户端语言

一个小演示,展示如何使用模型上下文协议(MCP) langchain / langgraph 通过 langchain_mcp_adapters.

要点:

  • 🧠 使用以下命令将MCP工具集成到LangChain/LangGraph中 langchain_mcp_adapters
  • ⚙️ 演示中的两个MCP服务器 chatbot_mcp.py:
    • 🚀 basic-demo --一个远程流式HTTP服务器(FastMCP演示),通过名字问候用户。
      • 🔢 math --作为子进程生成的基于stdio的本地计算器服务器(代码可在 arith_server_copy.py)与 add 和 multiply 工具。

快速启动

  1. 激活Python venv并安装依赖项(从repo根目录):
source ".venv/Scripts/activate"
python -m pip install -r requirements.txt
  1. 检查 chatbot_mcp.py 查看服务器的配置方式:
client = MultiServerMCPClient(
	{
		"math": {
			"transport": "stdio",
			"command": "python",
			# We include a copy of the math server in this repo
			"args": [r"./arith_server_copy.py"],
		},
		"basic-demo": {
			"transport": "streamable_http",
			"url": "https://demo-server.fastmcp.app/mcp",
			
		}
	}
)
  1. 检查arith_server_copy.py,了解如何在stdio传输中配置和使用数学服务器代码:
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("Math")

@mcp.tool()
def add(a: int, b: int) -> int:
    """Add two numbers"""
    return a + b

@mcp.tool()
def multiply(a: int, b: int) -> int:
    """Multiply two numbers"""
    return a * b

if __name__ == "__main__":
    mcp.run(transport="stdio")