get_weather_alert
Retrieve real-time weather alerts for specific coordinates, supporting multiple languages and unit systems. Ideal for monitoring severe weather conditions and ensuring safety.
Instructions
获取天气预警信息
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| language | No | 语言 | zh_CN |
| latitude | Yes | 纬度 | |
| longitude | Yes | 经度 | |
| unit | No | 单位制 (metric: 公制, imperial: 英制) | metric |
Implementation Reference
- src/index.ts:538-557 (handler)The main handler for the 'get_weather_alert' tool in the CallToolRequestSchema. Validates input, calls CaiyunWeatherService.getAlert(), formats the response using formatAlertData(), and returns the JSON string.case 'get_weather_alert': { if (!this.isValidLocationArgs(args)) { throw new McpError( ErrorCode.InvalidParams, '无效的位置参数' ); } const { longitude, latitude } = args; const weatherData = await weatherService.getAlert(longitude, latitude); return { content: [ { type: 'text', text: JSON.stringify(weatherService.formatAlertData(weatherData), null, 2), }, ], };
- src/index.ts:290-319 (schema)Input schema and metadata definition for the 'get_weather_alert' tool, returned in ListToolsRequestSchema response.{ name: 'get_weather_alert', description: '获取天气预警信息', inputSchema: { type: 'object', properties: { longitude: { type: 'number', description: '经度', }, latitude: { type: 'number', description: '纬度', }, 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:134-154 (helper)Helper method in CaiyunWeatherService that fetches weather alert data from Caiyun API by calling the /weather endpoint with alert: true.async getAlert(longitude: number, latitude: number): Promise<CaiyunWeatherResponse> { try { const url = `${this.baseUrl}/${this.apiKey}/${longitude},${latitude}/weather`; const response = await axios.get<CaiyunWeatherResponse>(url, { params: { alert: true, dailysteps: 1, hourlysteps: 1, 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:433-461 (helper)Helper method that formats the alert data from the API response into a structured object with alert details.formatAlertData(data: CaiyunWeatherResponse) { const alert = data.result.alert; if (!alert) { return { location: data.location, server_time: new Date(data.server_time * 1000).toISOString(), alerts: [] }; } return { location: data.location, server_time: new Date(data.server_time * 1000).toISOString(), alerts: alert.content.map(item => ({ title: item.title, description: item.description, code: item.code, source: item.source, location: item.location, region: { province: item.province, city: item.city, county: item.county, adcode: item.adcode }, pub_time: new Date(item.pubtimestamp * 1000).toISOString() })) }; }