天气服务MCP服务器

一个提供实时天气数据、天气预报和城市间天气比较的MCP服务器,适用于AI助手集成。

  • by Abhishek3689
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 Abhishek3689/Weather_MCP_Server

使用说明

气象MCP服务器🌤️

提供天气信息工具的模型上下文协议(MCP)服务器。该服务器使克劳德等人工智能助手能够获取实时天气数据、预报,并比较不同城市的天气。

特性

  • 当前天气:获取任何城市的实时天气信息
  • 天气预报:获取最多10天的天气预报(基于API计划)
  • 天气比较:比较两个城市的天气状况
  • 支持摄氏(公制)和华氏(英制)单位

先决条件

  • Python 3.8或更高版本
  • WeatherAPI.com API密钥(提供免费层)

安装

  1. 克隆存储库

    git clone https://github.com/Abhishek3689/Weather_MCP_Server.git
    cd weather-mcp-server
    
  2. 安装依赖项

    pip install -r requirements.txt
    
  3. 设置环境变量

    创建一个 .env 项目根目录中的文件:

    WEATHER_API_KEY=your_api_key_here
    

    从获取您的免费API密钥 WeatherAPI.com

用法

🚀 运行服务器(重要!) 👉 如果双击或运行该文件,则不会看到结果。

MCP服务器不直接显示输出。

他们等待克劳德与他们取得联系。

但您仍然可以手动启动服务器以确保其正常工作:

python weather_server_mcp.py

您会看到这样的情况,这意味着服务器运行正常

          ╭──────────────────────────────────────────────────────────────────────────────╮
          │                                                                              │
          │                         ▄▀▀ ▄▀█ █▀▀ ▀█▀ █▀▄▀█ █▀▀ █▀█                        │
          │                         █▀  █▀█ ▄▄█  █  █ ▀ █ █▄▄ █▀▀                        │
          │                                                                              │
          │                                FastMCP 2.13.1                                │
          │                                                                              │
          │                                                                              │
          │                    🖥  Server name: Weather Server                            │
          │                                                                              │
          │                    📦 Transport:   STDIO                                     │
          │                                                                              │
          │                    📚 Docs:        https://gofastmcp.com                     │
          │                    🚀 Hosting:     https://fastmcp.cloud                     │
          │                                                                              │
          ╰──────────────────────────────────────────────────────────────────────────────╯

与Claude Desktop集成

要将此服务器与Claude Desktop一起使用,请将以下内容添加到您的Claude Desktop配置中:

在macOS上: ~/Library/Application Support/Claude/claude_desktop_config.json

在Windows上: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "weather": {
      "command": "python",
      "args": [
        "weather_server_mcp.py"
      ],
      "env": {
        "WEATHER_API_KEY": "your_api_key_here"
      }
    }
  }
}

注: 对于“command”,通过在命令提示符中检查来写入绝对位置,而不仅仅是“python”

where python

你会看到这样的事情

C:\Users\YourName\AppData\Local\Programs\Python\Python312\python.exe

更新配置后,重新启动Claude Desktop。

可用工具

1.获取当前天气

get_current_weather(city: str, units: str = "metric")
  • 城市:城市名称(例如,“伦敦”、“纽约”、“东京”)
  • 单位:摄氏度用“公制”表示,华氏度用“英制”表示

例子:

get_current_weather("London", "metric")

2.获取天气预报

get_forecast(city: str, days: int = 3, units: str = "metric")
  • 城市:城市名称
  • 天:预测天数(1-10)
  • 单位:温度单位

例子:

get_forecast("Paris", days=5, units="metric")

3.比较天气

compare_weather(city1: str, city2: str, units: str = "metric")
  • 城市1:第一个城市名称
  • 城市2:第二个城市名称
  • 单位:温度单位

例子:

compare_weather("Tokyo", "New York", "metric")

项目结构

weather-mcp-server/
├── weather_server_mcp.py   # Main MCP server implementation
├── requirements.txt        # Python dependencies
├── .env                    # Environment variables (not committed)
├── .gitignore             # Git ignore file
└── README.md              # This file

api信息

该项目使用 WeatherAPI.com 服务:

  • 免费套餐:每月1000000次通话
  • 免费计划3天预测
  • 实时天气数据
  • 历史天气数据

示例输出

Current Weather in London, United Kingdom

📍 Location: 51.52, -0.11
🌡️  Temperature: 15°C
🤔 Feels Like: 14°C
☁️  Condition: Partly cloudy
💧 Humidity: 72%
🔽 Pressure: 1013.0 hPa
💨 Wind Speed: 15.1 kph
☁️  Cloudiness: 25%
👁️  Visibility: 10.0 km

贡献

欢迎投稿!请随时提交拉取请求。

  1. 复刻仓库
  2. 创建功能分支(git checkout -b feature/AmazingFeature)
  3. 提交您的更改(git commit -m 'Add some AmazingFeature')
  4. 推到分支(git push origin feature/AmazingFeature)
  5. 打开拉取请求

许可证

此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。

故障排除

“未设置WEATHER_API_KEY环境变量”

  • 确保您已创建 .env 使用API密钥文件
  • 或者在系统/Claude配置中设置环境变量

“HTTP错误:401”

  • 检查您的API密钥是否有效
  • 确保API密钥在您的 .env 文件

“只有X天可用”

  • WeatherAPI.com免费版提供3天天气预报
  • 升级到付费计划以进行扩展预测

支持

如果您遇到任何问题或有疑问:

作者

项目链接: https://github.com/Abhishek3689/Weather_MCP_Server

致谢