关于 Weather API MCP Server
Weather API MCP Server 通过 QWeather API 提供当前天气、预报和城市查询,适用于需要天气信息的 MCP 客户端,并支持配置单位、语言和预报时段。
主要功能
- 01查询当前天气
- 02获取 3 至 30 天预报
- 03获取未来 24 小时预报
- 04查询城市 ID 和位置信息
- 05配置单位和响应语言
适用场景
- 01查询指定地点的当前天气
- 02获取逐日或逐小时预报
- 03将城市名称解析为 QWeather 位置 ID
MCP 相关
该 MCP 服务提供 getWeather、getWeatherForecast、getHourlyWeather 和 lookupCity 四种工具。客户端可使用城市名称、坐标或 QWeather 位置 ID 调用。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
天气 API MCP 服务
天气信息的模型上下文协议(MCP)服务实现,使用和风天气(QWeather)API 提供当前天气数据和天气预报。
功能
- 当前天气:获取任意地点的当前天气数据
- 天气预报:获取未来 3 至 30 天的天气预报
- 逐小时预报:获取未来 24 小时的天气预报
- 城市查询:查询城市信息和 ID,以获取更精确的天气数据
- 可自定义选项:配置单位、语言和其他详细信息
- 由 QWeather 提供支持:集成和风天气(QWeather)API,获取准确的天气数据
安装
npm install mcp-weather-api
或者使用 npx 直接运行:
npx mcp-weather-api
API 参考
天气工具
该服务提供四种可通过 MCP 调用的天气工具:
1. 获取当前天气
// Tool name: getWeather
{
location: "New York, NY", // Can be city name, coordinates like "119.98,30.24", or QWeather location ID
options: {
units: "metric", // "metric" (Celsius) or "imperial" (Fahrenheit)
language: "en", // Language code (en, zh, etc.)
}
}
2. 获取天气预报
// Tool name: getWeatherForecast
{
location: "London, UK", // Can be city name, coordinates like "119.98,30.24", or QWeather location ID
options: {
units: "imperial", // "metric" (Celsius) or "imperial" (Fahrenheit)
days: 3, // Supports 3, 7, 10, 15, or 30 days
language: "en" // Language code (en, zh, etc.)
}
}
3. 获取逐小时天气预报
// Tool name: getHourlyWeather
{
location: "Tokyo, Japan", // Can be city name, coordinates like "119.98,30.24", or QWeather location ID
options: {
units: "metric", // "metric" (Celsius) or "imperial" (Fahrenheit)
hours: 24, // Number of hours (default: 24, max: 24)
language: "ja" // Language code (en, zh, ja, etc.)
}
}
4. 城市查询
// Tool name: lookupCity
{
location: "Beijing", // City name or coordinates like "119.98,30.24"
options: {
language: "en" // Language code (en, zh, etc.)
}
}
天气选项
当前天气工具选项:
interface WeatherOptions {
units?: "metric" | "imperial"; // Temperature units (default: metric)
language?: string; // Response language code
}
天气预报工具选项:
interface ForecastOptions {
units?: "metric" | "imperial"; // Temperature units (default: metric)
days?: number; // Number of days (default: 3)
language?: string; // Response language code
}
逐小时预报工具选项:
interface HourlyForecastOptions {
units?: "metric" | "imperial"; // Temperature units (default: metric)
hours?: number; // Number of hours (default: 24, max: 24)
language?: string; // Response language code
}
城市查询工具选项:
interface CityLookupOptions {
language?: string; // Response language code
}
响应格式
所有工具均按以下格式返回响应:
{
content: Array<{
type: "text";
text: string;
}>;
}
响应示例
当前天气响应
Weather for New York:
Observation Time: 2023-11-15T12:30+08:00
Current Conditions: Partly Cloudy (Icon: 101)
Temperature: 18.5°C
Feels Like: 19.2°C
Wind Information:
- Direction: Northeast (45°)
- Scale: 3
- Speed: 15 km/h
Other Information:
- Humidity: 65%
- Precipitation: 0.0 mm
- Pressure: 1013 hPa
- Visibility: 25 km
- Cloud Cover: 30%
- Dew Point: 12.1°C
Updated: 2023-11-15T12:35+08:00
Data Sources: QWeather
License: QWeather Developers License
天气预报响应
Weather Forecast for London:
2023-11-15:
Time Information:
- Sunrise: 07:12, Sunset: 16:30
- Moonrise: 15:40, Moonset: 03:25
- Moon Phase: Waxing Gibbous (Icon: 802)
Day Weather:
- Conditions: Rain (Icon: 305)
- Temperature Range: 12.0°F / 7.0°F
- Wind: Northwest (315°)
- Wind Scale: 3, Speed: 18 km/h
Night Weather:
- Conditions: Cloudy (Icon: 101)
- Wind: North (0°)
- Wind Scale: 2, Speed: 10 km/h
Other Information:
- Humidity: 75%
- Precipitation: 5.2 mm
- Pressure: 1008 hPa
- Visibility: 10 km
- Cloud Cover: 85%
- UV Index: 2
...additional days...
Data Sources: QWeather
License: QWeather Developers License
城市查询响应
Location Information:
1. Beijing (ID: 101010100)
Location: 39.90499, 116.40529
Region: Beijing, Beijing, China
Timezone: Asia/Shanghai (UTC +8.0)
Type: city, Rank: 10
2. Beijing Shi (ID: 101010000)
Location: 39.90998, 116.40529
Region: Beijing, Beijing, China
Timezone: Asia/Shanghai (UTC +8.0)
Type: city, Rank: 10
Note: Use the ID (e.g., "101010100") in other weather tools to get weather information for this location.
Data Sources: QWeather
License: QWeather Developers License
在 MCP 中使用
将天气 MCP 服务添加到 MCP 配置中:
{
"mcpServers": {
"weather": {
"command": "npx",
"args": ["-y", "mcp-weather-api"]
}
}
}
QWeather API
该服务使用和风天气(QWeather)API 获取天气数据。软件包中包含 API 密钥,仅供演示使用。用于生产环境时,请从 QWeather 获取自己的 API 密钥。
使用的 API 端点
-
当前天气 -
/weather/now- 返回当前天气状况
- 响应包含一个
now对象,其中包含温度、湿度等信息 - API 文档
-
天气预报 -
/weather/3d、/weather/7d、/weather/10d、/weather/15d、/weather/30d- 返回不同时间范围的天气预报
- 响应包含一个
daily数组,其中包含每日预报数据 - API 文档
-
逐小时天气预报 -
/weather/24h- 返回未来 24 小时的逐小时天气预报
- 响应包含一个
hourly数组,其中包含逐小时预报数据 - API 文档
-
城市查询 -
/city/lookup- 按名称或坐标查询城市信息
- 返回城市 ID 和其他位置信息
- API 文档
API 响应结构
当前天气(/weather/now)
{
"code": "200",
"updateTime": "2021-11-15T16:35+08:00",
"now": {
"temp": "22.5",
"humidity": "65",
"text": "Partly cloudy",
"windSpeed": "10.2",
"windDir": "East",
"feelsLike": "24.0",
"pressure": "1012",
"vis": "10",
"cloud": "30",
"dew": "15.5"
}
}
天气预报(/weather/3d)
{
"code": "200",
"updateTime": "2021-11-15T16:35+08:00",
"fxLink": "http://hfx.link/2ax1",
"daily": [
{
"fxDate": "2021-11-15",
"sunrise": "06:58",
"sunset": "16:59",
"moonrise": "15:16",
"moonset": "03:40",
"moonPhase": "盈凸月",
"moonPhaseIcon": "803",
"tempMax": "12",
"tempMin": "-1",
"iconDay": "101",
"textDay": "多云",
"iconNight": "150",
"textNight": "晴",
"wind360Day": "45",
"windDirDay": "东北风",
"windScaleDay": "1-2",
"windSpeedDay": "3",
"wind360Night": "0",
"windDirNight": "北风",
"windScaleNight": "1-2",
"windSpeedNight": "3",
"humidity": "65",
"precip": "0.0",
"pressure": "1020",
"vis": "25",
"cloud": "4",
"uvIndex": "3"
}
// Additional days...
]
}
逐小时天气预报(/weather/24h)
{
"code": "200",
"updateTime": "2021-11-15T16:35+08:00",
"fxLink": "http://hfx.link/2ax1",
"hourly": [
{
"fxTime": "2021-11-15T17:00+08:00",
"temp": "11",
"icon": "150",
"text": "晴",
"wind360": "335",
"windDir": "西北风",
"windScale": "3-4",
"windSpeed": "20",
"humidity": "73",
"pop": "7",
"precip": "0.0",
"pressure": "1013",
"cloud": "10",
"dew": "7"
}
// Additional hours...
]
}
城市查询(/city/lookup)
{
"code": "200",
"location": [
{
"name": "Beijing",
"id": "101010100",
"lat": "39.90499",
"lon": "116.40529",
"adm2": "Beijing",
"adm1": "Beijing",
"country": "China",
"tz": "Asia/Shanghai",
"utcOffset": "+08:00",
"isDst": "0",
"type": "city",
"rank": "10",
"fxLink": "http://hfx.link/2ax1"
}
// Additional locations...
]
}
配置
你可以通过环境变量配置各种选项:
# API Configuration
export QWEATHER_API_KEY=your-api-key
export QWEATHER_API_URL=https://devapi.qweather.com/v7
export QWEATHER_GEO_API_URL=https://geoapi.qweather.com/v2
export WEATHER_DEFAULT_LOCATION=101010100 # Default location code or coordinates
# Default Options
export WEATHER_DEFAULT_UNITS=metric # or 'imperial'
export WEATHER_DEFAULT_LANGUAGE=en # language code
export WEATHER_INCLUDE_DETAILS=true # or 'false'
export WEATHER_FORECAST_DAYS=3 # number of days (max 30)
或者在 MCP 配置中配置:
{
"mcpServers": {
"weather": {
"command": "npx",
"args": ["-y", "mcp-weather-api"],
"env": {
"QWEATHER_API_KEY": "your-api-key",
"QWEATHER_API_URL": "https://devapi.qweather.com/v7",
"QWEATHER_GEO_API_URL": "https://geoapi.qweather.com/v2",
"WEATHER_DEFAULT_LOCATION": "101010100",
"WEATHER_DEFAULT_UNITS": "imperial",
"WEATHER_DEFAULT_LANGUAGE": "zh",
"WEATHER_INCLUDE_DETAILS": "true",
"WEATHER_FORECAST_DAYS": "7"
}
}
}
}
位置格式
你可以使用以下三种格式指定位置:
- 城市名称:例如,“New York”“London”“Beijing”
- 坐标:例如,“119.98,30.24”(经度,纬度)
- QWeather 位置 ID:例如,“101010100”(北京)
使用坐标时,格式必须为 longitude,latitude(例如,“119.98,30.24”),该坐标将直接传递给 QWeather API。
使用 lookupCity 工具查找合适的位置 ID,以便更精确地定位。
中国城市位置代码
对于中国城市,您可以使用 QWeather 位置 ID,以便更精确地定位。完整的中国城市代码列表可在 QWeather LocationList 仓库中找到。
此 CSV 文件包含中国城市的位置 ID,格式如下:
开发
先决条件
- Node.js 16 或更高版本
- npm 或 yarn
设置
- 克隆仓库
- 安装依赖项:
npm install
构建
npm run build
在开发模式下运行
npm run dev
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。