Skip to main content
Glama
0xteamhq

Grafana MCP Server

by 0xteamhq

list_prometheus_label_names

Retrieve label names from Prometheus datasources with filtering by series selectors and time range to identify available metrics and dimensions for monitoring queries.

Instructions

List label names in a Prometheus datasource. Allows filtering by series selectors and time range.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
datasourceUidYesThe UID of the datasource to query
endRfc3339NoThe end time of the time range
limitNoMaximum number of results
matchesNoLabel matchers to filter the results
startRfc3339NoThe start time of the time range

Implementation Reference

  • The asynchronous handler function that implements the core logic of the 'list_prometheus_label_names' tool. It creates a PrometheusClient, builds match selectors, queries for label names with optional time range and limit, and returns the result or error.
    handler: async (params, context: ToolContext) => { try { const client = new PrometheusClient(context.config.grafanaConfig, params.datasourceUid); const match = params.matches?.map((m: any) => buildSelector(m.filters)) || []; const labels = await client.getLabelNames( match.length > 0 ? match : undefined, params.startRfc3339, params.endRfc3339 ); const limited = params.limit ? labels.slice(0, params.limit) : labels; return createToolResult(limited); } catch (error: any) { return createErrorResult(error.message); } },
  • Zod schema defining the input parameters for the 'list_prometheus_label_names' tool, including datasource UID, optional match filters, time range, and limit.
    const ListPrometheusLabelNamesSchema = z.object({ datasourceUid: z.string().describe('The UID of the datasource to query'), matches: 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 the results'), startRfc3339: z.string().optional().describe('The start time of the time range'), endRfc3339: z.string().optional().describe('The end time of the time range'), limit: z.number().optional().describe('Maximum number of results'), });
  • Function that registers the 'list_prometheus_label_names' tool (and other Prometheus tools) with the MCP server.
    export function registerPrometheusTools(server: any) { server.registerTool(queryPrometheus); server.registerTool(listPrometheusMetricNames); server.registerTool(listPrometheusLabelNames); server.registerTool(listPrometheusLabelValues); server.registerTool(listPrometheusMetricMetadata); }
  • Utility function to construct Prometheus label matchers string from filter objects, used by the label names handler to build query selectors.
    function buildSelector(filters: any[]): string { if (!filters || filters.length === 0) return '{}'; const parts = filters.map(f => { switch (f.type) { case '=': return `${f.name}="${f.value}"`; case '!=': return `${f.name}!="${f.value}"`; case '=~': return `${f.name}=~"${f.value}"`; case '!~': return `${f.name}!~"${f.value}"`; default: return ''; } }).filter(p => p); return `{${parts.join(',')}}`; }

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