Skip to main content

A FastMCP server for querying Loki logs from Grafana

Project description

Grafana-Loki MCP Server

Test PyPI version codecov License: MIT

A FastMCP server that allows querying Loki logs from Grafana.

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, default: 1 hour ago)
  • end: End time (ISO format, default: now)
  • limit: Maximum number of log lines to return (default: 100)
  • direction: Query direction ('forward' or 'backward', default: 'backward')

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')

Example Usage

# Example client code
from mcp.client import Client

async with Client() as client:
    # Query Loki logs
    results = await client.call_tool(
        "query_loki",
        {
            "query": '{app="my-app"} |= "error"',
            "limit": 50
        }
    )

    # Format the results
    formatted = await client.call_tool(
        "format_loki_results",
        {
            "results": results,
            "format_type": "markdown"
        }
    )

    print(formatted)

License

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

grafana_loki_mcp-0.1.1.tar.gz (58.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

grafana_loki_mcp-0.1.1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file grafana_loki_mcp-0.1.1.tar.gz.

File metadata

  • Download URL: grafana_loki_mcp-0.1.1.tar.gz
  • Upload date:
  • Size: 58.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for grafana_loki_mcp-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a0dc88f9ff1d7d85aa2a671fbf517edd04280f2a045a7faa45bca4d28c90856c
MD5 7b44ebf72a0a2230704a33dc4baac8be
BLAKE2b-256 688a34d041aaec944038d9a7947709124894d07f210169e92b395673d61803eb

See more details on using hashes here.

File details

Details for the file grafana_loki_mcp-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for grafana_loki_mcp-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 11806dfea6b99bef18ee3794645867fe19b4370363091cdac9d81faabab0ef63
MD5 942a710d72a563199afb44e05379a55e
BLAKE2b-256 1c124dae814ecf54a98c37ee078b1a6d83787dfff0356a28da58d8139a065b16

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page