Skip to main content
Glama
robobobby

mcp-swedish-weather

by robobobby

current_weather

Get current weather conditions for any location in Sweden using SMHI data. Provide a Swedish city name or coordinates to receive detailed meteorological information.

Instructions

Get current weather for a location in Sweden using SMHI.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
locationYesSwedish city name or lat,lon coordinates

Implementation Reference

  • Main handler function for current_weather tool that takes a location parameter, resolves it using helper functions, fetches forecast data from SMHI API, finds the closest time entry to current time, extracts weather parameters (temperature, humidity, wind, pressure, precipitation), and returns formatted weather data as text.
    async ({ location }) => {
      try {
        const loc = await getLocation(location);
        const data = await fetchForecast(loc.lat, loc.lon);
        const ts = data.timeSeries;
        if (!ts?.length) throw new Error("No forecast data");
        const now = Date.now();
        let closest = ts[0];
        let minDiff = Math.abs(new Date(ts[0].validTime).getTime() - now);
        for (const entry of ts.slice(1, 10)) {
          const diff = Math.abs(new Date(entry.validTime).getTime() - now);
          if (diff < minDiff) { closest = entry; minDiff = diff; }
        }
        const p = closest.parameters;
        const lines = [
          `## ${loc.name} — Current Weather`,
          `**Conditions:** ${WSYMB2[getParam(p, "Wsymb2")] || "Unknown"}`,
          `**Temperature:** ${getParam(p, "t")}°C`,
          `**Humidity:** ${getParam(p, "r")}%`,
          `**Wind:** ${getParam(p, "ws")} m/s from ${getParam(p, "wd")}° (gusts ${getParam(p, "gust")} m/s)`,
          `**Pressure:** ${getParam(p, "msl")} hPa`,
          getParam(p, "pmean") != null ? `**Precipitation:** ${getParam(p, "pmean")} mm/h` : null,
          `\n*SMHI — ${closest.validTime}*`,
        ].filter(Boolean);
        return { content: [{ type: "text", text: lines.join("\n") }] };
      } catch (err) {
        return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
      }
    }
  • src/index.js:106-139 (registration)
    Registration of the current_weather tool using server.tool() with tool name 'current_weather', description, input schema (z.string() for location parameter), and the async handler function that implements the weather logic.
    server.tool(
      "current_weather",
      "Get current weather for a location in Sweden using SMHI.",
      { location: z.string().describe("Swedish city name or lat,lon coordinates") },
      async ({ location }) => {
        try {
          const loc = await getLocation(location);
          const data = await fetchForecast(loc.lat, loc.lon);
          const ts = data.timeSeries;
          if (!ts?.length) throw new Error("No forecast data");
          const now = Date.now();
          let closest = ts[0];
          let minDiff = Math.abs(new Date(ts[0].validTime).getTime() - now);
          for (const entry of ts.slice(1, 10)) {
            const diff = Math.abs(new Date(entry.validTime).getTime() - now);
            if (diff < minDiff) { closest = entry; minDiff = diff; }
          }
          const p = closest.parameters;
          const lines = [
            `## ${loc.name} — Current Weather`,
            `**Conditions:** ${WSYMB2[getParam(p, "Wsymb2")] || "Unknown"}`,
            `**Temperature:** ${getParam(p, "t")}°C`,
            `**Humidity:** ${getParam(p, "r")}%`,
            `**Wind:** ${getParam(p, "ws")} m/s from ${getParam(p, "wd")}° (gusts ${getParam(p, "gust")} m/s)`,
            `**Pressure:** ${getParam(p, "msl")} hPa`,
            getParam(p, "pmean") != null ? `**Precipitation:** ${getParam(p, "pmean")} mm/h` : null,
            `\n*SMHI — ${closest.validTime}*`,
          ].filter(Boolean);
          return { content: [{ type: "text", text: lines.join("\n") }] };
        } catch (err) {
          return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
        }
      }
    );
  • Input schema definition for the current_weather tool using Zod: a single 'location' parameter of type string with description 'Swedish city name or lat,lon coordinates'.
    { location: z.string().describe("Swedish city name or lat,lon coordinates") },
  • fetchForecast helper function that constructs the SMHI API URL with latitude/longitude coordinates and fetches weather forecast data as JSON, throwing an error if the API request fails.
    async function fetchForecast(lat, lon) {
      const url = `${BASE_URL}/lon/${lon.toFixed(6)}/lat/${lat.toFixed(6)}/data.json`;
      const res = await fetch(url, { headers: { "User-Agent": USER_AGENT } });
      if (!res.ok) throw new Error(`SMHI API error (${res.status}): ${await res.text()}`);
      return res.json();
    }
  • getLocation helper function that first tries to resolve location from predefined Swedish cities or coordinate strings, then falls back to geocoding via Open-Meteo API if needed, and throws an error if location cannot be found in Sweden.
    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 Sweden. Try a city name or lat,lon coordinates.`);
    }
Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/robobobby/mcp-swedish-weather'

If you have feedback or need assistance with the MCP directory API, please join our Discord server