基于高德地图API的MCP(模型上下文协议)天气预报服务器,使用TypeScript开发。
npm install
npm run build
npm start
或者开发模式:
npm run dev
获取指定城市的天气信息。
| 参数名称 | 类型 | 是否必填 | 描述 | 示例 |
|---|---|---|---|---|
cityName | string | ✅ | 中文城市级别名称 | "北京市", "上海市", "广州市" |
date | string | ❌ | 查询日期 (YYYY-MM-DD) | "2024-01-15" (默认当天) |
forecastType | string | ❌ | 查询类型 | "current" (实时) 或 "forecast" (预报) |
此服务器依赖于环境变量 AMAP_API_KEY 在中间读取高德地图API密钥,调用时不传递。请确保在启动服务器之前设置此环境变量。
实时天气查询:
{
"cityName": "北京市",
"forecastType": "current"
}
天气预报查询:
{
"cityName": "广州市",
"date": "2024-01-15",
"forecastType": "forecast"
}
{
"mcpServers": {
"weather-forecast": {
"command": "node",
"args": [
"/path/to/your/build/index.js"
],
"env": {
"AMAP_API_KEY": "your_gaode_api_key_here"
}
}
}
}
实时天气:
🌈 天气查询结果
📍 北京 北京市
🌤️ 天气:晴
🌡️ 温度:15°C
💨 风向:西北风
🌪️ 风力:3级
💧 湿度:45%
⏰ 更新时间:224-01-15 14:30:00
📅 查询日期: 2024-01-15
天气预报:
🌈 天气查询结果
📍 上海 上海市
📅 未来几天天气预报:
今天 (星期一)
🌤️ 白天:晴 | 夜间:多云
🌡️ 温度:8°C ~ 18°C
💨 风向:东北风 3级 | 东风 2级
2024-01-16 (星期二)
🌤️ 白天:多云 | 夜间:阴
🌡️ 温度:10°C ~ 20°C
💨 风向:东风 2级 | 东南风 1级
📅 查询日期: 2024-01-15
mcp-weather-server/
├── src/
│ └── index.ts # 主服务器代码
├── build/ # 编译输出目录
├── package.json # 项目配置
├── tsconfig.json # TypeScript配置
└── README.md # 项目文档
地理编码模块 (getCityCode)
天气查询模块 (getWeatherInfo)
数据格式化模块
formatLiveWeather 格式化实时天气数据formatForecastWeather 格式化预报天气数据https://restapi.amap.com/v3/geocode/geohttps://restapi.amap.com/v3/weather/weatherInfo无法找到城市代码
API密钥无效
网络请求失败
MIT许可证
欢迎提交Issue和Pull Requests来改进这个项目!
如有任何疑问,请通过以下方式联系我们: