get-silences
Retrieve and filter silence rules from Prometheus Alertmanager to manage alert suppression and maintenance windows.
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "get-silences"
}
}Input 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 using the provided filter, formats them, and returns as formatted JSON or error message.
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:273-275 (schema)Input schema for the get-silences tool, defining an optional filter parameter using Zod.
{ filter: z.string().optional().describe("Filtering query (e.g. createdBy=~'.*admin.*')"), }, - src/index.ts:271-315 (registration)Registration of the 'get-silences' tool on the MCP server, including input schema and 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:83-97 (schema)TypeScript interface defining the structure of a Silence object used in the get-silences tool.
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:16-41 (helper)Helper function for making HTTP requests to the Alertmanager API 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; } }