使用说明
气象MCP服务器🌤️
一个简单的模型上下文协议(MCP)服务器,使用Open-Meteo API获取班加罗尔和德里的当前温度数据。
特性
- ✅ 获取班加罗尔和德里的当前温度
- ✅ 返回带有时间戳的温度读数
- ✅ 无需API密钥(使用Open-Meteo免费API)
- ✅ 简单REST API端点
- ✅ 网络问题的错误处理
- ✅ 使用pytest进行单元测试
设置
再进行
pip install -r requirements.txt
运行服务器
启动Flask服务器:
python app.py
服务器将于启动 http://127.0.0.1:5000
API终点
1.主页/信息
GET /
返回服务器信息和可用终结点。
2.获取城市的温度
GET /temperature?city=bangalore
GET /temperature?city=delhi
响应示例:
{
"city": "Bangalore",
"temperature": 24.5,
"unit": "°C",
"time": "2025-10-31T10:30",
"timezone": "Asia/Kolkata"
}
3.获取所有温度
GET /all
返回所有支持城市的温度数据。
测试
运行测试套件:
pytest tests/
运行具有详细输出的测试:
pytest tests/ -v
使用示例
使用curl(PowerShell)
# Get Bangalore temperature
curl http://127.0.0.1:5000/temperature?city=bangalore
# Get Delhi temperature
curl http://127.0.0.1:5000/temperature?city=delhi
# Get all cities
curl http://127.0.0.1:5000/all
使用Python请求
import requests
# Get temperature for Bangalore
response = requests.get('http://127.0.0.1:5000/temperature?city=bangalore')
data = response.json()
print(f"{data['city']}: {data['temperature']}°C at {data['time']}")
项目结构
WeatherMCP/
├── app.py # Flask MCP server
├── weather.py # Weather fetching logic
├── requirements.txt # Python dependencies
├── README.md # This file
└── tests/
└── test_weather.py # Unit tests
API数据源
该项目使用免费 开放气象API 其不需要API密钥并且根据CC BY 4.0许可证提供天气数据。
添加到VS代码作为MCP服务器
方法1:使用VS代码设置(推荐)
-
打开VS代码设置(JSON):
- 按
Ctrl+Shift+P(或Cmd+Shift+P在Mac上) - 键入“首选项:打开用户设置(JSON)”
- 按Enter键
- 按
-
将MCP服务器配置添加到您的
settings.json:
{
"github.copilot.chat.mcp.servers": {
"weather": {
"command": "python",
"args": [
"c:\\Users\\arush\\OneDrive\\Desktop\\WeatherMCP\\mcp_server.py"
]
}
}
}
- 重新启动VS Code或重新加载窗口(
Ctrl+Shift+P→ “开发人员:重新加载窗口”)
方法2:使用工作区设置
- 配置已在
.vscode/mcp-settings.json - 将内容复制到您的VS Code用户设置中,如上所示
在Copilot聊天中使用MCP服务器
配置后,您可以在GitHub Copilot Chat中使用它:
@weather get temperature for bangalore
@weather get temperature for delhi
@weather get all temperatures
或者问自然语言问题:
What's the current temperature in Bangalore?
Show me the weather for Delhi
使用此项目的两种方法
1.作为HTTP REST API(当前 app.py)
- 运行:
python app.py - 访问方式:
http://127.0.0.1:5000 - 与curl、Postman或任何HTTP客户端一起使用
2.作为VS代码的MCP服务器(mcp_server.py)
- 在VS代码设置中配置(见上文)
- 与GitHub Copilot聊天一起使用
- 与AI工具自动集成
备注
- 温度以摄氏度(°C)为单位返回
- API使用的坐标为:班加罗尔(北纬12.97°,东经77.59°)、德里(北纬28.70°,北纬77.10°)
- 时间戳采用ISO 8601格式,具有自动时区检测功能