get_alerts
Retrieve a list of alerts with optional filters for active, silenced, or inhibited status, paginated by count and offset.
Instructions
Get a list of alerts
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filter | No | ||
| silenced | No | ||
| inhibited | No | ||
| active | No | ||
| count | No | ||
| offset | No |
Implementation Reference
- The get_alerts tool handler function. Decorated with @mcp.tool, it accepts filter, silenced, inhibited, active, count, and offset parameters. Validates pagination, builds query params, calls make_request to GET /api/v2/alerts, then paginates results via paginate_results.
async def get_alerts(filter: Optional[str] = None, silenced: Optional[bool] = None, inhibited: Optional[bool] = None, active: Optional[bool] = None, count: int = DEFAULT_ALERT_PAGE, offset: int = 0): """Get a list of alerts currently in Alertmanager. Params ------ filter Filtering query (e.g. alertname=~'.*CPU.*')"), silenced If true, include silenced alerts. inhibited If true, include inhibited alerts. active If true, include active alerts. count Number of alerts to return per page (default: 10, max: 25). offset Number of alerts to skip before returning results (default: 0). To paginate through all results, make multiple calls with increasing offset values (e.g., offset=0, offset=10, offset=20, etc.). Returns ------- dict A dictionary containing: - data: List of Alert objects for the current page - pagination: Metadata about pagination (total, offset, count, has_more) Use the 'has_more' flag to determine if additional pages are available. """ # Validate pagination parameters count, offset, error = validate_pagination_params( count, offset, MAX_ALERT_PAGE) if error: return {"error": error} params = {"active": True} if filter: params = {"filter": filter} if silenced is not None: params["silenced"] = silenced if inhibited is not None: params["inhibited"] = inhibited if active is not None: params["active"] = active # Get all alerts from the API all_alerts = make_request( method="GET", route="/api/v2/alerts", params=params) # Apply pagination and return results return paginate_results(all_alerts, count, offset) - Pagination defaults: DEFAULT_ALERT_PAGE (10) and MAX_ALERT_PAGE (25) used as default and cap for count parameter in get_alerts.
# Pagination defaults and limits (configurable via environment variables) DEFAULT_SILENCE_PAGE = int(os.environ.get( "ALERTMANAGER_DEFAULT_SILENCE_PAGE", "10")) MAX_SILENCE_PAGE = int(os.environ.get("ALERTMANAGER_MAX_SILENCE_PAGE", "50")) DEFAULT_ALERT_PAGE = int(os.environ.get( "ALERTMANAGER_DEFAULT_ALERT_PAGE", "10")) MAX_ALERT_PAGE = int(os.environ.get("ALERTMANAGER_MAX_ALERT_PAGE", "25")) DEFAULT_ALERT_GROUP_PAGE = int(os.environ.get( "ALERTMANAGER_DEFAULT_ALERT_GROUP_PAGE", "3")) MAX_ALERT_GROUP_PAGE = int(os.environ.get( "ALERTMANAGER_MAX_ALERT_GROUP_PAGE", "5")) - src/alertmanager_mcp_server/server.py:436-436 (registration)Registration decorator: @mcp.tool(description="Get a list of alerts") on the get_alerts function registers it as an MCP tool via the FastMCP instance.
@mcp.tool(description="Get a list of alerts") - make_request helper – makes the actual HTTP GET request to /api/v2/alerts with auth, tenant headers, and returns parsed JSON.
) response.raise_for_status() result = response.json() # Ensure we always return something (empty list is valid but might cause issues) if result is None: return {"message": "No data returned"} return result except requests.exceptions.RequestException as e: return {"error": str(e)} - paginate_results helper – slices the full alert list by offset/count and returns paginated data with metadata (total, offset, count, has_more).
def paginate_results(items: List[Any], count: int, offset: int) -> Dict[str, Any]: """Apply pagination to a list of items and generate pagination metadata. Parameters ---------- items : List[Any] The full list of items to paginate count : int Number of items to return per page (must be >= 1) offset : int Number of items to skip (must be >= 0) Returns ------- Dict[str, Any] A dictionary containing: - data: List of items for the current page - pagination: Metadata including total, offset, count, requested_count, and has_more """ total = len(items) end_index = offset + count paginated_items = items[offset:end_index] has_more = end_index < total return { "data": paginated_items, "pagination": { "total": total, "offset": offset, "count": len(paginated_items), "requested_count": count, "has_more": has_more } }