Read-only Postgres: drop-in for the archived server-postgres, with schema context.
Copy the AI prompt to install this server into Claude Code, Cursor, or another agent β or use 1-click editor setup below.
One-click editor setup isnβt available for this listing yet β we donβt have a confirmed install command, and weβd rather show nothing than point your editor at the wrong package or host. Follow the projectβs own setup instructions, linked above.
The analytics MCP server for Postgres. Read-only by construction, with a context file you and the agent both write to, so the model answers the way your team would.
Built and maintained by Contextflo.
A drop-in replacement for the archived @modelcontextprotocol/server-postgres, which shipped with a
SQL injection vulnerability
that let COMMIT; DROP SCHEMA public CASCADE walk straight out of its read-only transaction.
Watch the 100-second demo: setup, the
agent working out that an events table is not what it looks like and saving that as a note, and a DROP the server
refuses.
Read-only that holds up. The archived server enforced read-only as a property of the SQL string. Here it is a property of the wire protocol, the connection, the Postgres parser, and the database role: four independent layers, each of which stops that payload on its own. The exploit is a test case in this repo.
Answers that make sense. A model that does not know fct_orders_v2 is the table your team actually uses, or that
revenue is gross rather than net, writes confident, wrong SQL. .contextflo/context.md is a markdown file you edit
and this server hands to the model, with nothing behind it but the file. The agent adds to it too: when it learns
something the schema does not say, add_table_context appends a note, which you review in a diff like any other change.
1. Create a read-only role. Run this as the database owner, in psql or your provider's SQL editor. Use your
database name and a real password, and repeat the schema lines for each schema the agent should see:
This makes read-only a property of the database, not just of this server's code.
2. Put that role's connection string in .env in your project folder:
Keep the quotes: hosted providers add ?sslmode=require&..., and the & needs them. The server reads DATABASE_URL
from .env in the directory it starts in, so the connection string never appears on a command line.
Point it at a read replica or a branch rather than your primary if you can. Read-only stops writes, not load: an agent
exploring your data can run a full-table scan or a heavy join, and on the primary that competes with your application.
Neon and Supabase can branch or replicate a database in a few clicks, and RDS, Cloud SQL, and most other hosts offer
read replicas. The statement timeout (30 seconds by default, --statement-timeout) and the row cap limit how long one
query runs and how much it returns; they do not make it cheap.
3. Generate the context file:
That writes .contextflo/context.md, seeded from your COMMENT ON values. Edit it: the business definitions section
is where the value is.
4. Add it to your client. Claude Code starts servers in your project folder, so it finds .env and the context
file on its own:
--scope project writes .mcp.json into the folder instead of your global config.
Other clients may start servers outside your project folder (Claude Desktop starts them in /), so give them the
connection string and the context file explicitly:
Cursor uses .cursor/mcp.json. Claude Desktop uses claude_desktop_config.json (macOS:
~/Library/Application Support/Claude/, Windows: %APPDATA%\Claude\). VS Code uses .vscode/mcp.json, with
servers in place of mcpServers.
| Tool | What it does |
|---|---|
query | Runs one read-only statement: SELECT, WITH ... SELECT, EXPLAIN, or SHOW. |
list_tables | Lists readable tables with descriptions. pattern matches anywhere in the name or description. |
get_table_context | Describes tables: columns, types, keys, foreign key targets, enum values, curated descriptions. |
add_table_context | Lets the agent write down a gotcha it found (amount is in cents, status has an undocumented value) in the context file. |
There is no separate search tool, and that is deliberate. information_schema and pg_catalog are ordinary tables,
so anything more specific, like finding every column named like %revenue% or listing tables with no primary key,
is a query the model can write itself:
Table schemas are also exposed as postgres://<host>/<table>/schema resources, matching the archived server, for
anything pinned to those URIs. Most clients never fetch resources on their own, which is why discovery lives in the
tools.
Four layers. Each one stops the archived server's exploit by itself.
1. Extended query protocol. User SQL goes through pg-cursor, which always issues Parse/Bind/Execute, so
Postgres itself rejects multi-statement input. The archived server called client.query(sql) with a bare string;
node-postgres only prepares a statement when there are bind values, so that took the simple protocol path, where
; separates statements. That is the whole bug.
2. Connection-level read-only. default_transaction_read_only=on is set in the startup packet, and every
statement runs inside an explicit BEGIN READ ONLY that always ends in ROLLBACK, never COMMIT. The rollback
also undoes any SET made inside the transaction, so a statement cannot leave a pooled connection weakened for
whoever gets it next.
3. A statement allowlist on the real Postgres parser. libpg-query
is the actual Postgres C parser compiled to WASM, not a JavaScript approximation of SQL. The whole parse tree is
walked rather than just the top-level node, which is what catches a data-modifying CTE:
That parses as a SelectStmt. A validator checking only the statement type runs it. Unknown node types fail closed.
Functions are checked against the database's own catalog. Postgres labels every function immutable, stable, or
volatile, and only volatile ones can have side effects, so a volatile function is refused unless it is on a short list
of harmless ones analysis needs (random(), clock_timestamp(), the table size functions). That covers dblink,
pg_logical_emit_message (which writes to the WAL even in a read-only transaction), advisory locks, statistics resets,
and whatever a future Postgres adds, without anyone having to name them. SECURITY DEFINER functions, which run with
their owner's privileges, are refused whatever their label. A fixed list of known escapes is checked as well.
4. A read-only database role. The layers above are code, and code has bugs. A role that cannot write is enforced
by Postgres regardless, which is why creating one is the first step of Setup. The server warns on startup
if you connect as a superuser, and init prints the role snippet if the role it connects as can write.
What this does not protect against. The function check sees the functions a query calls directly, and it trusts
how each is labelled. A user-defined function declared STABLE that writes anyway is allowed, and so is a function
reached indirectly: inside a view, behind an operator, or through a type cast. The read-only transaction still refuses
anything that changes table data that way; what can slip through is the rarer kind of side effect, such as a message
written to the WAL. Layer 4 is what stops those, which is why the read-only role is the recommended setup rather than
an optional extra. Read-only is also not
confidentiality: anything the connected role can read, a model can read, so grant it only what you want an agent
to see.
@modelcontextprotocol/server-postgresSwap the package name. The tool is still called query, still takes sql, still returns JSON rows, and the
connection string is still the first argument.
Four deliberate differences:
SET/RESET are rejected with a clear error. On the archived server these "worked",
and that was the vulnerability.public, and column descriptions come through from
COMMENT ON.No reviews yet β be the first to share how this listing worked for you.
Showcase your server listing on GitHub or your project documentation. Embed this dynamic SVG badge to highlight official listing status and live engagement.
[](https://allmcps.com/mcp/postgres-read-only)<a href="https://allmcps.com/mcp/postgres-read-only"><img src="https://allmcps.com/api/badge/postgres-read-only?style=directory" alt="Postgres (read Only) on AllMCPs" /></a>