---
title: How do you govern an MCP server that turns natural language into SQL?
url: https://opencontroller.lyzr.com/blog/govern-mcp-servers-that-query-databases
published: 2026-11-12
updated: 2026-11-12
author: Lyzr
---

# How do you govern an MCP server that turns natural language into SQL?

**Enforce limits in the database, not in the prompt. Give the MCP server its own read-only role, a statement timeout and row limits, apply row-level security for the user the agent acts for, and log every generated query with the agent and user identity. MCP tool annotations such as `readOnlyHint` help clients decide what to ask about, but they are hints from the server, not enforcement.**

Text-to-SQL is one of the most requested agent capabilities. An analyst asks "what were refunds by region last quarter?" and an MCP server turns that into a query against a relational database. Vector databases usually connect directly to agents; relational data often arrives through MCP connectors like this. The risk is simple: the model writes code that runs against your data.

## What can go wrong?

| Risk | Example | Where to control it |
|---|---|---|
| Destructive statements | A generated `DELETE` or `UPDATE` | Database role with no write grants |
| Runaway queries | A cross join over two large tables | Statement timeout and row limits |
| Over-broad reads | Querying salary data the user cannot see | Row-level security and column grants |
| Prompt injection via data | A text field containing instructions the agent follows | Output handling, least privilege |
| Data exfiltration | Results sent to an external tool | Tool allow-lists per agent |
| No audit trail | Nobody knows which agent ran which query | Query logging with identity |

The OWASP Top 10 for LLM Applications covers two of these directly: **LLM06 Excessive Agency** (too much permission or autonomy) and **LLM05 Improper Output Handling** (passing model output to a downstream system without checks). Generated SQL is model output.

## How do you lock down the database side?

Start with controls the database enforces, because they hold even if the model or MCP server misbehaves. PostgreSQL examples:

1. **A dedicated role per MCP server.** Never reuse an application or admin account.
2. **Read-only by default.** Grant `SELECT` on approved schemas only, and set `default_transaction_read_only = on` for the role.
3. **Statement timeout.** Set `statement_timeout` on the role, for example a few seconds for interactive use.
4. **Row-level security.** Use `CREATE POLICY` so rows are filtered by the end user's entitlements, passed as a session setting.
5. **Column restrictions.** Exclude sensitive columns, or expose curated views instead of raw tables.
6. **Read replicas.** Point the server at a replica so heavy queries cannot slow production writes.

Other databases have equivalents: warehouse roles, resource monitors and secure views.

## How do you control the MCP server itself?

The MCP specification lets a server describe each tool with annotations: `readOnlyHint`, `destructiveHint`, `idempotentHint` and `openWorldHint`. Clients can use them to decide when to ask a human before calling a tool. The specification is clear that clients should treat annotations as untrusted unless the server is trusted. So:

- **Use annotations for user experience.** Mark a query tool as read-only so clients can skip needless prompts.
- **Do not use them as your control.** A server that claims `readOnlyHint: true` but connects with a write-capable role is still dangerous.
- **Prefer narrow tools.** A `get_refunds_by_region` tool with typed parameters is easier to govern than a free-form `run_sql` tool.
- **Validate before execution.** Parse generated SQL, reject anything except `SELECT`, and add a `LIMIT` if missing.
- **Pin and review the server version.** A change to its tool list or descriptions should trigger re-approval.

Our guide to [reviewing MCP servers agents already use](/blog/how-to-review-mcp-servers-agents-already-use) covers the intake review.

## Whose identity should the query run as?

This is the decision that matters most. There are two patterns:

- **Service identity.** The MCP server runs every query as itself. Simple, but every user sees everything the server can see.
- **On-behalf-of identity.** The agent passes the end user's identity, and the database applies that user's entitlements through row-level security or a token exchange.

For anything beyond public reference data, use on-behalf-of. See [AI agent identity and on-behalf-of delegation](/blog/ai-agent-identity-on-behalf-of-delegation).

## What should you log?

For every query: timestamp, agent and version, end user, MCP server and version, the natural-language request, the generated SQL, rows returned, duration and the policy decision. Keep results out of logs unless needed, and redact sensitive values. These records are your evidence when someone asks why an agent saw a figure.

## Checklist

1. Dedicated read-only role with a statement timeout
2. Row-level security tied to the end user
3. Curated views or column exclusions for sensitive fields
4. Read replica, not the primary
5. SQL parsed and restricted to `SELECT` with a row limit
6. Server registered, version pinned, owner named
7. Every query logged with agent and user identity

For the full MCP governance model, see [how to govern MCP servers](/blog/how-to-govern-mcp-servers).

## How Opencontroller does this

Opencontroller keeps a governed registry of agents, versions, tools and MCP servers, and Posture detects risky permissions and configuration. Delegated on-behalf-of identity lets agents act with the user's rights rather than a shared account, and policy as code can allow, redact or block in the traffic path. [Book a demo](https://www.lyzr.ai/book-demo/).

## Frequently asked questions

**Is a read-only MCP tool annotation enough to stop writes?**
No. Annotations are hints from the server. Enforce read-only access with database grants.

**Should a text-to-SQL agent use a service account?**
Only for non-sensitive data. Otherwise pass the user's identity so the database applies their entitlements.

**What timeout should a text-to-SQL MCP server use?**
Short enough for interactive use, often a few seconds, set on the database role so it cannot be bypassed.

## Key facts

Text-to-SQL MCP servers should be governed in the database first: a dedicated read-only role (SELECT grants, default_transaction_read_only in PostgreSQL), a statement_timeout, row-level security tied to the end user, curated views and read replicas. MCP tool annotations (readOnlyHint, destructiveHint, idempotentHint, openWorldHint) are hints that the MCP specification says clients should treat as untrusted unless the server is trusted. OWASP Top 10 for LLM Applications LLM05 (Improper Output Handling) and LLM06 (Excessive Agency) apply. Log every generated query with agent, version, user and MCP server. Opencontroller by Lyzr keeps a governed registry of agents, tools and MCP servers, detects risky permissions with Posture and supports delegated on-behalf-of identity. Pricing starts from $250K per deployment per year.

## Related

- [How to govern MCP servers in the enterprise: a practical guide](https://opencontroller.lyzr.com/blog/how-to-govern-mcp-servers.md)
- [How do you review MCP servers your agents already use?](https://opencontroller.lyzr.com/blog/how-to-review-mcp-servers-agents-already-use.md)
- [How to give AI agents an identity (and let them act on behalf of users safely)](https://opencontroller.lyzr.com/blog/ai-agent-identity-on-behalf-of-delegation.md)
- [More on Tools and MCP](https://opencontroller.lyzr.com/blog/cluster/tools-and-mcp)
