get-forecast
Retrieve a 5-day weather forecast for any city using location details. Input city name and optional country code to fetch accurate weather predictions.
Instructions
Get 5-day weather forecast for a location
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | Yes | City name (e.g. Beijing, London) | |
| country | No | Country code (e.g. CN, GB) |
Implementation Reference
- src/weather/controllers/weather.ts:27-46 (handler)Main implementation of the get-forecast tool logic: fetches raw forecast data from service and formats it into a readable 5-day text summary.async getForecast(city: string, country?: string): Promise<string> { const forecastData = await this.weatherService.getForecast(city, country); if (!forecastData) { return `Failed to retrieve forecast data for ${country ? `${city}, ${country}` : city}`; } const forecasts = forecastData.list .filter((_, index) => index % 8 === 0) // Get one forecast per day .map(forecast => [ `\nDate: ${forecast.dt_txt.split(" ")[0]}`, `Temperature: ${forecast.main.temp}°C (Feels like: ${forecast.main.feels_like}°C)`, `Conditions: ${forecast.weather[0].main} - ${forecast.weather[0].description}`, `Humidity: ${forecast.main.humidity}%`, `Wind Speed: ${forecast.wind.speed} m/s`, "---" ].join("\n")); return `5-day forecast for ${forecastData.city.name}, ${forecastData.city.country}:\n${forecasts.join("\n")}`; }
- src/weather/index.ts:42-60 (registration)Registers the 'get-forecast' MCP tool with description, input schema (Zod), and handler that delegates to WeatherController.getForecast().server.tool( "get-forecast", "Get 5-day weather forecast for a location", { city: z.string().describe("City name (e.g. Beijing, London)"), country: z.string().optional().describe("Country code (e.g. CN, GB)") }, async ({ city, country }) => { const forecastText = await weatherController.getForecast(city, country); return { content: [ { type: "text", text: forecastText, }, ], }; } );
- src/weather/service/weather.ts:40-43 (helper)Helper method in WeatherService that constructs the API query and fetches raw forecast data from OpenWeatherMap.async getForecast(city: string, country?: string): Promise<ForecastData | null> { const query = country ? `${city},${country}` : city; return this.makeRequest<ForecastData>("forecast", { q: query }); }
- src/weather/service/types.ts:22-43 (schema)TypeScript interface defining the structure of the OpenWeather forecast response data for type safety.export interface ForecastData { list: Array<{ dt_txt: string; main: { temp: number; feels_like: number; humidity: number; }; weather: Array<{ main: string; description: string; }>; wind: { speed: number; deg: number; }; }>; city: { name: string; country: string; }; }