Skip to main content
Glama
sun873087

MCP Weather Sample

by sun873087

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
NameRequiredDescriptionDefault
stateYes

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")}
    """
Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sun873087/mcp-sample'

If you have feedback or need assistance with the MCP directory API, please join our Discord server