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
1# Grafana-Loki MCP Server23[](https://github.com/tumf/grafana-loki-mcp/actions/workflows/test.yml)4[](https://badge.fury.io/py/grafana-loki-mcp)5[](https://codecov.io/gh/tumf/grafana-loki-mcp)6[](https://opensource.org/licenses/MIT)78A [FastMCP](https://github.com/jlowin/fastmcp) server that allows querying Loki logs from Grafana.910## MCP Server Settings1112```json13{14 "mcpServers": {15 "loki": {16 "command": "uvx",17 "args": [18 "grafana-loki-mcp",19 "-u",20 "GRAFANA_URL",21 "-k",22 "GRAFANA_API_KEY"23 ]24 }25 }26}27```2829- `GRAFANA_URL`: URL of your Grafana instance30- `GRAFANA_API_KEY`: Grafana API key with appropriate permissions3132## Features3334- Query Loki logs through Grafana API35- Get Loki labels and label values36- Format query results in different formats (text, JSON, markdown)37- Support for both stdio and SSE transport protocols3839## Requirements4041- Python 3.10+42- FastMCP43- Requests4445## Installation4647### Using pip4849```bash50pip install grafana-loki-mcp51```5253### Development Setup54551. Clone this repository562. Install dependencies using uv:5758```bash59# Install uv60pip install uv6162# Create and activate virtual environment63uv venv64source .venv/bin/activate # On Windows: .venv\Scripts\activate6566# Install dependencies67uv pip install -e ".[dev]"68```6970## Usage7172### Environment Variables7374Set the following environment variables:7576- `GRAFANA_URL`: URL of your Grafana instance77- `GRAFANA_API_KEY`: Grafana API key with appropriate permissions7879### Command Line Arguments8081You can also provide these values as command line arguments:8283```bash84grafana-loki-mcp -u https://your-grafana-instance.com -k your-api-key85```8687Additional options:88- `--transport`: Transport protocol to use (`stdio` or `sse`, default: `stdio`)8990### Running the Server9192```bash93# Using environment variables94export GRAFANA_URL=https://your-grafana-instance.com95export GRAFANA_API_KEY=your-api-key96grafana-loki-mcp9798# Using command line arguments99grafana-loki-mcp -u https://your-grafana-instance.com -k your-api-key100101# Using SSE transport102grafana-loki-mcp --transport sse103```104105## Development106107### Testing108109Run the test suite:110111```bash112pytest113```114115Run with coverage:116117```bash118pytest --cov=. --cov-report=term119```120121### Linting and Formatting122123```bash124# Run ruff linter125ruff check .126127# Run black formatter128black .129130# Run type checking131mypy .132```133134## Available Tools135136### query_loki137138Query Loki logs through Grafana.139140Parameters:141- `query`: Loki query string142- `start`: Start time (ISO format, Unix timestamp, or Grafana-style relative time like 'now-1h', default: 1 hour ago)143- `end`: End time (ISO format, Unix timestamp, or Grafana-style relative time like 'now', default: now)144- `limit`: Maximum number of log lines to return (default: 100)145- `direction`: Query direction ('forward' or 'backward', default: 'backward')146- `max_per_line`: Maximum characters per log line (0 for unlimited, default: 100)147148### get_loki_labels149150Get all label names from Loki.151152### get_loki_label_values153154Get values for a specific label from Loki.155156Parameters:157- `label`: Label name158159### format_loki_results160161Format Loki query results in a more readable format.162163Parameters:164- `results`: Loki query results from query_loki165- `format_type`: Output format ('text', 'json', or 'markdown', default: 'text')166- `max_per_line`: Maximum characters per log line (0 for unlimited, default: 0)167168## Example Usage169170```python171# Example client code172from mcp.client import Client173174async with Client() as client:175 # Query Loki logs with max_per_line limit176 results = await client.call_tool(177 "query_loki",178 {179 "query": '{app="my-app"} |= "error"',180 "limit": 50,181 "max_per_line": 100, # Limit log lines to 100 characters182 "start": "now-6h", # Grafana-style relative time: 6 hours ago183 "end": "now" # Current time184 }185 )186187 # Format the results188 formatted = await client.call_tool(189 "format_loki_results",190 {191 "results": results,192 "format_type": "markdown",193 "max_per_line": 100 # Can also limit at formatting time194 }195 )196197 print(formatted)198```199200## License201202This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.203
Full transparency — inspect the skill content before installing.