by grafana
An MCP ( Model Context Protocol ) Server for Grafana Loki
# Add to your Claude Code skills
git clone https://github.com/grafana/loki-mcpA Go-based server implementation for the Model Context Protocol (MCP) with Grafana Loki integration.
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 and SSE following the Model Context Protocol (MCP). This makes it suitable for use with Claude Desktop and other MCP-compatible clients.
.
├── 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 stringOptional parameters:
url: The Loki server URL (default: from LOKI_URL environment variable or http://localhost:3100)No comments yet. Be the first to share your thoughts!
startend: 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 requestLOKI_ORG_ID: Default organization ID to use if not specified in the requestLOKI_USERNAME: Default username for basic authentication if not specified in the requestLOKI_PASSWORD: Default password for basic authentication if not specified in the requestLOKI_TOKEN: Default bearer token for authentication if not specified in the requestSecurity 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...