Skip to main content
Glama
mgcrea

@mgcrea/mcp-grafana

by mgcrea

Grafana: Query Loki

grafana_query_loki
Read-only

Run LogQL queries against Loki to fetch logs or aggregate metric series, returning time-ordered lines or stats for debugging and analysis.

Instructions

Run LogQL through a Loki datasource. A log query ({namespace="api"} |= "error") returns lines newest first, each with its time, level and the labels that differ between lines (the shared ones once, as common_labels), cut at max_line_chars. A metric query (sum by (app) (count_over_time({namespace="api"} |= "error" [5m]))) returns series on a step grid with stats. Always put a label matcher in the selector and keep the range short: Loki scans every line in it. To count, prefer a metric query over fetching lines.

Input Schema

NameRequiredDescriptionDefault
endNoRange end: now, now-1h, -30m, an RFC3339 timestamp (2026-01-31T12:00:00Z) or unix seconds/ms. Defaults to now.
exprYesA LogQL query, e.g. `{namespace="ingress-nginx"} |= "error" | json | status >= 500`. grafana_list_labels lists the stream labels to select on.
sortNoOrder of time series, applied before `limit` cuts: labels keeps the datasource's order; max/avg/last rank highest first, so a cut keeps the biggest.labels
stepNoResolution, as a duration (30s, 5m, 1h). Defaults to a round step giving `points` points. Grafana never goes below the datasource's scrape interval.
viewNoFor time series. values: stats (min/max/avg/last) plus one value per step on a shared grid given by start and step, null where there was no sample. stats: the stats only, about a tenth of the size — use it to survey many series before reading a few.values
limitNoMaximum log lines, or series for a metric query (1-5000). Defaults to 50.
startNoRange start: now, now-1h, -30m, an RFC3339 timestamp (2026-01-31T12:00:00Z) or unix seconds/ms. Defaults to now-1h.
pointsNoTarget points per series (2-1000), from which the step is picked. Defaults to 60; a dashboard in a browser asks for ~1000, which is rarely worth the tokens.
instantNoFor a metric query: evaluate once at `end` instead of over the range.
directionNobackward (default): the newest `limit` lines. forward: the oldest first.backward
precisionNoSignificant digits to round fractional values to (whole numbers are never rounded). Defaults to 4; 0 returns the exact value.
datasourceNoDatasource uid or name, e.g. "prometheus" or "Loki". Defaults to the first Loki datasource. grafana_list_datasources lists them.
max_line_charsNoCut each log line at this many characters (50-20000). Defaults to 1000.

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observedv0.1.0

TDQS

A4.7/5.0
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

The description goes well beyond the readOnlyHint/openWorldHint annotations to describe return shapes: log queries return lines newest first with time, level and per-line labels, with shared labels hoisted into common_labels and truncation at max_line_chars; metric queries return series on a step grid with stats. It also discloses a genuine cost/performance trap (full-selector scans, so keep ranges short) and a token-efficiency heuristic for counting.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Four tight sentences: what it does, then return shape for each query mode, then the two rules that prevent bad calls. Concrete example LogQL is embedded inline rather than in a separate block, and the safety-critical advice (label matcher, short range) is placed where it will be read.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

There are 13 parameters and no output schema, so the description carries the burden of explaining what comes back; it does exactly that for both response modes, including the common_labels hoisting and truncation. Together with the fully documented schema and the read-only annotation, an agent has everything needed to call this correctly and interpret the result.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100%, so the schema already does the heavy lifting and the baseline is 3. The description still adds meaning by explaining how parameters shape the response: that max_line_chars cuts each line, that metric output arrives on a step grid, and that results are counted rather than listed. It doesn't re-explain the timestamp formats or enum semantics, but it doesn't need to.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The opening sentence states a specific verb and resource ("Run LogQL through a Loki datasource"), and the following sentences split the tool into its two modes, log query vs metric query, with concrete example syntax for each. Because Loki/LogQL is named explicitly, an agent can distinguish this from grafana_query_prometheus and from the generic grafana_query without opening either schema.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

It gives real operating guidance: always include a label matcher, keep the range short because Loki scans every line, and prefer a metric query over fetching lines when counting; the schema descriptions also point to grafana_list_labels and grafana_list_datasources for discovery. What's missing is explicit sibling routing (for example, when to prefer grafana_query or grafana_run_panel_query), so it stops just short of a 5.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.