get_weather
Retrieve current weather data including temperature, conditions, humidity, wind speed, and atmospheric pressure for any specified 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 for the get_weather tool. It fetches current weather data for the 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)The input schema for the get_weather tool, defining the required 'city' parameter as a string.inputSchema: { type: "object", properties: { city: { type: "string", description: "Name of the city to get weather for", }, }, required: ["city"], },
- index.js:73-75 (registration)Dispatch logic in the CallToolRequest handler that routes 'get_weather' calls to the getWeather method.if (name === "get_weather") { return await this.getWeather(args.city); }
- index.js:34-47 (registration)Tool registration in the ListToolsRequest handler, providing name, description, and 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"], }, },