fetch-weather
Retrieve current weather data for any city by providing the city name as input.
Instructions
Tool to fetch the weather of a city
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | Yes | Nombre de la ciudad |
Implementation Reference
- main.ts:20-49 (handler)The handler function that performs geocoding to find coordinates for the city and then fetches current weather data from Open-Meteo API, returning the JSON data as text content.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.length === 0) { return { content: [ { type: "text", text: `No se encontró información del clima 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,is_day,precipitation,rain&forecast_days=1`) const weatherData = await weatherResponse.json(); return { content: [ { type: "text", text: JSON.stringify(weatherData, null, 2), }, ], }; }
- main.ts:17-19 (schema)Input schema using Zod: requires a 'city' string parameter.{ city: z.string().describe('Nombre de la ciudad'), },
- main.ts:14-50 (registration)The server.tool call that registers the 'fetch-weather' tool with name, description, input schema, and handler function.server.tool( 'fetch-weather', 'Tool to fetch the weather of a city', { city: z.string().describe('Nombre de la ciudad'), }, 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.length === 0) { return { content: [ { type: "text", text: `No se encontró información del clima 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,is_day,precipitation,rain&forecast_days=1`) const weatherData = await weatherResponse.json(); return { content: [ { type: "text", text: JSON.stringify(weatherData, null, 2), }, ], }; } );