get_alerts
Retrieve active weather alerts for any US state by providing a two-letter state code.
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 |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- weather.py:37-53 (handler)The main handler function for the 'get_alerts' tool. It takes a two-letter US state code, calls the NWS API to fetch active alerts, and returns them formatted as a string. Decorated with @mcp.tool() to register it as an 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:36-36 (registration)The @mcp.tool() decorator that registers get_alerts as an MCP tool on the FastMCP server instance.
@mcp.tool() - weather.py:13-22 (helper)Helper function that makes HTTP requests to the NWS API with proper error handling, used by get_alerts to fetch 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: return None - weather.py:25-34 (helper)Helper function that formats a single alert feature from the NWS API into a human-readable string, used by get_alerts for formatting output.
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")} """