Profile A: Rust Backend Service (Axum + MCP + Postgres / SQLite)
Use this profile for networked APIs, multi-tenant web backends, background workers, and MCP servers.
Recommended Stack Additions
- HTTP / MCP Surfaces (
apps/<prefix>-server):axum,tower-http,rmcp(if exposing MCP tools),tracing+tracing-subscriber. - Database (
crates/<prefix>-store):- Multi-tenant services: Postgres 15+ (
sqlxwith compile-time checked queries, migrations incrates/<prefix>-store/migrations/). - Single-node / embedded services: SQLite (
sqlxorrusqlite) isolated strictly inL3 store.
- Multi-tenant services: Postgres 15+ (
- External HTTP (
crates/<prefix>-integrations):reqwest(only crate allowed to importreqwest).
Multi-Tenant Postgres Rules
Apply these when the service holds data for multiple organizations/tenants:
- Private
TenantTxwrapper:TenantTxwraps asqlx::Transaction, keeps the inner transaction private, and executesSET LOCAL app.tenant_id = $1upon creation. No raw pool leaks to handlers or commands. - Tenant identity comes only from auth: Derive
tenant_idfrom verified JWT/session/API token claims in middleware, never from request body or path parameters. - Defense-in-depth schema: Every tenant table includes
tenant_id,ENABLE ROW LEVEL SECURITY,FORCE ROW LEVEL SECURITY, composite foreign keys(tenant_id, target_id), and unique indexes leading withtenant_id. - Least-privilege DB roles: Separate
migration_owner(DDL),app_runtime(DML constrained by RLS), andapp_worker(outbox/background consumer). - Outbox pattern: Commands write domain rows and outbox event rows inside the same
TenantTx. A separate worker drains the outbox and callsL5 integrationswith no transaction open.
Engine & Surface Hygiene
- Thin transport adapters: Axum routes, MCP tool handlers, and CLI subcommands only deserialize inputs, resolve
AuthContext, callEngine::execute, and map typed errors to HTTP status codes or JSON payloads. - Test DB guard: Integration test helpers must parse
DATABASE_URLwithPgConnectOptions::from_str(never substring checks) and refuse to run against non-test databases. - CI gates: Add an
xtask rlsgate that parses SQL migrations and verifies every newly created tenant table enables forced RLS (unless explicitly allowlisted with justification).