Loki MCP Server

by scottlepp

153 395 downloads Not rated yet

About

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

Explore

- Provides a loki_query tool for executing LogQL queries.
- Supports both stdin/stdout and SSE (HTTP) communication modes.
- Configurable via environment variables (LOKI_URL, LOKI_ORG_ID, LOKI_USERNAME, LOKI_PASSWORD, LOKI_TOKEN).
- Can be run locally with Docker Compose including Loki, Grafana, and a log generator.
- Supports multi-tenant setups via the X-Scope-OrgID header.
- Includes a test client and scripts for verification.

Setting up with Highlight

This MCP is not yet compatible with Highlight’s one-click setup. However, you can still use it with Highlight by following these steps:

  1. Download and install Highlight from highlightai.com/download
  2. Navigate to the plugins tab and select "Add Custom Plugin"
  3. Configure the plugin with the settings below
    Plugin Name Loki MCP Server
    Command (node, npx, python, etc.)

    Please refer to the README for specific instructions on how to obtain API keys or other required environment variables.

  4. Enable "Start Automatically" if you want the plugin to start when Highlight launches

From the repository

- Go 1.16 or higher

Build and run the server:


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.

export LOKI_URL="http://localhost:3100"
./loki-mcp-client loki_query "{job=\"varlogs\"}"

export LOKI_URL="http://localhost:3100"
export LOKI_ORG_ID="tenant-123"
./loki-mcp-client loki_query "{job=\"varlogs\"}"

export LOKI_URL="http://localhost:3100"
export LOKI_USERNAME="admin"
export LOKI_PASSWORD="password"
./loki-mcp-client loki_query "{job=\"varlogs\"}"

export LOKI_URL="http://localhost:3100"
export LOKI_TOKEN="your-bearer-token"
./loki-mcp-client loki_query "{job=\"varlogs\"}"

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\"}"

./loki-mcp-client loki_query "{job=\"varlogs\"}" "" "" "" "" "" "tenant-123"

./insert-loki-logs.sh --env "production" --interval 0.5

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 using go run with the current path
- claude_desktop_config_binary.json: Example using the compiled binary
- claude_desktop_config_script.json: Example using a shell script (recommended for go 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 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.

Or create your own configuration:

{
  "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 replace path/to/loki-mcp-server with the absolute path to the built binary or source code.

4. Restart Claude Desktop.

5. 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"
- "Query Loki for logs with org 'tenant-123' using query {job=\"varlogs\"}"

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

```bash

query

LogQL query string

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)

org

Organization ID for the query (sent as X-Scope-OrgID header)

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)
- org: Organization ID for the query (sent as X-Scope-OrgID header)

Claude Desktop / Cursor

Paste into your MCP client config file to install this server.

{
    "mcpServers": {
        "loki mcp server": {
            "loki-mcp": {
                "command": "docker",
                "args": [
                    "build",
                    "-t",
                    "loki-mcp-server",
                    "."
                ]
            }
        }
    }
}

McpServers

{
    "loki-mcp": {
        "command": "docker",
        "args": [
            "build",
            "-t",
            "loki-mcp-server",
            "."
        ]
    }
}

CI

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

Getting Started

Prerequisites

- Go 1.16 or higher

Building and Running

Build and run the server:

```bash

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.

Videos about Loki MCP Server

Relevant YouTube tutorials, setups, and demos