天气API服务

一个基于和风天气API的天气信息模型上下文协议(MCP)服务器,提供实时天气数据和天气预报功能。

  • by yoyooyooo
  • JavaScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 yoyooyooo/mcp-weather-server

使用说明

英语 | 中文

Weather 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"]
    }
  }
}

Q套管API

此服务器使用和风天气 (QWeather)API获取天气数据。API密钥包含在软件包中,用于演示目的。对于生产使用,您应该从获取您自己的API密钥 Q吃.

使用的API端点

  1. 当前天气 - /weather/now

    • 返回当前天气状况
    • 答复包括a now 带有温度、湿度等的物体。
    • API文档
  2. 天气预报 - /weather/3d, /weather/7d, /weather/10d, /weather/15d, /weather/30d

    • 返回不同时段的天气预报
    • 答复包括a daily 包含每日预测数据的数组
    • API文档
  3. 每小时天气预报 - /weather/24h

    • 返回未来24小时的每小时天气预报
    • 回复包括 hourly 包含每小时预测数据的数组
    • API文档
  4. 城市查询 - /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"
      }
    }
  }
}

位置格式

您可以用三种格式指定位置:

  1. 城市名称例如,“纽约”、“伦敦”、“北京”
  2. 坐标:例如,“119.98,30.24”(经度、纬度)
  3. Q餐厅位置ID例如,“101010100”(北京)

使用坐标时,格式必须为 longitude,latitude (例如,“119.98,30.24”),将直接传递给QWeather API。

使用 lookupCity 该工具用于查找适当的位置ID,以实现更精确的定位。

中国城市位置代码

对于中国城市,您可以使用QWeather位置ID,它提供了更精确的位置定位。中国城市代码的完整列表可以在 QWeather LocationList存储库.

此CSV文件包含中国城市的位置ID,格式如下:

发展

先决条件

  • Node.js 16或更高版本
  • npm或纱线

设置

  1. 克隆存储库
  2. 安装依赖项:
npm install

建筑

npm run build

在发展中奔跑

npm run dev