Loki Go MCP Server with Grafana Integration
Deploy a Go-based MCP server with Grafana Loki integration to centralize and visualize model context logs in real time.
npx -y @scottlepp/loki-mcpOverview
This project is a Go implementation of a Model Context Protocol (MCP) server that forwards and queries model context logs into Grafana Loki. It exposes MCP over stdin/stdout for direct agent integrations (for example, Claude Desktop or other MCP-compatible clients) and optionally as an HTTP server with Server-Sent Events (SSE) for real-time streaming and web integrations.
By centralizing model context and tool-output logs in Loki, teams can index, search, and visualize context history alongside application logs in Grafana. The server is lightweight, configurable via environment variables, and suitable for local development (Docker Compose) or production deployments that need multi-tenant Loki access and authentication.
Features
- MCP server implemented in Go (Go 1.16+)
- Communicates via stdin/stdout (MCP) and optional HTTP SSE endpoint
- Grafana Loki integration for querying and inserting logs
- Authentication support: basic auth and bearer token
- Configurable via environment variables and CLI
- Included test client and Docker/Docker Compose support for local testing
- Tool-based architecture: e.g., loki_query tool for LogQL queries
Installation / Configuration
Prerequisites: Go 1.16+ (or Docker)
Build and run locally:
# Build the server binary
# Run the server (stdin/stdout MCP mode)
Run directly with Go:
Docker:
# Build the Docker image
# Run the container (interactive, reads stdin)
Docker Compose (includes Loki + Grafana for local testing):
# Build and bring up services
Environment variables (defaults noted):
| Variable | Purpose |
|---|---|
| LOKI_URL | Default Loki server URL (default: http://localhost:3100) |
| LOKI_ORG_ID | Default X-Scope-OrgID header for multi-tenant Loki |
| LOKI_USERNAME | Username for basic auth |
| LOKI_PASSWORD | Password for basic auth |
| LOKI_TOKEN | Bearer token for Authorization header |
| SSE_PORT | Port for HTTP SSE server (default: 8080) |
Security note: Avoid checking credentials into source control. Prefer bearer tokens and secrets management; redact logs that may contain credentials.
Available Tools
The server exposes a set of tools via MCP. The main provided tool:
- loki_query
- Purpose: Run LogQL queries against Loki and return matched log entries.
- Required parameter: query (LogQL string)
- Optional parameters: url, start, end, limit, org
Example usage with the provided test client:
# Build the client
# Simple query
# Query with time range and limit
When parameters are omitted, the tool falls back to LOKI_* environment variables.
Endpoints (HTTP mode)
When started with HTTP/SSE enabled, default endpoints are:
- SSE stream: http://localhost:8080/sse
- MCP over http: http://localhost:8080/mcp
SSE is useful for web UIs, integrations like n8n, or any client that prefers a streaming HTTP interface.
Use Cases
- Real-time agent debugging: Stream model context and tool outputs into Loki, then view in Grafana to investigate agent decisions and environment state.
- Example: Run the MCP server locally with Docker Compose, have your agent connect over stdin/stdout, then open Grafana at http://localhost:3000 to explore logs and dashboards.
- Multi-tenant log queries: Use LOKI_ORG_ID to scope queries for tenant-specific logs in a multi-tenant Loki deployment.
- Example: set LOKI_ORG_ID=tenant-123 and invoke loki_query to retrieve only that tenant’s entries.
- Web integrations: Expose SSE to push model context events to a browser-based dashboard or an automation platform like n8n for live monitoring.
- Example: Connect to /sse and render received events in a live timeline UI.
Testing and Local Development
- Use the included Docker Compose to spin up Loki, Grafana, sample log generator, and the MCP server.
- Scripts provided in the repo:
- test-loki-query.sh — run sample queries
- insert-loki-logs.sh — insert synthetic logs for demo/QA
Example local test:
Project Layout (quick)
- cmd/server — MCP server
- cmd/client — test client
- internal/handlers — tool implementations
- internal/models — request/response models
- pkg/utils — shared helpers
For source, issues, and contributions: https://github.com/scottlepp/loki-mcp