get_alerts
Retrieve weather alerts for any US state using two-letter state codes to monitor hazardous conditions and stay informed about local warnings.
Instructions
Get weather alerts for a US state.
Args:
state: Two-letter US state code (e.g. CA, NY, TX)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| state | Yes |
Implementation Reference
- weather.py:105-127 (handler)The handler function for the 'get_alerts' tool. It validates the US state code, fetches active weather alerts from the National Weather Service API, formats them using helper functions, 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 to format individual weather alert data into a human-readable string format.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:23-26 (helper)Helper function to validate if the provided state code is a valid two-letter US state abbreviation. Relies on the US_STATES constant.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 to make HTTP requests to the National Weather Service API with proper headers, timeout, and comprehensive 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:105-105 (registration)The @mcp.tool() decorator registers the get_alerts function as an MCP tool in the FastMCP server.@mcp.tool()