get_alerts
Retrieve active weather alerts for any US state using two-letter state codes to monitor severe 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)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| state | Yes |
Implementation Reference
- src/weather/server.py:17-34 (handler)The main handler function for the 'get_alerts' tool. It is registered via the @mcp.tool() decorator. Fetches active weather alerts for a US state from the NWS API, formats them using helpers, and returns a formatted 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)
- src/weather/server.py:19-23 (schema)Input schema for the tool defined in the docstring: expects a 'state' parameter as a two-letter US state code."""Get weather alerts for a US state. Args: state: Two-letter US state code (e.g. CA, NY) """
- src/weather/server.py:75-88 (helper)Helper function used by get_alerts to make authenticated HTTP requests to the National Weather Service API.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(follow_redirects=True) as client: try: response = await client.get(url, headers=headers, timeout=30.0) response.raise_for_status() return response.json() except Exception: return None
- src/weather/server.py:90-100 (helper)Helper function used by get_alerts to format individual alert features into readable text.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')} """