天气服务

基于Model-Context-Protocol (MCP)构建的天气服务,提供印度城市的当前天气和天气预报。

  • by Sunilk240
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 sunilk240/mcp-server

使用说明

MCP气象服务

使用模型上下文协议(MCP)构建的天气服务,使用WeatherAPI.com为印度城市提供当前天气和预报。

先决条件

  • Python 3.13或更高版本
  • 紫外线 包管理器
  • WeatherAPI.com API密钥
  • Groq API密钥(用于聊天界面)
  • Claude for Desktop(用于与Claude一起测试)

安装

  1. 克隆存储库:
git clone <your-repo-url>
cd mcp-server
  1. 创建一个 .env 使用API密钥在项目根目录中创建文件:
# WeatherAPI.com API Key
WEATHER_API_KEY=your_weather_api_key_here

# Groq API Key
GROQ_API_KEY=your_groq_api_key_here
  1. 使用uv安装依赖项:
uv pip install -e .

运行服务

发展模式

要在开发模式下运行气象服务:

uv run mcp dev server/weather.py

安装并运行

要在桌面claude上安装并运行该服务,请执行以下操作:

uv run mcp install server/weather.py

设置weather.json

这 weather.json 聊天界面需要配置文件。

1、使用Claude for Desktop测试您的服务器:

  • 打开VS Code并运行:
    code $env:AppData\Claude\claude_desktop_config.json
    
  • 添加以下配置:
    {
        "mcpServers": {
            "weather": {
                "command": "uv",
                "args": [
                    "--directory",
                    "C:\\ABSOLUTE\\PATH\\TO\\YOUR\\mcp-server",
                    "run",
                    "server/weather.py"
                ]
            }
        }
    }
    
  • 替换 C:\\ABSOLUTE\\PATH\\TO\\YOUR\\mcp-server 使用您的实际项目路径
  1. 对于聊天界面(client.py):

    • 这 weather.json 文件应该在你的 server 目录
    • 确保它包含:
      {
          "mcpServers": {
              "weather": {
                  "command": "uv",
                  "args": [
                      "--directory",
                      "C:\\ABSOLUTE\\PATH\\TO\\YOUR\\mcp-server",
                      "run",
                      "server/weather.py"
                  ]
              }
          }
      }
      
    • 替换 C:\\ABSOLUTE\\PATH\\TO\\YOUR\\mcp-server 使用您的实际项目路径
  2. 更改后重新启动Claude for Desktop

使用聊天界面

运行聊天客户端与天气服务进行交互:

python server/client.py

聊天界面支持:

  • 获取印度任何城市的当前天气
  • 获取三天天气预报
  • 关于天气的自然语言查询

命令:

  • 键入“exit”或“quit”结束对话
  • 键入“clear”以清除对话历史记录

特性

  • 当前天气信息:

    • 温度(摄氏度)
    • 天气条件
    • 湿度
    • 风速
    • 上次更新时间戳
  • 三天天气预报:

    • 每日高温和低温
    • 天气情况
    • 特定日期的预测

项目结构

mcp-server/
├── server/
│   ├── weather.py      # Weather service implementation
│   ├── client.py       # Chat interface
│   └── weather.json    # MCP server configuration
├── .env               # API keys configuration
├── pyproject.toml     # Project dependencies
└── README.md         # This file

API密钥

  1. WeatherAPI.com:

  2. 绿色API:

    • 注册地址: Groq 控制台
    • 获取API密钥
    • 添加到 .env 像 GROQ_API_KEY

贡献

请随时提交问题和增强请求!