Skip to main content
Glama
0xteamhq

Grafana MCP Server

by 0xteamhq

list_prometheus_metric_names

Retrieve and filter metric names from a Prometheus datasource using regex patterns to identify specific monitoring metrics for analysis.

Instructions

List metric names in a Prometheus datasource. Retrieves all metric names and filters them using regex.

Input Schema

NameRequiredDescriptionDefault
datasourceUidYesThe UID of the datasource to query
limitNoThe maximum number of results to return
pageNoThe page number to return
regexNoThe regex to match against the metric names

Input Schema (JSON Schema)

{ "properties": { "datasourceUid": { "description": "The UID of the datasource to query", "type": "string" }, "limit": { "description": "The maximum number of results to return", "type": "number" }, "page": { "description": "The page number to return", "type": "number" }, "regex": { "description": "The regex to match against the metric names", "type": "string" } }, "required": [ "datasourceUid" ], "type": "object" }

Implementation Reference

  • The ToolDefinition export including the async handler function that implements the core logic: queries Prometheus series to extract unique metric names, applies optional regex filtering and pagination, sorts and returns the list.
    export const listPrometheusMetricNames: ToolDefinition = { name: 'list_prometheus_metric_names', description: 'List metric names in a Prometheus datasource. Retrieves all metric names and filters them using regex.', inputSchema: ListPrometheusMetricNamesSchema, handler: async (params, context: ToolContext) => { try { const client = new PrometheusClient(context.config.grafanaConfig, params.datasourceUid); // Get all series to extract metric names const series = await client.getSeries(['__name__=~".+"']); const metricNames = new Set<string>(); for (const s of series) { if (s.__name__) { metricNames.add(s.__name__); } } let names = Array.from(metricNames).sort(); // Apply regex filter if provided if (params.regex) { const regex = new RegExp(params.regex); names = names.filter(name => regex.test(name)); } // Apply pagination if (params.limit || params.page) { const limit = params.limit || 100; const page = params.page || 1; const start = (page - 1) * limit; names = names.slice(start, start + limit); } return createToolResult(names); } catch (error: any) { return createErrorResult(error.message); } }, };
  • Zod schema for input validation: requires datasourceUid, optional regex, limit, and page parameters.
    const ListPrometheusMetricNamesSchema = z.object({ datasourceUid: z.string().describe('The UID of the datasource to query'), regex: z.string().optional().describe('The regex to match against the metric names'), limit: z.number().optional().describe('The maximum number of results to return'), page: z.number().optional().describe('The page number to return'), });
  • Function to register all Prometheus tools with the MCP server, including the call to server.registerTool(listPrometheusMetricNames).
    export function registerPrometheusTools(server: any) { server.registerTool(queryPrometheus); server.registerTool(listPrometheusMetricNames); server.registerTool(listPrometheusLabelNames); server.registerTool(listPrometheusLabelValues); server.registerTool(listPrometheusMetricMetadata); }

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