get_weather_forecast
Retrieve weather forecasts for cities in Portugal using IPMA data to plan activities and prepare for conditions.
Instructions
Obter previsão meteorológica para uma cidade específica em Portugal
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | Yes | Nome da cidade (ex: Lisboa, Porto, Coimbra, Faro, etc.) | |
| days | No | Número de dias de previsão (máximo 10) |
Implementation Reference
- src/index.ts:234-297 (handler)Implements the core tool logic: fetches location ID, retrieves forecast data and weather types from IPMA API, formats and limits to specified days, returns formatted text response.
private async getWeatherForecast(city: string, days: number) { try { // Primeiro, obter a lista de locais para encontrar o globalIdLocal const locationsResponse = await fetch(`${this.baseUrl}/distrits-islands.json`); const locationsData = await locationsResponse.json() as ApiResponse<Location>; const location = locationsData.data.find((loc: Location) => loc.local.toLowerCase().includes(city.toLowerCase()) ); if (!location) { return { content: [ { type: "text", text: `Cidade "${city}" não encontrada. Use get_locations para ver cidades disponíveis.` } ] }; } // Obter previsão para o local encontrado const forecastResponse = await fetch( `${this.baseUrl}/forecast/meteorology/cities/daily/${location.globalIdLocal}.json` ); const forecastData = await forecastResponse.json() as ApiResponse<WeatherForecast>; // Obter tipos de tempo para descrições const weatherTypesResponse = await fetch(`${this.baseUrl}/weather-type-classe.json`); const weatherTypesData = await weatherTypesResponse.json() as ApiResponse<WeatherType>; const weatherTypes = weatherTypesData.data.reduce((acc: any, item: WeatherType) => { acc[item.idWeatherType] = item; return acc; }, {}); const limitedData = forecastData.data.slice(0, days); let result = `📍 **Previsão para ${location.local}**\n\n`; result += `📍 Coordenadas: ${location.latitude}, ${location.longitude}\n`; result += `🕐 Última atualização: ${forecastData.dataUpdate}\n\n`; limitedData.forEach((day: WeatherForecast) => { const weatherDesc = weatherTypes[day.idWeatherType]?.descWeatherTypePT || "Desconhecido"; result += `📅 **${day.forecastDate}**\n`; result += `🌡️ Temperatura: ${day.tMin}°C - ${day.tMax}°C\n`; result += `☁️ Condições: ${weatherDesc}\n`; result += `🌧️ Probabilidade de precipitação: ${day.precipitaProb}%\n`; result += `💨 Vento: ${day.predWindDir}\n\n`; }); return { content: [ { type: "text", text: result } ] }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); throw new McpError(ErrorCode.InternalError, `Erro ao obter previsão: ${errorMessage}`); } } - src/index.ts:132-150 (registration)Registers the 'get_weather_forecast' tool in the ListToolsRequestSchema handler with name, description, and input schema definition.
{ name: "get_weather_forecast", description: "Obter previsão meteorológica para uma cidade específica em Portugal", inputSchema: { type: "object", properties: { city: { type: "string", description: "Nome da cidade (ex: Lisboa, Porto, Coimbra, Faro, etc.)" }, days: { type: "number", description: "Número de dias de previsão (máximo 10)", default: 5 } }, required: ["city"] } }, - src/index.ts:135-149 (schema)Defines the input schema for the tool, specifying parameters 'city' (required string) and 'days' (optional number, default 5).
inputSchema: { type: "object", properties: { city: { type: "string", description: "Nome da cidade (ex: Lisboa, Porto, Coimbra, Faro, etc.)" }, days: { type: "number", description: "Número de dias de previsão (máximo 10)", default: 5 } }, required: ["city"] } - src/index.ts:206-210 (handler)Dispatcher case in CallToolRequestSchema handler that validates input and invokes the getWeatherForecast method.
case "get_weather_forecast": if (!toolArgs?.city) { throw new McpError(ErrorCode.InvalidParams, "City parameter is required"); } return await this.getWeatherForecast(toolArgs.city, toolArgs.days || 5);