weather_forecast
Get hourly weather forecasts for Norwegian locations to plan activities and prepare for changing conditions. Specify a city or coordinates and forecast duration up to 72 hours.
Instructions
Get hourly weather forecast for a location in Norway using MET Norway (yr.no).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| location | Yes | Norwegian city name or lat,lon coordinates | |
| hours | No | Hours ahead (default 24, max 72) |
Implementation Reference
- src/index.js:138-166 (registration)Registration of weather_forecast tool with the MCP server, including the schema definition (location and hours parameters) and the handler function
server.tool( "weather_forecast", "Get hourly weather forecast for a location in Norway using MET Norway (yr.no).", { location: z.string().describe("Norwegian city name or lat,lon coordinates"), hours: z.number().min(1).max(72).optional().describe("Hours ahead (default 24, max 72)"), }, async ({ location, hours }) => { try { const loc = await getLocation(location); const data = await fetchForecast(loc.lat, loc.lon); const maxHours = hours || 24; const slice = data.properties.timeseries.slice(0, maxHours); const lines = [`## ${loc.name} — ${maxHours}h Forecast\n`]; for (const entry of slice) { const t = new Date(entry.time); const time = t.toLocaleString("nb-NO", { timeZone: "Europe/Oslo", weekday: "short", day: "numeric", month: "short", hour: "2-digit", minute: "2-digit" }); const inst = entry.data.instant.details; const symbol = entry.data.next_1_hours?.summary?.symbol_code || entry.data.next_6_hours?.summary?.symbol_code || ""; const precip = entry.data.next_1_hours?.details?.precipitation_amount; lines.push(`**${time}:** ${inst.air_temperature}°C, ${symbolToText(symbol)}, wind ${inst.wind_speed} m/s${precip != null ? `, ${precip} mm` : ""}`); } lines.push(`\n*MET Norway Locationforecast 2.0*`); return { content: [{ type: "text", text: lines.join("\n") }] }; } catch (err) { return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true }; } } ); - src/index.js:145-165 (handler)The main handler logic for weather_forecast tool that fetches forecast data, processes hourly entries, and formats output with temperature, weather conditions, wind, and precipitation
async ({ location, hours }) => { try { const loc = await getLocation(location); const data = await fetchForecast(loc.lat, loc.lon); const maxHours = hours || 24; const slice = data.properties.timeseries.slice(0, maxHours); const lines = [`## ${loc.name} — ${maxHours}h Forecast\n`]; for (const entry of slice) { const t = new Date(entry.time); const time = t.toLocaleString("nb-NO", { timeZone: "Europe/Oslo", weekday: "short", day: "numeric", month: "short", hour: "2-digit", minute: "2-digit" }); const inst = entry.data.instant.details; const symbol = entry.data.next_1_hours?.summary?.symbol_code || entry.data.next_6_hours?.summary?.symbol_code || ""; const precip = entry.data.next_1_hours?.details?.precipitation_amount; lines.push(`**${time}:** ${inst.air_temperature}°C, ${symbolToText(symbol)}, wind ${inst.wind_speed} m/s${precip != null ? `, ${precip} mm` : ""}`); } lines.push(`\n*MET Norway Locationforecast 2.0*`); return { content: [{ type: "text", text: lines.join("\n") }] }; } catch (err) { return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true }; } } - src/index.js:99-104 (helper)fetchForecast helper function that makes the actual API call to MET Norway Locationforecast 2.0 API to retrieve weather data for given coordinates
async function fetchForecast(lat, lon) { const url = `${BASE_URL}/complete?lat=${lat.toFixed(4)}&lon=${lon.toFixed(4)}`; const res = await fetch(url, { headers: { "User-Agent": USER_AGENT } }); if (!res.ok) throw new Error(`MET Norway API error (${res.status}): ${await res.text()}`); return res.json(); } - src/index.js:91-97 (helper)getLocation helper function that resolves location input (city name or lat,lon coordinates) to actual coordinates using predefined cities or geocoding API
async function getLocation(input) { const loc = resolveLocation(input); if (loc) return loc; const geo = await geocode(input); if (geo) return geo; throw new Error(`Could not find location "${input}" in Norway. Try a city name or lat,lon coordinates.`); } - src/index.js:63-67 (helper)symbolToText helper function that converts MET Norway weather symbol codes to human-readable weather condition descriptions
function symbolToText(symbol) { if (!symbol) return "Unknown"; const base = symbol.replace(/_(day|night|polartwilight)$/, ""); return SYMBOL_MAP[base] || base; }