pgmcp
Enables safe, read-only querying of PostgreSQL databases with defense-in-depth protections including single-statement SELECT guard, row caps, and per-identity audit logging.
README
pgmcp
A hardened, read-only Model Context Protocol server over a PostgreSQL database. It gives an LLM client a guarded SQL query tool plus schema introspection — and nothing else. No writes, no filesystem, no admin surface.
Why
Handing an LLM a raw database connection is dangerous. pgmcp puts a database between the model and your data with defense-in-depth:
- Least-privilege role. pgmcp connects as a role with
SELECT-only grants on exactly the schemas you allow — notpg_read_all_data. Grants are the security boundary. - Single-statement SELECT guard. A pglast AST guard rejects everything that
isn't one read-only
SELECT: multi-statement SQL (theCOMMIT; DROP …escape), DML, DDL,COPY,SELECT … INTO, data-modifying CTEs, and filesystem/admin functions. - READ ONLY transaction envelope around every query.
- EXPLAIN cost pre-gate on the large schemas you designate — a runaway cartesian join is rejected before it runs.
- Row cap with a truncation flag in the response.
- Per-identity audit log — one structured JSON line per call, attributed to the authenticated user.
Everything domain-specific is configuration.
Tools
| Tool | Purpose |
|---|---|
run_query(sql) |
Run one guarded, read-only SELECT; returns columns + rows + truncation flag. |
list_objects(schema) |
List tables/views in one allowlisted schema. |
describe_table(schema, table) |
Column names, types, nullability for one table/view. |
Quickstart
# 1. Create the least-privilege role and grants (see db/README.md).
# db/03_grants.sql and db/02_curated_views.sql are examples to edit for your
# own schema; PGMCP_ALLOWED_SCHEMAS below must match whatever you actually grant.
psql -f db/01_role.sql
psql -f db/03_grants.sql
psql -c "ALTER ROLE pgmcp_ro PASSWORD 'choose-a-secret'"
# 2. Install
uv venv && uv pip install -e .
# 3. Configure and run
export PGMCP_DSN="postgresql://pgmcp_ro:choose-a-secret@127.0.0.1:5432/mydb"
export PGMCP_ALLOWED_SCHEMAS="app,analytics,public"
export PGMCP_COST_GATED_SCHEMAS="app,analytics"
pgmcp # or: python -m pgmcp.server
The server binds 127.0.0.1:8765 and speaks Streamable HTTP. It trusts an
X-Auth-User header injected by a reverse proxy — see deploy/ for an nginx +
systemd example that adds TLS and per-user bearer auth.
Configuration
| Env var | Meaning | Default |
|---|---|---|
PGMCP_DSN |
libpq conninfo — service=pgmcp_ro or postgresql://… |
service=pgmcp_ro |
PGMCP_ALLOWED_SCHEMAS |
comma list of queryable schemas (required) | — |
PGMCP_COST_GATED_SCHEMAS |
subset that gets the EXPLAIN pre-gate | (none) |
PGMCP_SCHEMA_CONTRACT |
path to a TOML schema-contract; asserted on boot | (unset → skip) |
PGMCP_INSTRUCTIONS_FILE |
path to model instructions; overrides the default | (unset → default) |
PGMCP_ROW_CAP |
max rows returned | 10000 |
PGMCP_EXPLAIN_COST_LIMIT |
cost-gate ceiling | 100000000 |
PGMCP_IDENTITY_HEADER |
trusted identity header | X-Auth-User |
PGMCP_HOST / PGMCP_PORT |
bind address (loopback) | 127.0.0.1 / 8765 |
PGMCP_ALLOWED_HOSTS / PGMCP_ALLOWED_ORIGINS |
DNS-rebinding / Origin allowlists | hosts: loopback; origins: empty |
PGMCP_ALLOWED_SCHEMAS must mirror the grants in db/03_grants.sql. The server
refuses to boot with an empty allowlist.
Exposing a table with some sensitive columns
Don't grant the base table. Create a view in a curated schema that projects
only the safe columns, grant the view, and add curated to
PGMCP_ALLOWED_SCHEMAS. See db/02_curated_views.sql. The base table stays
unreachable through pgmcp.
Optional: schema contract
Point PGMCP_SCHEMA_CONTRACT at a TOML file (see examples/schema-contract.toml)
declaring expected column types. On boot pgmcp asserts them against
information_schema and refuses to start on drift — a guard against a query
tool silently outliving the schema it was written for.
Development
uv pip install -e ".[dev]"
pytest tests/ # unit tests need no database
PGMCP_DSN=... pytest tests/test_live.py # opt-in end-to-end checks
Design
Architecture decisions and their rationale are in docs/adr/. Start with
CONTEXT.md (glossary) and SETUP.md (setup runbook).
License
Released under the MIT License — see LICENSE.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。