A FastMCP server for querying Loki logs from Grafana
Project description
Grafana-Loki MCP Server
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 instanceGRAFANA_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
- Clone this repository
- 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 instanceGRAFANA_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 (stdioorsse, 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 stringstart: 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_lokiformat_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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file grafana_loki_mcp-0.1.4.tar.gz.
File metadata
- Download URL: grafana_loki_mcp-0.1.4.tar.gz
- Upload date:
- Size: 73.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3127ed1896eccf280ef278ed4571740855c7790befefc3ac59951f041423ca6
|
|
| MD5 |
da0d2b32b83817ac6af9c8ef84fc4bea
|
|
| BLAKE2b-256 |
7da1fa279cfc30ec4c386680611cc37466a0cd180e20c2ba32caa9db2e1e8e0d
|
File details
Details for the file grafana_loki_mcp-0.1.4-py3-none-any.whl.
File metadata
- Download URL: grafana_loki_mcp-0.1.4-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
556ac0cab65fb066c53e4a7bf76b74ce95aeb8c06f2a52ba597ecff065823b8b
|
|
| MD5 |
addeaa466c6a01d9ad08bc37c3d1a165
|
|
| BLAKE2b-256 |
3dfc2f97d20a60915e394410676f568c6e106ff43d77ca521f9014dc1981b627
|