How to Configure Pydantic AI MySQL Permissions

Published
Length
3 min read

Luis Felipe Dias

All posts

To set up pydantic ai mysql permissions, create a dedicated database user with read-only grants. Then pass that user account into the agent dependency context at runtime. Many teams start by reusing a shared app login for their AI agent. That shortcut gives the model full write and delete rights across every table. A bad prompt or model mistake can then wipe out live rows in seconds. How pydantic ai mysql permissions work in practice Pydantic AI uses a dependency system to pass database clien

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 pydantic ai mysql permissions, create a dedicated database user with read-only grants. Then pass that user account into the agent dependency context at runtime. Many teams start by reusing a shared app login for their AI agent. That shortcut gives the model full write and delete rights across every table. A bad prompt or model mistake can then wipe out live rows in seconds.

How pydantic ai mysql permissions work in practice

Pydantic AI uses a dependency system to pass database clients into agent tool functions. The framework itself does not manage database users or check SQL rules. Instead, your Python app opens a MySQL session using a standard driver. The agent calls a registered tool when it needs data from the database. That tool runs the query through the active MySQL session.

To keep this safe, admins create a dedicated MySQL account just for the agent. They grant select rights only on the specific tables or columns the agent needs. If the agent must write data, admins limit insert rights to one staging table. They also set query time limits and max connection counts on that MySQL user. Finally, the app loads the database key from a secret store at startup.

What native MySQL grants solve and what they miss

Native MySQL grants stop an agent from touching tables outside its allowed list. They also block drop, delete, or alter statements when the account is read-only. This setup decides who can log in and which objects they can read. For static app code with fixed queries, table and column grants work well.

Still, AI agents often write dynamic SQL or call tools based on user prompts. A read-only grant still lets the agent pull every row from an allowed table by default. If that table holds emails, phone numbers, or card numbers, the model gets them. Native MySQL roles cannot rewrite matched fields in results on the fly. They also cannot pause a risky tool call for human review before it runs. Also, database logs only show the shared agent user, not the end user.

AI Agent Permissions + MySQL Access Governance: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Why runtime query checks matter for agent tools

When an agent serves many users, every query shares the same MySQL login. You lose track of which person or prompt triggered a heavy table scan. Teams need a way to inspect each statement right before the database runs it. They also need data masking that replaces national IDs or emails in query output. Adding these checks at the session level closes the gap left by static grants.

Inspect every MySQL query and response in flight

Static grants and network rules block bad logins. They cannot pause a risky query for review or mask emails in a result set. By design, hoop.dev is an open-source proxy that runs as a sidecar for your MySQL. It reads plain queries, prepared statements, and cursors on the wire.

First, hoop.dev guardrails block banned actions before MySQL runs them. An AI Analyzer you configure can score intent, and hoop.dev data masking rewrites sensitive string columns in results. Every query and verdict goes to the resource audit trail. Find the open-source binary on GitHub.

Frequently asked questions

Should a Pydantic AI agent use the main app database user?

No, never share the main app login with an AI agent. Create a separate MySQL user with read-only access to specific tables only. This limits damage if a prompt tricks the agent into running bad queries.

Can MySQL column grants hide sensitive data from an agent?

Column grants block access to a whole column for every row in a query by default. They cannot mask just the emails or card numbers inside a mixed text field. You need runtime data masking to rewrite matched fields in results dynamically.

How do you stop an agent from running slow MySQL queries?

Set a maximum execution time on the MySQL user account used by the agent. You should also add row limits inside the Python tool function code. Both steps keep a bad query from locking up your database server.

The query runs. The emails stay hidden.