Skip to main content
Glama
comqx

Prometheus Alertmanager MCP Server

by comqx

get-silences

Retrieve and filter silence rules from Prometheus Alertmanager to manage alert suppression and maintenance windows.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filterNoFiltering query (e.g. createdBy=~'.*admin.*')

Implementation Reference

  • 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
        };
      }
    }
  • 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
          };
        }
      }
    );
  • 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;
      }>;
    }
  • 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;
      }
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/comqx/alertmanager-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server