get-alerts
Retrieve and filter Prometheus Alertmanager alerts by status, name patterns, and suppression state to monitor system notifications and respond to incidents.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filter | No | Filtering query (e.g. alertname=~'.*CPU.*') | |
| silenced | No | Include silenced alerts | |
| inhibited | No | Include inhibited alerts | |
| active | No | Include active alerts (default: true) |
Implementation Reference
- src/index.ts:108-154 (handler)The handler function for the 'get-alerts' tool. It constructs query parameters based on input, fetches alerts from Alertmanager API using the helper function, formats them into a structured response, and returns the JSON stringified list or an error message.async ({ filter, silenced = false, inhibited = false, active = true }) => { try { // Build query parameters const params = new URLSearchParams(); if (filter) params.append("filter", filter); if (silenced) params.append("silenced", "true"); if (inhibited) params.append("inhibited", "true"); if (!active) params.append("active", "false"); // Fetch alerts const queryString = params.toString(); const path = `alerts${queryString ? '?' + queryString : ''}`; const alerts = await fetchFromAlertmanager(path) as Alert[]; // Format alerts const formattedAlerts = alerts.map((alert: Alert): FormattedAlert => ({ fingerprint: alert.fingerprint, alertname: alert.labels.alertname, severity: alert.labels.severity || 'unknown', summary: alert.annotations.summary || 'No summary provided', description: alert.annotations.description || 'No description provided', startsAt: alert.startsAt, status: { state: alert.status.state, silenced: alert.status.silencedBy.length > 0, inhibited: alert.status.inhibitedBy.length > 0, }, labels: alert.labels, })); return { content: [{ type: "text", text: JSON.stringify(formattedAlerts, null, 2) }] }; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [{ type: "text", text: `Error fetching alerts: ${errorMessage}` }], isError: true }; } }
- src/index.ts:102-107 (schema)Input schema for the 'get-alerts' tool defined using Zod, including optional parameters for filtering, including silenced/inhibited alerts, and active status.{ filter: z.string().optional().describe("Filtering query (e.g. alertname=~'.*CPU.*')"), silenced: z.boolean().optional().describe("Include silenced alerts"), inhibited: z.boolean().optional().describe("Include inhibited alerts"), active: z.boolean().optional().describe("Include active alerts (default: true)"), },
- src/index.ts:100-155 (registration)Registration of the 'get-alerts' tool on the MCP server using server.tool(), specifying the name, input schema, and handler function.server.tool( "get-alerts", { filter: z.string().optional().describe("Filtering query (e.g. alertname=~'.*CPU.*')"), silenced: z.boolean().optional().describe("Include silenced alerts"), inhibited: z.boolean().optional().describe("Include inhibited alerts"), active: z.boolean().optional().describe("Include active alerts (default: true)"), }, async ({ filter, silenced = false, inhibited = false, active = true }) => { try { // Build query parameters const params = new URLSearchParams(); if (filter) params.append("filter", filter); if (silenced) params.append("silenced", "true"); if (inhibited) params.append("inhibited", "true"); if (!active) params.append("active", "false"); // Fetch alerts const queryString = params.toString(); const path = `alerts${queryString ? '?' + queryString : ''}`; const alerts = await fetchFromAlertmanager(path) as Alert[]; // Format alerts const formattedAlerts = alerts.map((alert: Alert): FormattedAlert => ({ fingerprint: alert.fingerprint, alertname: alert.labels.alertname, severity: alert.labels.severity || 'unknown', summary: alert.annotations.summary || 'No summary provided', description: alert.annotations.description || 'No description provided', startsAt: alert.startsAt, status: { state: alert.status.state, silenced: alert.status.silencedBy.length > 0, inhibited: alert.status.inhibitedBy.length > 0, }, labels: alert.labels, })); return { content: [{ type: "text", text: JSON.stringify(formattedAlerts, null, 2) }] }; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [{ type: "text", text: `Error fetching alerts: ${errorMessage}` }], isError: true }; } } );
- src/index.ts:16-41 (helper)Helper function used by the 'get-alerts' handler to make HTTP requests to the Alertmanager API with timeout handling, error management, and configurable base URL.async function fetchFromAlertmanager(path: string, options: RequestInit = {}): Promise<any> { const baseUrl = process.env.ALERTMANAGER_URL || DEFAULT_ALERTMANAGER_URL; const url = `${baseUrl}/api/v2/${path}`; try { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), DEFAULT_TIMEOUT); const response = await fetch(url, { ...options, signal: controller.signal }); clearTimeout(timeoutId); if (!response.ok) { throw new Error(`Alertmanager API error: ${response.status} ${response.statusText}`); } return await response.json(); } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : String(error); console.error(`Error fetching from Alertmanager: ${errorMessage}`); throw error; } }
- src/index.ts:68-81 (schema)TypeScript interface defining the formatted structure of alerts returned by the 'get-alerts' handler.interface FormattedAlert { fingerprint: string; alertname: string; severity: string; summary: string; description: string; startsAt: string; status: { state: string; silenced: boolean; inhibited: boolean; }; labels: AlertLabel; }