getWeather
Retrieve real-time weather data for any location: temperature, humidity, and conditions. Specify units (metric/imperial) and language for tailored responses via the MCP Weather Server.
Instructions
Get current weather information for a location. Returns weather data including temperature, humidity, and conditions.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| location | No | The location to get weather for (city name, zip code, coordinates, etc.). If not provided, default location will be used. | |
| options | No | Weather configuration options, all fields are optional |
Implementation Reference
- index.js:26-47 (handler)The handler function that executes the get_weather tool logic, fetching weather data from wttr.in API using axios and returning formatted weather information.handler: async ({ location }) => { try { // Note: In a real implementation, you would use a proper API key // This is using a free weather API that doesn't require authentication const response = await axios.get(`https://wttr.in/${encodeURIComponent(location)}?format=j1`); const weatherData = response.data; return { location: location, current_condition: weatherData.current_condition[0], weather_description: weatherData.current_condition[0].weatherDesc[0].value, temperature_C: weatherData.current_condition[0].temp_C, temperature_F: weatherData.current_condition[0].temp_F, humidity: weatherData.current_condition[0].humidity, wind_speed: weatherData.current_condition[0].windspeedKmph }; } catch (error) { console.error('Error fetching weather:', error); throw new Error(`Failed to get weather for ${location}: ${error.message}`); } }
- index.js:16-25 (schema)Input schema (parameters) for the get_weather tool, requiring a 'location' string.parameters: { type: 'object', properties: { location: { type: 'string', description: 'The city name or location to get weather for' } }, required: ['location'] },
- index.js:13-48 (registration)Registration of the get_weather tool using server.registerTool, including name, description, schema, and inline handler.server.registerTool({ name: 'get_weather', description: 'Get current weather information for a location', parameters: { type: 'object', properties: { location: { type: 'string', description: 'The city name or location to get weather for' } }, required: ['location'] }, handler: async ({ location }) => { try { // Note: In a real implementation, you would use a proper API key // This is using a free weather API that doesn't require authentication const response = await axios.get(`https://wttr.in/${encodeURIComponent(location)}?format=j1`); const weatherData = response.data; return { location: location, current_condition: weatherData.current_condition[0], weather_description: weatherData.current_condition[0].weatherDesc[0].value, temperature_C: weatherData.current_condition[0].temp_C, temperature_F: weatherData.current_condition[0].temp_F, humidity: weatherData.current_condition[0].humidity, wind_speed: weatherData.current_condition[0].windspeedKmph }; } catch (error) { console.error('Error fetching weather:', error); throw new Error(`Failed to get weather for ${location}: ${error.message}`); } } });