fetch-Weather
Get current weather conditions for any city by providing the location name. Retrieve temperature, conditions, and other weather data through simple city queries.
Instructions
Get the weather for a given location
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | Yes | city name like 'Bogotá' |
Implementation Reference
- main.ts:26-50 (handler)The handler function that fetches location coordinates via geocoding API, retrieves current weather data, determines condition using helper, and returns formatted text response.async (params) => { const info = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${params.city}`); const data = await info.json(); const latitude = data.results[0].latitude; const longitude = data.results[0].longitude; const weather = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t_weather=true`); const weatherData = await weather.json(); const temperature = weatherData.current_weather.temperature; const condition = getWeatherCondition(temperature); return { content: [ { type: "text", text: `The weather in ${params.city} is ${condition} with a temperature of ${temperature}°C`, } ] } }
- main.ts:23-25 (schema)Input schema using Zod for the 'city' parameter.{ city: z.string().describe("city name like 'Bogotá'") },
- main.ts:20-51 (registration)Registration of the 'fetch-Weather' tool with McpServer, specifying name, description, input schema, and handler function.server.tool( 'fetch-Weather', 'Get the weather for a given location', { city: z.string().describe("city name like 'Bogotá'") }, async (params) => { const info = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${params.city}`); const data = await info.json(); const latitude = data.results[0].latitude; const longitude = data.results[0].longitude; const weather = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t_weather=true`); const weatherData = await weather.json(); const temperature = weatherData.current_weather.temperature; const condition = getWeatherCondition(temperature); return { content: [ { type: "text", text: `The weather in ${params.city} is ${condition} with a temperature of ${temperature}°C`, } ] } } );
- main.ts:10-18 (helper)Helper utility to map temperature to a human-readable weather condition with emoji.function getWeatherCondition(temperature: number): string { if (temperature < 0) return "freezing ❄️"; if (temperature < 10) return "cold 🥶"; if (temperature < 15) return "cool 🌤️"; if (temperature < 20) return "mild 😊"; if (temperature < 25) return "warm ☀️"; if (temperature < 30) return "hot 🔥"; return "very hot 🌡️"; }