Building an MCP Server for Enterprise Databases
How to design an MCP server that exposes enterprise databases to AI agents with scoped identity, query controls, audit, and predictable cost.

A production MCP server for enterprise databases stands on four joints: authentication, cost caps, row-level policy, and signed audit. Three prompt-injection CVEs (CVE-2025-68143, CVE-2025-68144, CVE-2025-68145) landed in the official mcp-server-git in late 2025, exploiting exactly those joints.
Anthropic introduced the Model Context Protocol as an open standard on November 25, 2024, specifying a wire format and an OAuth 2.1 authorization pattern. Everything the database cares about, from identity mapping to query shape to policy evaluation to the signed audit record, is left to the implementer. Postgres, Snowflake, and SQL Server each force a different choice at each joint.
What the Spec Actually Commits You To
The March 2025 authorization specification requires OAuth 2.1 with PKCE for every client, so a reference build cannot ship a long-lived API token and call itself compliant. The June 2025 revision then separated the MCP server from the authorization server, treating the server as an OAuth 2.1 resource server. Tokens are issued by a separate identity provider the enterprise already runs.
Resource Indicators (RFC 8707) are the second non-negotiable. Access tokens must be bound to a specific MCP server, so a token minted for the analytics server cannot be replayed against the HR server. Everything past that point, from query budgets to policy semantics, audit schema, and tool-level allowlists, is implementation choice.
Three Databases, Three Authentication Shapes
Postgres accepts the cleanest pattern. The MCP server verifies the OAuth 2.1 access token, extracts the subject claim, and opens a pooled connection as a low-privilege service role. Before the query runs it issues SET ROLE for the user's mapped role, so row-level security policies evaluate against the real user. The service role has only SET ROLE privilege to a predefined set of user roles.
Snowflake pushes more work to the warehouse. External OAuth, configured as a security integration bound to the identity provider, lets the MCP server exchange the user's token for a Snowflake session. Row access policies and masking policies on the tables enforce identity-scoped reads without further query rewriting. Per-tenant warehouse scoping keeps one agent's runaway query from draining another's credits.
SQL Server is the awkward middle. Azure AD passthrough plus row-level security predicate functions work for Entra-first enterprises, but on-premises deployments typically fall back to a service account with EXECUTE AS LOGIN and a mapping table. In either case, the audit trail depends on capturing the resolved principal before the connection is reused, which is where the argument that access control cannot live in the prompt lands hardest at the database layer.

Service Accounts Versus On-Behalf-Of Tokens
The architecture choice downstream of authentication is whether the MCP server holds one service identity or forwards a user-scoped token. A service account is simpler to operate and faster to connect, but the database sees one principal for every query. Row-level policy has to be reimplemented at the MCP layer, which is the pattern the 2025 CVE disclosures repeatedly broke.
On-behalf-of flows push identity through to the database. The MCP server exchanges the user's token for a short-lived database credential (Snowflake external OAuth, Postgres JWT role mapping, Entra passthrough for SQL Server) and connects as that identity. Native row-level security does the work. The cost is connection pooling: pools are now keyed per user, which Cloudflare's enterprise MCP reference architecture solves with per-tenant isolation.
The hybrid pattern that holds up is a service-account connection with scoped identity for AI agents established via SET ROLE after verifying a signed assertion carrying the subject claim. It preserves pooling, keeps the audit trail honest, and matches the governance posture the spec revisions have been steadily tightening.
Cost Caps That Hold Under Adversarial Prompts
Agent loops produce expensive queries. Sometimes by accident, where a plan joins three fact tables to answer a yes/no question. Sometimes by prompt injection, where the agent is pointed at a SELECT * against the largest table in the warehouse. Cost caps belong in the server, not the prompt.
Three caps hold up. First, per-query timeout and row limit enforced at the database (statement_timeout in Postgres, resource monitors and STATEMENT_TIMEOUT_IN_SECONDS in Snowflake, query governor cost limits in SQL Server). Second, per-tool-call token budget enforced in the MCP server before results are returned to the model. Third, a daily spend ceiling per subject claim, exported to the audit stream in real time.
The pricing spread across models is why the token cap belongs beside the SQL cap. Output tokens run roughly five times input per million across every current Claude tier, so a server that returns 50 rows into a reasoning loop that then writes a 2,000-token reply pays the output rate on the bulk of the exchange. A ten-call loop at Opus rates turns one tool call into real spend.
Row-Level Policy and Signed Audit Output
Row-level policy is best evaluated at the database, not the server. An MCP server that filters rows after the query has already read what it was not supposed to see, and any crash dump, log tail, or process memory snapshot can leak it. Postgres RLS, Snowflake row access policies, and SQL Server security predicates differ in syntax but not in intent: the database decides what the current principal can read.
Signed audit output is the second half. Each tool call produces a structured record: request id, subject claim, resolved database principal, rendered SQL, rows returned, token budget consumed, model identifier. The record is hashed and signed with an HSM-backed key, then written to an append-only sink. Without it, a breach investigation cannot establish what the model saw, let alone which rows were disclosed to it.
The signed record is also what a compliance team hands to an auditor under GDPR Article 30 processing records, HIPAA accounting-of-disclosures requirements, or EU AI Act high-risk logging. Unsigned logs on a shared sink struggle under all three; a signed, append-only chain tied to the user's subject claim meets them with margin.
The Build-Versus-Buy Line
The CVEs in the official mcp-server-git are the compressed answer. The maintainers wrote the specification; three of the four joints above still produced live vulnerabilities inside a year. Writing an MCP server for one database is a weekend. Writing one that holds against hostile prompts on Postgres, Snowflake, and SQL Server, under SOC 2 and the EU AI Act, is a roadmap.
The build-versus-buy line sits at the policy engine and the signed audit sink, not the SQL. OAuth 2.1 plumbing is now standardised, and warehouse-specific syntax stays with whoever runs the warehouse. Policy evaluation and audit scale across databases and model vendors, and are the parts worth consolidating. A governed private AI platform supplies that control point; the enterprise keeps its tool definitions, schemas, and per-tool budgets, documented alongside its own internal MCP tooling. The numbers on enterprise AI breaches are the forcing function; the reference architecture is how the build stops being the source of the next one.
Eric Lamanna is Director of Business Development at Data.co, where he works with the teams who have to live with an AI deployment after the demo ends — the security, data and infrastructure owners deciding what is safe to connect this quarter. He came to enterprise software through digital sales and product work, with a long-running interest in automation and security, and writes about approvals and autonomy: how much an AI agent should be allowed to do on its own, and what has to be true before you let it.