How to Configure Pydantic AI SQL Server Credentials
You set up pydantic ai sql server credentials by creating a database user and passing its details to your agent. Your Python app then uses a database driver to open a session and run queries. Many teams start by giving the agent a shared admin account to move fast. Broad permissions let an AI agent change or delete table rows by mistake. A safer path uses strict database roles that limit what the agent can do. How pydantic ai sql server credentials work in practice Pydantic AI agents use depe
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 pydantic ai sql server credentials by creating a database user and passing its details to your agent. Your Python app then uses a database driver to open a session and run queries. Many teams start by giving the agent a shared admin account to move fast. Broad permissions let an AI agent change or delete table rows by mistake. A safer path uses strict database roles that limit what the agent can do.
How pydantic ai sql server credentials work in practice
Pydantic AI agents use dependency injection to pass database sessions into tool functions. When the agent needs data, it calls a tool that holds your SQL Server login details. Your app reads these details from secret stores and opens a connection pool.
On the database side, SQL Server splits identity into logins and database users. A login lives in the master database and lets a user reach many databases on one instance. A database user represents that login inside one specific database. Teams that only need one or two databases can use a contained database user instead. This user type holds a password inside the database and does not need a master login. An admin must first turn on contained databases at the server and database levels. SQL Server offers thirteen types of database users to match different identity setups.
Setting up read-only roles for agent tools
Most AI agents only need to read data to answer user questions. SQL Server includes fixed database roles that help teams set up read-only access fast. Every user belongs to the public role, and all roles hold the basic right to connect. The fixed reader role lets members read all data from all user tables and views. If you want to block writes, you can also add the fixed deny writer role. That role stops the user from adding, changing, or deleting data in any user table.
You cannot change the permissions built into SQL Server fixed database roles. You should never give an AI agent the database owner role. Database owners can change settings, run maintenance tasks, and even drop the whole database. If thefixed reader role is too broad, admins can grant read rights on specific tables instead. You can also use a deny reader role to block access to all user tables and views.
Ephemeral Credentials + Kubernetes API Server Access: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
What native database roles solve and what they miss
Native database roles do a great job of deciding who can log into the server. A read-only user ensures that a confused agent cannot drop tables or wipe out records. It also keeps your setup simple because SQL Server checks these rules at login time.
Still, static roles cannot inspect what an individual query does while it runs. A user in the fixed reader role can still read every column in every user table by default. If a table holds emails, phone numbers, card numbers, or national IDs, the agent gets them all. Native roles also share one database user across every person who talks to your agent. Database logs will show the agent account name instead of the person who asked the question. Finally, your app still holds raw database passwords in memory to open the session.
Check SQL Server batches and results on the wire
SQL Server logins and roles decide who connects. They do not check what each TDS batch does or mask sensitive values in returned rows. With hoop.dev, you run an open-source proxy, a sidecar for your SQL Server. It reads plain batches and parameterized queries on the TDS wire while passing Windows login through untouched.
Before a batch hits the server, hoop.dev guardrails check the statement against your rules. Disallowed actions get a native TDS error with your message. On the way back, hoop.dev data masking rewrites sensitive values in supported result columns that match your rules. Every query and decision is logged. Explore the MIT-licensed project on GitHub.
Frequently asked questions
Should I use a master login or a contained user for my AI agent?
Use a contained database user if the agent only needs data from one database. It keeps the password inside that database and avoids extra setup in the master database. Use a standard login if the agent must query many databases on the same server instance.
Can the fixed reader role hide sensitive columns from Pydantic AI?
No, the fixed reader role grants read access to all user tables and views at once. You cannot alter the permissions inside a fixed role. To block specific columns, you must avoid the fixed role and grant table permissions by hand.
How do I stop an agent from changing data by mistake?
Remove the agent user from the fixed writer and owner roles right away. Add the user to the fixed deny writer role so SQL Server blocks all data changes. This rule holds true even if another role tries to grant write access later.
Related guides
In this post