Loki MCP server
The Loki MCP Server is a Model Context Protocol server for querying and analyzing Grafana Loki logs safely. It exposes 9 discovery-first tools, 8 loki:// resources, and 5 guided triage prompts that require AI agents to discover valid stream labels, inspect structured JSON/logfmt fields, and check /query_stats byte-scan costs before executing LogQL queries.
Discover cluster labels, inspect recurring log patterns, estimate query byte scans, and execute instant or range LogQL queries.
Read live label taxonomies (loki://labels), backend health status, and LogQL syntax cheat sheets.
Step-by-step runbooks for incident error triage, discovery-first LogQL construction, and log-to-trace correlation.
Configure LOKI_MAX_QUERY_BYTES (5 GB default), lookback window caps, and X-Scope-OrgID multi-tenant headers.
Core capabilities
| Capability Domain | What the Server Provides |
|---|---|
| Discovery-First Label Taxonomy | Inspects global label keys (get_cluster_labels), enumerates valid label values (get_label_values), and verifies active stream series (get_active_series) so agents never hallucinate non-existent stream selectors. |
| Log Pattern & Field Analysis | Extracts recurring log templates via Loki's pattern API (get_log_patterns) and discovers structured JSON/logfmt field names, types, and recommended parser stages (` |
| Preflight Query Cost Guardrails | Calls /loki/api/v1/index/stats (get_query_stats) to estimate streams, chunks, and bytes touched prior to execution—rejecting queries that exceed LOKI_MAX_QUERY_BYTES (5 GB default). |
| Unified LogQL Execution | Runs instant scalar aggregations (execute_logql_instant), range log/metric queries (execute_logql_query), and A2UI table formatting (loki_query_a2ui). |
Position in the TalkOps observability stack
Architecture
Tool summary
| Tool | Category | Description |
|---|---|---|
get_cluster_labels | Discovery | List all indexed label names in the Loki cluster before constructing a query |
get_label_values | Discovery | Enumerate valid values (namespace, app, pod) for a specific label key |
get_active_series | Discovery | Verify matching log streams and inspect per-label cardinality |
get_log_patterns | Structure | Extract recurring structural log shapes and suggested parser expressions |
get_detected_fields | Structure | Discover JSON/logfmt field names, data types, and cardinality estimates |
get_query_stats | Safety | Estimate streams, chunks, entries, and bytes scanned before executing a query |
execute_logql_instant | Execution | Execute point-in-time LogQL metric aggregations and scalar lookups |
execute_logql_query | Execution | Execute range queries returning log lines or metric time series |
loki_query_a2ui | UI | Stream structured LogQL results into interactive A2UI log tables |
Quickstart
Point LOKI_URL to your Grafana Loki gateway or querier endpoint:
docker run --rm -it \
-p 8770:8770 \
-e MCP_TRANSPORT=http \
-e LOKI_URL=http://loki.monitoring:3100 \
talkopsai/loki-mcp-server:latest
Register the HTTP endpoint or launch via uvx over stdio:
{
"mcpServers": {
"loki": {
"url": "http://localhost:8770/mcp",
"description": "TalkOps Loki MCP Server for Log Observability"
}
}
}
Security and cost guardrails
- Byte-scan enforcement:
LOKI_MAX_QUERY_BYTES(5 GBdefault),LOKI_MAX_QUERY_LOOKBACK_DAYS(14days), andLOKI_MAX_LIMIT(5000lines) prevent unbounded full-cluster scans from overwhelming Loki queriers or LLM context windows. - Tenant isolation: Set
LOKI_ORG_IDin multi-tenant Loki clusters to inject theX-Scope-OrgIDheader on every request automatically.