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
| Name | Required | Description | Default |
|---|---|---|---|
| datasourceUid | Yes | The UID of the datasource to query | |
| limit | No | The maximum number of results to return | |
| page | No | The page number to return | |
| regex | No | The 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
- src/tools/prometheus.ts:125-164 (handler)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); } }, };
- src/tools/prometheus.ts:14-19 (schema)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'), });
- src/tools/prometheus.ts:232-238 (registration)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); }