getWeather
Fetch real-time weather data for any city with this tool. Ideal for quick, accurate weather updates using 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)Core handler implementation for the getWeather tool. Performs input validation and sanitization, geocoding via Open-Meteo API, and fetches current and hourly weather data.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/main.ts:16-31 (registration)MCP server registration of the getWeather tool, including inline Zod schema and handler that delegates to WeatherService.getWeather.server.tool( "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 } ] }; } );
- src/types/weather.ts:28-32 (schema)Zod schema definition for getWeather input (WeatherSchema) and corresponding TypeScript type (WeatherInput). Used by the service and registry.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:9-14 (registration)Registration of getWeather tool within the service registry utility, using WeatherSchema and 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/services/weather.ts:7-13 (helper)Helper method in WeatherService for sanitizing city input to prevent injection attacks and limit length.private sanitizeInput(input: string): string { // Remove potentially dangerous characters and limit length return input .replace(/[<>'";&()]/g, '') // Remove dangerous characters .trim() .substring(0, 100); // Limit length }