Disclosure: Maintainer of MCP Production Kit. Posting in Show and tell to share architectural patterns for safely connecting AI coding agents (Claude Desktop, Cursor, Cline) to PostgreSQL and Supabase databases via the Model Context Protocol (MCP).
Connecting autonomous AI agents to a live Supabase database provides immense utility (schema introspection, telemetry analysis, data retrieval), but exposing SQL query tools to LLMs carries obvious operational risks: hallucinated mutations (DELETE, DROP, UPDATE), multi-statement query chaining, and transport crashes.
Relying solely on database user permissions can be brittle if an agent runs under an admin or service-role connection. A defense-in-depth tool should enforce query constraints before dispatch:
export function sanitizeReadOnlyQuery(sql: string): { cleanSql: string } {
// 1. Strip comments to prevent SQL injection smuggling
const stripped = sql
.replace(/\/\*[\s\S]*?\*\//g, '')
.replace(/--.*$/gm, '')
.trim();
// 2. Reject multi-statement query chaining
if (stripped.includes(';')) {
const statements = stripped.split(';').map(s => s.trim()).filter(Boolean);
if (statements.length > 1) {
throw new Error('Multi-statement execution rejected: single queries only.');
}
}
// 3. Token-based statement validation
const firstKeyword = stripped.split(/\s+/)[0]?.toUpperCase();
const ALLOWED_KEYWORDS = ['SELECT', 'WITH'];
if (!ALLOWED_KEYWORDS.includes(firstKeyword)) {
throw new Error(`Forbidden query keyword: ${firstKeyword}. Only read-only operations allowed.`);
}
// 4. Mutation blacklist check across entire payload
const FORBIDDEN_MUTATIONS = /\b(INSERT|UPDATE|DELETE|DROP|ALTER|TRUNCATE|GRANT|REVOKE)\b/i;
if (FORBIDDEN_MUTATIONS.test(stripped)) {
throw new Error('Destructive DDL/DML keyword detected in query payload.');
}
return { cleanSql: stripped };
}
A7mdMe shares architectural patterns for securely connecting AI coding agents to PostgreSQL and Supabase databases using the Model Context Protocol (MCP). The post highlights the risks of exposing SQL query tools to AI agents and presents safeguards such as read-only query sanitization and stdio isolation. Feedback on SQL sandboxing patterns and connection-pooling best practices is invited.
When running locally via Claude Desktop or Cursor over stdio, any rogue console.log or database client notice written to stdout corrupts JSON-RPC framing. All database logging and query metrics must route strictly to stderr.
We provide both an open-source MIT starter kit and a commercial production add-on for teams needing turnkey recipes:
| Capability | Free Open-Source Core | Pro Pack ($19 Single License) |
|---|---|---|
| MCP Protocol Support | Full (v2024-11-05 JSON-RPC 2.0) | Full (v2024-11-05 JSON-RPC 2.0) |
| Transports | Local stdio + HTTP / SSE | Local stdio + HTTP / SSE |
| Core Utilities | System metrics, SSRF guard, memory store | All core utilities included |
| Glama Registry Score | Grade B Verified | Grade B Verified |
| PostgreSQL / Supabase Tool | DIY implementation | Turnkey postgres_connector.ts with token sanitizer |
| Edge Serverless Hosting | - | Cloudflare Workers global edge deployment |
| Container Orchestration | Standard Dockerfile | Production Docker Compose (Fly.io / Railway / AWS) |
Feedback on agent SQL sandboxing patterns and connection-pooling best practices for Supabase is welcome!