Loki MCP Server

by gigapipehq

Not rated
GitHub

About

An MCP server for querying logs from Grafana Loki.

Details

Author
gigapipehq
Categories
Database, Other, Infrastructure

Setup

Install Loki MCP Server in your MCP client (Claude Desktop, Cursor, Windsurf, and others).

Repository: https://github.com/gigapipehq/gigapipe-logs-mcp

Follow the installation instructions in the repository README, then restart your MCP client.

A Go-based server implementation for the Model Context Protocol (MCP) with Grafana Loki integration.

# Build the server go build -o loki-mcp-server ./cmd/server # Run the server ./loki-mcp-server

The server communicates using stdin/stdout following the Model Context Protocol (MCP). This makes it suitable for use with Claude Desktop and other MCP-compatible clients. It does not run as an HTTP server on a port.

. ├── cmd/ │ ├── server/ # MCP server implementation │ └── client/ # Client for testing the MCP server ├── internal/ │ ├── handlers/ # Tool handlers │ └── models/ # Data models ├── pkg/ │ └── utils/ # Utility functions and shared code └── go.mod # Go module definition

The Loki MCP Server implements the Model Context Protocol (MCP) and provides the following tools:

Theloki_querytool allows you to query Grafana Loki log data:

- url: The Loki server URL (default: from LOKI_URL environment variable orhttp://localhost:3100)
- start: Start time for the query (default: 1h ago)
- end: End time for the query (default: now)
- limit: Maximum number of entries to return (default: 100)
- org: Organization ID for the query (sent as X-Scope-OrgID header)

The Loki query tool supports the following environment variables:

- LOKI_URL: Default Loki server URL to use if not specified in the request
- LOKI_ORG_ID: Default organization ID to use if not specified in the request
- LOKI_USERNAME: Default username for basic authentication if not specified in the request
- LOKI_PASSWORD: Default password for basic authentication if not specified in the request
- LOKI_TOKEN: Default bearer token for authentication if not specified in the request

Security Note: When using authentication environment variables, be careful not to expose sensitive credentials in logs or configuration files. Consider using token-based authentication over username/password when possible.

You can test the MCP server using the provided client:

# Build the client go build -o loki-mcp-client ./cmd/client # Loki query examples: ./loki-mcp-client loki_query "{job=\"varlogs\"}" ./loki-mcp-client loki_query "{job=\"varlogs\"}" "-1h" "now" 100 # Using environment variables: export LOKI_URL="http://localhost:3100" ./loki-mcp-client loki_query "{job=\"varlogs\"}" # Using environment variables for both URL and org: export LOKI_URL="http://localhost:3100" export LOKI_ORG_ID="tenant-123" ./loki-mcp-client loki_query "{job=\"varlogs\"}" # Using environment variables for authentication: export LOKI_URL="http://localhost:3100" export LOKI_USERNAME="admin" export LOKI_PASSWORD="password" ./loki-mcp-client loki_query "{job=\"varlogs\"}" # Using environment variables with bearer token: export LOKI_URL="http://localhost:3100" export LOKI_TOKEN="your-bearer-token" ./loki-mcp-client loki_query "{job=\"varlogs\"}" # Using all environment variables together: export LOKI_URL="http://localhost:3100" export LOKI_ORG_ID="tenant-123" export LOKI_USERNAME="admin" export LOKI_PASSWORD="password" ./loki-mcp-client loki_query "{job=\"varlogs\"}" # Using org parameter for multi-tenant setups: ./loki-mcp-client loki_query "{job=\"varlogs\"}" "" "" "" "" "" "tenant-123"

You can build and run the MCP server using Docker:

# Build the Docker image docker build -t loki-mcp-server . # Run the server docker run --rm -i loki-mcp-server

Alternatively, you can use Docker Compose:

# Build and run with Docker Compose docker-compose up --build

The project includes a complete Docker Compose setup to test Loki queries locally:

- A Loki server on port 3100
- A Grafana instance on port 3000 (pre-configured with Loki as a data source)
- A log generator container that sends sample logs to Loki
- The Loki MCP server

Use the provided test script to query logs:

# Run with default parameters (queries last 15 minutes of logs) ./test-loki-query.sh # Query for error logs ./test-loki-query.sh '{job="varlogs"} |= "ERROR"' # Specify a custom time range and limit ./test-loki-query.sh '{job="varlogs"}' '-1h' 'now' 50
# Insert 10 dummy logs with default settings ./insert-loki-logs.sh # Insert 20 logs with custom job and app name ./insert-loki-logs.sh --num 20 --job "custom-job" --app "my-app" # Insert logs with custom environment and interval ./insert-loki-logs.sh --env "production" --interval 0.5 # Show help message ./insert-loki-logs.sh --help

Access the Grafana UI athttp://localhost:3000to explore logs visually.

The server now supports two modes of communication:
- Standard input/output (stdin/stdout) following the Model Context Protocol (MCP)
- HTTP Server with Server-Sent Events (SSE) endpoint for integration with tools like n8n

The default port for the HTTP server is 8080, but can be configured using theSSE_PORTenvironment variable.

When running in HTTP mode, the server exposes the following endpoints:

- SSE Endpoint:http://localhost:8080/sse- For real-time event streaming
- MCP Endpoint:http://localhost:8080/mcp- For MCP protocol messaging

When running the server with Docker, make sure to expose port 8080:

# Build the Docker image docker build -t loki-mcp-server . # Run the server with port mapping docker run -p 8080:8080 --rm -i loki-mcp-server

You can integrate the Loki MCP Server with n8n workflows:
-

Install the MCP Client Tools node in n8n

Configure the node with these parameters:

- SSE Endpoint:http://your-server-address:8080/sse(replace with your actual server address)
- Authentication: Choose appropriate authentication if needed
- Tools to Include: Choose which Loki tools to expose to the AI Agent

Connect the MCP Client Tool node to an AI Agent node that will use the Loki querying capabilities

Example workflow: Trigger → MCP Client Tool (Loki server) → AI Agent (Claude)

The Loki MCP Server uses a modular architecture:

- Server: The main MCP server implementation incmd/server/main.go
- Client: A test client incmd/client/main.gofor interacting with the MCP server
- Handlers: Individual tool handlers ininternal/handlers/

- loki.go: Grafana Loki query functionality

You can use this MCP server with Claude Desktop to add Loki query tools. Follow these steps:

go build -o loki-mcp-server ./cmd/server

- Add the configuration to your Claude Desktop configuration file usingclaude_desktop_config_binary.json.

Option 2: Using Go Run with a Shell Script

- Add the configuration to your Claude Desktop configuration file usingclaude_desktop_config_script.json. - Add the configuration to your Claude Desktop configuration file usingclaude_desktop_config_docker.json.

The Claude Desktop configuration file is located at:

- On macOS:~/Library/Application Support/Claude/claude_desktop_config.json
- On Windows:%APPDATA%\Claude\claude_desktop_config.json
- On Linux:~/.config/Claude/claude_desktop_config.json

You can use one of the example configurations provided in this repository:

- claude_desktop_config.json: Generic template
- claude_desktop_config_example.json: Example usinggo runwith the current path
- claude_desktop_config_binary.json: Example using the compiled binary
- claude_desktop_config_script.json: Example using a shell script (recommended forgo run)
- claude_desktop_config_docker.json: Example using Docker (most reliable)

-

When usinggo runwith Claude Desktop, you may need to set several environment variables in both the script and the configuration file:

- HOME: The user's home directory
- GOPATH: The Go workspace directory
- GOMODCACHE: The Go module cache directory
- GOCACHE: The Go build cache directory

These are required to ensure Go can find its modules and build cache when run from Claude Desktop.

Using Docker is the most reliable approach as it packages all dependencies and environment variables in a container.

{ "mcpServers": { "lokiserver": { "command": "path/to/loki-mcp-server", "args": [], "env": { "LOKI_URL": "http://localhost:3100", "LOKI_ORG_ID": "your-default-org-id", "LOKI_USERNAME": "your-username", "LOKI_PASSWORD": "your-password", "LOKI_TOKEN": "your-bearer-token" }, "disabled": false, "autoApprove": ["loki_query"] } } }

Make sure to replacepath/to/loki-mcp-serverwith the absolute path to the built binary or source code.

- Loki query examples:

- "Query Loki for logs with the query {job="varlogs"}"
- "Find error logs from the last hour in Loki using query {job="varlogs"} |= "ERROR""
- "Show me the most recent 50 logs from Loki with job=varlogs"
- "Query Loki for logs with org 'tenant-123' using query {job="varlogs"}"

Using Organization ID in Natural Language Prompts

When using this MCP server with Claude Desktop or other AI assistants, users can naturally mention the organization ID in their prompts in several ways:

- "Query Loki for logs from organization 'tenant-123' with the query {job="varlogs"}"
- "Search Loki logs for org 'production-env' using {job="web"}"
- "Get logs from organization ID 'client-abc' matching {service="api"}"

- "Check the error logs for our production tenant (org: prod-001) using query {level="error"}"
- "Find all logs from customer organization 'customer-xyz' for the last hour"
- "Query Loki with org tenant-456 to find logs matching {job="backend"}"

- "Switch to organization 'dev-team' and query {job="logs"} for debugging"
- "Use org 'staging-env' to search for warning logs in the last 2 hours"
- "Search logs in tenant 'qa-environment' for any error messages"

- "Query Loki for organization 'prod-cluster' from 2 hours ago to now with limit 50"
- "Get the last 100 logs from org 'microservice-team' for query {app="payment"}"

When you mention any of these natural language prompts, the AI assistant will automatically map terms like "organization", "org", "tenant", or "organization ID" to theorgparameter in the Loki query tool, which gets sent as theX-Scope-OrgIDheader to your Loki server for proper multi-tenant filtering.

The key is to naturally mention any specific parameters in your request - the AI will understand how to map them to the appropriate Loki query tool parameters. When parameters are not explicitly mentioned, the system will automatically use defaults from environment variables:

- LOKI_URLfor the Loki server URL
- LOKI_ORG_IDfor the organization ID
- LOKI_USERNAMEandLOKI_PASSWORDfor basic authentication
- LOKI_TOKENfor bearer token authentication

This makes it very convenient to set up default connection parameters once and then use natural language queries without having to specify authentication details every time.

You can also integrate the Loki MCP server with the Cursor editor. To do this, add the following configuration to your Cursor settings:

{ "mcpServers": { "loki-mcp-server": { "command": "docker", "args": ["run", "--rm", "-i", "-e", "LOKI_URL=http://host.docker.internal:3100", "-e", "LOKI_ORG_ID=your-default-org-id", "-e", "LOKI_USERNAME=your-username", "-e", "LOKI_PASSWORD=your-password", "-e", "LOKI_TOKEN=your-bearer-token", "loki-mcp-server:latest"] } } }

After adding this configuration, restart Cursor, and you'll be able to use the Loki query tool directly within the editor.

This project is licensed under the MIT License - see the LICENSE file for details.

The project includes comprehensive unit tests and CI/CD workflows to ensure reliability:

# Run all tests go test ./... # Run tests with coverage go test -coverprofile=coverage.out ./... go tool cover -func=coverage.out # Run tests with race detection go test -race ./...

This project previously had a critical bug where timestamps were displayed as year 2262 instead of correct dates. This has been fixed and regression tests are in place:

- Root Cause: Loki returns timestamps in nanoseconds, but the code was incorrectly treating them as seconds and multiplying by 1,000,000,000
- Fix: Correctly handle nanosecond timestamps from Loki
- Testing: Comprehensive tests ensure timestamps display correctly (e.g., 2024, 2023) instead of 2262
- CI Protection: Automated tests prevent regression of this critical bug

- ✅ Timestamps show correct years instead of 2262
- ✅ Multiple timestamp formats work correctly
- ✅ Invalid timestamps have proper fallback behavior
- ✅ Integration with real Loki instances

A server for querying Loki logs from Grafana.

An MCP server for log analysis using the LogAI framework, with optional Grafana and GitHub integrations.

Debug and investigate app issues using AI and Grafana Loki

A Go-based server to query Grafana Loki logs using the Model Context Protocol (MCP).

An MCP server for querying Loki logs via logcli.

Query Grafana Loki logs using logcli with an automatic fallback to the HTTP API.

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.