README
🚀 MCP 天气 SSE 服务器
本服务器实现了模型上下文协议 (MCP),借助 Server-Sent Events (SSE) 与 OpenWeatherMap API 相连,能为 Claude、CursorAI 或 MCP-Inspector 等 AI 工具提供实时天气数据。
🚀 快速开始
本服务器借助 Server-Sent Events (SSE) 与 OpenWeatherMap API 相连,能为 Claude、CursorAI 或 MCP-Inspector 等 AI 工具提供实时天气数据。
✨ 主要特性
- 实现模型上下文协议,可与 AI 工具无缝集成。
- 采用 Server-Sent Events (SSE) 传输协议,实现实时通信。
- 连接 OpenWeatherMap API,获取实时天气数据。
- 提供三种实用工具:
get_current_weather:获取指定城市的当前天气状况。get_weather_forecast:获取指定城市的多日天气预报。get_weather_by_coordinates:依据地理坐标获取天气信息。
📦 安装指南
先决条件
- Python 3.8+
- OpenWeatherMap API 密钥(可在 openweathermap.org 注册获取)
安装步骤
-
克隆此仓库:
git clone https://github.com/yourusername/mcp-weather-sse.git cd mcp-weather-sse -
创建虚拟环境并安装依赖项:
python -m venv venv source venv/bin/activate # 在 Windows 上:venv\Scripts\activate pip install mcp requests
💻 使用示例
启动服务器
你可以通过以下几种方式启动服务器:
# 使用命令行参数
python mcp_weather_sse.py --api-key YOUR_API_KEY
# 使用环境变量
export OPENWEATHER_API_KEY=YOUR_API_KEY
python mcp_weather_sse.py
# 自定义主机和端口(默认:127.0.0.1:3001)
python mcp_weather_sse.py --host 0.0.0.0 --port 8080
与 MCP 客户端连接
Cursor AI
- 打开 Cursor AI。
- 转到 文件 → 偏好设置 → Cursor 设置 → MCP → 添加新服务器。
- 输入以下信息:
- 名称:天气 SSE
- 类型:sse
- URL:http://127.0.0.1:3001/sse
Claude Desktop
- 打开 Claude Desktop。
- 转到 设置 → MCP 服务器。
- 添加一个新的服务器,配置如下:
{ "mcpServers": { "weather": { "type": "sse", "url": "http://127.0.0.1:3001/sse" } } }
MCP-Inspector
你可以使用 MCP-Inspector 工具来测试你的服务器:
npm install -g @anthropic-ai/mcp-inspector
mcp-inspector http://127.0.0.1:3001/sse
📚 详细文档
API 参考
get_current_weather
获取指定城市的当前天气信息。
请求
GET /weather/current?city={city}
响应
{
"temperature": {number},
"condition": {string},
"humidity": {number},
"windSpeed": {number}
}
get_weather_forecast
获取指定城市的多日天气预报。
请求
GET /weather/forecast?city={city}&days={days}
响应
[
{
"date": "{date}",
"temperature": {number},
"condition": {string},
"humidity": {number},
"windSpeed": {number}
},
...
]
get_weather_by_coordinates
根据经纬度获取天气信息。
请求
GET /weather/coordinates?lat={latitude}&lon={longitude}
🔧 技术细节
安全注意事项
- 确保 OpenWeatherMap API 密钥的安全性,避免在代码中明文存储。
- 使用 HTTPS 在生产环境中传输数据以确保安全性。
Scan to contact