本服务器实现了模型上下文协议 (MCP),借助 Server-Sent Events (SSE) 与 OpenWeatherMap API 相连,能为 Claude、CursorAI 或 MCP-Inspector 等 AI 工具提供实时天气数据。
本服务器借助 Server-Sent Events (SSE) 与 OpenWeatherMap API 相连,能为 Claude、CursorAI 或 MCP-Inspector 等 AI 工具提供实时天气数据。
get_current_weather:获取指定城市的当前天气状况。get_weather_forecast:获取指定城市的多日天气预报。get_weather_by_coordinates:依据地理坐标获取天气信息。克隆此仓库:
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
{
"mcpServers": {
"weather": {
"type": "sse",
"url": "http://127.0.0.1:3001/sse"
}
}
}
你可以使用 MCP-Inspector 工具来测试你的服务器:
npm install -g @anthropic-ai/mcp-inspector
mcp-inspector http://127.0.0.1:3001/sse
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}