Grafana-Loki MCP 服务器是一个基于 FastMCP 的服务器,它允许用户通过 Grafana 对 Loki 日志进行查询操作,为日志管理和分析提供了便利。
在使用 Grafana-Loki MCP 服务器之前,需要进行相应的配置。以下是 MCP 服务器的配置示例:
{
"mcpServers": {
"loki": {
"command": "uvx",
"args": [
"grafana-loki-mcp",
"-u",
"GRAFANA_URL",
"-k",
"GRAFANA_API_KEY"
]
}
}
}
GRAFANA_URL:您的 Grafana 实例 URL。GRAFANA_API_KEY:具备相应权限的 Grafana API 密钥。stdio 和 SSE 两种传输协议。pip install grafana-loki-mcp
git clone https://github.com/tumf/grafana-loki-mcp.git
cd grafana-loki-mcp
pip install -r requirements.txt
python -m pytest tests/
export GRAFANA_URL="http://localhost:3000"
export GRAFANA_API_KEY="your_api_key_here"
grafana-loki-mcp --config config.yaml
config.yamlmcpServers:
loki:
command: uvx
args:
- grafana-loki-mcp
- -u
- http://localhost:3000
- -k
- your_api_key_here
import asyncio
from pygql import client
async def main():
# 初始化客户端
cli = await client("http://localhost:8090")
# 查询日志
results = await cli.call(
"query_loki",
{
"query": '{app="my-app"} |= "error"',
"limit": 50,
"max_per_line": 100,
"start": "now-6h",
"end": "now"
}
)
# 格式化结果
formatted = await cli.call(
"format_loki_results",
{
"results": results,
"format_type": "markdown",
"max_per_line": 100
}
)
print(formatted)
asyncio.run(main())
query_loki){
"query": "{app=\"my-app\"} |= \"error\"",
"limit": 50,
"max_per_line": 100,
"start": "now-6h",
"end": "now"
}
get_loki_labels){
"query": "{__name__}"
}
format_loki_results){
"results": "...",
"format_type": "markdown",
"max_per_line": 100
}
运行测试套件:
pytest tests/ -v
使用以下工具确保代码规范:
查看 官方文档 了解详细信息。
本项目遵循 MIT 许可证,具体内容请参见 LICENSE 文件。