get_hourly_forecast
Retrieve hyperlocal hourly weather forecasts for specific coordinates, with customizable language, unit system, and forecast duration up to 360 hours.
Instructions
获取小时级天气预报
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| hourly_steps | No | 小时预报数量 (1-360) | |
| language | No | 语言 | zh_CN |
| latitude | Yes | 纬度 | |
| longitude | Yes | 经度 | |
| unit | No | 单位制 (metric: 公制, imperial: 英制) | metric |
Implementation Reference
- src/index.ts:216-252 (registration)Registration of the get_hourly_forecast tool in the ListTools response, including description and input schema.{ name: 'get_hourly_forecast', description: '获取小时级天气预报', inputSchema: { type: 'object', properties: { longitude: { type: 'number', description: '经度', }, latitude: { type: 'number', description: '纬度', }, hourly_steps: { type: 'number', description: '小时预报数量 (1-360)', minimum: 1, maximum: 360, default: 24, }, 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/index.ts:494-514 (handler)Handler case in CallToolRequestSchema for executing get_hourly_forecast: validates args, calls CaiyunWeatherService.getHourly, formats response.case 'get_hourly_forecast': { if (!this.isValidLocationArgs(args)) { throw new McpError( ErrorCode.InvalidParams, '无效的位置参数' ); } const { longitude, latitude, hourly_steps = 24 } = args; const weatherData = await weatherService.getHourly(longitude, latitude, hourly_steps); return { content: [ { type: 'text', text: JSON.stringify(weatherService.formatHourlyData(weatherData), null, 2), }, ], }; }
- src/caiyun-service.ts:81-98 (handler)Core handler: Makes HTTP request to Caiyun API hourly forecast endpoint with validated parameters.async getHourly(longitude: number, latitude: number, hourlysteps: number = 24): Promise<CaiyunWeatherResponse> { try { const url = `${this.baseUrl}/${this.apiKey}/${longitude},${latitude}/hourly`; const response = await axios.get<CaiyunWeatherResponse>(url, { params: { hourlysteps: Math.min(Math.max(hourlysteps, 1), 360), 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:306-343 (helper)Helper function that processes and structures the raw hourly forecast data for output.formatHourlyData(data: CaiyunWeatherResponse) { const hourly = data.result.hourly; if (!hourly) { throw new Error('没有小时级天气预报数据'); } return { location: data.location, server_time: new Date(data.server_time * 1000).toISOString(), description: hourly.description, forecast: hourly.temperature.map((temp, index) => ({ time: temp.datetime, temperature: temp.value, apparent_temperature: hourly.apparent_temperature?.[index]?.value, weather: this.getSkyconText(hourly.skycon[index].value), weather_code: hourly.skycon[index].value, wind: { speed: hourly.wind[index].speed, direction: hourly.wind[index].direction }, humidity: hourly.humidity[index].value, cloudrate: hourly.cloudrate[index].value, pressure: hourly.pressure[index].value, visibility: hourly.visibility[index].value, precipitation: { value: hourly.precipitation[index].value, probability: hourly.precipitation[index].probability, type: this.getPrecipitationTypeText(hourly.precipitation[index].type) }, air_quality: { aqi: hourly.air_quality.aqi[index].value.chn, pm25: hourly.air_quality.pm25[index].value, trend: hourly.air_quality.aqi[index].trend, primary_pollutant: hourly.air_quality.primary_pollutant?.[index]?.value } })) }; }