grafana-loki-mcp

MCP.Pizza Chef: tumf

Grafana-Loki MCP Server is a FastMCP-based server that enables querying and retrieving logs from Grafana Loki. It integrates with Grafana instances using API keys, allowing real-time access to log data for monitoring, debugging, and analysis. This server facilitates structured log data exposure to LLMs, enabling advanced log querying and automation workflows within the MCP ecosystem.

Use This MCP server To

Query Grafana Loki logs in real-time via MCP Integrate log data into AI workflows for analysis Automate log retrieval for incident investigation Enable LLMs to access and interpret Loki logs Combine Loki logs with other data sources in MCP Monitor application logs through natural language queries

README

Grafana-Loki MCP Server

Test PyPI version codecov License: MIT

A FastMCP server that allows querying Loki logs from Grafana.

MCP Server Settings

{
  "mcpServers": {
    "loki": {
      "command": "uvx",
      "args": [
        "grafana-loki-mcp",
        "-u",
        "GRAFANA_URL",
        "-k",
        "GRAFANA_API_KEY"
      ]
    }
  }
}
  • GRAFANA_URL: URL of your Grafana instance
  • GRAFANA_API_KEY: Grafana API key with appropriate permissions

Features

  • Query Loki logs through Grafana API
  • Get Loki labels and label values
  • Format query results in different formats (text, JSON, markdown)
  • Support for both stdio and SSE transport protocols

Requirements

  • Python 3.10+
  • FastMCP
  • Requests

Installation

Using pip

pip install grafana-loki-mcp

Development Setup

  1. Clone this repository
  2. Install dependencies using uv:
# Install uv
pip install uv

# Create and activate virtual environment
uv venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install dependencies
uv pip install -e ".[dev]"

Usage

Environment Variables

Set the following environment variables:

  • GRAFANA_URL: URL of your Grafana instance
  • GRAFANA_API_KEY: Grafana API key with appropriate permissions

Command Line Arguments

You can also provide these values as command line arguments:

grafana-loki-mcp -u https://your-grafana-instance.com -k your-api-key

Additional options:

  • --transport: Transport protocol to use (stdio or sse, default: stdio)

Running the Server

# Using environment variables
export GRAFANA_URL=https://your-grafana-instance.com
export GRAFANA_API_KEY=your-api-key
grafana-loki-mcp

# Using command line arguments
grafana-loki-mcp -u https://your-grafana-instance.com -k your-api-key

# Using SSE transport
grafana-loki-mcp --transport sse

Development

Testing

Run the test suite:

pytest

Run with coverage:

pytest --cov=. --cov-report=term

Linting and Formatting

# Run ruff linter
ruff check .

# Run black formatter
black .

# Run type checking
mypy .

Available Tools

query_loki

Query Loki logs through Grafana.

Parameters:

  • query: Loki query string
  • start: Start time (ISO format, Unix timestamp, or Grafana-style relative time like 'now-1h', default: 1 hour ago)
  • end: End time (ISO format, Unix timestamp, or Grafana-style relative time like 'now', default: now)
  • limit: Maximum number of log lines to return (default: 100)
  • direction: Query direction ('forward' or 'backward', default: 'backward')
  • max_per_line: Maximum characters per log line (0 for unlimited, default: 100)

get_loki_labels

Get all label names from Loki.

get_loki_label_values

Get values for a specific label from Loki.

Parameters:

  • label: Label name

format_loki_results

Format Loki query results in a more readable format.

Parameters:

  • results: Loki query results from query_loki
  • format_type: Output format ('text', 'json', or 'markdown', default: 'text')
  • max_per_line: Maximum characters per log line (0 for unlimited, default: 0)

Example Usage

# Example client code
from mcp.client import Client

async with Client() as client:
    # Query Loki logs with max_per_line limit
    results = await client.call_tool(
        "query_loki",
        {
            "query": '{app="my-app"} |= "error"',
            "limit": 50,
            "max_per_line": 100,  # Limit log lines to 100 characters
            "start": "now-6h",    # Grafana-style relative time: 6 hours ago
            "end": "now"          # Current time
        }
    )

    # Format the results
    formatted = await client.call_tool(
        "format_loki_results",
        {
            "results": results,
            "format_type": "markdown",
            "max_per_line": 100  # Can also limit at formatting time
        }
    )

    print(formatted)

License

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

grafana-loki-mcp FAQ

How do I configure the Grafana-Loki MCP server?
Set the GRAFANA_URL to your Grafana instance URL and provide a GRAFANA_API_KEY with appropriate permissions in the MCP server settings.
What permissions does the Grafana API key require?
The API key must have read access to Loki logs in your Grafana instance to allow querying via the MCP server.
Can this server handle multiple Grafana instances?
Yes, you can configure multiple MCP server instances with different URLs and API keys for each Grafana environment.
How does the server authenticate requests to Grafana?
It uses the provided Grafana API key for secure authentication when querying Loki logs.
Is the Grafana-Loki MCP server compatible with all Loki versions?
It supports Loki versions compatible with the Grafana API used; check compatibility in the server documentation.
Can I extend the server to support custom Loki queries?
Yes, the server can be extended or configured to handle custom Loki query parameters as needed.
How is the server deployed?
The server runs as a FastMCP server process, typically deployed alongside your MCP client or host environment.
Does the server support secure connections?
Yes, it supports HTTPS connections to Grafana instances for secure data transmission.