Skip to main content
Glama
marcusbai

Caiyun Weather MCP Server

by marcusbai

get_daily_forecast

Retrieve daily weather forecasts for up to 15 days by providing latitude, longitude, and optional parameters like language and unit system. Supports Chinese and English.

Instructions

获取天级天气预报

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
daily_stepsNo每日预报天数 (1-15)
languageNo语言zh_CN
latitudeYes纬度
longitudeYes经度
unitNo单位制 (metric: 公制, imperial: 英制)metric

Implementation Reference

  • MCP tool handler for 'get_daily_forecast': validates input parameters, calls CaiyunWeatherService.getDaily(), formats the response using formatDailyData(), and returns JSON stringified content.
    case 'get_daily_forecast': { if (!this.isValidLocationArgs(args)) { throw new McpError( ErrorCode.InvalidParams, '无效的位置参数' ); } const { longitude, latitude, daily_steps = 5 } = args; const weatherData = await weatherService.getDaily(longitude, latitude, daily_steps); return { content: [ { type: 'text', text: JSON.stringify(weatherService.formatDailyData(weatherData), null, 2), }, ], }; }
  • src/index.ts:253-289 (registration)
    Tool registration in the MCP server's tools list, including name, description, and input schema definition.
    { name: 'get_daily_forecast', description: '获取天级天气预报', inputSchema: { type: 'object', properties: { longitude: { type: 'number', description: '经度', }, latitude: { type: 'number', description: '纬度', }, daily_steps: { type: 'number', description: '每日预报天数 (1-15)', minimum: 1, maximum: 15, default: 5, }, language: { type: 'string', enum: ['zh_CN', 'en_US'], description: '语言', default: 'zh_CN', }, unit: { type: 'string', enum: ['metric', 'imperial'], description: '单位制 (metric: 公制, imperial: 英制)', default: 'metric', }, }, required: ['longitude', 'latitude'], }, },
  • Core helper method in CaiyunWeatherService that makes API call to Caiyun weather service for daily forecast data.
    async getDaily(longitude: number, latitude: number, dailysteps: number = 5): Promise<CaiyunWeatherResponse> { try { const url = `${this.baseUrl}/${this.apiKey}/${longitude},${latitude}/daily`; const response = await axios.get<CaiyunWeatherResponse>(url, { params: { dailysteps: Math.min(Math.max(dailysteps, 1), 15), lang: this.language, unit: this.unit } }); return response.data; } catch (error) { if (axios.isAxiosError(error)) { throw new Error(`彩云天气API错误: ${error.response?.data?.error || error.message}`); } throw error; } }
  • Helper method to format the raw daily forecast data from Caiyun API into a structured, readable object with mapped weather descriptions and indices.
    formatDailyData(data: CaiyunWeatherResponse) { const daily = data.result.daily; if (!daily) { throw new Error('没有每日天气预报数据'); } return { location: data.location, server_time: new Date(data.server_time * 1000).toISOString(), forecast: daily.temperature.map((temp, index) => ({ date: temp.date, temperature: { max: temp.max, min: temp.min, avg: temp.avg }, temperature_day: daily.temperature_08h_20h?.[index] ? { max: daily.temperature_08h_20h[index].max, min: daily.temperature_08h_20h[index].min, avg: daily.temperature_08h_20h[index].avg } : undefined, temperature_night: daily.temperature_20h_32h?.[index] ? { max: daily.temperature_20h_32h[index].max, min: daily.temperature_20h_32h[index].min, avg: daily.temperature_20h_32h[index].avg } : undefined, weather: this.getSkyconText(daily.skycon[index].value), weather_code: daily.skycon[index].value, weather_day: this.getSkyconText(daily.skycon_08h_20h[index].value), weather_day_code: daily.skycon_08h_20h[index].value, weather_night: this.getSkyconText(daily.skycon_20h_32h[index].value), weather_night_code: daily.skycon_20h_32h[index].value, wind: { speed: daily.wind[index].avg.speed, direction: daily.wind[index].avg.direction }, wind_day: daily.wind_08h_20h?.[index] ? { speed: daily.wind_08h_20h[index].avg.speed, direction: daily.wind_08h_20h[index].avg.direction } : undefined, wind_night: daily.wind_20h_32h?.[index] ? { speed: daily.wind_20h_32h[index].avg.speed, direction: daily.wind_20h_32h[index].avg.direction } : undefined, humidity: daily.humidity[index].avg, cloudrate: daily.cloudrate[index].avg, pressure: daily.pressure[index].avg, visibility: daily.visibility[index].avg, precipitation: { max: daily.precipitation[index].max, min: daily.precipitation[index].min, avg: daily.precipitation[index].avg, probability: daily.precipitation[index].probability, type: this.getPrecipitationTypeText(daily.precipitation[index].type) }, air_quality: { aqi: daily.air_quality.aqi[index].avg.chn, pm25: daily.air_quality.pm25[index].avg, trend: daily.air_quality.aqi[index].trend, primary_pollutant: daily.air_quality.primary_pollutant?.[index]?.value }, astro: { sunrise: daily.astro[index].sunrise.time, sunset: daily.astro[index].sunset.time }, life_index: { comfort: daily.life_index.comfort?.[index]?.desc || '暂无数据', ultraviolet: daily.life_index.ultraviolet?.[index]?.desc || '暂无数据', carWashing: daily.life_index.carWashing?.[index]?.desc || '暂无数据', dressing: daily.life_index.dressing?.[index]?.desc || '暂无数据', coldRisk: daily.life_index.coldRisk?.[index]?.desc || '暂无数据', sport: daily.life_index.sport?.[index]?.desc, travel: daily.life_index.travel?.[index]?.desc } })) }; }

Other Tools

Related Tools

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/marcusbai/caiyun-weather-mcp'

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