Skip to main content
Glama
BerkantACUN

pg-guard-mcp

by BerkantACUN

pg-guard-mcp

PyPI

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

安装

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

Related MCP server: MCP PostgreSQL Read-Only 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. 预检层。 在查询发送之前,会检查其中是否包含多条语句或事务控制关键字(COMMITROLLBACKBEGINSAVEPOINT……),并带上清晰的错误信息拒绝。这一层是为了快速、明显“失败并”,而不是主要防御。

此外,使用一个已 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.pytests/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

Install Server
A
license - permissive license
A
quality
C
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • A
    license
    Not graded
    quality
    Not graded
    maintenance
    Enables read-only access to PostgreSQL databases with multi-tenant support, allowing users to query data, explore schemas, inspect table structures, and view function definitions across different tenant schemas safely.
    55
    1
  • A
    license
    Not graded
    quality
    D
    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.
    505
    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.
    11
    1
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    Enables safe interaction with PostgreSQL databases through read-only queries, schema exploration, and performance analysis.
    225
    MIT

View all related MCP servers

Related MCP Connectors

  • Query PostgreSQL databases in plain English — LLM-generated, safety-validated SQL.

  • Comprehensive PostgreSQL documentation and best practices, including ecosystem tools

  • Read-only bank access for your AI agent. Connects Claude, ChatGPT, Cursor, Gemini, Codex.

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/BerkantACUN/pg-guard-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server