Skip to main content
Glama
0xteamhq

Grafana MCP Server

by 0xteamhq

list_alert_rules

Retrieve and filter Grafana alert rules by labels to monitor their current state, UID, title, and labels for effective alert management.

Instructions

Lists Grafana alert rules, returning a summary including UID, title, current state, and labels

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
label_selectorsNoLabel matchers to filter alert rules
limitNoMaximum number of results to return
pageNoPage number to return

Implementation Reference

  • The main handler function for the 'list_alert_rules' tool. It constructs filters from input params, calls GrafanaClient.listAlertRules, formats the rules into a summary (uid, title, state, labels, folder), and returns the result or error.
    handler: async (params, context: ToolContext) => {
      try {
        const client = new GrafanaClient(context.config.grafanaConfig);
        
        // Build filters object for API
        const filters: any = {};
        if (params.label_selectors) {
          // Convert label selectors to query params
          // Note: Real implementation would need proper label selector formatting
          filters.labels = params.label_selectors;
        }
        if (params.limit) filters.limit = params.limit;
        if (params.page) filters.page = params.page;
        
        const rules = await client.listAlertRules(filters);
        
        // Format the response
        const formatted = rules.map((rule: any) => ({
          uid: rule.uid,
          title: rule.title,
          state: rule.state || 'inactive',
          labels: rule.labels || {},
          folder: rule.folderUID,
        }));
        
        return createToolResult(formatted);
      } catch (error: any) {
        return createErrorResult(error.message);
      }
  • Zod schema defining the input parameters for the list_alert_rules tool: optional label_selectors, limit, and page.
    const ListAlertRulesSchema = z.object({
      label_selectors: z.array(z.object({
        filters: z.array(z.object({
          name: z.string().describe('The name of the label to match against'),
          value: z.string().describe('The value to match against'),
          type: z.enum(['=', '!=', '=~', '!~']).describe('The match operator'),
        })),
      })).optional().describe('Label matchers to filter alert rules'),
      limit: z.number().optional().describe('Maximum number of results to return'),
      page: z.number().optional().describe('Page number to return'),
    });
  • Registration function that registers the list_alert_rules tool (along with related tools) to the MCP server.
    export function registerAlertingTools(server: any) {
      server.registerTool(listAlertRules);
      server.registerTool(getAlertRuleByUid);
      server.registerTool(listContactPoints);
    }
  • Helper method in GrafanaClient that performs the actual API call to list alert rules from Grafana's provisioning endpoint, used by the tool handler.
    async listAlertRules(filters?: any): Promise<AlertRule[]> {
      try {
        const response = await this.client.get('/api/v1/provisioning/alert-rules', {
          params: filters,
        });
        return response.data;
      } catch (error) {
        this.handleError(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/0xteamhq/mcp-grafana'

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