使用说明
气象MCP服务器
模型上下文协议(MCP)服务器,使用OpenWeatherMap API提供实时天气信息。
特性
- 当前天气:获取全球任何城市的实时天气状况
- 天气预报:3天天气预报,每小时详细信息
- 天气警报:基于当前条件的模拟天气警报
- 多地点支持:使用可选国家代码查询任何城市的天气
- 错误处理:对无效位置和API故障的优雅处理
先决条件
- .NET 8.0或更高版本
- OpenWeatherMap API密钥(提供免费层)
安装说明
1.获取OpenWeatherMap API密钥
- 访问 OpenWeatherMap
- 注册一个免费帐户
- 从您的帐户仪表板生成API密钥
2.配置环境变量
Windows(PowerShell):
$env:OPENWEATHERMAP_API_KEY = "api_key"
Windows(命令提示符):
set OPENWEATHERMAP_API_KEY=api_key
Linux/macOS:
export OPENWEATHERMAP_API_KEY="api_key"
3.构建和运行
cd WeatherMcpServer
dotnet restore
dotnet run
可用工具
get_current天气
获取指定城市的当前天气状况。
参数:
city(必填):获取天气的城市名称countryCode(可选):国家代码(例如“美国”、“英国”)
例子:
{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"get_current_weather","arguments":{"city":"London"}}}
get_weather_forecast
获取未来3天的天气预报。
参数:
city(必填):要获取预测的城市名称countryCode(可选):国家代码(例如“美国”、“英国”)
例子:
{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"get_weather_forecast","arguments":{"city":"Tokyo"}}}
获取_餐厅_警报
获取某个位置的天气警报和警告(基于条件模拟)。
参数:
city(必填):要检查警报的城市名称countryCode(可选):国家代码(例如“美国”、“英国”)
例子:
{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"get_weather_alerts","arguments":{"city":"Miami","countryCode":"US"}}}
测试服务器
- 初始化服务器:
{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"1.0.0","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0.0"}}}
- 列出可用工具:
{"jsonrpc":"2.0","id":2,"method":"tools/list"}
- 调用工具:
{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"get_current_weather","arguments":{"city":"London"}}}
本地开发配置
要在IDE中配置服务器进行本地开发,请执行以下操作:
VS代码:创建 .vscode/mcp.json:
{
"servers": {
"WeatherMcpServer": {
"type": "stdio",
"command": "dotnet",
"args": [
"run",
"--project",
"C:/path/to/WeatherMcpServer"
]
}
}
}
Visual Studio:创建 .mcp.json 在解决方案目录中:
{
"servers": {
"WeatherMcpServer": {
"type": "stdio",
"command": "dotnet",
"args": [
"run",
"--project",
"WeatherMcpServer"
]
}
}
}
项目结构
WeatherMcpServer/
├── Program.cs # Entry point
├── Services/
│ └── WeatherService.cs # OpenWeatherMap API integration
├── Tools/
│ └── WeatherTools.cs # MCP tool implementations
├── WeatherMcpServer.csproj # Project configuration
├── .mcp/
│ └── server.json # MCP server metadata
└── README.md # This file
实现细节
- 默认情况下使用公制单位(摄氏度,m/s)
- 根据当前条件模拟天气警报
- 预测以3小时为间隔提供3天的展望
- 所有响应都包括对无效输入的正确错误处理
故障排除
401未经授权的错误:
- 验证API密钥是否正确设置为环境变量
- 请确保在设置API密钥后重新启动了服务器
- 检查您的API密钥在OpenWeatherMap上是否处于活动状态
未找到城市:
- 验证城市名称拼写
- 尝试添加国家代码以获得更具体的结果
- 使用主要城市名称以获得最佳效果
示例响应
当前天气:
Current weather in London:
Temperature: 15.2°C (feels like 14.8°C)
Condition: light rain
Humidity: 82%
Wind: 4.1 m/s
Visibility: 10 km
天气预报:
Weather forecast for London, GB:
Monday, January 20
Temperature: 8.5°C - 12.3°C
Condition: light rain
09:00 - 9.2°C, light rain
12:00 - 11.8°C, overcast clouds
15:00 - 12.1°C, light rain
天气警报:
Weather alerts for Phoenix:
HEAT WARNING: Extreme temperatures expected. Stay hydrated and avoid prolonged sun exposure.
