使用说明
MCP天气预报服务器和客户端
一个模型上下文协议(MCP)服务器,使用免费的Open-Meteo API以及Claude提供的交互式Python客户端提供天气预报。
特性
- MCP气象服务器:在TypeScript(Node.js)和Python实现中都可用
- 交互式Python客户端:
- 通过stdio连接的本地客户端
- 远程客户端通过HTTP/SSE连接到部署的服务器
- 免费API:使用Open-Meteo API(天气数据不需要API密钥)
- 7天预测:当前状况加上7天预测
- 全球覆盖:查询全球各城市的天气
- Docker支持:Node.js和Python服务器都是容器化的,并且支持AWS
先决条件
- 适用于Node.js服务器:Node.js(v18或更高版本)
- 对于Python服务器:Python 3.8或更高版本
- 对于客户:Python 3.8+和Anthropic API密钥
- 用于Docker部署:支持buildx的Docker
安装
选项A:Node.js服务器
# Install dependencies
npm install
# Build the server
npm run build
# Run the server
npm start
选项B:Python服务器
# Install dependencies
pip install -r requirements.txt
# Run the server
python src/index.py
客户端设置
# Install Python dependencies
pip install -r requirements.txt
# Configure API Key
cp .env.example .env
# Edit .env and add your Anthropic API key:
# ANTHROPIC_API_KEY=your_actual_api_key_here
用法
运行本地客户端(stdio)
通过stdio连接到本地MCP服务器:
python client.py
运行远程客户端(HTTP/SSE)
通过HTTP连接到远程MCP服务器:
python client_remote.py
然后用自然语言提问:
🌤️ Your question: What is the weather forecast in Zurich?
示例:
- “苏黎世的天气预报是什么?”
- “东京的天气怎么样?”
- “跟我说说纽约的天气吧”
- “巴黎的气温是多少?”
类型 quit 或 exit 阻止客户。
项目结构
mcp-weather-claude/
├── src/
│ ├── index.ts # Node.js MCP server implementation
│ └── index.py # Python MCP server implementation
├── dist/ # Compiled JavaScript (generated)
├── client.py # Interactive local Python client (stdio)
├── client_remote.py # Interactive remote Python client (HTTP/SSE)
├── Dockerfile # Docker config for Node.js server
├── Dockerfile.python # Docker config for Python server
├── package.json # Node.js dependencies
├── tsconfig.json # TypeScript configuration
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
└── README.md # This file
运作原理
MCP服务器(Node.js或Python)
两个服务器实现都公开了相同的MCP接口:
get_weather_forecast工具:接受城市名称并返回天气数据- 使用Open-Meteo地理编码API将城市名称地理编码为坐标
- 从Open-Meteo weather API获取天气数据
- 返回当前状况和7天预报的格式化天气报告
端点
所有服务器都公开以下端点:
/health-健康检查端点/weather?city=CityName-用于直接天气查询的简单REST端点/sse-MCP协议通信的SSE端点/messages-MCP协议消息的POST端点
Python客户端
-
本地客户端(Client.py):
- 通过stdio(进程通信)连接到MCP服务器
- 使用Claude解释自然语言查询
- 调用相应的MCP工具
- 最适合本地开发和测试
-
远程客户端(Client_Remote.py):
- 通过HTTP/SSE连接到远程MCP服务器
- 使用Claude解释自然语言查询
- 向发出HTTP请求
/weather端点 - 最适合在AWS或其他云平台上进行生产部署
API学分
气象数据由 开放天气 -不需要API密钥的免费天气API。
Docker部署
Node.js服务器
构建Docker镜像
docker buildx build --platform linux/amd64 -t mcp-weather-server .
在本地运行
docker run -d -p 8320:8320 --name mcp-weather mcp-weather-server
部署到AWS ECR
# Tag the image
docker tag mcp-weather-server:latest 190016928273.dkr.ecr.eu-central-1.amazonaws.com/mcp-weather-server:latest
# Push to ECR
docker push 190016928273.dkr.ecr.eu-central-1.amazonaws.com/mcp-weather-server:latest
Python服务器
构建Docker镜像(AWS的AMD64)
docker build --platform linux/amd64 -f Dockerfile.python -t mcp-weather-python .
在本地运行
docker run -p 8320:8320 mcp-weather-python
服务器将可在以下位置访问 http://localhost:8320 SSE端点位于 http://localhost:8320/sse.
部署到AWS ECR
# Authenticate with ECR
aws ecr get-login-password --region eu-central-1 | docker login --username AWS --password-stdin 190016928273.dkr.ecr.eu-central-1.amazonaws.com
# Tag the image
docker tag mcp-weather-python:latest 190016928273.dkr.ecr.eu-central-1.amazonaws.com/mcp-weather-python:latest
# Push to ECR
docker push 190016928273.dkr.ecr.eu-central-1.amazonaws.com/mcp-weather-python:latest
测试服务器
健康检查
curl http://localhost:8320/health
获得天气
curl "http://localhost:8320/weather?city=Zurich"
SSE端点(用于MCP客户端)
# Terminal 1 - Connect to SSE
curl -N http://localhost:8320/sse
# Terminal 2 - Send tool call
curl -X POST http://localhost:8320/messages \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "get_weather_forecast",
"arguments": {
"city": "Zurich"
}
}
}'
许可证
麻省理工学院