Prometheus MCP服务器

一个允许LLMs通过API与运行中的Prometheus实例交互的MCP服务器,用于生成和执行PromQL查询、列出和分析指标等。

作者 By tjhop
本地部署 Prometheus MCP协议
GitHub

普罗米修斯MCP服务器

关于

这是一个 主控程序 服务器,允许LLM与正在运行的 普罗米修斯 通过API实例来执行诸如生成和执行promql查询、列出和分析度量等操作。

演示和示例

让克劳德调查慢速查询

使用的提示是:

查询我的指标很慢,你能帮我找出原因吗?

调查MCP服务器本身产生的指标,并建议SLO的记录规则

使用的提示是:

使用prometheus mcp服务器中的工具调查以下指标 mcp服务器,并建议SLO的prometheus录制规则

总结Prometheus度量/标签命名最佳实践

使用的提示是:

总结普罗米修斯指标/标签名称最佳实践

关于为普罗米修斯提供动力的普罗米修斯实例健康状况的报告.demo.promotheus.io

使用的提示是:

请提供prometheus服务器的全面回顾和总结。 查看它的配置、标志、运行时/构建信息以及其他任何信息 你觉得可以提供对普罗米修斯实例状态的洞察, 包括分析指标和执行查询

工具

Prometheus HTTP API输出JSON数据,该MCP服务器中的工具将该JSON返回给LLM,以便按照LLM的结构和理解方式进行处理。

LLM和令牌/上下文效率

此MCP服务器支持以下选项,这些选项有可能减少令牌/上下文的使用:

TOON编码

如果关注令牌/上下文使用,则此MCP服务器还支持将API的JSON数据转换为 面向令牌的对象表示法(TOON)格式. 虽然不能保证减少代币使用,但它的设计考虑了代币效率。 正如TOON的文档所述,它擅长于对象的均匀数组;在JSON中,非均匀/复杂对象可能仍然更具令牌效率。 真实世界的代币使用将取决于使用模式,请查看常见的工作流程,以确定TOON输出是否有益。 请参阅 旗帜 有关可用标志及其相应环境变量的更多信息。

API响应截断

此功能允许您设置从的Prometheus API返回的行数或条目的最大限制,这有助于减少发送到LLM的数据量。 将限制设置为 0 禁用截断。 默认情况下禁用截断。 请注意,对于支持的工具,能够处理工具请求参数的LLM可以在每次工具调用的基础上覆盖此全局截断限制。 请参阅 旗帜 有关可用标志及其相应环境变量的更多信息。

完整工具列表

工具名称描述
alertmanagers获取Prometheus警报管理器发现概述
build_info获取Prometheus构建信息
config获取Prometheus配置
docs_list普罗米修斯官方文档文件列表
docs_read从Prometheus/docs仓库中读取包含Prometheus官方文档的命名markdown文件
docs_search从Prometheus/docs仓库中搜索包含Prometheus官方文档的markdown文件
exemplar_query根据给定的查询和时间范围对示例执行查询
flags获取运行时标志
healthy管理可用于检查Prometheus健康状况的API端点
label_names按给定的时间范围和匹配项按排序顺序返回块中存在的唯一标签名称
label_values对给定标签、时间范围和匹配项的值执行查询
list_alerts列出所有活动警报
list_rules列出已加载的所有警报和记录规则
list_targets获取普罗米修斯目标发现概述
metric_metadata返回有关当前由度量名称抓取的度量的元数据
query对Prometheus数据源执行即时查询
quit管理API端点,可用于触发Prometheus的优雅关闭
range_query对Prometheus数据源执行范围查询
ready管理API端点,可用于检查Prometheus是否准备好提供流量(即响应查询
reload管理API端点,可用于触发重新加载Prometheus配置和规则文件
runtime_info获取Prometheus运行时信息
series按标签匹配器查找系列
targets_metadata返回目标当前抓取的指标的元数据
tsdb_stats从TSDB获取使用率和基数统计信息
wal_replay_status获取当前WAL重播状态

注:

因为 TSDB管理员API端点 考虑到删除数据等潜在的破坏性操作,它们不是 默认情况下启用。为了启用TSDB管理API端点,MCP 必须使用该标志启动服务器 --dangerous.enable-tsdb-admin-tools 到 承认这些终点带来的相关风险。

工具名称描述
clean_tombstones从磁盘中删除已删除的数据并清理现有的墓碑
delete_series删除时间范围内选定系列的数据
snapshot将所有当前数据的快照创建为快照/- 在TSDB的数据目录下,并返回该目录作为响应

工具集

服务器提供了许多与Prometheus交互的工具。有一些工具可以通过API与Prometheus交互,还有一些附加的工具可以进行读取文档等操作。 默认情况下,它们都已注册并可供使用(TSDBAdminneneneba API工具需要一个额外的标志)。

为了考虑具有较小上下文窗口的LLM,可以传入一个特定工具的白名单,以便在服务器上注册。 始终加载以下“核心”工具: [docs_list, docs_read, docs_search, query, range_query, metric_metadata, label_names, label_values, series]. 可以使用指定其他工具 --mcp.tools 旗帜.

例如,命令行:

SHELL``` 1prometheus-mcp-server —mcp.tools=build_info —mcp.tools=flags —mcp.tools=runtime_info


将导致加载以下工具:


- `build_info`
- `docs_list`
- `docs_read`
- `docs_search`
- `flags`
- `label_names`
- `label_values`
- `metric_metadata`
- `query`
- `range_query`
- `runtime_info`
- `series`


#### Prometheus兼容后端


有许多兼容Prometheus的后端可用于以各种方式扩展Prometheus,通常是为了提供长期存储或从多个Prometheus实例进行查询聚合。
一些例子可以在 [远程存储](https://prometheus.io/docs/operating/integrations/#remote-endpoints-and-storage) 普罗米修斯的文件。


其中许多服务还提供了一个“与普罗米修斯兼容”的API,可用于使用本机promQL查询数据/与数据交互。
一般来说,此MCP服务器应至少为其他兼容prometheus API的服务工作,以执行查询并与系列/标签/元数据端点交互,从而进行度量和标签发现。
除此之外,API可能存在差异,因为不同的系统根据其需求实现了API的不同部分/扩展。


示例:


- Thanos不使用集中式配置,因此配置端点未实现,因此配置工具失败。
- Mimir和Cortex实现了额外的端点来管理/添加/删除规则


为了解决这个问题,并在一些常用的Prometheus兼容系统上提供更好的体验,该项目可能会添加对选定系统的直接支持,以提供不同/更多的工具。
选择特定的prometheus后端实现可以通过以下方式完成 `--prometheus.backend` 旗帜.
给定版本的MCP服务器上可用后端实现的列表可以在以下输出中找到 `--help` 旗帜.
资格和支持标准仍在考虑中,请打开问题以请求特定后端的支持/功能以供进一步讨论。


##### Prometheus后端实现差异


| 后端 | 工具 | 添加/删除/更改 | 注释 |
| --- | --- | --- | --- |
| `prometheus` | n/a | 无 | 标准普罗米修斯工具。功能上等同于 `--mcp.tools="all"`默认的MCP服务器工具集。 |
| [`thanos`](https://thanos.io/) | `alertmanagers` | remove | Thanos不实现端点,工具返回 `404`. |
| [`thanos`](https://thanos.io/) | `config` | remove | Thanos不使用集中式配置,因此它不实现端点,工具返回 `404`. |
| [`thanos`](https://thanos.io/) | `wal_replay_status` | remove | Thanos不实现端点,工具返回 `404`. |
| [`thanos`](https://thanos.io/) | `list_stores` | add | Thanos提供了一个额外的端点来列出存储API服务器。 |
| [`thanos`](https://thanos.io/) | `reload` | remove | Thanos不实现端点,工具返回 `404`. |
| [`thanos`](https://thanos.io/) | `quit` | remove | Thanos不实现端点,工具返回 `404`. |


### 资源


| 资源名称 | 资源URI | 描述 |
| --- | --- | --- |
| 普罗米修斯官方文档文件列表 | `prometheus://docs` | Prometheus官方文档列表 |
| 阅读普罗米修斯官方文档 | `prometheus://docs/{+file}` | 按名称阅读Prometheus官方文档文件 |


## 安装与使用


当与工具完全集成和/或作为工具服务器安装在另一个系统中时,此MCP服务器最有用。
安装程序和集成支持将根据所使用的工具而有所不同。
例如:


- 有些系统只能与MCP工具交互,而不能与资源/提示交互
- 有些系统使用mcp.json配置文件格式来管理mcp服务器,有些系统需要自定义格式
- 有些系统不直接使用MCP,需要像MCP到openapi这样的工具来代理


请查看正在使用/集成的工具的文档,了解具体说明和支持级别。


### 二进制


从下载适合您系统的版本 [发布](https://github.com/tjhop/prometheus-mcp-server/releases) 页面。
请参阅 旗帜 有关可用标志及其相应环境变量的更多信息。


SHELL```
1
2
3/path/to/prometheus-mcp-server <flags>
# or using env vars
PROMETHEUS_MCP_SERVER_PROMETHEUS_URL="https://$yourPrometheus:9090" /path/to/prometheus-mcp-server

码头工人

请参阅 旗帜 有关可用标志及其相应环境变量的更多信息。

SHELL``` 1 2 3 4# Stdio transport docker run —rm -i ghcr.io/tjhop/prometheus-mcp-server:latest —prometheus.url “https://$yourPrometheus:9090”

or using env vars

docker run —rm -i -e PROMETHEUS_MCP_SERVER_PROMETHEUS_URL=“https://$yourPrometheus:9090” ghcr.io/tjhop/prometheus-mcp-server:latest


SHELL```
1
2
3
4# Streamable HTTP transport (capable of SSE as well)
docker run --rm -p 8080:8080 ghcr.io/tjhop/prometheus-mcp-server:latest --prometheus.url "https://$yourPrometheus:9090" --mcp.transport "http" --web.listen-address ":8080"
# or using env vars
docker run --rm -p 8080:8080 -e PROMETHEUS_MCP_SERVER_PROMETHEUS_URL="https://$yourPrometheus:9090" -e PROMETHEUS_MCP_SERVER_MCP_TRANSPORT="http" -e PROMETHEUS_MCP_SERVER_WEB_LISTEN_ADDRESS=":8080" ghcr.io/tjhop/prometheus-mcp-server:latest

Helm Chart(Kubernetes)

Helm chart可用于部署到Kubernetes。该图表在每个版本上都作为OCI工件发布。

BASH``` 1 2 3helm install prometheus-mcp-server oci://ghcr.io/tjhop/charts/prometheus-mcp-server
—version
—set prometheus.url=http://prometheus:9090


看 图表自述 查看配置值和示例的完整列表。


### Kubernetes(原始清单)


如果你喜欢在没有Helm的情况下部署,可以使用Kubernetes清单示例:


- Kubernetes部署
- OpenShift部署


### 系统包


从下载适合您系统的版本 [发布](https://github.com/tjhop/prometheus-mcp-server/releases) 页面。Systemd服务文件包含在构建的系统包中。


SHELL```
1
2
3
4
5# install system package (example assuming Debian based)
apt install /path/to/package
# create unit override, add any needed flags or environment variables
systemctl edit prometheus-mcp-server.service
systemctl enable --now prometheus-mcp-server.service

备注:虽然包是为几个系统构建的,但目前没有计划尝试将包提交到上游包存储库。

安全与认证

连接到安全的Prometheus实例

MCP服务器支持 Prometheus HTTP配置 用于连接到受保护的Prometheus实例的文件。 可以找到一个示例配置 在此处的示例文件夹中. 使用 --http.config 命令行标志,用于提供HTTP配置文件。 请参阅 旗帜 了解更多信息。

保护MCP服务器端点

MCP服务器支持 Prometheus Web配置文件 文件,以暴露可选基本身份验证和自定义TLS配置背后的端点。 使用 --web.config.file 命令行标志,用于提供HTTP配置文件。 请参阅 旗帜 了解更多信息。

遥测

指标

运行后,服务器在配置的侦听地址和遥测路径上公开Prometheus指标(:8080/metrics,默认情况下)。 请参阅 旗帜 有关如何更改监听接口、端口或遥测路径的更多信息。

Prometheus MCP Server Metrics

度量名称类型描述标签
prom_mcp_build_infoGauge一个具有常量“1”值的指标,带有用于构建prometheus mcp服务器的版本、提交和构建日期的标签。version, commit, build_date, goversion
prom_mcp_server_readyGauge如果MCP服务器已就绪,则信息度量为静态“1”,否则为“0”。
prom_mcp_api_calls_failed_totalCounter每个终结点的Prometheus API失败总数。target_path
prom_mcp_api_call_duration_secondsHistogramPrometheus API调用的持续时间,每个端点,以秒为单位。target_path
prom_mcp_tool_calls_failed_totalCounter每个工具的故障总数。tool_name
prom_mcp_tool_call_duration_secondsHistogram每个工具的工具调用持续时间(秒)。tool_name
prom_mcp_resource_calls_failed_totalCounter每个资源的故障总数。resource_uri
prom_mcp_resource_call_duration_secondsHistogram每个资源的资源调用持续时间(秒)。resource_uri
prom_mcp_docs_last_update_timestamp_secondsGauge上次成功文档自动更新的Unix时间戳。
prom_mcp_docs_update_failures_totalCounter文档自动更新失败的总数。
go_*Gauge/Counter标准Go运行时指标来自 client_golang 图书馆。
process_*Gauge/Counter来自 client_golang 图书馆。

Grafana仪表板

A预制 格拉法纳 仪表板包含在 grafana/ 用于可视化MCP服务器公开的指标的目录。将仪表板json导入grafana,它应该已经准备好了。

日志

该项目大量使用结构化、水平化的测井。 请参阅 旗帜 有关如何设置日志格式、级别和可选文件的更多信息。

发展

Devbox+Direnv开发环境

如果你使用 DevboxDirenv,然后简单地输入repo的目录 应该设置所需的软件。

Ollama本地法学硕士

看 mcp.json 用于工具的MCP配置示例。 需要 ollama 待安装。

注:

覆盖默认LLM(ollama:gpt-oss:20b),跑 export OLLAMA_MODEL="ollama:your_model" 在运行之前覆盖它 make .

Using MCP Inspector and a local ollama instance: 需要 MCP注射器 待安装:

BASH``` 1make inspector




Using mcphost and a local ollama instance:
需要 [`mcphost`](https://github.com/mark3labs/mcphost) 待安装:


BASH```
1make mcphost

双子座与双子座cli

看 settings.json 以获取在prometheus mcp服务器上运行gemini-cli的示例配置文件。 需要 双子座气候 待安装。

Using gemini-cli and hosted models: BASH``` 1make gemini



### 手动设置


所需软件:


- Go工作环境
- Docker用于本地测试/linting/镜像构建等
- GNU制造
- [奥拉玛](https://github.com/ollama/ollama)
- [mcp检查员](https://github.com/modelcontextprotocol/inspector)
- [mcphost](https://github.com/mark3labs/mcphost)


### 建筑


包含的Makefile有几个目标来帮助开发:


BASH```
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20~/go/src/github.com/tjhop/prometheus-mcp-server (main [ ]) -> make
Usage:
  make <target>
Targets:
  help                           print this help message
  submodules                     ensure git submodules are initialized and updated
  tidy                           tidy modules
  fmt                            apply go code style formatter
  lint                           run linters
  binary                         build a binary
  build                          alias for `binary`
  build-all                      test release process with goreleaser, does not publish/upload
  container                      build container images with goreleaser, alias for `build-all`
  image                          build container images with goreleaser, alias for `build-all`
  test                           run tests
  mcphost                        use mcphost to run the prometheus-mcp-server against a local ollama model
  inspector                      use inspector to run the prometheus-mcp-server in STDIO transport mode
  inspector-http                 use inspector to run the prometheus-mcp-server in streamable HTTP transport mode
  open-webui                     use open-webui to run the prometheus-mcp-server
  gemini                         use gemini-cli to run the prometheus-mcp-server against Google Gemini models

命令行标志

可用的命令行标志记录在帮助标志中:

BASH``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97~/go/src/github.com/tjhop/prometheus-mcp-server (main [ ]) -> ./prometheus-mcp-server —help usage: prometheus-mcp-server [] Flags: -h, —[no-]help Show context-sensitive help (also try —help-long and —help-man). ($PROMETHEUS_MCP_SERVER_HELP) —mcp.tools=all … List of mcp tools to load. The target all can be used to load all tools. The target core loads only the core tools: docs_list,docs_read,docs_search,query,range_query,metric_metadata,label_names,label_values,series Otherwise, it is treated as an allow-list of tools to load, in addition to the core tools. Please see project README for more information and the full list of tools. ($PROMETHEUS_MCP_SERVER_MCP_TOOLS) —[no-]mcp.enable-toon-output
Enable Token-Oriented Object Notation (TOON) output for tools instead of JSON ($PROMETHEUS_MCP_SERVER_MCP_ENABLE_TOON_OUTPUT) —[no-]mcp.enable-client-logging
Enable sending log messages to connected MCP clients as protocol notifications. When enabled, tool execution logs are sent both to the server’s primary log output and to the MCP client, allowing LLMs to observe server activity. ($PROMETHEUS_MCP_SERVER_MCP_ENABLE_CLIENT_LOGGING) —mcp.transport=“stdio” The type of transport to use for the MCP server [stdio, http]. ($PROMETHEUS_MCP_SERVER_MCP_TRANSPORT) —prometheus.backend=PROMETHEUS.BACKEND
Customize the toolset for a specific Prometheus API compatible backend. Supported backends include: prometheus,thanos ($PROMETHEUS_MCP_SERVER_PROMETHEUS_BACKEND) —prometheus.url=“http://127.0.0.1:9090
URL of the Prometheus instance to connect to ($PROMETHEUS_MCP_SERVER_PROMETHEUS_URL) —prometheus.timeout=1m Timeout for API calls to the Prometheus backend ($PROMETHEUS_MCP_SERVER_PROMETHEUS_TIMEOUT) —prometheus.truncation-limit=0
If enabled, this controls the maximum query response size in number of lines/entries provided to the LLM from the API response. LLMs can override truncation limits if needed on a per-tool-call basis via tool request arguments on supported tools. To disable truncation limits, set to 0. ($PROMETHEUS_MCP_SERVER_PROMETHEUS_TRUNCATION_LIMIT) —http.config=HTTP.CONFIG Path to config file to set Prometheus HTTP client options ($PROMETHEUS_MCP_SERVER_HTTP_CONFIG) —web.telemetry-path=“/metrics”
Path under which to expose metrics. ($PROMETHEUS_MCP_SERVER_WEB_TELEMETRY_PATH) —web.max-requests=40 Maximum number of parallel scrape requests. Use 0 to disable. ($PROMETHEUS_MCP_SERVER_WEB_MAX_REQUESTS) —[no-]dangerous.enable-tsdb-admin-tools
Enable and allow using tools that access Prometheus’ TSDB Admin API endpoints (snapshot, delete_series, and clean_tombstones tools). This is dangerous, and allows for destructive operations like deleting data. It is not the fault of this MCP server if the LLM you’re connected to nukes all your data. Docs: https://prometheus.io/docs/prometheus/latest/querying/api/#tsdb-admin-apis ($PROMETHEUS_MCP_SERVER_DANGEROUS_ENABLE_TSDB_ADMIN_TOOLS) —[no-]docs.auto-update Enable automatic documentation updates from the official prometheus/docs repository. Checks every 24h0m0s. ($PROMETHEUS_MCP_SERVER_DOCS_AUTO_UPDATE) —log.file=LOG.FILE The name of the file to log to (file rotation policies should be configured with external tools like logrotate) ($PROMETHEUS_MCP_SERVER_LOG_FILE) —[no-]web.systemd-socket Use systemd socket activation listeners instead of port listeners (Linux only). ($PROMETHEUS_MCP_SERVER_WEB_SYSTEMD_SOCKET) —web.listen-address=:8080 …
Addresses on which to expose metrics and web interface. Repeatable for multiple addresses. Examples: :9100 or [::1]:9100 for http, vsock://:9100 for vsock ($PROMETHEUS_MCP_SERVER_WEB_LISTEN_ADDRESS) —web.config.file="" Path to configuration file that can enable TLS or authentication. See: https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md ($PROMETHEUS_MCP_SERVER_WEB_CONFIG_FILE) —log.level=info Only log messages with the given severity or above. One of: [debug, info, warn, error] ($PROMETHEUS_MCP_SERVER_LOG_LEVEL) —log.format=logfmt Output format of log messages. One of: [logfmt, json] ($PROMETHEUS_MCP_SERVER_LOG_FORMAT) —[no-]version Show application version. ($PROMETHEUS_MCP_SERVER_VERSION)