Weather MCP Tool

by SaintDoresh
Verified

local-only server

The server can only run on the client’s local machine because it depends on local resources.

Integrations

  • Enables secure storage and loading of the OpenWeatherMap API key through environment variables, keeping sensitive data out of the codebase.

  • Offers a comprehensive tutorial on setting up and using the Weather MCP Tool with Claude Desktop, published on the Medium platform.

  • Requires Python 3.10+ as the runtime environment for the MCP server that processes weather data requests.

Claude 桌面版的天气 MCP 工具

一种 MCP(模型上下文协议)工具,使用 OpenWeatherMap API 提供实时天气数据、预报和历史天气信息,专为 Claude Desktop 设计。

教程

有关设置和使用此工具的详细指南,请查看我们全面的 Medium 教程: 教程:使用 Claude Desktop 和 Weather MCP 工具访问全球实时天气数据

特征

  • 任何地点的实时天气状况
  • 天气预报(最多 5 天)
  • 历史天气数据(最近5天)
  • 空气质量信息
  • 天气警报和警告
  • 位置搜索功能

设置

  1. 确保安装了 Python 3.10 或更高版本
  2. 安装依赖项:
pip install -r requirements.txt
  1. OpenWeatherMap获取 API 密钥(提供免费套餐)
    • 注意:新的 API 密钥可能需要最多 24 小时才能激活
  2. 设置 API 密钥的环境变量(推荐方法):
    • 在项目目录中创建.env文件
    • 将您的 API 密钥添加到文件:GXP2
    • .env添加到.gitignore文件以防止提交敏感数据
  3. 更新main.py以使用环境变量:
    # Add these imports at the top import os from dotenv import load_dotenv # Load environment variables load_dotenv() # Replace the API_KEY line with API_KEY = os.getenv("OPENWEATHER_API_KEY")

与 Claude Desktop 集成

  1. 通过将以下内容添加到您的 MCP 配置中,在 Claude Desktop 中配置您的 MCP 设置:
{ "mcpServers": { "weather-mcp": { "command": "py", "args": ["-3.13", "C:\\Path\\To\\Your\\Weather-MCP-ClaudeDesktop\\main.py"] } } }
  1. 将路径替换为 main.py 文件的完整路径
  2. 使用以下命令运行服务器:
py -3.13 main.py
  1. 使用 Claude Desktop 时保持服务器运行

可用工具

1. 获取当前天气

获取某个地点的实时天气状况:

{ "location": { "name": "New York", "country": "US", "lat": 40.7128, "lon": -74.006 }, "temperature": { "current": 25.6, "feels_like": 26.2, "min": 23.4, "max": 27.8 }, "weather_condition": { "main": "Clear", "description": "clear sky", "icon": "01d" }, "wind": { "speed": 3.6, "deg": 220 }, "clouds": 5, "humidity": 65, "pressure": 1015, "visibility": 10000, "sunrise": "2025-03-16T06:12:34", "sunset": "2025-03-16T18:04:23", "timestamp": "2025-03-16T14:30:00" }

2. 获取天气预报

获取某个地点的天气预报:

{ "location": { "name": "London", "country": "GB", "lat": 51.5074, "lon": -0.1278 }, "forecast": [ { "datetime": "2025-03-16T15:00:00", "temperature": { "temp": 18.2, "feels_like": 17.8, "min": 17.5, "max": 19.1 }, "weather_condition": { "main": "Rain", "description": "light rain", "icon": "10d" }, "wind": { "speed": 4.2, "deg": 180 }, "clouds": 75, "humidity": 82, "pressure": 1010, "visibility": 8000, "pop": 0.4 } // ... more forecast items ], "days": 5 }

3. 获取空气质量

获取某个地点的空气质量数据:

{ "location": { "name": "Beijing", "country": "CN", "lat": 39.9042, "lon": 116.4074 }, "air_quality_index": 3, "air_quality_level": "Moderate", "components": { "co": 250.34, "no": 0.5, "no2": 15.2, "o3": 140.8, "so2": 5.1, "pm2_5": 8.2, "pm10": 12.3, "nh3": 0.7 }, "timestamp": "2025-03-16T14:30:00" }

4. 获取历史天气

获取特定日期的历史天气数据:

{ "location": { "name": "Paris", "country": "FR", "lat": 48.8566, "lon": 2.3522 }, "date": "2025-03-14", "temperature": { "temp": 20.3, "feels_like": 19.8 }, "weather_condition": { "main": "Clouds", "description": "scattered clouds", "icon": "03d" }, "wind": { "speed": 2.8, "deg": 150 }, "clouds": 45, "humidity": 60, "pressure": 1012, "visibility": 10000, "sunrise": "2025-03-14T06:30:45", "sunset": "2025-03-14T18:15:22", "timestamp": "2025-03-14T12:00:00" }

5. 搜索位置

按名称搜索地点:

{ "results": [ { "name": "Tokyo", "state": "", "country": "JP", "lat": 35.6762, "lon": 139.6503 }, { "name": "Tokyo", "state": "Tokyo", "country": "JP", "lat": 35.6895, "lon": 139.6917 } // ... more results ] }

6. 获取天气警报

获取某个位置的天气警报:

{ "location": { "name": "Miami", "country": "US", "lat": 25.7617, "lon": -80.1918 }, "alerts": [ { "sender": "NWS Miami", "event": "Heat Advisory", "start": "2025-03-16T12:00:00", "end": "2025-03-16T20:00:00", "description": "Heat index values between 105 and 110 expected", "tags": ["Extreme temperature value"] } // ... more alerts if any ], "alert_count": 1 }

示例查询

您可以向 Claude Desktop 询问以下问题:

  • “纽约现在的天气怎么样?”
  • “显示伦敦未来 5 天的天气预报”
  • “今天北京的空气质量怎么样?”
  • “3月14日巴黎的天气怎么样?”
  • “搜索名为‘东京’的地点”
  • “迈阿密有天气预警吗?”
  • “比较芝加哥、迈阿密和西雅图的当前天气”
  • “给我看看北京、洛杉矶和德里的空气质量对比”

错误处理

所有工具都包含适当的错误处理,如果出现问题,将返回错误消息:

{ "error": "Failed to fetch current weather for InvalidLocation: Location not found" }

故障排除

如果 MCP 服务器在 Claude Desktop 中无法运行:

  1. 确保服务器正在运行 - 启动脚本时您应该看到输出
  2. 验证设置中的路径是否正确且绝对
  3. 确保 Python 3.10+ 位于你的系统 PATH 中
  4. 检查所有依赖项是否已安装
  5. 尝试重启 Claude Desktop
  6. 检查日志中是否有任何错误消息

常见 API 问题

  • API 密钥激活:新的 OpenWeatherMap API 密钥可能需要长达 24 小时才能激活
  • 无效 API 密钥错误:如果收到 401 错误,请验证您的 API 密钥是否正确且有效
  • 速率限制:免费套餐允许每分钟最多 60 次通话,在高使用率时可能会超过此限制

速率限制

此工具使用 OpenWeatherMap API,该 API 具有速率限制。免费套餐每分钟最多允许 60 次调用,这足以满足个人使用需求。请注意,非常频繁的请求可能会受到 API 的限制。

执照

MIT 许可证

-
security - not tested
A
license - permissive license
-
quality - not tested

为 Claude Desktop 提供来自 OpenWeatherMap API 的实时天气数据、预报和历史天气信息,支持通过自然语言查询全球当前状况、预报、空气质量和天气警报。

  1. Tutorial
    1. Features
      1. Setup
        1. Integration with Claude Desktop
          1. Available Tools
            1. 1. get_current_weather
            2. 2. get_weather_forecast
            3. 3. get_air_quality
            4. 4. get_historical_weather
            5. 5. search_location
            6. 6. get_weather_alerts
          2. Sample Queries
            1. Error Handling
              1. Troubleshooting
                1. Common API Issues
              2. Rate Limits
                1. License
                  ID: vh7tgzyyy2