Weather MCP Server
天气 MCP 服务器
一个 Model Context Protocol 服务器,提供实时天气数据,并使用 AWS Cognito OAuth 2.1 Bearer 令牌进行身份验证。
实现了完整的 MCP 授权规范 (2025-11-25):
RFC 9728 — 受保护资源元数据 (PRM) 发现
RFC 6750 — Bearer 令牌使用
RFC 7591 — 桥接到 Cognito 的动态客户端注册 (DCR)
架构
Client / AI Agent
│
├─ GET /.well-known/oauth-protected-resource → discover auth server
├─ POST /register → dynamic client registration (optional)
├─ POST Cognito /oauth2/token → exchange credentials for JWT
└─ POST /mcp Authorization: Bearer <token> → call MCP tools天气数据来源于 Open-Meteo — 免费,无需 API 密钥。
Related MCP server: Python MCP Weather Server with OAuth 2.1 Authentication
项目结构
weather-mcp/
├── weather_mcp/
│ ├── __init__.py
│ ├── config.py # Env var loading (COGNITO_REGION/USER_POOL_ID/DOMAIN_PREFIX, SERVER_URL)
│ ├── auth.py # JWT validation, middleware, PRM + DCR handlers
│ ├── tools.py # MCP instance + weather tools
│ └── main.py # Starlette app factory + uvicorn entrypoint
├── infra/
│ └── cognito.yaml # CloudFormation — Cognito User Pool, IAM role
├── docs/
│ └── deploy-ecs-express.md # ECS Express Mode deployment guide
├── pyproject.toml
├── Dockerfile
├── .env.example
└── README.md前置要求
Python 3.13+ 和 uv
配置了 CLI 的 AWS 账户 (
aws configure)Docker(可选,用于容器化部署)
快速开始
1 — 部署 AWS Cognito
aws cloudformation deploy \
--template-file infra/cognito.yaml \
--stack-name weather-mcp \
--region us-east-1 \
--capabilities CAPABILITY_NAMED_IAM获取输出值:
aws cloudformation describe-stacks \
--stack-name weather-mcp \
--query "Stacks[0].Outputs" \
--output table2 — 配置环境变量
cp .env.example .env
# Fill in COGNITO_REGION, COGNITO_USER_POOL_ID, COGNITO_DOMAIN_PREFIX from CloudFormation Outputs3 — 运行
本地运行:
uv sync
uv run python -m weather_mcp.mainDocker 运行:
docker build -t weather-mcp:local .
docker run --env-file .env -p 8000:8000 weather-mcp:local服务器将在 http://0.0.0.0:8000 启动。
部署到 AWS ECS Express 模式:
请参阅 docs/deploy-ecs-express.md 获取完整指南 — 该指南涵盖构建镜像、推送到 ECR 以及创建带有自动扩缩容的公共 HTTPS 服务。
API 端点
端点 | 身份验证 | 描述 |
| 无 | 健康检查 |
| 无 | RFC 9728 发现文档 |
| 无 | RFC 7591 动态客户端注册 |
| Bearer 令牌 | MCP 工具 (流式 HTTP) |
MCP 工具
工具 | 描述 |
| 通过经纬度获取任何位置的当前天气 |
使用方法
动态客户端注册(无需预先配置)
# 1. Register a new client
curl -s -X POST http://localhost:8000/register \
-H "Content-Type: application/json" \
-d '{"client_name":"my-agent","grant_types":["client_credentials"],"scope":"weather-mcp/read"}'
# 2. Get a token
curl -s -X POST https://weather-mcp-auth.auth.us-east-1.amazoncognito.com/oauth2/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=<CLIENT_ID>&client_secret=<CLIENT_SECRET>&scope=weather-mcp/read"
# 3. Call MCP
curl -s -X POST http://localhost:8000/mcp \
-H "Authorization: Bearer <TOKEN>" \
-H "Accept: application/json, text/event-stream" \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"tools/list","id":1}'环境变量
变量 | 描述 |
| AWS 区域 (例如 |
| Cognito 用户池 ID |
| 托管 UI 域名后缀 |
| 此服务器的公共 URL (默认: |
身份验证工作原理
请求在没有令牌的情况下到达
/mcp→ 服务器响应401并返回指向/.well-known/oauth-protected-resource的WWW-Authenticate标头客户端获取发现文档以找到 Cognito 授权服务器
客户端从 Cognito 获取 JWT 访问令牌(通过
client_credentials或动态客户端注册)客户端在后续请求中包含
Authorization: Bearer <token>中间件根据 Cognito 的 JWKS 端点验证 JWT 签名 (RS256,缓存 1 小时)
注意事项
MCP 服务器绑定到
0.0.0.0:8000,并禁用了 DNS 重绑定保护 (host="0.0.0.0")。当在负载均衡器(例如 ECS Express 模式 ALB)后运行且Host标头为公共域名而非localhost时,这是必需的。部署到 ECS Express 模式时,镜像通过摘要 (digest) 锁定 — 请参阅 部署指南 了解如何更新它。
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
This server cannot be deployed
Maintenance
Related MCP Connectors
NOAA and ECMWF weather forecast MCP for discovery, validation, and GribStream OAuth queries.
Hosted MCP server for Xweather weather data: conditions, forecasts, alerts, and more.
Weather MCP — wraps Open-Meteo API (free, no auth)
Open-Meteo MCP — weather forecast + historical reanalysis + sister APIs
Related MCP Servers
- -licenseNot gradedqualityNot gradedmaintenanceEnables real-time weather data streaming through a secure MCP server. Features authentication using Descope and can be deployed to Fly.io for remote access.13 npm-
- FlicenseNot gradedqualityDmaintenanceProvides weather information from the National Weather Service API with full MCP OAuth 2.1 compliance, including weather alerts and forecasts for US locations through secure Azure AD authentication.28-
- AlicenseNot gradedqualityDmaintenanceDemonstrates how to secure an MCP server with OAuth 2.1 using AWS Cognito, with support for dynamic client registration and client ID metadata documents.68MIT
- FlicenseNot gradedqualityDmaintenanceEnables MCP clients like Kiro and Claude to access current weather conditions and forecasts for any location using the OpenWeatherMap API.-