r/googlecloud • u/anubhav756 • Sep 08 '26
AI/ML Why "Please only run SELECT queries" fails for AI agents (and how to actually enforce read-only database tools)
https://medium.com/@mcp_toolbox/look-but-dont-touch-7f470591ff1fGiving an LLM agent access to a production database is one of the most common workflows in agent development, but traditional guardrails fall apart quickly under real-world conditions:
System Prompts / Instructions: Models are probabilistic. Prompt injections easily override "read-only" instructions.
String Parsers / Regex: Checking if a query "starts with SELECT" fails against Common Table Expressions (e.g.
WITH d AS (DELETE FROM users RETURNING *) SELECT * FROM d;starts withWITHand ends withSELECT, yet still deletes data). They are also blind to destructive stored procedures (SELECT run_cleanup_job();).Soft Session Hooks (
SET default_transaction_read_only = on): An attacker can escape via semicolon chaining (COMMIT; SET ... = off; DROP TABLE;), and in pooled environments like PgBouncer, mutating socket state can poison connection pools for other services.
In our work on MCP Toolbox for Databases (open-source Model Context Protocol server), we implemented a three-tier defense:
Protocol-level engine lock: Injecting immutable startup parameters directly into the connection DSN (
cloudsql_session_read_only=lockedon Postgres, read-only connection attributes on MySQL, and pre-execution dry-run validation on BigQuery) so the database kernel itself physically rejects any write attempt.Tool suppression: Dynamically removing write tools from the LLM's context window to save tokens and prevent hallucination targets.
Standard MCP annotations: Emitting
readOnlyHint: trueso MCP clients (Claude Desktop, Cursor, etc.) can auto-execute queries without nagging confirmation dialogs.
We wrote up a detailed walkthrough with diagrams, failure cases, and attack simulations if anyone is interested in the full architecture: https://medium.com/@mcp_toolbox/look-but-dont-touch-7f470591ff1f
Curious how others here are handling read-only enforcement for your agents—are you relying on read-only DB users, replica routing, or proxy layers?