使用MCP的数学代理
构建一个可以使用不同mcp服务器的数学代理。
🚀 特性
- 多MCP集成:使用多个MCP服务器进行不同的数学运算
- 会话上下文:维护多个查询的对话历史记录
- 智能刀具选择:根据用户查询自动选择合适的工具
- 错误处理:稳健的错误处理和适当的验证
- 可扩展架构:易于添加新的MCP服务器和数学运算
🏗️ 建筑
该系统由以下部分组成:
- 主要代理人 (
client.py):基于LangGraph的代理,协调多个MCP服务器 - 基础数学服务器 (
basic_math_server.py):基本算术运算 - 高级数学服务器 (
advanced_math_server.py):电力操作和高等数学
┌─────────────────┐
│ Math Agent │
└─────────┬───────┘
│
├── Basic Math MCP Server
│ ├── Addition
│ ├── Subtraction
│ ├── Multiplication
│ └── Division
│
└── Advanced Math MCP Server
├── Power/Exponentiation
├── Square
├── Cube
└── Square Root📋 先决条件
- Python 3.8+
- 谷歌云凭据(适用于Vertex AI Gemini模型)
- MCP服务器运行能力
🛠️ 安装
- 克隆存储库:
git clone
cd Math_Agent_MCP- 安装依赖项:
pip install -r requirements.txt- 设置Google Cloud凭据:
# Option 1: Using gcloud CLI
gcloud auth application-default login
# Option 2: Using service account key
export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/service-account-key.json"🚀 用法
基本用法
运行主演示:
python client.py这将启动数学代理,并演示跨多个查询的各种数学操作。
交互示例
演示包括几个示例查询:
- 基本算术:
"What's (3 + 5) × 12?" - 背景跟进:
"Now I want to get the square of that result" - 高级操作:
"What's 2^8 + 15?"
样本输出
🧮 LangGraph Multi-MCP Math Agent Demo
========================================
🔍 Query 1: What's (3 + 5) × 12?
----------------------------------------
Elementary Math: 3 + 5 = 8
Elementary Math: 8 × 12 = 96
AI: The result of (3 + 5) × 12 is 96.
🔍 Query 2: Now I want to get the square of that result
----------------------------------------
Advanced Math: 96² = 9216
AI: The square of 96 is 9,216.
🔍 Query 3: What's 2^8 + 15?
----------------------------------------
Advanced Math: 2^8 = 256
Elementary Math: 256 + 15 = 271
AI: 2^8 + 15 equals 271.📁 项目结构
├── client.py # Main LangGraph agent
├── basic_math_server.py # Basic math operations MCP server
├── advanced_math_server.py # Advanced math operations MCP server
├── requirements.txt # Python dependencies
└── README.md # This file🔧 可用操作
初等数学服务器
- 加法:
add(a, b)-将两个整数相加 - 减法:
subtract(a, b)-减去两个整数 - 乘法:
multiply(a, b)-将两个整数相乘 - 部门:
divide(a, b)-除以商和余数
高级数学服务器
- 力量:
power(base, exponent)-计算基数指数 - 方形:
square(number)-计算数字² - 立方体:
cube(number)-计算数量³ - 平方根:
square_root(number)-计算√数
🎯 关键组件
MathAgent类
地心末日 MathAgent 类提供:
- 初始化:设置MCP客户端和工具
- 图形构建:创建LangGraph工作流
- 查询处理:根据上下文处理单个查询
- 消息管理:格式化和显示对话历史记录
LangGraph工作流
代理使用简单但有效的工作流程:
- 模型调用:LLM分析查询并决定工具的使用
- 工具执行:如果需要工具,请通过MCP服务器执行它们
- 响应生成:根据工具结果生成最终响应
- 连续逻辑:确定是否需要更多的工具调用
🔄 扩展系统
添加新的MCP服务器
- 创建新的MCP服务器:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("Your Server Name")
@mcp.tool()
def your_function(param: int) -> int:
"""Your function description"""
return param * 2
if __name__ == "__main__":
mcp.run(transport="stdio")- 添加到客户端配置:
server_config = {
"your_server": {
"command": "python",
"args": ["your_server.py"],
"transport": "stdio",
},
# ... existing servers
}添加新操作
只需添加新 @mcp.tool() 将装饰函数添加到相应的服务器文件中。
🐛 故障排除
常见问题
- 谷歌云身份验证:
# Verify authentication
gcloud auth list
gcloud config list- MCP服务器连接问题:
- 确保Python脚本可执行 - 检查MCP服务器是否正常启动 - 验证stdio传输是否正常工作
- 工具未找到错误:
- 检查所有MCP服务器是否配置正确 - 验证服务器和客户端之间的工具名称是否匹配
调试模式
通过修改日志级别启用详细日志记录:
logging.basicConfig(level=logging.DEBUG)📊 性能注意事项
- 工具缓存:初始化期间加载一次MCP工具
- 消息上下文:对话历史记录随着每次查询而增长
- 并行操作:MCP服务器异步处理请求
