get_weather_forecast
Retrieve detailed 36-hour weather forecasts for Taiwan by specifying a county or city name in Traditional Chinese. This tool connects to the Central Weather Administration API, providing accurate weather data for all regions in Taiwan.
Instructions
Get Taiwan weather forecast for the next 36 hours by county/city name. Available locations: 宜蘭縣, 花蓮縣, 臺東縣, 澎湖縣, 金門縣, 連江縣, 臺北市, 新北市, 桃園市, 臺中市, 臺南市, 高雄市, 基隆市, 新竹縣, 新竹市, 苗栗縣, 彰化縣, 南投縣, 雲林縣, 嘉義縣, 嘉義市, 屏東縣
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| locationName | Yes | Taiwan county/city name in Traditional Chinese (e.g., 臺北市, 高雄市) |
Implementation Reference
- cwa-server.js:96-113 (handler)Executes the get_weather_forecast tool: destructures locationName from arguments, validates presence and validity against Taiwanese locations, calls fetchCWAData with F-C0032-001 endpoint, returns formatted JSON of forecast records.case "get_weather_forecast": { const { locationName } = request.params.arguments; if (!locationName) { throw new Error("locationName parameter is required"); } if (!LOCATION_NAMES.includes(locationName)) { throw new Error(`Invalid locationName. Available options: ${LOCATION_NAMES.join(', ')}`); } // F-C0032-001 is the endpoint for 36-hour weather forecast const result = await fetchCWAData('F-C0032-001', { locationName }); return { content: [{ type: "text", text: JSON.stringify(result.records, null, 2) }], }; }
- cwa-server.js:73-82 (schema)Input schema definition for get_weather_forecast tool, specifying locationName as required string parameter with description.inputSchema: { type: "object", properties: { locationName: { type: "string", description: "Taiwan county/city name in Traditional Chinese (e.g., 臺北市, 高雄市)" } }, required: ["locationName"] }
- cwa-server.js:67-86 (registration)Registers the get_weather_forecast tool in the ListToolsRequestSchema handler by returning it in the tools array with name, description, and schema.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ { name: "get_weather_forecast", description: `Get Taiwan weather forecast for the next 36 hours by county/city name. Available locations: ${LOCATION_NAMES.join(', ')}`, inputSchema: { type: "object", properties: { locationName: { type: "string", description: "Taiwan county/city name in Traditional Chinese (e.g., 臺北市, 高雄市)" } }, required: ["locationName"] } } ], }; });
- cwa-server.js:39-64 (helper)Helper utility function used by the tool to make authenticated requests to the CWA Open Data API, handling API key, errors, and returning parsed data.async function fetchCWAData(endpoint, params = {}) { const apiKey = process.env.CWA_API_KEY; if (!apiKey) { throw new Error('CWA API key not set. Set the CWA_API_KEY environment variable.'); } const url = `${API_BASE_URL}/${endpoint}`; try { const response = await axios.get(url, { params: { ...params, Authorization: apiKey } }); if (response.status !== 200) { throw new Error(`CWA API Error: ${response.status} ${response.statusText}`); } return response.data; } catch (error) { throw new Error(`Error fetching CWA data: ${error.message}`); } }