Grafana-Loki MCP Server
Allows querying Loki logs through the Grafana API, providing tools to execute Loki queries, retrieve label names and values, and format log output.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@Grafana-Loki MCP ServerShow me error logs for {app='api-gateway'} from the last 30 minutes"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
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
Related MCP server: Datadog Logs MCP Server
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-mcpDevelopment 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-keyAdditional 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 sseDevelopment
Testing
Run the test suite:
pytestRun with coverage:
pytest --cov=. --cov-report=termLinting 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.
Maintenance
Related MCP Connectors
Query application logs, traces, and metrics from your AI coding assistant via Foam's MCP server.
Ingest and search LogsLoom logs from coding agents.
- SuperlogOAuthsh.superlog
Open-source agent that observes and fixes your application. Query logs, traces, metrics, incidents.
Search your AI chat history (ChatGPT, Claude, Codex) from any MCP client. Remote, private, read-only
Related MCP Servers
- FlicenseNot gradedqualityDmaintenanceEnables querying log data stored in SQLite databases through the Model Context Protocol, allowing natural language interactions with log analysis.-
- FlicenseBqualityDmaintenanceEnables searching and retrieving Datadog logs through the Model Context Protocol with customizable queries, time ranges, and result limits.3-
- FlicenseNot gradedqualityDmaintenanceEnables AI models to query and analyze Kubernetes cluster logs through Grafana Loki, supporting semantic operations like error aggregation and pod restart detection. It provides tools for regex-based log searching and namespace discovery to facilitate natural language troubleshooting.-
- AlicenseNot gradedqualityDmaintenanceQuery CrowdStrike Falcon LogScale logs from AI assistants via the Model Context Protocol.2MIT