天气预测服务器

一个基于Model Context Protocol (MCP)的服务器,通过National Weather Service API提供天气预测和警报服务。

  • by MinhHieu-Nguyen-dn
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 minhhieu-nguyen-dn/weather-mcp-server

使用说明

气象MCP服务器

模型上下文协议(MCP)服务器,使用美国国家气象局API提供天气预报和警报。

按照官方规定建造 模型上下文协议服务器开发指南

快速入门(Mac/Linux)

1.克隆和设置

# Clone the repository to your preferred location
git clone <repository-url> ~/weather-mcp
cd ~/weather-mcp

# Check the project structure
ls -la
# You should see: main.py, weather.py, pyproject.toml, README.md, etc.

2.配置MCP客户端

将此配置添加到您的MCP客户端(例如Gemini CLI、Claude Desktop等):

{
  "mcpServers": {
    "weather": {
      "command": "uv",
      "args": [
        "--directory",
        "~/weather-mcp",
        "run",
        "weather.py"
      ]
    }
  }
}

重要:替换 ~/weather-mcp 使用克隆存储库的实际路径。例如:

  • 如果克隆到主目录: "/home/yourusername/weather-mcp"
  • 如果克隆到项目文件夹: "/home/yourusername/projects/weather-mcp"

3.监控服务器活动

服务器记录所有活动,以帮助您了解发生了什么:

# Navigate to your cloned repository
cd ~/weather-mcp

# Watch server logs in real-time
tail -f weather_mcp.log

在使用MCP服务器时保持该终端打开,以查看天气请求、API调用和任何错误的实时日志。

运作原理

此MCP服务器充当您的人工智能客户端与美国国家气象局API之间的桥梁:

  1. 您的AI客户端 通过stdio向MCP服务器发送请求
  2. MCP服务器 处理请求并对weather.gov进行API调用
  3. 天气数据 以结构化格式返回给您的AI客户端
  4. 所有活动 已登录到 weather_mcp.log 用于调试和监控

特性

  • 天气预报:使用纬度/经度坐标获取任何位置的详细天气预报
  • 天气警报:检索美国任何州的活动天气警报
  • 服务器信息:获取有关服务器配置和功能的详细信息
  • 综合录井:内置日志记录到控制台和文件,并带有进度报告

开发设置

先决条件

  • 已安装Python 3.11+
  • uv 包管理器(安装uv)

地方发展

# 1. Clone the repository (if not already done)
git clone <repository-url> ~/weather-mcp-dev
cd ~/weather-mcp-dev

# 2. Install dependencies
uv sync

# 3. Test the server locally
uv run python weather.py

# 4. In another terminal, monitor logs
tail -f weather_mcp.log

进行更改

  1. 编辑代码:修改 weather.py 或根据需要提供其他文件
  2. 测试您的更改:运行 uv run python weather.py 在本地进行测试
  3. 检查日志:监视器 weather_mcp.log 对于任何问题
  4. 更新您的MCP客户端:重新启动MCP客户端以获取更改

项目结构

weather-mcp/
├── weather.py          # Main MCP server implementation
├── main.py            # Alternative entry point
├── pyproject.toml     # Project configuration and dependencies
├── weather_mcp.log    # Server logs (created when running)
├── LOGGING.md         # Detailed logging documentation
└── README.md          # This file

测试工具

一旦服务器在MCP客户端中运行,您就可以测试这些工具:

get_forecast(latitude: float, longitude: float)

获取特定地点的详细天气预报。

例子:

get_forecast(40.7128, -74.0060)  # New York City

get_alerts(state: str)

获取美国某个州的活跃天气警报(2个字母的州代码)。

例子:

get_alerts("CA")  # California alerts

server_info()

获取有关服务器配置和功能的信息。

配置详情

服务器使用具有以下设置的美国国家气象局API:

  • 用户代理: weather-app/1.0
  • 基本URL: https://api.weather.gov
  • 超时:30秒
  • 认证:不需要(公共API)

故障排除

常见问题

  1. 服务器无法启动:检查一下 uv 已安装,MCP配置中的路径正确
  2. 无天气数据:确保您有互联网连接,并且可以访问weather.gov API
  3. MCP客户端无法连接:验证stdio连接和服务器日志

调试步骤

# Check if uv is installed
uv --version

# Test the server directly
cd ~/weather-mcp
uv run python weather.py

# Check recent logs
tail -20 weather_mcp.log

# Test with verbose logging
export MCP_LOG_LEVEL=debug
uv run python weather.py

需求

  • Python 3.11+
  • httpx>=0.28.1
  • mcp[cli]>=1.13.1

贡献

  1. 分叉存储库
  2. 创建要素分支: git checkout -b feature-name
  3. 进行更改并在本地进行测试
  4. 检查日志是否有任何问题: tail -f weather_mcp.log
  5. 承诺并推动: git commit -m "Description" && git push
  6. 提交拉取请求

api参考

此服务器使用美国国家气象局API:

  • 基本URL: https://api.weather.gov
  • 文档: https://www.weather.gov/documentation/services-web-api
  • 速率限制:没有具体说明,但请尊重
  • 认证:无需