Weather lookup
get_weatherRetrieve current weather for any city. Supports optional temperature unit; demo data by default, live data when configured.
Instructions
Get current weather for a city. By default this returns deterministic demo data. Set NANOMCP_LIVE_WEATHER=1 to try wttr.in.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| location | Yes | City or place name, for example Shanghai. | |
| unit | No | Temperature unit. | celsius |
Implementation Reference
- nanomcp/server.py:151-167 (handler)The main handler function for the 'get_weather' tool. Takes location and optional unit arguments, dispatches to live_weather (if NANOMCP_LIVE_WEATHER=1) or offline_weather (deterministic demo data).
def get_weather(arguments: dict[str, Any]) -> str: location = str(arguments.get("location", "")).strip() if not location: raise ToolError("location is required") unit = str(arguments.get("unit", "celsius")) if unit not in {"celsius", "fahrenheit"}: raise ToolError("unit must be celsius or fahrenheit") if os.environ.get("NANOMCP_LIVE_WEATHER") == "1": try: return live_weather(location, unit) except Exception as exc: offline = offline_weather(location, unit) return f"Live weather lookup failed ({exc}).\n\n{offline}" return offline_weather(location, unit) - nanomcp/server.py:23-48 (schema)Tool schema definition for 'get_weather' in the TOOLS list, specifying inputSchema with location (required string) and unit (optional enum: celsius/fahrenheit).
TOOLS: list[dict[str, Any]] = [ { "name": "get_weather", "title": "Weather lookup", "description": ( "Get current weather for a city. By default this returns deterministic " "demo data. Set NANOMCP_LIVE_WEATHER=1 to try wttr.in." ), "inputSchema": { "type": "object", "properties": { "location": { "type": "string", "description": "City or place name, for example Shanghai.", }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "Temperature unit.", "default": "celsius", }, }, "required": ["location"], "additionalProperties": False, }, }, - nanomcp/server.py:136-139 (registration)Registration/dispatch point in call_tool() that routes the 'get_weather' tool name to the get_weather() handler function.
def call_tool(name: str, arguments: dict[str, Any]) -> dict[str, Any]: try: if name == "get_weather": return tool_result(get_weather(arguments)) - nanomcp/server.py:170-189 (helper)Helper function that performs live weather lookup via the wttr.in API. Called when NANOMCP_LIVE_WEATHER=1 is set.
def live_weather(location: str, unit: str) -> str: encoded = urllib.parse.quote(location) url = f"https://wttr.in/{encoded}?format=j1" request = urllib.request.Request(url, headers={"User-Agent": "nanomcp/0.1"}) with urllib.request.urlopen(request, timeout=5) as response: payload = json.loads(response.read().decode("utf-8")) current = payload["current_condition"][0] temp_key = "temp_C" if unit == "celsius" else "temp_F" suffix = "C" if unit == "celsius" else "F" condition = current["weatherDesc"][0]["value"] humidity = current.get("humidity", "unknown") wind = current.get("windspeedKmph", "unknown") return ( f"Live weather for {location} from wttr.in:\n" f"- Temperature: {current[temp_key]}°{suffix}\n" f"- Condition: {condition}\n" f"- Humidity: {humidity}%\n" f"- Wind: {wind} km/h" ) - nanomcp/server.py:192-214 (helper)Helper function that generates deterministic demo weather data using SHA-256 hash of location + today's date for reproducibility.
def offline_weather(location: str, unit: str) -> str: seed = f"{location.lower()}:{date.today().isoformat()}".encode("utf-8") digest = hashlib.sha256(seed).digest() celsius = 6 + digest[0] % 29 humidity = 35 + digest[1] % 55 conditions = ["sunny", "cloudy", "light rain", "windy", "hazy", "clear"] condition = conditions[digest[2] % len(conditions)] if unit == "fahrenheit": temperature = round(celsius * 9 / 5 + 32) suffix = "F" else: temperature = celsius suffix = "C" return ( f"Demo weather for {location}:\n" f"- Temperature: {temperature}°{suffix}\n" f"- Condition: {condition}\n" f"- Humidity: {humidity}%\n" "\nThis is deterministic demo data. Set NANOMCP_LIVE_WEATHER=1 " "to try a live wttr.in lookup." )