How to Configure OpenAI Agents SDK SQL Server Least Privilege

Published
Length
3 min read

Luis Felipe Dias

All posts

To set up openai agents sdk sql server least privilege, create a dedicated database user. Then grant that user read rights only on the tables your agent needs. Many teams start by giving their agent a broad admin or full reader role. This shortcut lets the agent run any query across the whole database. If a prompt goes wrong, the agent can read private rows or drop tables. Scoping permissions at the database level stops the agent from touching unauthorized objects. How openai agents sdk sql se

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

To set up openai agents sdk sql server least privilege, create a dedicated database user. Then grant that user read rights only on the tables your agent needs. Many teams start by giving their agent a broad admin or full reader role. This shortcut lets the agent run any query across the whole database. If a prompt goes wrong, the agent can read private rows or drop tables. Scoping permissions at the database level stops the agent from touching unauthorized objects.

How openai agents sdk sql server least privilege works

The OpenAI Agents SDK uses Python functions or external servers as tools for models. When an agent needs data, it calls a tool that opens a database connection. That tool logs in using a SQL Server identity that you set up. In SQL Server, a login in the master database maps to a database user. You can also use a contained database user with a password for one database. An admin must enable contained databases on the server and database first. Microsoft provides thirteen types of database users to match different identity setups.

Many teams place the agent user into the fixed reader role. Still, that role lets the user read all data from every user table. You cannot change the permissions assigned to fixed database roles in SQL Server. Even worse, the owner role can change settings and drop the whole database. Instead, create a custom role for the agent and grant select on specific views. You can also use the deny writer role to block data changes.

Why fixed database roles are not enough for agent tools

Building a custom database role is a strong first step for any team. It stops the agent from reading tables outside its assigned schema or altering data. If the model generates a delete statement, the database engine blocks the action right away. This setup controls which tables and views the agent account can reach.

Still, database roles only check who logs in and which tables they touch. They do not inspect what an individual query does while it runs in flight. An agent with select rights on a customer table can still pull every row. That query results in a massive data dump into the model context window. Also, SQL Server permissions do not rewrite sensitive fields inside an allowed table. If a permitted table holds emails, phone numbers, or card numbers, the query returns them. Static credentials stored in the agent app env files also create risk for teams. If an attacker steals that password, they get the same database access as the agent.

Least Privilege Principle + Kubernetes API Server Access: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

How to scope tables and views for agent queries

Start by listing the exact questions your agent needs to answer for users. Map those questions to the minimum set of columns and tables required. Create dedicated SQL views that include only those safe columns for the agent. Leave out columns with national IDs, emails, phone numbers, or card numbers. Next, create a fresh SQL user dedicated to this single agent workload. Do not share this user account with web apps or human analysts.

Create a custom database role and add your new agent user to it. Grant select permissions to that role on your safe views only. Never grant select permissions on the underlying base tables to the agent role. Add the user to the deny data writer role for extra safety. Test the setup by running a select query against a base table. Confirm that SQL Server rejects the query before you deploy the agent tool.

Govern SQL Server queries and responses in flight

Table grants decide which SQL Server objects a caller may touch. They do not check query intent or rewrite sensitive values in results across every client and agent. By design, hoop.dev is an open-source proxy that sits next to SQL Server as a sidecar for your SQL Server. It inspects TDS batches and parameterized queries in real time.

With hoop.dev guardrails and hoop.dev data masking, banned statements are refused with your message. Matched sensitive fields in supported result columns are masked in memory, and each decision is logged per listener. Check out the code on GitHub.

Frequently asked questions

Should I use the built-in reader role for an AI agent?

No, the built-in reader role grants read rights on all user tables and views. You cannot edit fixed role permissions, so create a custom role for specific views instead.

What is the difference between a SQL login and a contained database user?

A standard SQL login lives in the master database and works across many databases. A contained user stays inside one database after an admin enables containment on the server.

Can database roles stop an agent from reading sensitive columns in allowed tables?

Table roles alone return all columns, so you must use scoped views or column permissions by default. Even then, native roles will not rewrite emails or card numbers in query results.

The query runs. The emails stay hidden.