Skip to main content
Glama
Weather MCP Server

Weather MCP Server

Weather MCP Server

AWS Cognito OAuth 2.1 Bearerトークン認証で保護された、リアルタイムの気象データを提供するModel Context Protocolサーバーです。

完全な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 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

Bearerトークン

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 と /.well-known/oauth-protected-resource を指す WWW-Authenticate ヘッダーで応答します。

  2. クライアントは検出ドキュメントを取得して、Cognito認可サーバーを見つけます。

  3. クライアントはCognitoからJWTアクセストークンを取得します (client_credentials または動的クライアント登録経由)。

  4. クライアントは後続のリクエストに Authorization: Bearer <token> を含めます。

  5. ミドルウェアはCognitoのJWKSエンドポイントに対してJWT署名を検証します (RS256、1時間キャッシュ)。


注意事項

  • MCPサーバーはDNSリバインディング保護を無効にして (host="0.0.0.0") 0.0.0.0:8000 にバインドします。これは、Hostヘッダーが localhost ではなくパブリックドメインであるロードバランサー (例: ECS Express Mode ALB) の背後で実行する場合に必要です。

  • ECS Expressモードにデプロイする場合、イメージはダイジェストで固定されます。更新方法についてはデプロイガイドを参照してください。


Related MCP Connectors

Related MCP Servers