How to Configure Pydantic AI SQL Server Sandbox

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up a pydantic ai sql server sandbox by creating a read-only database user. Then you pass those restricted credentials to your agent tool functions. Many teams start by giving their agent a broad admin account during testing. That shortcut lets a bad model prompt modify rows or drop a whole database. A safe setup scopes the database user to only the tables the agent needs. How to set up a pydantic ai sql server sandbox First, choose how your agent will log in to the database engine. A

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.

You set up a pydantic ai sql server sandbox by creating a read-only database user. Then you pass those restricted credentials to your agent tool functions. Many teams start by giving their agent a broad admin account during testing. That shortcut lets a bad model prompt modify rows or drop a whole database. A safe setup scopes the database user to only the tables the agent needs.

How to set up a pydantic ai sql server sandbox

First, choose how your agent will log in to the database engine. A database user represents the identity of a login inside one specific database. If the agent needs many databases on one instance, create a login in master. If the agent only needs one database, you can use a contained database user. A contained user has its own password and does not need a master login. An admin must turn on contained databases on the server and database first.

Next, assign the right fixed database role to your new agent user. Every database user starts in the public role, which grants basic connect rights. To let the agent read rows, add the user to the db_datareader role. Members of db_datareader can read all data from all user tables and views. You can also add the user to db_denydatawriter to block all data changes. Members of db_denydatawriter cannot add, change, or delete data in any user table. Never put an AI agent user into the db_owner fixed database role. Members of db_owner can run any maintenance task and even drop the database.

Finally, wire the database session into your agent dependency object in Python. Your agent framework uses typed tools to run queries and return structured results. Keep the database password in your secret store rather than in agent code. When the model asks for data, the tool runs the query under that role.

Why native roles alone leave gaps in agent sandboxes

Fixed database roles solve the first part of securing an AI agent workflow. They check who logs in and enforce broad read or write rules on tables. Still, the permissions assigned to fixed database roles cannot be changed in SQL Server. Because db_datareader grants read rights to all user tables, the agent sees everything.

AI Sandbox Environments + Kubernetes API Server Access: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

That broad read access creates a real risk when models query production schemas. A user table might hold customer emails, phone numbers, card numbers, or national IDs. If an agent queries that table, those sensitive fields enter the model prompt context. Adding a user to db_denydatareader blocks all reads across every user table and view. Native roles decide who logs in, not what individual statements do in flight.

Managing sensitive data and query limits for AI tools

To protect specific columns, database admins must build custom views or table grants. Doing this by hand across dozens of tables takes a lot of team time. Schema changes also break custom views when developers add new columns to tables. Teams also need a clear log of which prompt triggered each SQL query. Standard database logs show the shared agent user, not the end user or prompt. When you pair native read-only roles with field masking, you close this gap. Masking rewrites matched fields like emails or card numbers in query results before return.

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

What database role is safest for a read-only AI agent?

Combine the db_datareader role with the db_denydatawriter role for your agent database user. This pairing lets the agent read user tables while blocking any data writes.

Should I use a standard login or a contained user for my sandbox?

Use a contained database user if the agent only queries one or a few databases. It keeps the user isolated from the master database on your SQL Server instance.

Can I edit the permissions of the db_datareader role to hide one table?

No, SQL Server does not let you change permissions on fixed database roles. You must create a custom role or grant permissions on specific tables and views.

The query runs. The emails stay hidden.