get_alerts
Retrieve weather alerts for a specific U.S. state by providing its two-letter abbreviation to access current warnings and advisories.
Instructions
獲取美國特定州份的警報資料.
Args:
state (str): 美國州份的縮寫 (e.g., "CA", "TX", "NY")
Returns:
str: 警報資料的文字描述Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| state | Yes |
Implementation Reference
- get_alerts handler for stdio transport - fetches weather alerts from NOAA API for a given US state code. Uses @mcp.tool() decorator for registration.
@mcp.tool() async def get_alerts(state: str) -> str: """ 獲取美國特定州份的警報資料. Args: state (str): 美國州份的縮寫 (e.g., "CA", "TX", "NY") Returns: str: 警報資料的文字描述 """ url = f"{NWS_API_BASE_URL}/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 found for the given state." alerts = [format_alert(feature) for feature in data["features"]] return "\n\n".join(alerts) - get_alerts handler for streamable-http transport - similar logic with added Context parameter for logging via ctx.info(). Uses @mcp.tool() decorator.
@mcp.tool() async def get_alerts(state: str, ctx: Context) -> str: """Get weather alerts for a US state. Args: state: Two-letter US state code (e.g. CA, NY) """ await ctx.info(f"Fetching alerts for state: {state}") url = f"{NWS_API_BASE}/alerts/active/area/{state}" data = await make_nws_request(url) await ctx.info(f"Received data: {data}") 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) - get_alerts handler for SSE transport - identical to streamable-http version with Context parameter for logging. Uses @mcp.tool() decorator.
@mcp.tool() async def get_alerts(state: str, ctx: Context) -> str: """Get weather alerts for a US state. Args: state: Two-letter US state code (e.g. CA, NY) """ await ctx.info(f"Fetching alerts for state: {state}") url = f"{NWS_API_BASE}/alerts/active/area/{state}" data = await make_nws_request(url) await ctx.info(f"Received data: {data}") 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) - Helper function make_nws_request - handles HTTP requests to NOAA API with proper headers and error handling.
async def make_nws_request(url: str) -> dict[str, Any] | None: """ 發送 HTTP 請求到 NOAA 天氣 API 並返回 JSON 響應 """ 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: print(f"HTTP error occurred: {e}") return None - Helper function format_alert - formats individual alert feature into readable string with event, area, severity, description, and instructions.
def format_alert(feature: dict) -> str: """ 格式化 NOAA 警報資料 """ 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")} """