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 "Install 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
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
Development Setup
Clone this repository
Install dependencies using uv:
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:
Additional options:
--transport: Transport protocol to use (stdioorsse, default:stdio)
Running the Server
Development
Testing
Run the test suite:
Run with coverage:
Linting and Formatting
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
License
This project is licensed under the MIT License - see the LICENSE file for details.