Sponsored by Deepsite.site

Loki MCP Server

Created By
scottlepp8 months ago
Content

Loki MCP Server

A Model Context Protocol (MCP) server for Grafana Loki that provides AI agents with access to log data.

Overview

The Loki MCP Server allows AI agents to query and analyze logs stored in Grafana Loki, providing additional context to Large Language Models (LLMs). It implements the Model Context Protocol to expose tools and resources for interacting with Loki.

Features

Tools

  • query_logs: Execute LogQL queries and retrieve matching log entries
  • get_log_labels: Retrieve all label names available in the logs
  • get_label_values: Get all values for a specific label
  • get_log_volume: Analyze log volume over time

Resources

  • Log streams by label selector: Access log streams filtered by a LogQL label selector

    • URI template: loki://logs/{selector}
    • Example: loki://logs/app="frontend",env="prod"
  • Recent error logs: Access recent error logs across all services

    • URI: loki://errors/recent
  • Service logs: Access logs for a specific service

    • URI template: loki://service/{service_name}/logs
    • Example: loki://service/frontend/logs

Installation

Prerequisites

  • Go 1.18 or later
  • Access to a Grafana Loki instance

Building from Source

  1. Clone the repository:

    git clone https://github.com/scottlepper/loki-mcp-server.git
    cd loki-mcp-server
    
  2. Build the server:

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

Configuration

The server can be configured using command-line flags or environment variables:

FlagEnvironment VariableDescriptionDefault
--loki-urlLOKI_URLURL of the Loki instancehttp://localhost:3100/
--usernameLOKI_USERNAMEUsername for basic auth
--passwordLOKI_PASSWORDPassword for basic auth
--api-keyLOKI_API_KEYAPI key for authentication
--tenant-idLOKI_TENANT_IDTenant ID for multi-tenant setups
--query-timeoutQUERY_TIMEOUTTimeout for queries30s

Usage

Starting the Server

./loki-mcp-server --loki-url=http://loki.example.com:3100/

Or with environment variables:

LOKI_URL=http://loki.example.com:3100/ ./loki-mcp-server

Using Docker Compose

A Docker Compose setup is provided for easy testing with a complete Loki stack:

  1. Start the environment:

    docker-compose up -d
    

    This will start:

    • Grafana Loki for log storage
    • Promtail for log collection
    • Grafana for log visualization
    • The Loki MCP Server
  2. Access Grafana at http://localhost:3000 to view logs

  3. Configure an MCP-compatible AI agent to connect to the Loki MCP Server

  4. For convenience, you can use the provided test script:

    ./examples/docker-test.sh
    

Using with MCP-compatible AI Agents

To use the Loki MCP Server with an MCP-compatible AI agent, you need to configure the agent to connect to the server. The specific configuration depends on the agent you're using.

Standard Configuration

For example, with Claude Desktop, you would add the following to your claude_desktop_config.json:

{
  "mcpServers": {
    "loki": {
      "command": "/path/to/loki-mcp-server",
      "args": ["--loki-url=http://loki.example.com:3100/"],
      "env": {
        "LOKI_API_KEY": "your-api-key"
      }
    }
  }
}

Docker Compose Configuration

If you're using the Docker Compose setup, you can configure Claude Desktop to connect to the containerized MCP server:

{
  "mcpServers": {
    "loki": {
      "command": "docker",
      "args": ["exec", "-i", "loki-mcp-server-loki-mcp-server-1", "/app/loki-mcp-server"],
      "env": {},
      "disabled": false,
      "autoApprove": []
    }
  }
}

Example configurations are provided in the examples directory:

  • examples/claude_desktop_config.json: Standard configuration
  • examples/claude_desktop_docker_config.json: Docker Compose configuration

Examples

Querying Logs

Use the query_logs tool to find error logs in the frontend service from the last hour.

Getting Label Values

Use the get_label_values tool to list all possible values for the "app" label.

Accessing Service Logs

Access the logs for the authentication service using the loki://service/auth/logs resource.

Development

Project Structure

  • cmd/loki-mcp-server/: Main application entry point
  • pkg/mcp/: MCP protocol implementation
  • pkg/loki/: Loki client and tools implementation

Running Tests

go test ./...

License

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

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
ChatWiseThe second fastest AI chatbot™
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Playwright McpPlaywright MCP server
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
WindsurfThe new purpose-built IDE to harness magic
Amap Maps高德地图官方 MCP Server
Tavily Mcp
Serper MCP ServerA Serper MCP Server
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
CursorThe AI Code Editor
DeepChatYour AI Partner on Desktop