get_alerts
Retrieve weather alerts for any US state using two-letter state codes to stay informed about hazardous conditions and warnings.
Instructions
Get weather alerts for a US state.
Args:
state: Two-letter US state code (e.g. CA, NY)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| state | Yes |
Input Schema (JSON Schema)
{
"properties": {
"state": {
"title": "State",
"type": "string"
}
},
"required": [
"state"
],
"type": "object"
}
Implementation Reference
- weather.py:105-127 (handler)The primary handler for the 'get_alerts' tool. Registered via @mcp.tool() decorator. Validates the state input, fetches active alerts from NWS API, formats them, and returns a formatted string response.@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, TX) """ state = state.upper().strip() if not validate_state_code(state): return f"Error: '{state}' is not a valid US state code. Please use a 2-letter code (e.g., CA, NY, TX)." url = f"{NWS_API_BASE}/alerts/active/area/{state}" data = await make_nws_request(url) if not data or "features" not in data: return f"Unable to fetch alerts for {state}. The location may not be supported or the service is unavailable." if not data["features"]: return f"No active weather alerts for {state}." alerts = [format_alert(feature) for feature in data["features"]] return f"Weather Alerts for {state}:\n\n" + "\n---\n".join(alerts)
- weather.py:93-102 (helper)Helper function used by get_alerts to format each alert feature into a human-readable 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:22-26 (helper)Helper function called by get_alerts to validate the input state code against a list of US states.def validate_state_code(state: str) -> bool: """Validate that the state code is a valid 2-letter US state code.""" return state.upper() in US_STATES
- weather.py:73-92 (helper)Helper function used by get_alerts to perform HTTP requests to the NWS API with error handling.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 httpx.HTTPStatusError as e: if e.response.status_code == 404: return None return None except (httpx.RequestError, httpx.TimeoutException): return None except Exception: return None
- weather.py:14-20 (helper)Constant list of valid 2-letter US state codes used by validate_state_code in get_alerts.US_STATES = { "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY", "DC" }