G

Grafana MCP服务器

一种用于 Grafana 的模型上下文协议(MCP)服务器。 这为您提供了对 Grafana 实例及其周边生态系统的访问。

monitoringdeveloper-tools

1.1k 查看 · 2026-07-07 更新

简介

一种用于 Grafana 的模型上下文协议(MCP)服务器。

这为您提供了对 Grafana 实例及其周边生态系统的访问。

简介

一种用于 Grafana 的模型上下文协议(MCP)服务器。

这为您提供了对 Grafana 实例及其周边生态系统的访问。

Grafana MCP 服务器

一个用于 Grafana 的 Model Context Protocol (MCP) 服务器。

它提供了对您的 Grafana 实例及其周围生态系统的访问。

功能

  • 搜索仪表盘
  • 通过 UID 获取仪表盘
  • 列出并获取数据源信息
  • 查询数据源
    • Prometheus
    • Loki
      • 日志查询
      • 指标查询
    • Tempo
    • Pyroscope
  • 查询 Prometheus 元数据
    • 指标元数据
    • 指标名称
    • 标签名称
    • 标签值
  • 查询 Loki 元数据
    • 标签名称
    • 标签值
    • 统计信息
  • 搜索、创建、更新和关闭事件
  • 启动 Sift 调查并查看结果
  • 告警
    • 列出并获取告警规则信息
    • 获取告警规则状态(触发/正常/错误等)
    • 创建和修改告警规则
    • 列出联系点
    • 创建和修改联系点
  • 访问 Grafana OnCall 功能
    • 列出和管理排班
    • 获取轮班详情
    • 获取当前待命用户
    • 列出团队和用户
    • 列出告警组

工具列表是可配置的,因此您可以选择要向 MCP 客户端提供的工具。 如果您不使用某些功能,或者不想占用太多上下文窗口空间,这将非常有用。 要禁用某一类工具,在启动服务器时使用 --disable-<category> 标志。例如,要禁用 OnCall 工具,请使用 --disable-oncall

工具

ToolCategoryDescription
search_dashboardsSearchSearch for dashboards
get_dashboard_by_uidDashboardGet a dashboard by uid
list_datasourcesDatasourcesList datasources
get_datasource_by_uidDatasourcesGet a datasource by uid
get_datasource_by_nameDatasourcesGet a datasource by name
query_prometheusPrometheusExecute a query against a Prometheus datasource
list_prometheus_metric_metadataPrometheusList metric metadata
list_prometheus_metric_namesPrometheusList available metric names
list_prometheus_label_namesPrometheusList label names matching a selector
list_prometheus_label_valuesPrometheusList values for a specific label
list_incidentsIncidentList incidents in Grafana Incident
create_incidentIncidentCreate an incident in Grafana Incident
add_activity_to_incidentIncidentAdd an activity item to an incident in Grafana Incident
resolve_incidentIncidentResolve an incident in Grafana Incident
query_loki_logsLokiQuery and retrieve logs using LogQL (either log or metric queries)
list_loki_label_namesLokiList all available label names in logs
list_loki_label_valuesLokiList values for a specific log label
query_loki_statsLokiGet statistics about log streams
list_alert_rulesAlertingList alert rules
get_alert_rule_by_uidAlertingGet alert rule by UID
list_oncall_schedulesOnCallList schedules from Grafana OnCall
get_oncall_shiftOnCallGet details for a specific OnCall shift
get_current_oncall_usersOnCallGet users currently on-call for a specific schedule
list_oncall_teamsOnCallList teams from Grafana OnCall
list_oncall_usersOnCallList users from Grafana OnCall

使用方法

  1. 在 Grafana 中创建一个具有足够权限的服务账号,以便使用你想要使用的工具,生成服务账号令牌,并将其复制到剪贴板以供配置文件使用。详细步骤请参阅 [Grafana 文档][service-account]。

  2. 发布页面 下载最新版本的 mcp-grafana 并将其放置在你的 $PATH 中。

    如果你已安装了 Go 工具链,也可以通过源码构建并安装它,使用 GOBIN 环境变量指定二进制文件应安装的目录。该目录也应包含在你的 PATH 中。

    GOBIN="$HOME/go/bin" go install github.com/grafana/mcp-grafana/cmd/mcp-grafana@latest
  3. 将服务器配置添加到客户端配置文件中。例如,对于 Claude Desktop:

    { "mcpServers": { "grafana": { "command": "mcp-grafana", "args": [], "env": { "GRAFANA_URL": "http://localhost:3000", "GRAFANA_API_KEY": "<your service account token>" } } } }

注意:如果在 Claude Desktop 中看到 Error: spawn mcp-grafana ENOENT 错误,你需要指定 mcp-grafana 的完整路径。

调试模式

你可以通过向命令中添加 -debug 标志来启用 Grafana 传输的调试模式。这将提供 MCP 服务器与 Grafana API 之间 HTTP 请求和响应的详细日志记录,有助于故障排除。

要在 Claude Desktop 配置中使用调试模式,请按如下方式更新你的配置:

{ "mcpServers": { "grafana": { "command": "mcp-grafana", "args": ["-debug"], "env": { "GRAFANA_URL": "http://localhost:3000", "GRAFANA_API_KEY": "<your service account token>" } } } }

开发

欢迎贡献!如果你有任何建议或改进,请打开一个问题或提交一个拉取请求。

此项目是用 Go 编写的。请按照适用于你平台的说明安装 Go。

要运行服务器,请使用:

make run

你也可以使用 Docker 内的 SSE 传输运行服务器。要构建镜像,请使用

make build-image

要运行镜像,请使用:

docker run -it --rm -p 8000:8000 mcp-grafana:latest

测试

有三种类型的测试可用:

  1. 单元测试(无需外部依赖):
make test-unit

你也可以运行单元测试:

make test
  1. 集成测试(需要 docker 容器正在运行):
make test-integration
  1. 云测试(需要云 Grafana 实例和凭据):
make test-cloud

注意:云测试在 CI 中自动配置。对于本地开发,你需要设置自己的 Grafana Cloud 实例和凭据。

更全面的集成测试需要在本地端口 3000 上运行一个 Grafana 实例;你可以使用 Docker Compose 启动一个:

docker-compose up -d

集成测试可以这样运行:

make test-all

如果你添加了更多工具,请为它们添加集成测试。现有的测试应该是一个很好的起点。

代码检查

要进行代码检查,请运行:

make lint

这包括一个自定义的 linter,用于检查 jsonschema 结构标签中未转义的逗号。description 字段中的逗号必须用 \\, 转义,以防止无声截断。你可以单独运行这个 linter:

make lint-jsonschema

有关更多详细信息,请参阅 JSONSchema Linter 文档

许可证

本项目采用 Apache License, Version 2.0 许可。

服务配置

[{'mcpServers': {'grafana': {'args': [], 'command': 'mcp-grafana', 'env': {'GRAFANA_API_KEY': '<your service account token>', 'GRAFANA_URL': 'http://localhost:3000'}}}}, {'mcpServers': {'grafana': {'args': ['-debug'], 'command': 'mcp-grafana', 'env': {'GRAFANA_API_KEY': '<your service account token>', 'GRAFANA_URL': 'http://localhost:3000'}}}}]

来源