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
| Name | Required | Description | Default |
|---|---|---|---|
| daily_steps | No | 每日预报天数 (1-15) | |
| language | No | 语言 | zh_CN |
| latitude | Yes | 纬度 | |
| longitude | Yes | 经度 | |
| unit | No | 单位制 (metric: 公制, imperial: 英制) | metric |
Implementation Reference
- src/index.ts:516-536 (handler)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'], }, }, - src/caiyun-service.ts:108-126 (helper)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; } } - src/caiyun-service.ts:350-426 (helper)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 } })) }; }