Skip to main content
Glama

天气查询MCP服务器

基于百度地图API提供天气查询功能的MCP(Model Context Protocol)工具。

服务配置

{ "mcpServers": { "weather-mcp": { "command": "python", "args": ["-m", "weather_mcp.server"] } } }

环境变量配置: 请在系统环境变量中设置:

BAIDU_AK=your_baidu_ak_here

功能特性

  • 🌤️ 实时天气查询 - 获取当前天气状况、温度、湿度、风向等信息

  • 📅 天气预报 - 提供未来5天的详细天气预报

  • 📊 生活指数 - 包含晨练、洗车、感冒、紫外线、穿衣、运动等生活指数

  • ⚠️ 气象预警 - 实时获取气象预警信息

  • 逐小时预报 - 24小时逐小时天气预报

  • 🗺️ 多种查询方式 - 支持行政区划代码和经纬度坐标查询

安装依赖

pip install -r requirements.txt

配置

设置系统环境变量:

Windows

set BAIDU_AK=your_baidu_ak_here

Linux/macOS

export BAIDU_AK=your_baidu_ak_here

或者在系统环境变量中永久设置 BAIDU_AK

获取百度地图API密钥

  1. 访问 百度地图开放平台

  2. 注册并登录账号

  3. 创建应用并获取AK(API Key)

  4. 确保开通天气服务权限

使用方法

启动MCP服务器

python server.py

工具说明

get_weather

获取指定地区的天气信息,支持多种数据类型和查询方式。

参数:

  • district_id (可选): 区县的行政区划编码,例如:'110101'(北京东城区)

  • location (可选): 经纬度坐标,格式:'经度,纬度',例如:'116.404,39.915'

  • data_type (可选): 请求数据类型

    • now: 实时天气

    • fc: 天气预报

    • index: 生活指数

    • alert: 气象预警

    • fc_hour: 逐小时预报

    • all: 所有数据(默认)

  • coordtype (可选): 坐标类型,仅在使用location时有效

    • wgs84: WGS84坐标系(默认)

    • bd09ll: 百度经纬度坐标系

    • bd09mc: 百度墨卡托坐标系

    • gcj02: 国测局坐标系

注意: district_idlocation 必须提供其中一个。

使用示例

1. 使用行政区划代码查询北京东城区天气

{ "name": "get_weather", "arguments": { "district_id": "110101", "data_type": "all" } }

2. 使用经纬度查询天安门广场天气

{ "name": "get_weather", "arguments": { "location": "116.404,39.915", "data_type": "all", "coordtype": "wgs84" } }

3. 只查询实时天气

{ "name": "get_weather", "arguments": { "district_id": "110101", "data_type": "now" } }

测试

测试百度地图API连接

python test_baidu_api.py

测试MCP服务器功能

python test_mcp_server.py

项目结构

weather_query/ ├── server.py # MCP服务器主文件 ├── requirements.txt # Python依赖 ├── .env # 环境变量配置 ├── .env.example # 环境变量模板 ├── test_baidu_api.py # API测试脚本 ├── test_mcp_server.py # MCP服务器测试脚本 ├── pyproject.toml # 项目配置 ├── setup.py # 安装脚本 ├── __init__.py # 包初始化文件 └── README.md # 项目说明文档

常见问题

Q: 如何获取行政区划代码?

A: 行政区划代码是中国标准的区县编码。常见的代码包括:

  • 110101: 北京市东城区

  • 110102: 北京市西城区

  • 310101: 上海市黄浦区

  • 440101: 广州市荔湾区

您可以通过百度地图API的其他接口或相关文档获取完整的行政区划代码列表。

Q: 支持哪些坐标系?

A: 支持以下坐标系:

  • WGS84: 国际标准坐标系

  • BD09LL: 百度经纬度坐标系

  • BD09MC: 百度墨卡托坐标系

  • GCJ02: 国测局坐标系(火星坐标系)

Q: API调用频率限制?

A: 请参考百度地图开放平台的API调用频率限制说明。建议合理使用API,避免频繁调用。

MCP服务配置

本地使用配置

{ "mcpServers": { "weather-mcp": { "command": "python", "args": ["-m", "weather_mcp.server"] } } }

托管部署配置

{ "mcpServers": { "weather-mcp": { "command": "python", "args": ["-m", "weather_mcp.server"] } } }

注意:使用前请确保已在系统环境变量中设置 BAIDU_AK

许可证

MIT License

贡献

欢迎提交Issue和Pull Request来改进这个项目!

-
security - not tested
F
license - not found
-
quality - not tested

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jiushan-test/weather-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server