天气数据服务

一个基于MCP协议的生产级天气数据服务器,提供来自国家气象局API的天气数据,具有缓存、监控、健壮的错误处理和全面的测试功能。

  • by srjv11
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 srjv11/mcp-server-weather-py

使用说明

气象MCP服务器

一个全面的、可用于生产的MCP(模型上下文协议)服务器,提供来自美国国家气象局API的天气数据,包括缓存、监控、强大的错误处理和全面的测试。

🚀 快速开始

# Install dependencies
uv sync --group dev

# Run the weather MCP server
uv run weather-mcp
# or
uv run -m src.main

# Run tests
uv run pytest tests/ -v

# Run code quality checks
ruff check --fix && ruff format

📁 项目结构

mcp-server-weather-py/
├── 📄 Core Application
│   └── src/
│       ├── main.py              # Main entry point
│       ├── client.py            # HTTP client with caching
│       ├── config.py            # Configuration management
│       ├── exceptions.py        # Custom exception classes
│       ├── formatters.py        # Data formatting utilities
│       ├── models.py            # Data models and types
│       ├── tools.py             # MCP tools implementation
│       ├── validators.py        # Input validation
│       └── weather.py           # Alternative entry point
│
├── 🧪 Testing & Quality
│   └── tests/
│       ├── test_client.py       # HTTP client tests
│       ├── test_config.py       # Configuration tests
│       ├── test_exceptions.py   # Exception handling tests
│       ├── test_formatters.py   # Data formatting tests
│       ├── test_models.py       # Data model tests
│       ├── test_tools.py        # MCP tools tests
│       ├── test_validators.py   # Validation tests
│       └── test_weather.py      # Main module tests
│
├── 🐳 Deployment
│   ├── Dockerfile               # Container configuration
│   ├── docker-compose.yml       # Multi-service orchestration
│   └── .dockerignore            # Container build optimization
│
├── 📚 Configuration & Documentation
│   ├── pyproject.toml           # Package configuration & dependencies
│   ├── CLAUDE.md                # Development guide
│   ├── MCP_SETUP.md             # MCP integration guide
│   └── docs/README_ENHANCED.md  # Detailed enhancement documentation
│
└── 📄 Examples & Utilities
    ├── examples.py              # Usage examples & demos
    ├── example_state_machine.py # Educational state machine example
    └── setup_script.py          # Setup automation script

✨ 特性

  • 🛡️ 稳健的错误处理:具有重试逻辑的自定义异常层次结构
  • ⚡ 演出:具有TTL和速率限制的内存缓存
  • 📊 监控:健康检查和结构化日志记录
  • 🧪 综合测试:8个模块的测试覆盖率超过95%
  • 🐳 生产就绪:Docker支持和部署配置
  • 🔧 可配置的:基于环境的配置
  • 🌐 MCP集成:已准备好集成Claude Code

🛠️ api参考

核心MCP工具

  • get_alerts(state, severity_filter=None):通过可选的严重性过滤获取天气警报
  • get_forecast(latitude, longitude):获取特定坐标的天气预报
  • get_location_forecast(city, state):按城市和州获取天气预报
  • health_check():检查服务器运行状况和性能指标

🧪 测试

# Run all tests with coverage
uv run pytest tests/ -v --cov=. --cov-report=html

# Run specific test modules
uv run pytest tests/test_validators.py -v
uv run pytest tests/test_tools.py -v

测试覆盖率:8个模块的46个测试,涵盖验证、数据模型、HTTP客户端、MCP工具、格式化、异常和配置。

🐳 部署

地方发展

uv sync --group dev
uv run weather-mcp

Docker部署

docker build -t weather-mcp .
docker run -it weather-mcp

使用Docker Compose进行生产

docker-compose up -d

🔗 MCP集成

连接到Claude Code进行交互式天气查询。看 MCP_SETUP.md 有关完整的设置说明。

📚 文档

快速参考

  • CLAUDE.md:包含命令和架构细节的开发指南
  • MCP_SETUP.md:Claude Code集成说明
  • examples.py:交互式使用演示

综合文档


气象MCP服务器 -大型语言模型的生产就绪天气数据🌤️