Skip to main content
Glama
pg-guard-mcp

pg-guard-mcp

pg-guard-mcp

PyPI

一个 PostgreSQL MCP 服务器,在协议和权限层面强制执行只读访问——而不是通过解析查询字符串来碰运气。

安装

pip install pg-guard-mcp
# or, without installing anything permanently:
uvx pg-guard-mcp

Related MCP server: PostgreSQL MCP Server

为什么存在

官方 @modelcontextprotocol/server-postgres 曾附带一个只读模式,单个 COMMIT; 就能绕过它:该模式把智能体的查询包装在 BEGIN TRANSACTION READ ONLY 中,并把整个内容作为一个字符串发送。在该模式下,Postgres 接受以分号分隔的多个语句,因此 SELECT 1; COMMIT; DROP SCHEMA public CASCADE; 会让只读事务提前结束,并把 DROP 作为普通写入执行。这个包因此被弃用。(Datadog Security Labs 分析)

pg-guard-mcp 存在的原因正是这类 bug——“只读”仅靠字符串来强制检查——在 MCP 生态系统中依然普遍。它通过三个独立防御层来防护,所以任何单一失误都不会造成致命后果:

  1. 协议层(真正的边界)。 每条查询都通过 Postgres 的 扩展 查询协议(Parse/Bind/Execute)运行,绝不使用简单查询协议。扩展协议在结构上拒绝单个 Parse 消息中包含多个语句——在我们任何代码运行之前,Postgres 自己就会拒绝。因此,无论提交什么字符串,Datadog 的漏洞利用在这里都无法生效。

  2. 会话层。 每次连接都在会话级别设置 default_transaction_read_only = on,所以即使某条查询被当作写入意外到达数据库,也会被 Postgres 拒绝。

  3. 预检层。 在查询发送之前,会检查其中是否包含多条语句或事务控制关键字(COMMIT、ROLLBACK、BEGIN、SAVEPOINT……),并带上清晰的错误信息拒绝。这一层是为了快速、明显“失败并”,而不是主要防御。

此外,使用一个已 REVOKE 了写权限的数据库角色进行连接是推荐的(也是启动时会检查的)配置——在权限层面也加一道双保险。

工具

工具

说明

pg_run_query(sql)

运行一条只读查询,返回结果行

pg_explain_query(sql)

返回查询计划但不执行

pg_list_tables(schema="public")

列出某个 schema 中的表/视图

pg_describe_table(table_name, schema="public")

列出某张表的列

pg_check_privileges()

报告已连接角色实际持有的任何写授权——应始终为空

设置

pip install pg-guard-mcp
export PG_GUARD_DSN="host=127.0.0.1 dbname=mydb user=myapp_readonly password=..."
pg-guard-mcp

将你的 MCP 客户端指向 pg-guard-mcp 命令(或使用 uvx pg-guard-mcp 来跳过永久安装),并在其环境配置中设置 PG_GUARD_DSN。

参见 .env.example 获取所有支持的环境变量,并参见 scripts/setup_dev_db.sh 获取一个正确受限的只读角色配置示例(本项目的测试正是基于该配置运行的)。

测试

pip install -e ".[dev]"
pytest tests/ -v

tests/test_safety.py 是纯 Python,不需要数据库。tests/test_db.py 与 tests/test_server.py 会针对真实本地 Postgres 实例运行——包括导致官方 Postgres MCP server 被弃用的精确攻击载荷——并且在 pgguard_test 不可达时自动跳过。运行一次 scripts/setup_dev_db.sh 即可创建该实例。

状态

v0.1.0,已发布 PyPI。58 个测试通过(单元测试 + 真实 Postgres 集成测试,包括导致官方 server-postgres 被弃用的那个精确 exploit,并且都是对已发布包的全新 pip install 运行)。

许可证

MIT

Available Tools

5 tools
pg_check_privilegesA

Report any write privilege (INSERT/UPDATE/DELETE/TRUNCATE) the connected role actually holds on any table. An empty list is the expected, safe result — anything else means the privilege layer of defense is missing and the role should be locked down (see README.md), even though the protocol and session layers still hold on their own.

Inputs: no named parameters.

pg_describe_tableA

List the columns of a table: name, data type, nullability, and default, in column order.

Inputs: table_name (required), schema.

pg_explain_queryA

Return the PostgreSQL query plan for a read-only SELECT/WITH statement, without running it. Useful for checking whether a query will be slow before running it for real.

Inputs: sql (required).

pg_list_tablesA

List the tables and views visible to the connected role in the given schema (default: public).

Inputs: schema.

pg_run_queryA

Run a single read-only SQL statement (SELECT / WITH / EXPLAIN / SHOW) against the configured PostgreSQL database and return the rows.

Rejects anything that isn't exactly one plain read-only statement — multiple statements, transaction-control keywords (COMMIT, ROLLBACK, BEGIN, ...), and any write/DDL/admin keyword anywhere in the query are all refused before the query is sent to the database. Results are capped at PG_GUARD_ROW_LIMIT rows (default 1000).

Inputs: sql (required).

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A4.3/5.0

    Scored across 5 tools

    Disambiguation5/5

    Each tool has a clearly distinct role: executing read-only queries, explaining query plans, listing tables, describing table schemas, and checking write privileges. There is no functional overlap or ambiguity between them.

    Naming Consistency5/5

    All tool names follow the same pg_ prefix with a consistent verb_noun pattern: run_query, explain_query, list_tables, check_privileges, describe_table. The naming is uniform and predictable.

    Tool Count5/5

    Five tools is well-scoped for a PostgreSQL guard/observer server. Each tool serves a clear, non-redundant purpose and the set feels complete without being bloated.

    Completeness5/5

    The tool surface covers the core read-only workflow: exploring available tables, inspecting schemas, validating query plans, running queries, and verifying the security posture. No obvious dead ends or missing security-relevant operations are apparent.

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      B
      quality
      C
      maintenance
      Enables secure read-only access to PostgreSQL databases through SELECT queries only, with tools for exploring schemas, listing tables, and executing common queries while preventing any data modification operations.
      4
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      Provides secure, read-only access to PostgreSQL databases for schema inspection and data querying. It enables users to list tables, describe structures, and execute SELECT statements while strictly blocking destructive operations.
      1
      MIT