fetch-weather
Retrieve weather data for any city using the Open-Meteo API. Input the city name to access accurate weather information directly.
Instructions
Tool to fetch weather information
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | Yes | City name |
Implementation Reference
- main.ts:19-46 (handler)Handler function that performs geocoding for the city, fetches current weather data from Open-Meteo API, and returns the weather data as a JSON string.async ({ city }) => { const response = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${city}&count=10&language=en&format=json`); const data = await response.json(); if (!data.results || data.results.length === 0) { return { content: [ { type: 'text', text: `No se encontró información para la ciudad: ${city}.`, } ] }; } const { latitude, longitude } = data.results[0]; const weatherResponse = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&hourly=temperature_2m¤t=temperature_2m,precipitation,is_day,rain&forecast_days=1`); const weatherData = await weatherResponse.json(); return { content: [ { type: 'text', text: JSON.stringify(weatherData, null, 2), } ] }; }
- main.ts:16-18 (schema)Zod schema defining the input parameter 'city' as a string.{ city: z.string().describe('City name'), },
- main.ts:13-47 (registration)Registration of the 'fetch-weather' tool using McpServer.tool() method, including name, description, input schema, and handler function.server.tool( 'fetch-weather', 'Tool to fetch weather information', { city: z.string().describe('City name'), }, async ({ city }) => { const response = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${city}&count=10&language=en&format=json`); const data = await response.json(); if (!data.results || data.results.length === 0) { return { content: [ { type: 'text', text: `No se encontró información para la ciudad: ${city}.`, } ] }; } const { latitude, longitude } = data.results[0]; const weatherResponse = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&hourly=temperature_2m¤t=temperature_2m,precipitation,is_day,rain&forecast_days=1`); const weatherData = await weatherResponse.json(); return { content: [ { type: 'text', text: JSON.stringify(weatherData, null, 2), } ] }; } );