Kenning PG MCP
A secure PostgreSQL Model Context Protocol server that provides read-only database access through tools like schema listing, object inspection, query execution, and EXPLAIN analysis, with database-enforced access control via read-only transactions and a minimal role rather than SQL text parsing.
README
Kenning PG MCP
A PostgreSQL Model Context Protocol server built on two convictions:
- Dependencies are pinned exactly. Every dependency is resolved once,
recorded in a committed lockfile, and upgraded only as a deliberate,
reviewed act. The published package carries
==pins; the Docker image is built withuv sync --frozen. Nothing resolves at launch time. - Read-only means the database says no — not a regex. Access control is
transaction- and role-based. The server never inspects SQL text to decide
whether a statement is "safe," because that model is unwinnable: a
data-modifying CTE, a
VOLATILEfunction that writes, orCOPY ... TO PROGRAMall pass keyword filters. PostgreSQL itself is the enforcement.
Built on the MCP Python SDK v2 (2026-07-28 stateless protocol revision),
psycopg 3, and pydantic-settings. Serves stdio and streamable HTTP from
one set of handlers.
The security model
Four layers, none of which parse SQL:
- Read-only transactions. Every read path runs inside
BEGIN ... READ ONLY. PostgreSQL rejects any write attempt with SQLSTATE25006— including data-modifying CTEs,SELECT ... FOR UPDATE, and volatile functions that write. - One statement per call, enforced by the server. Every user statement is executed through the extended query protocol, where PostgreSQL rejects multi-command strings outright. Statement stacking dies in the database, not in a parser.
- A purpose-built role. Connect as a minimal role (SQL below). This is
what blocks
COPY ... TO PROGRAM,pg_read_file(),lo_export(), andpg_terminate_backend()— capabilities that begin withSELECTorCOPYand would sail through any keyword guard. Privilege, not pattern matching. - Timeouts on every session.
statement_timeoutandidle_in_transaction_session_timeoutare set at connection time, so a badly planned query cannot pin a connection indefinitely.
On startup (in read-only mode), the server probes that a write inside a
read-only transaction really fails with 25006 — and exits non-zero if it
doesn't, rather than silently serving a read-only server that isn't. An
adversarial test suite attempts every bypass listed above against a real
PostgreSQL; every case must fail at the database.
Write access is never inferred. In the default restricted mode the write
tool is not merely refused — it is not registered, so it never appears in
tools/list. Setting PG_MCP_ACCESS_MODE=unrestricted registers a single
DML tool; read tools still run read-only transactions.
The role
CREATE ROLE mcp_ro LOGIN PASSWORD '...';
GRANT CONNECT ON DATABASE mydb TO mcp_ro;
GRANT USAGE ON SCHEMA public TO mcp_ro;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO mcp_ro;
ALTER ROLE mcp_ro SET default_transaction_read_only = on;
ALTER ROLE mcp_ro SET statement_timeout = '30s';
ALTER ROLE mcp_ro SET idle_in_transaction_session_timeout = '60s';
For read-write deployments, the equivalent role carries INSERT, UPDATE,
DELETE and nothing more. DDL is an operator decision, not a server feature.
Install
The server is a deployable tool, not a library — install it isolated, where exact pins are a feature:
uv tool install kenning-pg-mcp # or: pipx install kenning-pg-mcp
Or use the Docker image (the primary distribution artifact — resolution happens at build time, never at launch):
docker build -t kenning-pg-mcp:0.1.0 .
Do not run this server via uvx. uvx re-resolves against PyPI on every
invocation, which is the exact failure mode this project exists to eliminate.
Install once, pin the version, upgrade deliberately.
Configuration
| Setting | Env var | Default |
|---|---|---|
| Connection URI (required) | DATABASE_URI |
— |
| Access mode | PG_MCP_ACCESS_MODE |
restricted |
| Transport | PG_MCP_TRANSPORT |
stdio |
| HTTP bind host / port | PG_MCP_HOST / PG_MCP_PORT |
127.0.0.1 / 8000 |
| Max rows / response bytes | PG_MCP_MAX_ROWS / PG_MCP_MAX_BYTES |
1000 / 1048576 |
| Statement timeout | PG_MCP_STATEMENT_TIMEOUT |
30s |
| Pool min / max | PG_MCP_POOL_MIN / PG_MCP_POOL_MAX |
1 / 5 |
| Schema allowlist (comma-sep) | PG_MCP_SCHEMAS |
all non-system |
| Log level | PG_MCP_LOG_LEVEL |
INFO |
Flags --access-mode, --transport, --host, --port override the
environment. The effective configuration is logged at startup with the
connection password redacted.
Tools
| Tool | Purpose |
|---|---|
list_schemas |
Schemas, honoring the allowlist. |
list_objects |
Tables, views, matviews, sequences in a schema. |
describe_object |
Columns, PK, FKs both directions, indexes, constraints, comments, approximate row count. |
execute_query |
One read statement in a read-only transaction; capped results with an explicit truncation notice (no LIMIT is ever injected into your SQL). |
explain_query |
EXPLAIN (FORMAT JSON); analyze=true always runs inside a rolled-back transaction. |
list_extensions |
Installed and available extensions. |
server_info |
Version, role, database, access mode, key settings. |
execute_statement |
Single DML statement — registered only in unrestricted mode. |
Results serialize honestly: numeric → string (never float), timestamps →
ISO 8601 with timezone, bytea → base64 with a length note, json/jsonb →
nested structures, NULL → null.
Claude Desktop
{
"mcpServers": {
"postgres": {
"command": "docker",
"args": ["run", "-i", "--rm", "-e", "DATABASE_URI", "kenning-pg-mcp:0.1.0"],
"env": {
"DATABASE_URI": "postgresql://mcp_ro:PASSWORD@host.docker.internal:5432/mydb"
}
}
}
}
Or with a tool install:
{
"mcpServers": {
"postgres": {
"command": "/absolute/path/to/kenning-pg-mcp",
"env": {
"DATABASE_URI": "postgresql://mcp_ro:PASSWORD@localhost:5432/mydb"
}
}
}
}
HTTP mode
PG_MCP_TRANSPORT=http kenning-pg-mcp
Clients connect to http://127.0.0.1:8000/mcp. Binding beyond loopback
requires PG_MCP_ALLOW_REMOTE=true, and there is no built-in
authentication — put the server behind a reverse proxy that authenticates,
and set PG_MCP_ALLOWED_HOSTS / PG_MCP_ALLOWED_ORIGINS to match your
deployment.
Development
uv sync --frozen
make check # lint + type-check + full test suite
The test suite has three layers: unit (no database), integration against a real PostgreSQL via testcontainers, and an adversarial layer that attempts every write-bypass in the threat model as a low-privilege role — each case must fail at the database. Docker is required for the latter two.
License
Licensed under the Hippocratic License 3.0 with the Mass Surveillance module — an ethical-source license. This project is source-available, not OSI open source. See LICENSE.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。