A Go-based server implementation for the Model Context Protocol (MCP) with Grafana Loki integration.
- Go 1.16 or higher
Build and run the server:
# Build the server
go build -o loki-mcp-server ./cmd/server
# Run the server
./loki-mcp-server
Or run directly with Go:
go run ./cmd/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:
The loki_query
tool allows you to query Grafana Loki log data:
-
Required parameters:
query
: LogQL query string
-
Optional parameters:
url
: The Loki server URL (default: from LOKI_URL environment variable or http://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)
The Loki query tool supports the following environment variables:
LOKI_URL
: Default Loki server URL to use if not specified in the request
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 variable:
export LOKI_URL="http://localhost:3100"
./loki-mcp-client loki_query "{job=\"varlogs\"}"
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:
-
Start the Docker Compose environment:
docker-compose up -d
This will start:
- 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 dummy logs for testing:
# 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 at http://localhost:3000 to 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 the SSE_PORT
environment 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
- SSE Endpoint:
-
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 in
cmd/server/main.go
- Client: A test client in
cmd/client/main.go
for interacting with the MCP server - Handlers: Individual tool handlers in
internal/handlers/
loki.go
: Grafana Loki query functionality
You can use this MCP server with Claude Desktop to add Loki query tools. Follow these steps:
- Build the server:
go build -o loki-mcp-server ./cmd/server
- Add the configuration to your Claude Desktop configuration file using
claude_desktop_config_binary.json
.
- Make the script executable:
chmod +x run-mcp-server.sh
- Add the configuration to your Claude Desktop configuration file using
claude_desktop_config_script.json
.
- Build the Docker image:
docker build -t loki-mcp-server .
- Add the configuration to your Claude Desktop configuration file using
claude_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 templateclaude_desktop_config_example.json
: Example usinggo run
with the current pathclaude_desktop_config_binary.json
: Example using the compiled binaryclaude_desktop_config_script.json
: Example using a shell script (recommended forgo run
)claude_desktop_config_docker.json
: Example using Docker (most reliable)
Notes:
-
When using
go run
with Claude Desktop, you may need to set several environment variables in both the script and the configuration file:HOME
: The user's home directoryGOPATH
: The Go workspace directoryGOMODCACHE
: The Go module cache directoryGOCACHE
: 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.
Or create your own configuration:
{
"mcpServers": {
"lokiserver": {
"command": "path/to/loki-mcp-server",
"args": [],
"env": {
"LOKI_URL": "http://localhost:3100"
},
"disabled": false,
"autoApprove": ["loki_query"]
}
}
}
Make sure to replace path/to/loki-mcp-server
with the absolute path to the built binary or source code.
-
Restart Claude Desktop.
-
You can now use the tools in Claude:
- 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"
- Loki query examples:
You can also integrate the Loki MCP server with the Cursor editor. To do this, add the following configuration to your Cursor settings:
Docker configuration:
{
"mcpServers": {
"loki-mcp-server": {
"command": "docker",
"args": ["run", "--rm", "-i", "-e", "LOKI_URL=http://host.docker.internal:3100", "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.