使用说明
MCP碰撞课程
一个教程项目,演示如何将MCP(模型上下文协议)服务器与LangChain和LangGraph集成。
概述
该项目连接多个MCP服务器,并使用LangChain的ReAct代理根据用户问题自动选择和使用适当的工具。
特性
- 数学服务器:数学运算工具(加法、乘法)
- 天气服务器:天气信息查找工具
- 多服务器客户端:同时连接和管理多个MCP服务器
- 反应代理:根据问题自动选择合适的工具
安装
-
需要Python 3.11或更高版本。
-
安装依赖项:
uv sync
或者如果使用pip:
pip install -r requirements.txt
设置
- 创建一个
.env文件并设置您的OpenAI API密钥:
OPENAI_API_KEY=your_api_key_here
- 检查中的路径
main.py.确保绝对路径math_server.py是正确的。
用法
1.启动天气服务器
在终端中将天气服务器作为单独的进程运行:
uv run python servers/weather_server.py
或者:
python servers/weather_server.py
此服务器将在 http://localhost:8000/sse.
2.运行主程序
在另一个终端中:
uv run python main.py
或者:
python main.py
例子
- 天气问题:
"what is the weather in nyc?" - 数学题:
"what is 5 + 3?"或"what is 10 * 7?"
您可以修改最后一行的问题 main.py 文件。
项目结构
mcp-crash-course/
├── main.py # Main execution file
├── langchain_client.py # LangChain client setup (similar to main.py)
├── servers/
│ ├── math_server.py # Math MCP server (stdio transport)
│ └── weather_server.py # Weather MCP server (sse transport)
├── pyproject.toml # Project dependencies
└── README.md # This file
技术栈
备注
- Math服务器使用stdio传输(进程通信)
- 天气服务器使用SSE(服务器发送事件)传输(基于HTTP)
- ReAct代理分析问题并自动选择适当的工具
