使用说明
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工具。
- 🔢
- 🚀
快速启动
- 激活Python venv并安装依赖项(从repo根目录):
source ".venv/Scripts/activate"
python -m pip install -r requirements.txt
- 检查
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",
}
}
)
- 检查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")
