VENTURESAurabase acquires & invests in your apps, startups, mobile apps, SaaS and software.Read more →

Native AI · 10 min read

AI Agent Backend Architecture: Postgres 18, MCP, RLS and Guardrails (2026)

Affane Daylami · Founder ·

Back to blog

Handing an autonomous coding agent or customer-facing LLM a raw superuser PostgreSQL connection string is a production outage waiting to happen. Here is the 4-layer AI agent backend architecture built natively into Aurabase — from engine-enforced read-only MCP transactions to sqlparser AST-verified NL2SQL.

Quick answer: A production AI agent backend architecture must combine four layers without relying on prompt instructions alone: PostgreSQL 18 + pgvector HNSW for structured & semantic state, Row-Level Security (RLS) for tenant isolation, a hardened MCP server (@aurabase/mcp-server: 86 tools, 82 with project key, 37 engine-enforced read-only tools, --destructive-policy), and AST-verified read-only NL2SQL + multi-provider AI Gateway (OpenAI, Anthropic, Gemini) hosted in the EU.

#

The Problem

Why prompt-only guardrails fail in production

Most tutorials wire an LLM straight to a database driver and add "Please never run DROP TABLE" to the system prompt. Prompt instructions are probabilistic — indirect prompt injection inside user-generated text or hallucinated schema edits will eventually bypass them.

A real AI agent backend moves every boundary out of the prompt and into the database and protocol engines: PostgreSQL transactions, RLS roles, AST validation, and fail-closed MCP servers.

#

4-Layer Blueprint

The four layers of a hardened AI agent backend

LayerSecurity & architectural responsibilityAurabase native implementation
1. Memory & StateStructured records + sub-10ms vector similarity + flexible docsDedicated PostgreSQL 18 + pgvector HNSW (and --engine mongodb projects for NoSQL)
2. Agent Tooling (MCP)Typed tool discovery, zero fabricated values, destructive gates@aurabase/mcp-server: 86 tools across 13 domains (--read-only, --destructive-policy, --allowed-tools)
3. Data Isolation & NL2SQLTenant isolation + zero SQL injection for end-user queriesPostgreSQL RLS (auth.uid()) + Rust sqlparser AST-validated read-only NL2SQL with mandatory LIMIT
4. LLM Gateway & AuditMulti-provider routing + full EU query/latency audit trailNative AI Gateway (OpenAI, Anthropic, Gemini, SSE streaming) + EU ClickHouse observability
#

MCP Hardening

Inside Aurabase's 86-tool MCP server (13 domains)

Unlike thin database wrappers that only expose one raw run_sql tool, @aurabase/mcp-server structures access into 13 typed domains with validated output schemas (82 tools with a project key; 4 reserved for management-plane credentials):

  • Database (23) & RLS (6): list_tables, describe_table, diff_schema, apply_migration, explain_query, get_security_advisors, create_rls_policy, toggle_rls
  • AI (5) & Observability (4): index_document, rag_query, vector_search, create_vector_index, ai_completion, list_slow_queries, list_active_queries
  • Storage (9), Auth (7), Edge Functions (6), Backups (5), Notifications (10), Realtime (3), Project (4), API keys (3), Docs (1)
Engine-enforced read-only mode (37 of 86 tools)

Passing --read-only removes every mutating tool from tools/list and instructs PostgreSQL to run every query inside a READ ONLY transaction — even a clever SQL payload cannot write or drop data.

# Local stdio for Cursor / Claude Code / VS Code (37 read-only tools)
npx -y -p @aurabase/mcp-server aurabase-mcp \
  --url "https://<slug>.aurabase.cloud" \
  --project-id "<project-uuid>" \
  --service-role-key "aura_sk_..." \
  --read-only
#

End-User AI

AST-verified NL2SQL + pgvector RAG for end users

For customer-facing AI features (analytics copilots, semantic document search), Aurabase combines two complementary paths directly inside aura-ai:

  1. RAG with pgvector HNSW: embed and index tenant documents in the same PostgreSQL 18 database as your application data — no separate Pinecone/Qdrant cluster to synchronize or leak cross-tenant vectors.
  2. AST-verified NL2SQL: natural-language questions compile into SQL parsed through Rust's sqlparser, rejecting anything other than a bounded read-only SELECT and honoring your existing RLS policies.
#

IDE Guides

Connect your AI coding agent in 60 seconds

Follow our step-by-step setup guides for Cursor, Claude Code, VS Code Copilot Agent, or read the full 86-tool MCP reference and Native AI overview.

#

Frequently Asked Questions

FAQs

What is the best backend architecture for AI agents in 2026?+
A production AI agent backend separates four layers: unified relational + vector memory (PostgreSQL 18 + pgvector HNSW), a hardened Model Context Protocol (MCP) server with read-only and destructive-command policies, strict Row-Level Security (RLS) plus AST-validated read-only NL2SQL, and a multi-provider AI Gateway with EU observability.
How does Aurabase prevent an AI coding agent from dropping production tables?+
@aurabase/mcp-server supports --read-only (which strips mutating tools down to 37 of 86 AND executes every SQL query inside a PostgreSQL READ ONLY transaction), --destructive-policy (elicit, signed token, or deny), and --allowed-tools whitelisting.
How does Aurabase NL2SQL prevent SQL injection?+
Natural-language questions are compiled into a single SELECT query parsed and verified by a Rust sqlparser abstract syntax tree (AST), forced to read-only execution with a strict row LIMIT, and executed under the caller's Row-Level Security context.
Which coding IDEs work with Aurabase's MCP server?+
Cursor, Claude Code, Claude Desktop, VS Code Copilot Agent Mode, Windsurf, Cline, Roo Code, Continue.dev, and Google Antigravity — over local stdio or Streamable HTTP (MCP 2025-11-25 and 2026-07-28).

READY TO DEPLOY?

Your backend in five minutes.

No credit card required · 500 MB free · 50,000 MAU