get_weather
Retrieve current weather conditions including temperature, humidity, wind speed, and atmospheric pressure for any city worldwide.
Instructions
Get current weather for a city
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | Yes | Name of the city to get weather for |
Implementation Reference
- index.js:85-113 (handler)The main handler function that fetches current weather data for a given city using the OpenWeatherMap API via axios, formats the response as MCP content, and handles errors.async getWeather(city) { if (!API_KEY) { throw new Error("OpenWeatherMap API key not found. Set OPENWEATHER_API_KEY environment variable."); } try { const response = await axios.get( `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${API_KEY}&units=metric` ); const weather = response.data; return { content: [ { type: "text", text: `Weather in ${weather.name}, ${weather.sys.country}: Temperature: ${weather.main.temp}°C (feels like ${weather.main.feels_like}°C) Condition: ${weather.weather[0].description} Humidity: ${weather.main.humidity}% Wind: ${weather.wind.speed} m/s Pressure: ${weather.main.pressure} hPa`, }, ], }; } catch (error) { throw new Error(`Failed to get weather for ${city}: ${error.message}`); } }
- index.js:37-46 (schema)Input schema definition for the get_weather tool, specifying a required 'city' string parameter.inputSchema: { type: "object", properties: { city: { type: "string", description: "Name of the city to get weather for", }, }, required: ["city"], },
- index.js:34-47 (registration)Registration of the get_weather tool in the ListToolsRequestSchema handler, including name, description, and input schema.{ name: "get_weather", description: "Get current weather for a city", inputSchema: { type: "object", properties: { city: { type: "string", description: "Name of the city to get weather for", }, }, required: ["city"], }, },
- index.js:73-75 (registration)Dispatch/registration logic in the CallToolRequestSchema handler that checks the tool name and invokes the getWeather handler.if (name === "get_weather") { return await this.getWeather(args.city); }