Loki MCP Server for AI Log Analysis
Enable AI assistants to query and analyze Grafana Loki logs with an MCP server for intelligent troubleshooting, monitoring, and log-driven insights.
npx -y @mo-silent/loki-mcp-serverOverview
This MCP server bridges AI assistants with Grafana Loki so models can query, search, and reason over log data programmatically. Implemented as a Model Context Protocol (MCP) endpoint, the server accepts MCP requests (typically over stdio) from an assistant and translates them into Loki HTTP queries. The responses are structured for downstream reasoning: timestamps, labels, and contextual snippets are returned in a consistent format.
For developers, the server removes the friction of wiring assistant tooling to Loki. Rather than copying logs or crafting ad-hoc query adapters, you expose a small set of tools (query, search, label discovery) that the assistant can call directly. This enables smarter troubleshooting, automated incident summaries, and log-driven insights inside an LLM workflow.
Features
- Query LogQL (range and instant queries) against a Loki backend
- Keyword-based search with label filtering and boolean operators
- Stream label discovery and value enumeration
- MCP protocol compatibility for use with AI assistants (e.g., Claude, other MCP clients)
- Authentication: support for Basic Auth and Bearer tokens
- Configurable caching to reduce repeated Loki calls and improve latency
- Structured, enriched results including timestamps, labels, and surrounding context
- CLI entrypoint for local/dev usage; intended to be embedded in assistant MCP configurations
Installation / Configuration
Requirements:
- Python 3.8+
- Network access to a Grafana Loki instance
Install from source:
Development install (includes test and lint deps):
Start the server (CLI):
The server reads configuration from environment variables. Key variables:
| Variable | Required | Description | Example |
|---|---|---|---|
| LOKI_URL | Yes | Base URL of the Loki HTTP API | http://localhost:3100 |
| LOKI_USERNAME | No | Username for Basic Auth | service-account |
| LOKI_PASSWORD | No | Password for Basic Auth | verysecret |
| LOKI_BEARER_TOKEN | No | Bearer token for Authorization | eyJhbGci… |
Example .env:
LOKI_URL=http://localhost:3100
LOKI_BEARER_TOKEN=your-api-token
Production examples:
Basic auth
Bearer token
Available Tools
The MCP server exposes small, focused tools that an assistant can call.
query_logs
- Purpose: Run arbitrary LogQL queries (range or instant).
- Parameters:
- query (string, required)
- start (RFC3339 or epoch ms, optional)
- end (RFC3339 or epoch ms, optional)
- limit (int, default 100)
- direction (forward|backward)
- Example:
search_logs
- Purpose: Keyword-driven search with label filters and boolean operators.
- Parameters:
- keywords (array[string], required)
- labels (object, optional)
- start, end, limit (optional)
- case_sensitive (bool, default false)
- operator (AND|OR, default AND)
- Example:
get_labels
- Purpose: List available labels or values for a specific label within a time range.
- Parameters:
- label_name (string, optional)
- start, end (optional)
- use_cache (bool, default true)
- Example:
Responses are returned as JSON with entries including timestamp, stream labels, and the log line or snippet. The server applies optional caching and will surface HTTP errors and rate-limit information where relevant.
Use Cases
- Incident Triage: An assistant can call search_logs to surface recent ERROR/exception patterns, group by label (pod, job, service), and produce a short root-cause hypothesis for on-call engineers.
- Alert Verification: Use query_logs to fetch logs for a specific alert window and validate whether the alert conditions correlate with application errors or infrastructure events.
- Trend Detection: Periodically run label discovery and top-N searches to detect spikes (e.g., increased 5xx responses) and feed those summaries into dashboards or incident reports.
- Log-driven Debugging: Combine fine-grained LogQL queries with surrounding context to let an assistant produce step-by-step debugging instructions based on exact traces in the logs.
Development & Troubleshooting
Run tests:
Run linters and formatters:
Project layout (high level):
app/
├── main.py # CLI entrypoint
├── server.py # MCP server and message handling
├── config.py # env/config parsing
├── loki_client.py # HTTP client for Loki
├── query_builder.py# helpers for LogQL generation
Source and issues: https://github.com/mo-silent/loki-mcp-server
For integration, register the CLI as an MCP server in your assistant configuration so the model can call the three tools above. The server is intended to be small, auditable, and safe to run alongside assistant tooling in development and on-call environments.