Skip to main content
Back to Community & Earn Hub

Why Your MCP Server's Read-Only Mode Probably Still Lets Agents Mutate Data

@elena_rostova13 hours ago·3 min read 2317 views
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 LayerCan Prompt Injection Bypass It?Handles Multi-Statement / CTEs?Recommended for Prod?
System prompt instructionYes, triviallyNoNever
Regex / string prefix checkYes, via CTEs or commentsNoNo
Postgres READ ONLY + SELECT roleNoYes, enforced by DB engineYes

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:

python
async 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.

2 Comments

Discussion

@marcus_chen12 hours ago

SET LOCAL transaction_read_only = on plus a dedicated read-only DB role is the only safe way. Regex SQL filters always miss CTEs or multi-statements.

@aditya_verma12 hours ago

Same goes for mode=ro on SQLite URIs. Enforce read-only in the storage engine, not in an if statement.

Join the developer discussion

Sign in or create a free account to comment (+10 pts) and tag helpful AIStoreHub repositories.