get_alerts
Retrieve active weather alerts for any US state using the two-letter state code to stay informed about hazardous conditions.
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:38-55 (handler)The primary handler for the 'get_alerts' tool. It constructs the NWS API URL for the given state, fetches alert data using make_nws_request, processes the features, formats each alert, and joins them into a response string.@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:26-35 (helper)Helper function specifically used by get_alerts to format individual alert features from the API response into human-readable multiline strings.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:12-22 (helper)Shared helper function for making HTTP requests to the NWS API, used by get_alerts to fetch the alert data.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"} async with httpx.AsyncClient() as client: try: response = await client.get(url, headers=headers, timeout=30.0) response.raise_for_status() return response.json() except Exception: