get-silences
Retrieve and filter alert silences from Prometheus Alertmanager using query parameters to manage notification suppression.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filter | No | Filtering query (e.g. createdBy=~'.*admin.*') |
Implementation Reference
- src/index.ts:276-314 (handler)Handler function that fetches the list of silences from Alertmanager API (/api/v2/silences) with optional filter, formats them into a simplified structure, and returns as formatted JSON text. Includes error handling.async ({ filter }) => { try { // Build query parameters const params = new URLSearchParams(); if (filter) params.append("filter", filter); // Fetch silences const queryString = params.toString(); const path = `silences${queryString ? '?' + queryString : ''}`; const silences = await fetchFromAlertmanager(path) as Silence[]; // Format silences const formattedSilences = silences.map((silence: Silence) => ({ id: silence.id, status: silence.status.state, createdBy: silence.createdBy, comment: silence.comment, startsAt: silence.startsAt, endsAt: silence.endsAt, matchers: silence.matchers, })); return { content: [{ type: "text", text: JSON.stringify(formattedSilences, null, 2) }] }; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [{ type: "text", text: `Error fetching silences: ${errorMessage}` }], isError: true }; } }
- src/index.ts:83-97 (schema)TypeScript interface defining the Silence object structure used for typing the API response in the get-silences handler.interface Silence { id: string; status: { state: string; }; createdBy: string; comment: string; startsAt: string; endsAt: string; matchers: Array<{ name: string; value: string; isRegex: boolean; }>; }
- src/index.ts:273-275 (schema)Zod input schema for the get-silences tool defining optional filter parameter for querying silences.{ filter: z.string().optional().describe("Filtering query (e.g. createdBy=~'.*admin.*')"), },
- src/index.ts:271-315 (registration)Registration of the 'get-silences' MCP tool using server.tool(), specifying name, input schema, and inline handler function.server.tool( "get-silences", { filter: z.string().optional().describe("Filtering query (e.g. createdBy=~'.*admin.*')"), }, async ({ filter }) => { try { // Build query parameters const params = new URLSearchParams(); if (filter) params.append("filter", filter); // Fetch silences const queryString = params.toString(); const path = `silences${queryString ? '?' + queryString : ''}`; const silences = await fetchFromAlertmanager(path) as Silence[]; // Format silences const formattedSilences = silences.map((silence: Silence) => ({ id: silence.id, status: silence.status.state, createdBy: silence.createdBy, comment: silence.comment, startsAt: silence.startsAt, endsAt: silence.endsAt, matchers: silence.matchers, })); return { content: [{ type: "text", text: JSON.stringify(formattedSilences, null, 2) }] }; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [{ type: "text", text: `Error fetching silences: ${errorMessage}` }], isError: true }; } } );
- src/index.ts:16-41 (helper)Reusable helper function for making HTTP requests to Alertmanager API endpoints with timeout and error handling, used by the get-silences 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; } }