OpenWeather MCP Server

OpenWeather MCP Server

site icon
2025.03.17 4
Python天气预报天气服务位置服务
OpenWeather MCP Server 是一个简单的天气预报 MCP(模型控制协议)服务器,提供全球天气预报和当前天气状况。它支持查询世界各地的天气情况,提供当前天气和未来预报,包括温度、湿度、风速等详细信息,并支持不同时区。
View on GitHub

Overview

基本能力

产品定位

OpenWeather MCP Server 是一个天气预报服务,旨在通过 MCP 协议提供全球天气数据。

核心功能

  • 无需单独的配置文件,API 密钥可以通过环境变量或参数直接传递
  • 支持查询世界各地的天气情况
  • 提供当前天气和未来预报
  • 详细的天气信息,包括温度、湿度、风速等
  • 支持不同时区

适用场景

  • 需要集成天气信息的 AI 助手
  • 需要实时天气数据的应用程序
  • 需要全球天气预报的服务

工具列表

  1. get_weather: 获取指定位置的当前天气和预报
  2. 参数: location(位置名称)、api_key(OpenWeatherMap API 密钥)、timezone_offset(时区偏移)
  3. get_current_weather: 获取指定位置的当前天气
  4. 参数: location(位置名称)、api_key(OpenWeatherMap API 密钥)、timezone_offset(时区偏移)

常见问题解答

  • 错误: "No API key provided": 确保已设置 OPENWEATHER_API_KEY 环境变量或在调用工具时提供 api_key 参数。
  • 错误: "Location not found": 尝试使用更准确的城市名称或添加国家代码,例如 "Beijing,CN" 或 "Paris,FR"。

使用教程

使用依赖

pip install mcp-server requests pydantic

安装教程

  1. 获取 OpenWeatherMap API 密钥 访问 OpenWeatherMap 并注册账户以获取 API 密钥。

  2. 运行服务器

  3. 方法 1: 使用环境变量 ```bash # 设置环境变量 export OPENWEATHER_API_KEY="your_api_key" # Linux/Mac set OPENWEATHER_API_KEY=your_api_key # Windows

    # 运行服务器 python weather_mcp_server.py - **方法 2: 在调用工具时提供**bash python weather_mcp_server.py `` 调用工具时需要提供api_key` 参数。

  4. 在 MCP 客户端配置中使用 json { "weather_forecast": { "command": "python", "args": [ "/full_path/weather_mcp_server.py" ], "env": { "OPENWEATHER_API_KEY": "your_openweathermap_key_here" }, "disabled": false, "autoApprove": ["get_weather", "get_current_weather"] } }

调试方式

  • 确保 API 密钥正确设置
  • 检查位置名称是否正确
  • 验证服务器是否正常运行

示例

User: What's the weather like in New York right now?

AI: Let me check the current weather in New York for you.
[Calling get_current_weather("New York", timezone_offset=-4)]

Current weather in New York: 18°C, partly cloudy, humidity 65%, wind speed 3.5m/s.

许可证

该项目遵循 MIT 开源许可条款,请参阅 MIT 了解完整条款。