Sign inSign up

talkopsai/loki-mcp-server

By talkopsai

•Updated 4 months ago

MCP Server for Grafana Loki log exploration, LogQL queries, and schema discovery

Image
Developer tools
Monitoring & observability
0

272

talkopsai/loki-mcp-server repository overview

⁠Loki MCP Server by TalkOps.ai⁠

An MCP server that gives AI assistants the power to explore, query, and analyze logs from Grafana Loki — with LogQL query construction, log structure discovery, cardinality-aware schema exploration, and production-safe query guardrails.

⁠✨ Features

  • Schema Discovery: Discover global label taxonomy, enumerate label values with scope queries, and validate active series with per-label cardinality — so the AI never halluccinates labels that don't exist.
  • Log Structure Analysis: Detect structural log patterns via Loki's pattern ingester and discover JSON/logfmt keys with types, cardinality, and auto-suggested parser pipelines.
  • Production-Safe Execution: Pre-check query cost (streams, chunks, bytes) against configurable guardrails, validate time windows, detect high-cardinality labels in selectors, and clamp result limits.
  • Unified LogQL Execution: Both log range queries and metric queries (rate(), count_over_time(), avg_over_time()) handled by a single tool with automatic result formatting.
  • Guided Workflows: Five built-in prompt workflows guide the AI through multi-step journeys — error investigation, health checks, log structure analysis, LogQL query building, and schema exploration.
  • Multi-Tenancy: Bearer token, Basic Auth, and X-Scope-OrgID header injection for multi-tenant Loki deployments.

⁠🚀 How to Use This Image

This Docker image runs an MCP server that connects to your Grafana Loki backend. AI assistants (Claude, Cline, Cursor, or any MCP client) connect to the server over HTTP, SSE, or stdio.

⁠Running Standalone (HTTP Transport)
docker run --rm -it \
  -p 8770:8770 \
  -e LOKI_URL=http://host.docker.internal:3100 \
  -e MCP_TRANSPORT=http \
  talkopsai/loki-mcp-server:latest

The server is now listening on http://localhost:8770/mcp.

Then configure your MCP client to connect:

{
  "mcpServers": {
    "loki": {
      "url": "http://localhost:8770/mcp",
      "description": "MCP Server for Grafana Loki log observability"
    }
  }
}
⁠Multi-Tenant Setup

For multi-tenant Loki deployments, inject the tenant org ID:

docker run --rm -it \
  -p 8770:8770 \
  -e LOKI_URL=http://host.docker.internal:3100 \
  -e LOKI_ORG_ID=my-tenant \
  -e MCP_TRANSPORT=http \
  talkopsai/loki-mcp-server:latest

⁠🛠️ Configuration (Claude Desktop — Stdio)

To use this image over stdio transport directly inside Claude Desktop:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "loki-mcp-server": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-e", "LOKI_URL=http://host.docker.internal:3100",
        "-e", "MCP_TRANSPORT=stdio",
        "-e", "MCP_LOG_LEVEL=INFO",
        "talkopsai/loki-mcp-server:latest"
      ]
    }
  }
}

For authenticated or multi-tenant backends, add additional -e args:

{
  "mcpServers": {
    "loki-mcp-server": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-e", "LOKI_URL=https://loki.example.com",
        "-e", "LOKI_AUTH_TOKEN=your-bearer-token",
        "-e", "LOKI_ORG_ID=my-tenant",
        "-e", "MCP_TRANSPORT=stdio",
        "-e", "MCP_LOG_LEVEL=INFO",
        "talkopsai/loki-mcp-server:latest"
      ]
    }
  }
}

⁠⚙️ Environment Variables

⁠Server Configuration
VariableDefaultDescription
MCP_TRANSPORTstdioTransport protocol: http, sse, streamable-http, or stdio
MCP_HOST0.0.0.0Host interface to bind to
MCP_PORT8770Port for HTTP server
MCP_PATH/mcpMCP endpoint path
MCP_LOG_LEVELINFOLog level: DEBUG, INFO, WARNING, ERROR
MCP_LOG_FORMATjsonLog format: json or text
MCP_HTTP_TIMEOUT300HTTP server timeout (seconds)
⁠Loki Backend
VariableDefaultDescription
LOKI_URLhttp://loki:3100Loki HTTP API base URL
LOKI_TIMEOUT30HTTP timeout per request (seconds)
LOKI_VERIFY_SSLtrueVerify SSL certificates
⁠Authentication
VariableDefaultDescription
LOKI_AUTH_TOKEN(empty)Bearer token for Loki authentication
LOKI_BASIC_AUTH_USER(empty)Basic auth username
LOKI_BASIC_AUTH_PASSWORD(empty)Basic auth password
LOKI_ORG_ID(empty)Multi-tenant org ID (X-Scope-OrgID header)
⁠Query Guardrails
VariableDefaultDescription
LOKI_MAX_QUERY_BYTES5000000000 (5 GB)Maximum bytes a query may scan before rejection
LOKI_MAX_TIME_WINDOW_HOURS336 (14 days)Maximum query time range in hours
LOKI_MAX_LOG_LIMIT5000Maximum log lines per query
LOKI_HIGH_CARDINALITY_THRESHOLD10000Warn when a label exceeds this many unique values

⁠🔧 Available Tools (8 Total)

⁠Discovery
ToolDescription
get_cluster_labelsDiscover global label taxonomy — always call first before writing any LogQL
get_label_valuesEnumerate concrete values for a label with optional scope queries
get_active_seriesValidate selectors against active streams with per-label cardinality
⁠Structure
ToolDescription
get_log_patternsDetect structural patterns and auto-suggest parser pipelines
get_detected_fieldsDiscover JSON/logfmt keys, types, cardinality, and required parsers (Loki 3.0+)
⁠Safety
ToolDescription
get_query_statsPreflight a selector to estimate query cost before execution
⁠Execution
ToolDescription
execute_logql_instantPoint-in-time LogQL query for scalar answers (count, rate, avg)
execute_logql_queryPrimary query tool. Execute LogQL range queries for log lines or metric time-series with guardrails
loki_query_a2uiExecute LogQL and return data formatted for A2UI interactive log tables

⁠📖 Available Resources (8 URIs)

ResourceDescription
loki://system/healthLoki reachability, readiness status, and label count
loki://schema/labelsAll available label names
loki://config/guardrailsCurrent safety thresholds
loki://config/backendsConfigured backend connection details
loki://reference/logqlLogQL syntax guide and examples
loki://reference/best-practicesCardinality rules, parser selection, structured metadata
loki://reference/query-templatesCommon incident, debug, audit, and performance query patterns
loki://reference/label-governanceLabel naming conventions and cardinality governance

⁠🔐 Security Best Practices

  1. Never expose the MCP server to the public internet without proper authentication.
  2. All tools are read-only — the server only performs HTTP GET requests against Loki's query APIs. No data is created, modified, or deleted.
  3. Tenant isolation: In multi-tenant deployments, verify that LOKI_ORG_ID is correctly scoped to prevent cross-tenant data leakage.
  4. Protect auth credentials: LOKI_AUTH_TOKEN and LOKI_BASIC_AUTH_PASSWORD are included in every request to the backend — treat them as secrets.
  5. Query guardrails: The server enforces byte, time window, limit, and cardinality thresholds. Review and tune for your environment.

⁠🐳 Docker Compose Example

services:
  loki-mcp-server:
    image: talkopsai/loki-mcp-server:latest
    ports:
      - "8770:8770"
    environment:
      - LOKI_URL=http://loki:3100
      - MCP_TRANSPORT=http
      - MCP_LOG_LEVEL=INFO
    depends_on:
      - loki

  loki:
    image: grafana/loki:latest
    ports:
      - "3100:3100"
    command: -config.file=/etc/loki/local-config.yaml

If you find this MCP server useful, consider leaving a ⭐ on the GitHub repository⁠!

Tag summary

Content type

Image

Digest

sha256:fcdc27e5b…

Size

71.6 MB

Last updated

4 months ago

docker pull talkopsai/loki-mcp-server