Skip to main content
Glama
marcusbai

Caiyun Weather MCP Server

by marcusbai

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
NameRequiredDescriptionDefault
languageNo语言zh_CN
latitudeYes纬度
longitudeYes经度
unitNo单位制 (metric: 公制, imperial: 英制)metric

Implementation Reference

  • 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),
          },
        ],
      };
  • 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'],
      },
    },
  • 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;
      }
    }
  • 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()
        }))
      };
    }
Install Server

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