天气查询与计算代理

一个基于Google Gemini 2.5 Flash模型的多代理系统,通过Model Context Protocol (MCP)服务器提供天气查询和计算功能。

  • by Ovi-Poddar
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 Ovi-Poddar/mcp-openweather

使用说明

气象代理与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.txt

requirements.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密钥

  1. 访问https://openweathermap.org/api
  2. 创建免费帐户
  3. 生成并复制API密钥(称为 appid)
  4. 注意: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密钥:

  1. 访问https://aistudio.google.com/app/apikey
  2. 创建新的API密钥
  3. 复制并粘贴到 .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工具还是直接来自模型。


运作原理

消息流

  1. 用户输入 →人道信息
  2. 双子座分析 → 决定调用哪个工具
  3. 带有工具调用的AIMessage → 列出工具名称(天气、计算器)
  4. 工具消息 → MCP服务器执行并返回数据
  5. 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

快乐查询! 🚀