feat: default list_alerts to active alerts only
Change list_alerts (MCP tool) and alerts (CLI command) to show only active (non-silenced, non-inhibited) alerts by default. Add state=all option and --all CLI flag to show all alerts when needed. - MCP: list_alerts with no state param now returns active alerts only - MCP: list_alerts with state=all returns all alerts (previous default) - CLI: alerts command defaults to active, --all shows everything - Add tests for new default behavior and state=all option - Update README with new CLI examples - Bump version to 0.3.0 - Clarify version bumping rules in CLAUDE.md Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -91,8 +91,8 @@ func listAlertsTool() mcp.Tool {
|
||||
Properties: map[string]mcp.Property{
|
||||
"state": {
|
||||
Type: "string",
|
||||
Description: "Filter by alert state: 'active', 'suppressed', or 'unprocessed'",
|
||||
Enum: []string{"active", "suppressed", "unprocessed"},
|
||||
Description: "Filter by alert state: 'active', 'suppressed', 'unprocessed', or 'all' (default: active)",
|
||||
Enum: []string{"active", "suppressed", "unprocessed", "all"},
|
||||
},
|
||||
"severity": {
|
||||
Type: "string",
|
||||
@@ -236,22 +236,24 @@ func makeListAlertsHandler(am *AlertmanagerClient) mcp.ToolHandler {
|
||||
return func(ctx context.Context, args map[string]interface{}) (mcp.CallToolResult, error) {
|
||||
filters := AlertFilters{}
|
||||
|
||||
if state, ok := args["state"].(string); ok && state != "" {
|
||||
switch state {
|
||||
case "active":
|
||||
active := true
|
||||
filters.Active = &active
|
||||
silenced := false
|
||||
filters.Silenced = &silenced
|
||||
inhibited := false
|
||||
filters.Inhibited = &inhibited
|
||||
case "suppressed":
|
||||
active := false
|
||||
filters.Active = &active
|
||||
case "unprocessed":
|
||||
unprocessed := true
|
||||
filters.Unprocessed = &unprocessed
|
||||
}
|
||||
state, _ := args["state"].(string)
|
||||
switch state {
|
||||
case "active", "":
|
||||
// Default to active alerts only (non-silenced, non-inhibited)
|
||||
active := true
|
||||
filters.Active = &active
|
||||
silenced := false
|
||||
filters.Silenced = &silenced
|
||||
inhibited := false
|
||||
filters.Inhibited = &inhibited
|
||||
case "suppressed":
|
||||
active := false
|
||||
filters.Active = &active
|
||||
case "unprocessed":
|
||||
unprocessed := true
|
||||
filters.Unprocessed = &unprocessed
|
||||
case "all":
|
||||
// No filters - return everything
|
||||
}
|
||||
|
||||
if severity, ok := args["severity"].(string); ok && severity != "" {
|
||||
|
||||
@@ -81,6 +81,92 @@ func TestHandler_ListAlerts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandler_ListAlertsDefaultsToActive(t *testing.T) {
|
||||
// Test that list_alerts with no state param defaults to active filters
|
||||
server, cleanup := setupTestServer(t,
|
||||
nil,
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
// Default should apply active filters
|
||||
if q.Get("active") != "true" {
|
||||
t.Errorf("expected default active=true, got %s", q.Get("active"))
|
||||
}
|
||||
if q.Get("silenced") != "false" {
|
||||
t.Errorf("expected default silenced=false, got %s", q.Get("silenced"))
|
||||
}
|
||||
if q.Get("inhibited") != "false" {
|
||||
t.Errorf("expected default inhibited=false, got %s", q.Get("inhibited"))
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`[]`))
|
||||
},
|
||||
)
|
||||
defer cleanup()
|
||||
|
||||
result := callTool(t, server, "list_alerts", map[string]interface{}{})
|
||||
if result.IsError {
|
||||
t.Fatalf("unexpected error: %s", result.Content[0].Text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandler_ListAlertsStateAll(t *testing.T) {
|
||||
// Test that list_alerts with state=all applies no filters
|
||||
server, cleanup := setupTestServer(t,
|
||||
nil,
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
// state=all should not set any filter params
|
||||
if q.Get("active") != "" {
|
||||
t.Errorf("expected no active param for state=all, got %s", q.Get("active"))
|
||||
}
|
||||
if q.Get("silenced") != "" {
|
||||
t.Errorf("expected no silenced param for state=all, got %s", q.Get("silenced"))
|
||||
}
|
||||
if q.Get("inhibited") != "" {
|
||||
t.Errorf("expected no inhibited param for state=all, got %s", q.Get("inhibited"))
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`[
|
||||
{
|
||||
"annotations": {},
|
||||
"endsAt": "2024-01-01T01:00:00Z",
|
||||
"fingerprint": "fp1",
|
||||
"receivers": [{"name": "default"}],
|
||||
"startsAt": "2024-01-01T00:00:00Z",
|
||||
"status": {"inhibitedBy": [], "silencedBy": [], "state": "active"},
|
||||
"updatedAt": "2024-01-01T00:00:00Z",
|
||||
"generatorURL": "",
|
||||
"labels": {"alertname": "ActiveAlert", "severity": "critical"}
|
||||
},
|
||||
{
|
||||
"annotations": {},
|
||||
"endsAt": "2024-01-01T01:00:00Z",
|
||||
"fingerprint": "fp2",
|
||||
"receivers": [{"name": "default"}],
|
||||
"startsAt": "2024-01-01T00:00:00Z",
|
||||
"status": {"inhibitedBy": [], "silencedBy": ["s1"], "state": "suppressed"},
|
||||
"updatedAt": "2024-01-01T00:00:00Z",
|
||||
"generatorURL": "",
|
||||
"labels": {"alertname": "SilencedAlert", "severity": "warning"}
|
||||
}
|
||||
]`))
|
||||
},
|
||||
)
|
||||
defer cleanup()
|
||||
|
||||
result := callTool(t, server, "list_alerts", map[string]interface{}{
|
||||
"state": "all",
|
||||
})
|
||||
if result.IsError {
|
||||
t.Fatalf("unexpected error: %s", result.Content[0].Text)
|
||||
}
|
||||
if !strings.Contains(result.Content[0].Text, "2 alert") {
|
||||
t.Errorf("expected output to contain '2 alert', got: %s", result.Content[0].Text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandler_GetAlert(t *testing.T) {
|
||||
server, cleanup := setupTestServer(t,
|
||||
nil,
|
||||
|
||||
Reference in New Issue
Block a user