Skip to main content
Glama
0xteamhq

Grafana MCP Server

by 0xteamhq

query_loki_logs

Execute LogQL queries against Loki datasources to retrieve log entries and metric values within specified time ranges for monitoring and analysis.

Instructions

Executes a LogQL query against a Loki datasource to retrieve log entries or metric values

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
datasourceUidYesThe UID of the datasource to query
directionNoDirection of the query
endRfc3339NoThe end time of the query in RFC3339 format
limitNoMaximum number of log lines to return (default: 10, max: 100)
logqlYesThe LogQL query to execute against Loki
startRfc3339NoThe start time of the query in RFC3339 format

Implementation Reference

  • The ToolDefinition object for 'query_loki_logs', including the async handler function that creates a LokiClient instance and executes the LogQL query to retrieve logs.
    export const queryLokiLogs: ToolDefinition = { name: 'query_loki_logs', description: 'Executes a LogQL query against a Loki datasource to retrieve log entries or metric values', inputSchema: QueryLokiLogsSchema, 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 logs = await client.queryLogs( params.logql, params.startRfc3339 || timeRange.start, params.endRfc3339 || timeRange.end, Math.min(params.limit || 10, 100), params.direction || 'backward' ); return createToolResult(logs); } catch (error: any) { return createErrorResult(error.message); } }, };
  • Zod input schema defining parameters for the query_loki_logs tool: datasourceUid, logql query, optional time range, limit, and direction.
    const QueryLokiLogsSchema = z.object({ datasourceUid: z.string().describe('The UID of the datasource to query'), logql: z.string().describe('The LogQL query to execute against Loki'), 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'), limit: z.number().optional().describe('Maximum number of log lines to return (default: 10, max: 100)'), direction: z.enum(['forward', 'backward']).optional().describe('Direction of the query'), });
  • Registration function for all Loki tools, including server.registerTool(queryLokiLogs) which registers the query_loki_logs tool.
    export function registerLokiTools(server: any) { server.registerTool(listLokiLabelNames); server.registerTool(listLokiLabelValues); server.registerTool(queryLokiLogs); server.registerTool(queryLokiStats); server.registerTool(findErrorPatternLogs); }
  • src/cli.ts:110-112 (registration)
    Invocation of registerLokiTools(server) in the CLI entrypoint, conditionally enabling Loki tools including query_loki_logs.
    if (enabledTools.has('loki')) { registerLokiTools(server); }
  • Helper function used by the query_loki_logs handler to provide default time range (last hour) if not specified.
    function getDefaultTimeRange(): { start: string; end: string } { const now = Math.floor(Date.now() / 1000); const oneHourAgo = now - 3600; return { start: oneHourAgo.toString(), end: now.toString(), }; }

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