# Grafana MCP服务器
## 基本信息
- Slug: `grafana-mcp-grafana`
- Source: modelscope
- Publisher: @grafana/mcp-grafana
- Categories: monitoring / developer-tools
- Hosted: No
- License: Apache License 2.0
- Source URL: https://www.modelscope.cn/mcp/servers/@grafana/mcp-grafana
## 简介
一种用于 Grafana 的模型上下文协议（MCP）服务器。 这为您提供了对 Grafana 实例及其周边生态系统的访问。
## 安装提示

```bash
GOBIN="$HOME/go/bin" go install github.com/grafana/mcp-grafana/cmd/mcp-grafana@latest
```

## MCP Server 详情

# Grafana MCP 服务器

一个用于 Grafana 的 [Model Context Protocol](https://modelcontextprotocol.io/) (MCP) 服务器。

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

## 功能

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

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

### 工具

| Tool                              | Category    | Description                                                        |
|-----------------------------------|-------------|--------------------------------------------------------------------|
| `search_dashboards`               | Search      | Search for dashboards                                              |
| `get_dashboard_by_uid`            | Dashboard   | Get a dashboard by uid                                             |
| `list_datasources`                | Datasources | List datasources                                                   |
| `get_datasource_by_uid`           | Datasources | Get a datasource by uid                                            |
| `get_datasource_by_name`          | Datasources | Get a datasource by name                                           |
| `query_prometheus`                | Prometheus  | Execute a query against a Prometheus datasource                    |
| `list_prometheus_metric_metadata` | Prometheus  | List metric metadata                                               |
| `list_prometheus_metric_names`    | Prometheus  | List available metric names                                        |
| `list_prometheus_label_names`     | Prometheus  | List label names matching a selector                               |
| `list_prometheus_label_values`    | Prometheus  | List values for a specific label                                   |
| `list_incidents`                  | Incident    | List incidents in Grafana Incident                                 |
| `create_incident`                 | Incident    | Create an incident in Grafana Incident                             |
| `add_activity_to_incident`        | Incident    | Add an activity item to an incident in Grafana Incident            |
| `resolve_incident`                | Incident    | Resolve an incident in Grafana Incident                            |
| `query_loki_logs`                 | Loki        | Query and retrieve logs using LogQL (either log or metric queries) |
| `list_loki_label_names`           | Loki        | List all available label names in logs                             |
| `list_loki_label_values`          | Loki        | List values for a specific log label                               |
| `query_loki_stats`                | Loki        | Get statistics about log streams                                   |
| `list_alert_rules`                | Alerting    | List alert rules                                                   |
| `get_alert_rule_by_uid`           | Alerting    | Get alert rule by UID                                              |
| `list_oncall_schedules`           | OnCall      | List schedules from Grafana OnCall                                 |
| `get_oncall_shift`                | OnCall      | Get details for a specific OnCall shift                           |
| `get_current_oncall_users`        | OnCall      | Get users currently on-call for a specific schedule                |
| `list_oncall_teams`               | OnCall      | List teams from Grafana OnCall                                     |
| `list_oncall_users`               | OnCall      | List users from Grafana OnCall                                     |


## 使用方法

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

2. 从 [发布页面](https://github.com/grafana/mcp-grafana/releases) 下载最新版本的 `mcp-grafana` 并将其放置在你的 `$PATH` 中。

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

   ```bash
   GOBIN="$HOME/go/bin" go install github.com/grafana/mcp-grafana/cmd/mcp-grafana@latest
   ```

3. 将服务器配置添加到客户端配置文件中。例如，对于 Claude Desktop：

   ```json
   {
     "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 配置中使用调试模式，请按如下方式更新你的配置：

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


## 开发

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

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

要运行服务器，请使用：

```bash
make run
```


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

```
make build-image
```


要运行镜像，请使用：

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


### 测试

有三种类型的测试可用：

1. 单元测试（无需外部依赖）：
```bash
make test-unit
```


你也可以运行单元测试：
```bash
make test
```


2. 集成测试（需要 docker 容器正在运行）：
```bash
make test-integration
```


3. 云测试（需要云 Grafana 实例和凭据）：
```bash
make test-cloud
```
> 注意：云测试在 CI 中自动配置。对于本地开发，你需要设置自己的 Grafana Cloud 实例和凭据。

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

```bash
docker-compose up -d
```


集成测试可以这样运行：

`…

