天气数据服务

一个基于Open-Meteo API的天气数据服务,提供地理位置解析、当前天气、天气预报和空气质量数据。

  • by agentic-forge
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 agentic-forge/mcp-weather

使用说明

mcp天气

天气MCP服务器使用 开放气象API 建造于 FastMCP.

特性

  • 不需要API密钥 -使用免费的Open-Meteo API
  • 智能定位分辨率 -城市名称会自动进行地理编码
  • 基于人口的消歧 -当多个城市匹配时,选择最有可能的一个
  • 国家过滤 -用“英国伦敦”与“加拿大伦敦”来消除分歧
  • 公制和英制单位 -完全支持两个单元系统
  • 流式HTTP传输 -部署为远程MCP服务器

工具

geocode

按城市名称搜索地点。

geocode(city="Berlin")
geocode(city="Springfield", country="US", limit=10)

get_current_weather

获取当前天气状况。

get_current_weather(city="Berlin")
get_current_weather(city="London", country="UK")
get_current_weather(latitude=52.52, longitude=13.41)
get_current_weather(city="New York", units="imperial")

get_forecast

获取天气预报(每日或每小时)。

get_forecast(city="Berlin", days=7)
get_forecast(city="Miami", units="imperial", days=5, hourly=True)

get_air_quality

获取空气质量和污染物数据。

get_air_quality(city="Beijing")
get_air_quality(city="Berlin", include_pollen=True)  # Pollen: Europe only

安装

# Clone the repository
git clone https://github.com/agentic-forge/mcp-weather.git
cd mcp-weather

# Install dependencies
uv sync

运行服务器

HTTP传输(建议用于远程访问)

# Default: HTTP on port 8000
uv run python -m forge_mcp_weather

# Custom port
uv run python -m forge_mcp_weather --port 3000

服务器将在 http://localhost:8000/mcp

STDIO传输(适用于本地MCP客户端)

uv run python -m forge_mcp_weather --stdio

MCP客户端配置

克劳德桌面版

增添 claude_desktop_config.json:

{
  "mcpServers": {
    "weather": {
      "command": "uv",
      "args": ["run", "python", "-m", "forge_mcp_weather", "--stdio"],
      "cwd": "/path/to/mcp-weather"
    }
  }
}

远程HTTP服务器

{
  "mcpServers": {
    "weather": {
      "url": "http://localhost:8000/mcp"
    }
  }
}

发展

# Install dev dependencies
uv sync --group dev

# Run tests
uv run pytest

# Run tests with coverage
uv run pytest --cov

# Type checking
uv run basedpyright

# Linting
uv run ruff check .

# Install pre-commit hooks
uv run pre-commit install

测试服务器

使用mcp工具cli

mcp工具cli 是用于测试任何MCP服务器的通用MCP客户端。

全局安装(一次):

uv tool install mcp-tools-cli
# or: pipx install mcp-tools-cli

创建配置文件 (mcp_config.json):

{
  "mcpServers": {
    "weather": {
      "url": "http://localhost:8000/mcp"
    }
  }
}

用途:

# Start the server
uv run python -m forge_mcp_weather

# List available tools
mcp-tools-cli list-tools --mcp-name weather

# Call a tool
mcp-tools-cli call-tool --mcp-name weather --tool-name get_current_weather --tool-args '{"city": "Berlin"}'

直接HTTP测试

在HTTP模式下运行服务器时,您可以直接使用curl进行测试:

# Start the server
uv run python -m forge_mcp_weather

# List available tools
curl -X POST http://localhost:8000/mcp \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc": "2.0", "method": "tools/list", "id": 1}'

# Call get_current_weather
curl -X POST http://localhost:8000/mcp \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc": "2.0", "method": "tools/call", "params": {"name": "get_current_weather", "arguments": {"city": "Berlin"}}, "id": 1}'

# Call geocode
curl -X POST http://localhost:8000/mcp \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc": "2.0", "method": "tools/call", "params": {"name": "geocode", "arguments": {"city": "London", "country": "UK"}}, "id": 1}'

使用MCP客户端进行程序化测试

FastMCP提供了一个Python客户端,可以与任何支持HTTP传输的MCP服务器(而不仅仅是FastMCP服务器)配合使用:

import asyncio
from fastmcp import Client

async def test_weather_server():
    async with Client("http://localhost:8000/mcp") as client:
        # List available tools
        tools = await client.list_tools()
        print(f"Available tools: {[t.name for t in tools]}")

        # Get current weather
        weather = await client.call_tool(
            "get_current_weather",
            {"city": "Berlin"}
        )
        print(f"Weather: {weather}")

        # Get forecast
        forecast = await client.call_tool(
            "get_forecast",
            {"city": "Tokyo", "days": 3}
        )
        print(f"Forecast: {forecast}")

asyncio.run(test_weather_server())

此客户端可以使用以下方式连接到任何MCP服务器:

  • HTTP/SSE传输: Client("http://server:port/mcp")
  • STDIO传输: Client("uvx some-mcp-server") 或 Client(["python", "-m", "server"])

API 参考

此服务器使用以下Open Meteo API:

许可证

麻省理工学院