Why Your MCP Server's Read-Only Mode Probably Still Lets Agents Mutate Data
Direct Answer
A "read-only" flag inside an MCP server fails whenever it relies on prompt instructions, naive SQL string matching (query.startswith("SELECT")), or hiding write tools from the tool list while leaving shared endpoints active. To guarantee an AI agent cannot mutate state, enforce read-only permissions at the database role, filesystem mount, and OAuth token scope outside the MCP server process.
A fresh security audit making rounds on Hacker News today (Of 10 verified MCP server vulns, 3 were "read-only" modes the code didn't keep) shows how common this gap is across real open-source MCP servers.
Where MCP Read-Only Checks Break
Most teams implement read-only mode in one of two fragile ways: checking an if read_only: branch in application code, or scanning SQL strings for keywords like DELETE and UPDATE.
Both break easily on multi-statement queries (SELECT 1; DROP TABLE ...), Postgres CTE writes (WITH d AS (DELETE FROM ...) SELECT * FROM d), or HTTP helper methods that follow redirects or hit mutating endpoints.
| Enforcement Layer | Can Prompt Injection Bypass It? | Handles Multi-Statement / CTEs? | Recommended for Prod? |
|---|---|---|---|
| System prompt instruction | Yes, trivially | No | Never |
| Regex / string prefix check | Yes, via CTEs or comments | No | No |
Postgres READ ONLY + SELECT role | No | Yes, enforced by DB engine | Yes |
Enforcing Real Read-Only in Postgres MCP Tools
Never trust your own SQL parser when Postgres already has a battle-tested transaction and role engine. Connect your read-only MCP tool using a dedicated readonly_role and wrap every query in SET TRANSACTION READ ONLY:
pythonasync def run_readonly_query(conn, sql: str, params: dict): async with conn.transaction(): # Enforced by Postgres itself: rejects INSERT/UPDATE/DELETE/DDL, even inside CTEs await conn.execute("SET LOCAL transaction_read_only = on") await conn.execute("SET LOCAL statement_timeout = '3000ms'") return await conn.fetch(sql, *params.values())
Pair that with mount -o ro for local filesystem tools and read-only OAuth scopes (drive.readonly, repo:status) for SaaS integrations.
Frequently Asked Questions
Is hiding write tools in list_tools enough for read-only mode?
No. If the underlying server still registers the write handler or shares a privileged DB connection pool, a bug in parameter routing or a crafted tool call can still trigger mutations. Always enforce permissions on the downstream resource itself.
What about SQLite MCP servers?
Open the SQLite database using the URI flag file:/path/to/db.sqlite?mode=ro (or PRAGMA query_only = ON;) so SQLite rejects writes at the C library level.