这是一个基于LangChain和MCP (Model Control Protocol) 的智能客户端示例项目,展示了如何使用LangChain与多个MCP服务进行交互,实现智能对话和工具调用功能。
- 支持多服务器MCP客户端配置
- 集成LangChain的ReAct Agent功能
- 提供工具列表和资源列表查询
- 包含天气查询和产品知识库访问示例
- 支持异步操作
- Python >= 3.12
- 依赖包:详见pyproject.toml
- 克隆项目并安装依赖:
git clone <repository-url>
cd mcp-client
uv pip install .- 配置环境变量 创建.env文件,添加以下配置:
MODEL=<your-model-name>
BASE_URL=<your-base-url>
OPENAI_API_KEY=<your-api-key>- 配置MCP服务器 在config.json中配置MCP服务器信息:
{
"mcpServers": {
"SmartCustomerSupportMCP": {
"command": "uv",
"args": [
"--directory",
"/path/to/SmartCustomerSupportMCP",
"run",
"mcp-smart-customer-support"
]
},
"weather": {
"url": "http://localhost:8000/sse",
"transport": "sse"
}
}
}await list_all_tools()await list_all_resources()await get_weather_example()await read_product_knowledge_example()response = await run_mcp_client("北京天气")
print(response)MultiServerMCPClient: 多服务器MCP客户端类initialize_client(): 客户端初始化函数run_mcp_client(): ReAct Agent运行函数- 各种工具和资源访问示例函数
- 确保所有MCP服务器都已正确配置并运行
- 环境变量中的API密钥需要妥善保管
- 异步函数调用需要使用asyncio运行