How to Configure Pydantic AI MySQL Least Privilege

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up pydantic ai mysql least privilege by creating a restricted database user. Then you pass that user key to your agent dependencies at runtime. Many engineering teams rush this step during early testing. They give the agent a shared root account or a broad write role. That shortcut lets a bad prompt alter tables, delete rows, or read secret columns. A tight role blocks those mistakes before the database runs the query. How pydantic ai mysql least privilege works Pydantic AI uses a cle

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 mysql least privilege by creating a restricted database user. Then you pass that user key to your agent dependencies at runtime. Many engineering teams rush this step during early testing. They give the agent a shared root account or a broad write role. That shortcut lets a bad prompt alter tables, delete rows, or read secret columns. A tight role blocks those mistakes before the database runs the query.

How pydantic ai mysql least privilege works

Pydantic AI uses a clean dependency injection system to manage external tools. You define a database pool and pass it into the agent context. The agent uses registered tool functions to run queries through that pool. The database itself enforces all permission rules based on the active login role. Your Python code does not decide which tables the database user can read. MySQL checks the user grant table every time the agent sends a statement.

To build a safe setup, start inside your MySQL server. Create a new database account that only your AI agent will use. Do not reuse the account that runs your main backend app or migrations. Next, grant read rights only on the exact tables the agent needs. If the agent only answers support questions, grant access to order and ticket views. Leave out billing tables, password hashes, and internal audit logs.

You can also restrict privileges down to the individual column level in MySQL. Grant the agent user permission to read order status and creation dates. Withhold access to columns that hold emails, phone numbers, or card numbers. Database views offer another clean way to limit what the agent can query. You build a view that joins safe columns and hides sensitive rows. Then you grant the agent user read rights on that single view alone.

Structuring agent tools and query limits

Database grants are your strong first line of defense, but tool design also matters. Avoid giving your agent a single tool that runs raw SQL strings. When an agent writes raw SQL, it can craft heavy joins or full scans. Instead, write small tool functions that accept typed inputs through Pydantic models. Each tool runs a fixed query template with safe parameters.

You should also set resource limits on the MySQL account itself. MySQL lets you cap the number of queries and updates per hour. You can also limit how many connections the agent account can open at once. Set a short statement timeout so a bad query cannot lock your tables. These native server limits stop a runaway agent loop from taking down production.

Least Privilege Principle + MySQL Access Governance: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

What native grants solve and what keeps the session active

Native MySQL roles do a great job of blocking unauthorized table access. They stop an agent from dropping tables or reading restricted schema areas. If a prompt injection tries to run a delete command, MySQL rejects it. This static boundary is essential for every production agent deployment.

Still, static grants only check who logs in and which table they touch. They cannot inspect what individual statements do in flight or redact live results. If an agent needs access to a customer table, it gets the whole column. Native grants cannot rewrite emails or card numbers inside query results on the fly. You also have to rotate and store static database keys across every agent host. Also, standard database logs do not tie queries back to specific user prompts.

Check MySQL queries and results on the wire

MySQL grants control who logs in. They do not check single queries or mask sensitive columns once a session starts. With hoop.dev, you run an open-source proxy, a sidecar for your MySQL. It runs next to MySQL, RDS, or Aurora and reads the MySQL wire protocol.

Multi-statement batches and prepared queries are split and checked one by one by hoop.dev guardrails. When a rule refuses a query, MySQL returns a native error packet with your message. On the response path, hoop.dev data masking rewrites string values in returned rows that match your rules. Every query and decision is logged per resource. See the MIT-licensed project on GitHub.

Frequently asked questions

Should a Pydantic AI agent ever have write permissions in MySQL?

Most agents should only hold read permissions on safe tables or views. If your agent must write data, restrict it to a dedicated staging table. Never allow an agent to update core production records without a human review step.

How do database views help with MySQL least privilege for AI agents?

Views let you expose a filtered slice of your data without copying tables. You grant the agent user permission to query the view instead of base tables. This keeps sensitive columns and deleted records out of the agent query path.

Why are typed tool inputs safer than raw SQL execution tools?

Raw SQL tools let the model invent any query structure it wants. Typed tools force the model to pass validated fields into a fixed query template. This prevents syntax errors, heavy table scans, and prompt injection tricks.

The query runs. The emails stay hidden.