A FastMCP server that allows querying Loki logs from Grafana. - GRAFANAURL: URL of your Grafana instance - GRAFANAAPIKEY: Grafana API key with appropriate permissions - 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 - Python 3.10+ 1. Clone this repository
Add this skill
npx mdskills install tumf/grafana-loki-mcpWell-documented MCP server for querying Grafana Loki logs with clear setup and comprehensive tool descriptions
A FastMCP server that allows querying Loki logs from Grafana.
{
"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 permissionspip install grafana-loki-mcp
# 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]"
Set the following environment variables:
GRAFANA_URL: URL of your Grafana instanceGRAFANA_API_KEY: Grafana API key with appropriate permissionsYou 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)# 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
Run the test suite:
pytest
Run with coverage:
pytest --cov=. --cov-report=term
# Run ruff linter
ruff check .
# Run black formatter
black .
# Run type checking
mypy .
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 all label names from Loki.
Get values for a specific label from Loki.
Parameters:
label: Label nameFormat 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 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)
This project is licensed under the MIT License - see the LICENSE file for details.
Install via CLI
npx mdskills install tumf/grafana-loki-mcpGrafana-Loki MCP Server is a free, open-source AI agent skill. A FastMCP server that allows querying Loki logs from Grafana. - GRAFANAURL: URL of your Grafana instance - GRAFANAAPIKEY: Grafana API key with appropriate permissions - 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 - Python 3.10+ 1. Clone this repository
Install Grafana-Loki MCP Server with a single command:
npx mdskills install tumf/grafana-loki-mcpThis downloads the skill files into your project and your AI agent picks them up automatically.
Grafana-Loki MCP Server works with Claude Code, Claude Desktop, Cursor, Vscode Copilot, Windsurf, Continue Dev, Gemini Cli, Amp, Roo Code, Goose. Skills use the open SKILL.md format which is compatible with any AI coding agent that reads markdown instructions.