气象代理与MCP&Gemini
一个多智能体系统,使用 模型上下文协议(MCP) 服务器使用谷歌的Gemini 2.5 Flash模型回答天气和计算查询。
建筑
- AI模型:谷歌Gemini 2.5 Flash(免费版)
- 框架:LangGraph(代理工作流)
- MCP服务器:天气(Go)+计算器(Python)
- 工具:通过tool_use功能绑定到Gemini
______________________________________________________________________
先决条件
- Python 3.10+ (或Python 3.14)
- 转到1.16+ (用于构建天气服务器)
- Git (用于克隆存储库)
- 点 (Python包管理器)
______________________________________________________________________
安装说明
第一步:创建Python虚拟环境
python -m venv venv
# On Windows:
.\venv\Scripts\Activate.ps1
# On macOS/Linux:
source venv/bin/activate步骤2:安装Python依赖项
pip install -r requirements.txtrequirements.txt包括:
python-dotenv-环境变量管理langchain-google-genai-Gemini集成langchain-mcp-adapters-MCP协议支持langgraph-代理工作流编排mcp-MCP协议实现mcp-server-calculator-计算器MCP服务器
______________________________________________________________________
设置MCP服务器
气象MCP服务器(Go)
步骤1:安装Go
从以下位置下载并安装:https://go.dev/dl/
步骤2:克隆和构建
git clone https://github.com/mschneider82/mcp-openweather.git
cd mcp-openweather
go build -o mcp-weather.exe步骤3:获取OpenWeatherMap API密钥
- 访问https://openweathermap.org/api
- 创建免费帐户
- 生成并复制API密钥(称为
appid) - 注意:API密钥可能需要几个小时才能激活
步骤4:验证可执行文件
建造完成后,确认 mcp-weather.exe 存在于 mcp-openweather 文件夹。
______________________________________________________________________
计算器MCP服务器(Python)
计算器服务器通过pip安装:
pip install mcp-server-calculator这为代理提供了算术和计算能力。
______________________________________________________________________
配置
环境变量
创建一个 .env 项目根目录中的文件:
GOOGLE_API_KEY=your_gemini_api_key_here
OWM_API_KEY=your_openweathermap_api_key_here获取Gemini API密钥:
- 访问https://aistudio.google.com/app/apikey
- 创建新的API密钥
- 复制并粘贴到
.env
______________________________________________________________________
运行代理
终端设置(需要3个终端)
终端1——气象MCP服务器:
cd mcp-openweather
.\mcp-weather.exe⚠️ 让它在后台运行。
终端2–计算器MCP服务器:
python -m mcp_server_calculator⚠️ 让它在后台运行。
3号航站楼——您的客户(主代理):
# Activate virtual environment (if not already active)
.\venv\Scripts\Activate.ps1
# Run the agent
python weather_mcp.py______________________________________________________________________
使用代理
运行后,您将看到提示:
--- Weather Query ---
Ask me anything (weather or calculation) → 查询示例
天气:
- “伦敦今天天气怎么样?”
- “东京现在的温度?”
- “孟加拉国达卡的天气?”
计算:
- “152乘以8是多少?”
- “计算1000除以25”
- “99+101是什么?”
混合的:
- “柏林的天气怎么样,计算一下华氏5°C有多冷?”
调试输出
代理提供详细的调试信息:
[DEBUG] Message audit:
[0] HumanMessage
[1] AIMessage with tool_calls: ['weather']
[2] ToolMessage (tool executed successfully)
[3] AIMessage (no tool calls - model answered directly)
[DEBUG] Tool used: YES这证实了答案是来自MCP工具还是直接来自模型。
______________________________________________________________________
运作原理
消息流
- 用户输入 →人道信息
- 双子座分析 → 决定调用哪个工具
- 带有工具调用的AIMessage → 列出工具名称(天气、计算器)
- 工具消息 → MCP服务器执行并返回数据
- A消息(综合) → Gemini为用户格式化最终答案
工具集成
- 天气工具:返回当前天气、温度、湿度、风、压力
- 计算器工具:执行数学运算
- 签名:当Gemini收到工具数据时,它会在响应中签名
'extras': {'signature': ...}
______________________________________________________________________
文件结构
mcp-openweather/
├── weather_mcp.py # Main agent script
├── .env # API keys (not in repo)
├── requirements.txt # Python dependencies
├── README.md # This file
├── mcp-openweather/
│ ├── main.go # Weather server source
│ ├── mcp-weather.exe # Built executable
│ └── go.mod # Go dependencies
└── venv/ # Virtual environment______________________________________________________________________
快乐查询! 🚀
