feat: include active alert count in MCP server instructions
Add InstructionsFunc callback to ServerConfig, called during each initialize handshake to generate dynamic instructions. The lab-monitoring server uses this to query Alertmanager and include a count of active non-silenced alerts, so the LLM can proactively inform the user. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,50 @@ import (
|
||||
"git.t-juice.club/torjus/labmcp/internal/mcp"
|
||||
)
|
||||
|
||||
// AlertSummary queries Alertmanager for active (non-silenced) alerts and returns
|
||||
// a short summary string. Returns empty string if there are no alerts or if
|
||||
// Alertmanager is unreachable.
|
||||
func AlertSummary(am *AlertmanagerClient) string {
|
||||
active := true
|
||||
silenced := false
|
||||
inhibited := false
|
||||
alerts, err := am.ListAlerts(context.Background(), AlertFilters{
|
||||
Active: &active,
|
||||
Silenced: &silenced,
|
||||
Inhibited: &inhibited,
|
||||
})
|
||||
if err != nil || len(alerts) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Count by severity
|
||||
severities := make(map[string]int)
|
||||
for _, a := range alerts {
|
||||
sev := a.Labels["severity"]
|
||||
if sev == "" {
|
||||
sev = "unknown"
|
||||
}
|
||||
severities[sev]++
|
||||
}
|
||||
|
||||
var parts []string
|
||||
// Show critical first if present
|
||||
if n, ok := severities["critical"]; ok {
|
||||
parts = append(parts, fmt.Sprintf("%d critical", n))
|
||||
delete(severities, "critical")
|
||||
}
|
||||
if n, ok := severities["warning"]; ok {
|
||||
parts = append(parts, fmt.Sprintf("%d warning", n))
|
||||
delete(severities, "warning")
|
||||
}
|
||||
for sev, n := range severities {
|
||||
parts = append(parts, fmt.Sprintf("%d %s", n, sev))
|
||||
}
|
||||
|
||||
return fmt.Sprintf("ALERT STATUS: %d active alert(s) firing (%s). Use list_alerts for details. Let the user know about this.",
|
||||
len(alerts), strings.Join(parts, ", "))
|
||||
}
|
||||
|
||||
// RegisterHandlers registers all monitoring tool handlers on the MCP server.
|
||||
func RegisterHandlers(server *mcp.Server, prom *PrometheusClient, am *AlertmanagerClient) {
|
||||
server.RegisterTool(listAlertsTool(), makeListAlertsHandler(am))
|
||||
|
||||
Reference in New Issue
Block a user