Skip to main content
Glama
ParthibanRajasekaran

MCP Weather Server

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
NameRequiredDescriptionDefault
cityYesThe city to get the weather for

Implementation Reference

  • 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'}`;
        }
    }
  • 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>;
  • 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
                }
            ]
        };
    }
Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ParthibanRajasekaran/mcp-weather'

If you have feedback or need assistance with the MCP directory API, please join our Discord server