一个简单的天气预报 MCP(模型控制协议)服务器,可提供全球天气预报和当前天气状况,让你随时随地掌握各地天气信息。
该服务器使用简单,只需按以下步骤操作,即可轻松获取全球天气信息。
使用以下命令安装所需依赖:
pip install mcp-server requests pydantic
访问 OpenWeatherMap 注册账户并获取 API 密钥。
提供 API 密钥有以下两种方式:
# 设置环境变量
export OPENWEATHER_API_KEY="your_api_key" # Linux/Mac
set OPENWEATHER_API_KEY=your_api_key # Windows
# 启动服务器
python weather_mcp_server.py
python weather_mcp_server.py
当调用工具时,需要提供 api_key 参数。
将以下配置添加到支持 MCP 的客户端中:
{
"weather_forecast": {
"command": "python",
"args": [
"/full_path/weather_mcp_server.py"
],
"env": {
"OPENWEATHER_API_KEY": "your_openweathermap_key_here"
},
"disabled": false,
"autoApprove": ["get_weather", "get_current_weather"]
}
}
location:地点名称,例如“北京”、“纽约”、“东京”。api_key:OpenWeatherMap API 密钥(可选,默认从环境变量读取)。timezone_offset:时区偏移量(小时),例如 8 表示北京, -4 表示纽约。默认为 0(UTC 时间)。location:地点名称,例如“北京”、“纽约”、“东京”。api_key:OpenWeatherMap API 密钥(可选,默认从环境变量读取)。timezone_offset:时区偏移量(小时),例如 8 表示北京, -4 表示纽约。默认为 0(UTC 时间)。以下是 AI 助手调用示例:
用户:纽约现在的天气怎么样?
AI:让我帮你查看纽约的当前天气。
[调用 get_current_weather("New York", timezone_offset=-4)]
纽约当前天气:18°C,多云,湿度 65%,风速 3.5m/s。
api_key 参数。