getWeather
Retrieve current weather conditions for any city using real-time data from the MCP Weather Server.
Instructions
Get the current weather for a given location
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | Yes | The city to get the weather for |
Implementation Reference
- src/services/weather.ts:15-59 (handler)The core handler function in WeatherService that performs geocoding, fetches weather data from Open-Meteo APIs, sanitizes input, and returns formatted JSON.async getWeather(input: WeatherInput): Promise<string> { try { // Validate and sanitize input if (!input.city || input.city.trim() === '') { return 'Error: City name cannot be empty'; } const sanitizedCity = this.sanitizeInput(input.city); if (sanitizedCity === '') { return 'Error: Invalid city name provided'; } // Geocoding const geocodeResponse = await fetch( `${WeatherService.GEOCODING_API}?name=${encodeURIComponent(sanitizedCity)}&count=1&language=en&format=json` ); if (!geocodeResponse.ok) { return `Error fetching location data: ${geocodeResponse.status} ${geocodeResponse.statusText}`; } const geocodeData: GeocodingResult = await geocodeResponse.json(); // Handle city not found if (!geocodeData.results || geocodeData.results.length === 0) { return 'Error: City not found. Please check the spelling and try again.'; } // Get weather data const { latitude, longitude } = geocodeData.results[0]; const weatherResponse = await fetch( `${WeatherService.WEATHER_API}?latitude=${latitude}&longitude=${longitude}&hourly=temperature_2m&models=ukmo_seamless¤t=temperature_2m,apparent_temperature,is_day,rain` ); if (!weatherResponse.ok) { return `Error fetching weather data: ${weatherResponse.status} ${weatherResponse.statusText}`; } const weatherData: WeatherData = await weatherResponse.json(); return JSON.stringify(weatherData, null, 2); } catch (error) { return `Error retrieving weather: ${error instanceof Error ? error.message : 'Unknown error'}`; } }
- src/types/weather.ts:28-32 (schema)Zod schema defining the input as { city: string } and the inferred TypeScript type WeatherInput.export const WeatherSchema = z.object({ city: z.string().min(1, "City name cannot be empty").describe("The city to get the weather for") }); export type WeatherInput = z.infer<typeof WeatherSchema>;
- src/utils/registry.ts:10-14 (registration)Tool registration in the service registry, including name, description, schema, and handler delegating to WeatherService.getWeather.name: "getWeather", description: "Get the current weather for a given location", inputSchema: WeatherSchema, handler: async (input) => await weatherService.getWeather(input) }
- src/main.ts:17-30 (registration)Direct MCP server tool registration calling the WeatherService handler and formatting response."getWeather", "Get the current weather for a given location", { city: z.string().describe("The city to get the weather for") }, async ({ city }: { city: string }) => { const result = await weatherService.getWeather({ city }); return { content: [ { type: "text", text: result } ] }; }