get_alerts
Retrieve weather alerts for any US state using two-letter state codes to stay informed about hazardous conditions and warnings from the National Weather Service.
Instructions
Get weather alerts for a US state.
Args:
state: Two-letter US state code (e.g. CA, NY)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| state | Yes |
Implementation Reference
- weather.py:58-75 (handler)The core handler function for the 'get_alerts' tool, registered via @mcp.tool() decorator. Fetches active weather alerts for a US state from NWS API, handles errors, formats using helpers, and returns a concatenated string of alerts.@mcp.tool() async def get_alerts(state: str) -> str: """Get weather alerts for a US state. Args: state: Two-letter US state code (e.g. CA, NY) """ url = f"{NWS_API_BASE}/alerts/active/area/{state}" data = await make_nws_request(url) if not data or "features" not in data: return "Unable to fetch alerts or no alerts found." if not data["features"]: return "No active alerts for this state." alerts = [format_alert(feature) for feature in data["features"]] return "\n---\n".join(alerts)
- weather.py:33-43 (helper)Helper function that formats a single weather alert feature dictionary into a human-readable multi-line string.def format_alert(feature: dict) -> str: """Format an alert feature into a readable string.""" props = feature["properties"] return f""" Event: {props.get("event", "Unknown")} Area: {props.get("areaDesc", "Unknown")} Severity: {props.get("severity", "Unknown")} Description: {props.get("description", "No description available")} Instructions: {props.get("instruction", "No specific instructions provided")} """
- weather.py:16-28 (helper)Reusable async helper for making HTTP GET requests to the NWS API, with proper headers, timeout, error handling, and JSON parsing.async def make_nws_request(url: str) -> dict[str, Any] | None: """Make a request to the NWS API with proper error handling.""" headers = {"User-Agent": USER_AGENT, "Accept": "application/geo+json"} # Creates an HTTP client using httpx library (async alternative to requests) # async with ensures client closes properly async with httpx.AsyncClient() as client: try: response = await client.get(url, headers=headers, timeout=30.0) response.raise_for_status() # Raises error if status code is 4xx/5xx return response.json() # If anything fails, returns None instead of crashing except Exception: return None