天气数据服务

一个基于Model Context Protocol(MCP)的实时天气数据服务器,支持AWS Cognito OAuth 2.1认证,适用于需要实时天气数据的应用场景。

  • by sauravkumar329
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 sauravkumar329/weather-mcp

使用说明

气象MCP服务器

A. 模型上下文协议 提供实时天气数据的服务器,采用 AWS Cognito OAuth 2.1 承载令牌身份验证。

实施完整 MCP授权规范(2025-11-25):

  • RFC 9728 --受保护资源元数据(PRM)发现
  • RFC 6750 --承载令牌使用情况
  • RFC 7591 --动态客户端注册(DCR)桥接到Cognito

建筑

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

天气数据来源于 开放天气 -免费,不需要API密钥。


项目结构

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+和 紫外线
  • 已配置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 table

2--配置环境

cp .env.example .env
# Fill in COGNITO_REGION, COGNITO_USER_POOL_ID, COGNITO_DOMAIN_PREFIX from CloudFormation Outputs

3--运行

当地:

uv sync
uv run python -m weather_mcp.main

Docker:

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终点

端点 身份验证 描述
GET /health 无 健康检查
GET /.well-known/oauth-protected-resource 无 RFC 9728发现文档
POST /register 无 RFC 7591动态客户端注册
POST /mcp 承载令牌 MCP工具(流式HTTP)

MCP工具

工具 说明
get_current_weather 任何地点的当前天气(按纬度/经度)

用法

动态客户端注册(零预配置)

# 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}'

环境变量

变量 描述
COGNITO_REGION AWS区域(例如。 us-east-1)
COGNITO_USER_POOL_ID Cognito用户池ID
COGNITO_DOMAIN_PREFIX 托管UI域前缀
SERVER_URL 此服务器的公共URL(默认值: http://localhost:8000)

身份验证的工作原理

  1. 请求到达 /mcp 没有令牌→ 服务器响应如下 401 和一个 WWW-Authenticate 标题指向 /.well-known/oauth-protected-resource
  2. 客户端获取发现文档以查找Cognito授权服务器
  3. 客户端从Cognito获得JWT访问令牌(通过 client_credentials 或动态客户端注册)
  4. 客户包括 Authorization: Bearer <token> 后续请求
  5. 中间件根据Cognito的JWKS端点(RS256,缓存1小时)验证JWT签名

备注

  • MCP服务器绑定到 0.0.0.0:8000 禁用DNS重新绑定保护(host="0.0.0.0").当在负载平衡器(如ECS Express Mode ALB)后面运行时,这是必需的,其中 Host header是公共域,不是 localhost.
  • 部署到ECS Express模式时,映像由摘要固定——请参阅 部署指南 如何更新它。