Skip to main content
Glama

getWeather

Retrieve current weather conditions for any city using the OpenWeatherMap API. Provide a city name to get temperature, humidity, and other weather data.

Instructions

Get current weather for a city

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
cityYesName of the city

Implementation Reference

  • index.js:12-41 (handler)
    Core handler logic for fetching weather data from OpenWeatherMap API, formatting response as MCP content, and handling errors.
    async getWeather(city) { if (!this.apiKey) { throw new Error('OPENWEATHER_API_KEY environment variable is not set'); } try { const response = await axios.get( `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${this.apiKey}&units=metric` ); const data = response.data; return { content: [{ type: 'text', text: JSON.stringify({ city: data.name, temperature: data.main.temp, description: data.weather[0].description, }, null, 2), }], }; } catch (error) { return { content: [{ type: 'text', text: `Error: ${error.message}`, }], isError: true, }; } }
  • index.js:52-57 (registration)
    Registers the 'getWeather' tool with the MCP server, including description, input schema, and an inline handler that delegates to WeatherService.getWeather.
    server.registerTool('getWeather', { description: 'Get current weather for a city', inputSchema: { city: z.string().describe('Name of the city'), }, }, async ({ city }) => await weatherService.getWeather(city));
  • Zod input schema defining the 'city' parameter as a required string.
    inputSchema: { city: z.string().describe('Name of the city'), },
  • index.js:7-42 (helper)
    WeatherService class providing the getWeather method and managing the OpenWeather API key.
    class WeatherService { constructor(apiKey = process.env.OPENWEATHER_API_KEY) { this.apiKey = apiKey; } async getWeather(city) { if (!this.apiKey) { throw new Error('OPENWEATHER_API_KEY environment variable is not set'); } try { const response = await axios.get( `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${this.apiKey}&units=metric` ); const data = response.data; return { content: [{ type: 'text', text: JSON.stringify({ city: data.name, temperature: data.main.temp, description: data.weather[0].description, }, null, 2), }], }; } catch (error) { return { content: [{ type: 'text', text: `Error: ${error.message}`, }], isError: true, }; } } }
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/bensinclair/weather-mcp-server'

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