list_loki_label_values
Retrieve all unique values for a specific label within a Loki datasource and time range to analyze log data patterns and filter queries effectively.
Instructions
Retrieves all unique values associated with a specific labelName within a Loki datasource and time range
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| datasourceUid | Yes | The UID of the datasource to query | |
| endRfc3339 | No | The end time of the query in RFC3339 format | |
| labelName | Yes | The name of the label to retrieve values for | |
| startRfc3339 | No | The start time of the query in RFC3339 format |
Input Schema (JSON Schema)
{
"properties": {
"datasourceUid": {
"description": "The UID of the datasource to query",
"type": "string"
},
"endRfc3339": {
"description": "The end time of the query in RFC3339 format",
"type": "string"
},
"labelName": {
"description": "The name of the label to retrieve values for",
"type": "string"
},
"startRfc3339": {
"description": "The start time of the query in RFC3339 format",
"type": "string"
}
},
"required": [
"datasourceUid",
"labelName"
],
"type": "object"
}
Implementation Reference
- src/tools/loki.ts:85-102 (handler)The async handler function for the list_loki_label_values tool. Instantiates LokiClient and calls getLabelValues with the provided parameters and time range.handler: async (params, context: ToolContext) => { try { const client = new LokiClient(context.config.grafanaConfig, params.datasourceUid); const timeRange: TimeRange = params.startRfc3339 || params.endRfc3339 ? { start: '', end: '' } : getDefaultTimeRange(); const values = await client.getLabelValues( params.labelName, params.startRfc3339 || timeRange.start, params.endRfc3339 || timeRange.end ); return createToolResult(values); } catch (error: any) { return createErrorResult(error.message); } },
- src/tools/loki.ts:27-32 (schema)Zod input schema defining parameters for datasourceUid, labelName, and optional start/end times.const ListLokiLabelValuesSchema = z.object({ datasourceUid: z.string().describe('The UID of the datasource to query'), labelName: z.string().describe('The name of the label to retrieve values for'), startRfc3339: z.string().optional().describe('The start time of the query in RFC3339 format'), endRfc3339: z.string().optional().describe('The end time of the query in RFC3339 format'), });
- src/tools/loki.ts:174-180 (registration)Function to register all Loki tools, including listLokiLabelValues, with the MCP server.export function registerLokiTools(server: any) { server.registerTool(listLokiLabelNames); server.registerTool(listLokiLabelValues); server.registerTool(queryLokiLogs); server.registerTool(queryLokiStats); server.registerTool(findErrorPatternLogs); }
- src/clients/loki-client.ts:98-114 (helper)LokiClient.getLabelValues method that queries the Loki API endpoint /loki/api/v1/label/{label}/values to retrieve unique values for the specified label.async getLabelValues(label: string, start?: string, end?: string): Promise<string[]> { try { const params: any = {}; if (start) params.start = start; if (end) params.end = end; const response = await this.client.get(`/loki/api/v1/label/${label}/values`, { params }); if (response.data.status !== 'success') { throw new Error(`Failed to get label values: ${response.data.error || 'Unknown error'}`); } return response.data.data || []; } catch (error) { this.handleError(error); } }
- src/cli.ts:111-111 (registration)Call to registerLokiTools in the CLI entry point, which registers the list_loki_label_values tool.registerLokiTools(server);