get_alert_groups
List alert groups from Alertmanager, filtered by silenced, inhibited, or active status, with configurable count and offset.
Instructions
Get a list of alert groups
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| silenced | No | ||
| inhibited | No | ||
| active | No | ||
| count | No | ||
| offset | No |
Implementation Reference
- The main tool handler function for 'get_alert_groups', decorated with @mcp.tool. Makes a GET request to /api/v2/alerts/groups with optional silencing/inhibited/active filters, then applies pagination via paginate_results().
@mcp.tool(description="Get a list of alert groups") async def get_alert_groups(silenced: Optional[bool] = None, inhibited: Optional[bool] = None, active: Optional[bool] = None, count: int = DEFAULT_ALERT_GROUP_PAGE, offset: int = 0): """Get a list of alert groups Params ------ silenced If true, include silenced alerts. inhibited If true, include inhibited alerts. active If true, include active alerts. count Number of alert groups to return per page (default: 3, max: 5). Alert groups can be large as they contain all alerts within the group. offset Number of alert groups to skip before returning results (default: 0). To paginate through all results, make multiple calls with increasing offset values (e.g., offset=0, offset=3, offset=6, etc.). Returns ------- dict A dictionary containing: - data: List of AlertGroup 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_GROUP_PAGE) if error: return {"error": error} params = {"active": True} 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 alert groups from the API all_groups = make_request(method="GET", route="/api/v2/alerts/groups", params=params) # Apply pagination and return results return paginate_results(all_groups, count, offset) - Default and max pagination constants for alert groups (DEFAULT_ALERT_GROUP_PAGE=3, MAX_ALERT_GROUP_PAGE=5), configurable via environment variables.
# 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:518-518 (registration)The @mcp.tool decorator registers 'get_alert_groups' as an MCP tool with description 'Get a list of alert groups'.
@mcp.tool(description="Get a list of alert groups") - validate_pagination_params() validates and normalizes count/offset against a max_count limit, used by get_alert_groups.
def validate_pagination_params(count: int, offset: int, max_count: int) -> tuple[int, int, Optional[str]]: """Validate and normalize pagination parameters. Parameters ---------- count : int Requested number of items per page offset : int Requested offset for pagination max_count : int Maximum allowed count value Returns ------- tuple[int, int, Optional[str]] A tuple of (normalized_count, normalized_offset, error_message). If error_message is not None, the parameters are invalid and should return an error to the caller. """ error = None # Validate count parameter if count < 1: error = f"Count parameter ({count}) must be at least 1." elif count > max_count: error = ( f"Count parameter ({count}) exceeds maximum allowed value ({max_count}). " f"Please use count <= {max_count} and paginate through results using the offset parameter." ) # Validate offset parameter if offset < 0: error = f"Offset parameter ({offset}) must be non-negative (>= 0)." return count, offset, error - paginate_results() applies slicing to items and returns a dict with 'data' and 'pagination' metadata (total, offset, count, requested_count, has_more), used by get_alert_groups.
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 } }