Grafana MCP Server 是一個基於模型上下文協議 (Model Context Protocol, MCP) 的伺服器,專為與 Grafana 實例及其周邊生態系統集成而設計。該項目為 AI 代理(如 Claude Desktop)提供了訪問和操作 Grafana 各種功能的能力。
該項目提供了靈活的工具配置選項,您可以選擇啟用或禁用特定的功能類別:
工具名稱 | 分類 | 描述 |
---|---|---|
search_dashboards |
Search | 搜索儀表板 |
get_dashboard_by_uid |
Dashboard | 通過 UID 獲取儀表板 |
update_dashboard |
Dashboard | 更新或創建新儀表板 |
list_datasources |
Datasources | 列出數據源 |
query_prometheus |
Prometheus | 執行 Prometheus 查詢 |
query_loki_logs |
Loki | 查詢和檢索日誌 |
list_incidents |
Incident | 列出事件 |
list_alert_rules |
Alerting | 列出告警規則 |
list_oncall_schedules |
OnCall | 列出 OnCall 排班計劃 |
find_error_pattern_logs |
Sift | 查找錯誤模式 |
使用 --disable-<category>
標誌可以禁用特定的工具類別。例如,使用 --disable-oncall
可以禁用 OnCall 相關工具。
docker pull mcp/grafana
docker run -p 8000:8000 -e GRAFANA_URL=http://localhost:3000 -e GRAFANA_API_KEY=<your_token> mcp/grafana
從 releases 頁面 下載最新版本並放置在 $PATH
目錄中。
GOBIN="$HOME/go/bin" go install github.com/grafana/mcp-grafana/cmd/mcp-grafana@latest
使用二進制文件:
{
"mcpServers": {
"grafana": {
"command": "mcp-grafana",
"args": [],
"env": {
"GRAFANA_URL": "http://localhost:3000",
"GRAFANA_API_KEY": "<your service account token>"
}
}
}
}
使用 Docker:
{
"mcpServers": {
"grafana": {
"command": "docker",
"args": [
"run", "--rm", "-p", "8000:8000",
"-e", "GRAFANA_URL", "-e", "GRAFANA_API_KEY",
"mcp/grafana"
],
"env": {
"GRAFANA_URL": "http://localhost:3000",
"GRAFANA_API_KEY": "<your service account token>"
}
}
}
}
通過添加 -debug
標誌啟用調試模式,可以提供詳細的 HTTP 請求和響應日誌:
{
"mcpServers": {
"grafana": {
"command": "mcp-grafana",
"args": ["-debug"],
"env": {
"GRAFANA_URL": "http://localhost:3000",
"GRAFANA_API_KEY": "<your service account token>"
}
}
}
}
# 運行伺服器
make run
# 構建 Docker 鏡像
make build-image
# 運行 Docker 容器
docker run -it --rm -p 8000:8000 mcp-grafana:latest
make test-unit
# 或者
make test
# 需要啟動 Docker 容器
make test-integration
# 需要雲 Grafana 實例和憑證
make test-cloud
# 啟動本地 Grafana 實例
docker-compose up -d
# 運行所有測試
make test-all
make lint
該項目包含自定義的 linter,用於檢查 jsonschema
結構標籤中未轉義的逗號:
make lint-jsonschema
這個項目代表了 AI 與傳統監控工具集成的新趨勢,為開發者提供了一個強大的橋樑,連接人工智能助手和 Grafana 生態系統。