get-alerts
Retrieve active, silenced, or inhibited alerts from Prometheus Alertmanager using customizable filters to monitor system notifications.
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 that executes the 'get-alerts' tool: builds query params based on inputs, fetches alerts from Alertmanager, formats them into a structured response, and handles errors.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)Zod input schema defining optional parameters for filtering and including different types of 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)"), },
- src/index.ts:100-155 (registration)Registration of the 'get-alerts' tool on the MCP server using server.tool(), specifying 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)Utility function for making API requests to Alertmanager with timeout handling and error management, used by the get-alerts handler.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; } }