
pgmcp
io.github.pascalallen由 GitHub 帳號 pascalallen 發布v1.1.0更新於 Oct 24, 2026
Read-only Postgres ops/DBA diagnostics: slow queries, EXPLAIN, locks, index and table health.
概覽
唯讀的 PostgreSQL 維運/DBA MCP 伺服器,讓助理診斷慢查詢、執行計畫、鎖定、索引與資料表健康、連線與複寫狀態。
- 可以做什麼
- 依總時間、呼叫次數等排序慢語句,並解釋某條語句為何慢,提供計畫樹與 plan_hash
- 檢查索引與資料表健康:從未被掃描、重複、無效或膨脹的索引,死元組比例、vacuum 與 analyze 紀錄
- 診斷即時問題:鎖定等待圖與死結、依狀態與等待事件分組的後端、備援延遲與佔用 WAL 的複寫槽
- 對照記憶體、autovacuum、WAL 與連線數經驗值檢查伺服器設定,給出 ok/review/warn 結論
- 執行一條受限的唯讀 SELECT、EXPLAIN 或 SHOW(支援綁定參數),可整體停用或限定在指定 schema
- 讀取 overview 與 settings 資源,並使用引導式慢查詢診斷提示詞
- 適用情境
- 適合讓助理協助排查 PostgreSQL 事故或日常調校:找出慢語句、閱讀執行計畫、發現阻塞的工作階段、檢查 autovacuum 或索引膨脹、查看複寫延遲,同時不授予寫入權限。
- 執行需求
- 以本機程序執行(二進位檔、Claude Desktop 的 .mcpb 套件、Go 安裝或容器映像),或部署為 HTTP 服務。需要 PostgreSQL 13 以上版本的連線字串,放在 PGMCP_DATABASE_URL,最好使用專用唯讀角色。HTTP 模式可選用靜態 API 金鑰或 JWT 設定,並應置於負責 TLS 的反向代理之後。
安裝
在 SourceWeft 中
- 開啟 儀表板中的 pgmcp,將其新增到工作區。
- 為需要使用其工具的對話啟用該服務。
Desktop only,透過 STDIO。 STDIO 服務會啟動本機處理程序,因此需要 SourceWeft 桌面主機。
其他 MCP 客戶端
參照 儲存庫 中的啟動說明。
README
pgmcp
[Go Reference] [GitHub go.mod Go version] [Go Report Card] [GitHub Workflow Status (with branch)] [GitHub] [GitHub code size in bytes]
pgmcp is a read-only PostgreSQL ops/DBA server for the Model Context Protocol, built on the official modelcontextprotocol/go-sdk. It answers the questions a DBA asks during an incident — which statements are slow, why this plan is slow, which indexes are dead weight, which tables autovacuum has fallen behind on, who is blocking whom, how far the standby is lagging — and it is read-only by construction rather than by convention: a dedicated database role that holds no write privilege, a BEGIN READ ONLY transaction with a statement timeout around every single statement, and a SQL parser guard that rejects anything that is not one SELECT/EXPLAIN/SHOW and every function that could mutate state from inside a read-only transaction. Tested against PostgreSQL 16; requires 13 or later.
Installation
Claude Desktop, one click: download pgmcp_<version>.mcpb from Releases and open it. Claude Desktop asks for a Postgres connection string, keeps it in the OS keychain, and launches the bundled binary itself — nothing on PATH, no config file to edit. One bundle covers macOS (universal) and Windows (x64).
Otherwise download a binary for your platform from Releases — darwin, linux and windows, amd64 and arm64, with checksums.
Or build from source with the Go CLI tool go:
Or run the released image, which is distroless, non-root and multi-arch:
pgmcp is listed in the MCP Registry as io.github.pascalallen/pgmcp.
Before you point it at anything, create the read-only role — see Database role. It is the layer that still holds if the other two have a bug.
Usage
One MCP surface, two transports. Which one you run is configuration, not a different build.
Claude Code, stdio — the client launches the binary and talks over stdin/stdout:
Claude Desktop, stdio — install the .mcpb bundle from Releases (see Installation), or write the same thing into claude_desktop_config.json by hand:
HTTP — Streamable HTTP behind a static bearer key, for a shared deployment. pgmcp speaks plain HTTP and never terminates TLS itself; run it on loopback behind a reverse proxy.
TLS termination, the proxy settings the streaming transport needs, JWT auth against an identity provider, and wiring pgmcp up as a claude.ai custom connector are all in docs/DEPLOYING.md.
Tools
Every tool is annotated readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false, and returns a typed output schema.
query is the only tool that carries free-form SQL, and it is optional. --disable-query drops it from the catalogue entirely — a deployment that only needs the eight diagnostics tools can run without any ad hoc SQL surface at all. --query-schemas=public,app restricts it to named schemas instead — and bounds explain with it, since analyze=true executes the statement; read what that does and does not stop in docs/SECURITY.md.
Resources & prompt
Configuration
Every setting has a --flag and a PGMCP_<KEY> environment variable. Flags win over the environment, which wins over the default. Configuration errors exit 2 with every offending key named in one message; runtime failures exit 1.
The auth block applies to the HTTP transport only. Over stdio the operating system decides who the caller is: the parent process that launched the binary, and nobody else.
Security model
- Read-only three independent ways. A dedicated role with no write privilege (
pg_monitorplusSELECT, and deliberately notpg_signal_backend);BEGIN READ ONLYwithSET LOCAL statement_timeoutandlock_timeout = '2s'around every statement the adapter runs, always rolled back; and a parser-level guard, because a read-only transaction alone does not stoppg_terminate_backend,pg_read_file,pg_sleeporsetval. - The SQL guard is allow-list first. One top-level statement, and it must be a
SELECT,EXPLAINorSHOW; no nested write statement anywhere in the tree; noFOR UPDATE/FOR SHARElocking clause; noSELECT INTO; and no call to a denied function — file access, backup and WAL control, replication slots, advisory locks,dblink, sequence mutation, stats resets. - The schema allowlist is a guardrail, not a boundary.
--query-schemasmatches the schemas qualifying table references in the parsed statement, case-insensitively, and it bounds both tools that carry caller-supplied SQL —queryandexplain, soexplainwithanalyze=truecannot execute against a schema you excluded. A view, a set-returning function, or aSECURITY DEFINERfunction inside an allowed schema can still read outside it. Database privileges are the boundary; the allowlist just narrows the obvious path. - Authenticated, fail-closed, over HTTP. Static keys are compared in constant time against every stored hash without an early exit; JWTs are validated against a JWK set with asymmetric algorithms only (no
alg=none, no HMAC confusion) and a requirediss,audandexp, and the verifier holds no keys until the JWKS arrives, so it starts closed rather than open. RFC 9728 protected resource metadata advertises where to get a token. The server refuses to start on a non-loopback address with auth off. - Bounded. Per-principal rate limiting, a per-call timeout, a statement timeout and lock timeout inside the transaction, a row cap on the
querytool, a cap on a result's structured content, and a 1 MiB request body limit. - Nothing sensitive is logged. A tool call logs its name, duration, outcome and the caller's user id — never arguments, SQL text, result rows or error text. Parse failures come back as a fixed phrase rather than echoing the statement, and the DSN is redacted from connection errors.
The threat model, the full enumeration of the layers, and the limitations each one does not cover are in docs/SECURITY.md.
Testing
Run the test suite with the race detector and coverage:
Integration tests need a Postgres database and are skipped when PGMCP_TEST_DSN is unset. To run them against a scratch Postgres with pg_stat_statements preloaded:
Create and view a coverage profile:
Drive a running server through the official MCP conformance suite, or smoke it with the Inspector:
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
來源:README.md,提交 09953f9
工具
0版本歷史
1- v1.1.0最新Sep 16, 2026
評分
已滿足適用檢查項的 50%
該評分衡量此收錄條目的完整程度,依據註冊中心條目、端點與公開來源計算得出。它不是對伺服器本身的評價,也不表示伺服器安全或目前可用:可連通性是帶日期的檢查結果,評分也不包含安裝量或使用者評價。
- 近 30 天內可連通25 分
請保持端點能夠回應 MCP 握手;SourceWeft 每 30 天探測一次。
- 已宣告認證方式10 分
請在註冊中心條目中宣告憑證請求標頭,或保持端點能夠回應探測,以便判定其認證方式。
- 已列出工具15 分
請讓伺服器在無憑證時回應 tools/list,或發布可在沙箱中無憑證啟動的軟體包。
- 描述必需15 分
- 圖示10 分
- 文件15 分
- 授權條款10 分