weather-get_hourly
Retrieve hourly weather forecasts for the next 12 hours to plan activities and prepare for changing conditions. Specify location and temperature units (metric/imperial) for accurate predictions.
Instructions
Get hourly weather forecast for the next 12 hours
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| location | Yes | The city or location for which to retrieve the weather forecast. | |
| units | No | Temperature unit system (metric for Celsius, imperial for Fahrenheit). Default is metric. |
Implementation Reference
- src/tools/WeatherTool.ts:34-105 (handler)Core execution logic for the 'weather-get_hourly' tool: validates inputs, queries AccuWeather API for location and hourly forecast, formats output, handles errors.export async function handler( args: { [x: string]: any }, extra: RequestHandlerExtra<any, any> ): Promise<{ content: TextContent[] }> { // Validate args using the Zod schema let validatedArgs: z.infer<typeof inputSchema>; try { validatedArgs = inputSchema.parse(args); } catch (error) { if (error instanceof z.ZodError) { const errorMessages = error.errors .map((e) => `${e.path.join('.')}: ${e.message}`) .join(', '); return { content: [{ type: 'text', text: `Invalid input: ${errorMessages}` }] }; } return { content: [{ type: 'text', text: 'An unexpected error occurred during input validation.' }] }; } const { location, units = "metric" } = validatedArgs; const apiKey = process.env.ACCUWEATHER_API_KEY; if (!apiKey) { return { content: [{ type: 'text', text: 'Error: AccuWeather API key not configured' }] }; } try { // Step 1: Get location key const locationUrl = `https://dataservice.accuweather.com/locations/v1/cities/search?apikey=${apiKey}&q=${encodeURIComponent(location)}`; const locationResp = await axios.get(locationUrl); if (!locationResp.data || locationResp.data.length === 0) { return { content: [{ type: 'text', text: `No location found for: ${location}` }] }; } const locationKey = locationResp.data[0].Key; // Step 2: Get forecast with location key const forecastUrl = `https://dataservice.accuweather.com/forecasts/v1/hourly/12hour/${locationKey}?apikey=${apiKey}&metric=${units === "metric" ? "true" : "false"}`; const forecastResp = await axios.get(forecastUrl); const data = forecastResp.data; if (!data || !Array.isArray(data) || data.length === 0) { return { content: [{ type: 'text', text: `No weather data available for location: ${location}` }] }; } const unitSymbol = units === "metric" ? "C" : "F"; const content: TextContent[] = data.map((hour: any) => ({ type: 'text', text: `${hour.DateTime}: ${hour.Temperature.Value}°${unitSymbol}, ${hour.IconPhrase}`, })); return { content }; } catch (error) { console.error("WeatherTool handler error:", error); let errorMessage = "An error occurred while fetching weather data."; if (axios.isAxiosError(error)) { if (error.response?.status === 401) { errorMessage = "Invalid AccuWeather API key. Please check your credentials."; } else if (error.response?.status === 404) { errorMessage = `Location not found: ${location}`; } else if (error.response) { errorMessage = `AccuWeather API error (${error.response.status}): ${error.response.data?.Message || error.message}`; } else if (error.request) { errorMessage = "Network error: Unable to connect to AccuWeather API."; } } return { content: [{ type: 'text', text: errorMessage }] }; } }
- src/index.ts:10-28 (registration)Defines the Tool metadata for 'weather-get_hourly', including name, description, and input schema.const hourlyWeatherTool: Tool = { name: "weather-get_hourly", description: "Get hourly weather forecast for the next 12 hours", inputSchema: { type: "object", properties: { location: { type: "string", description: "The city or location for which to retrieve the weather forecast." }, units: { type: "string", description: "Temperature unit system (metric for Celsius, imperial for Fahrenheit). Default is metric.", enum: ["metric", "imperial"] } }, required: ["location"] } };
- src/index.ts:69-71 (registration)Registers the available tools, including 'weather-get_hourly', in response to ListTools requests.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [hourlyWeatherTool, dailyWeatherTool], }));
- src/index.ts:83-85 (registration)Dispatches CallTool requests to the appropriate handler based on tool name 'weather-get_hourly'.if (request.params.name === "weather-get_hourly") { return await hourlyHandler(args, {} as RequestHandlerExtra<any, any>); } else if (request.params.name === "weather-get_daily") {
- src/tools/WeatherTool.ts:7-13 (schema)Zod schema for input validation used within the handler.export const inputShape = { location: z.string().min(1, "Location must be at least 1 character"), units: z.enum(["imperial", "metric"]).default("metric").optional().describe("Temperature unit system") }; export const inputSchema = z.object(inputShape).describe("Get hourly weather forecast");