Show and tell: Hardening MCP Database & SQL Tools for Autonomous Agents — CTE Defenses, Read-Only Guardrails & Stdio Isolation #38002
A7mdMe
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Context & Maintainer Disclosure
Maintainer of MCP Production Kit (an open-source TypeScript Model Context Protocol server starter kit).
When building autonomous agents in Eliza or extending agents with external Model Context Protocol (MCP) tools, giving an agent access to a live database (PostgreSQL / Supabase / Neon) is one of the highest-leverage capabilities. However, connecting an autonomous agent directly to raw SQL creates significant production security risks: unintended hallucinations, prompt-injected mutations, CTE deletions, and process stdio framing pollution.
We wanted to share a concrete walkthrough on hardening MCP database tools for agent environments, complete with reproducible test scenarios and architectural boundaries.
1. Attack Vectors in Autonomous Agent Tool Execution
An agent reading tweets, scraping websites, or processing Discord messages can encounter adversarial text instructing it to modify records:
Agents attempting complex analytics can hallucinate or be tricked into executing data-modifying Common Table Expressions:
WITH, a naive prefix validator allows it to pass through to execution.When running MCP tools as subprocesses over
stdio, any library writing log output or performance statistics toprocess.stdoutcorrupts the JSON-RPC message stream, crashing the agent client.2. Multi-Tier Defense: Application-Level Inspection + DB Permissions
(Note: This application-level guardrail uses token & regex sanitization, not a full SQL AST compiler, and is engineered specifically to accompany database-level read-only permissions like
GRANT SELECT).3. Core Implementation: Safe Query Inspection
4. Verified Automated Test Matrix
We run an automated test suite verifying edge cases:
SELECT id, username FROM users WHERE active = true-> PASSSELECT * FROM trades WHERE agent_id = $1-> PASSWITH t AS (SELECT count(*) as c FROM memory_entries) SELECT * FROM t-> PASSSELECT * FROM audit_log WHERE action = 'DROP TABLE'-> PASS (string literals normalized)DELETE FROM users WHERE id = 1-> BLOCKEDDROP TABLE memory_entries-> BLOCKEDSELECT * FROM users; DROP TABLE users;-> BLOCKED (multiple statements rejected)WITH d AS (DELETE FROM users RETURNING *) SELECT * FROM d-> BLOCKED (CTE mutation detected)/* comment */ DROP TABLE users-> BLOCKED (comment stripped)5. Architectural Comparison: Free Core vs. Pro Pack Add-on
Includes JSON-RPC 2.0 dual transport (stdio + SSE), zero-leak stderr logging, diagnostic telemetry tool, SSRF-guarded HTTP fetch tool, in-memory KV state store, and standard Dockerfile.
For engineering teams deploying to cloud production, includes pre-packaged
postgres_connector.ts, Cloudflare Workers serverless edge template, and hardened multi-container Docker Compose with 26 passing automated tests.Community Discussion & Feedback
We would love feedback from the Eliza agent builder community:
get_user_summary)?Full walkthrough guide: examples/SAFE_POSTGRES_MCP_WALKTHROUGH.md
All reactions