getWeather
Retrieve current weather data for any location, including temperature, humidity, and conditions, with configurable units and language options.
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)Handler function that fetches current weather data for the given location using the wttr.in API and returns formatted weather information including temperature, humidity, wind speed, etc.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:17-25 (schema)Input schema defining the required 'location' parameter as a string.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 with MCP server, including name, description, input schema, and handler function.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}`); } } });