How to Configure Pydantic AI MySQL MCP Server

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up a pydantic ai mysql mcp server by linking your agent to a database process. The agent sends tool calls over standard input or network ports to run SQL queries. Many teams start by giving the server a broad database user role for fast testing. This choice creates high risk if the agent runs a bad query or reads sensitive rows. How a pydantic ai mysql mcp server works The setup uses two main parts that talk over the Model Context Protocol. Your Python app runs the agent and holds the

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 mysql mcp server by linking your agent to a database process. The agent sends tool calls over standard input or network ports to run SQL queries. Many teams start by giving the server a broad database user role for fast testing. This choice creates high risk if the agent runs a bad query or reads sensitive rows.

How a pydantic ai mysql mcp server works

The setup uses two main parts that talk over the Model Context Protocol. Your Python app runs the agent and holds the logic for your task. The database server process waits for requests and talks to your MySQL instance. When you start the app, the agent asks the server for a list of tools. The server replies with tools that can list tables, check schemas, and run SQL.

You must pass database login details to the server process when it starts. Teams usually store the host name, user name, and password in local secret files. The server uses these details to open a pool of database sessions. When a user asks the agent a question, the model picks a database tool. The agent checks the tool inputs with strict type rules before sending the request.

Next, the server takes that request and runs the SQL statement against MySQL. MySQL checks if the database user role has the right to read that table. The database sends the rows back to the server process. The server formats the rows as text and sends them back to the agent. Finally, the agent reads the data and writes an answer for the user.

Why you should restrict database roles for AI agents

Database roles are your first line of defense when an agent runs queries. If you use an admin account, the model can drop tables or change data. You should create a dedicated MySQL user just for your AI app. Give this user read permissions only on the specific tables the agent needs. Do not let this user read system tables or secret user records.

You can also use database views to limit what columns the agent can query. For example, a view can leave out password hashes from a customer table. The MySQL user then gets permission to read the view instead of the base table. This step keeps raw login keys out of the agent context window. You should also set query time limits on the MySQL user account. Time limits stop a bad join from locking up your database server.

MySQL Access Governance + Kubernetes API Server Access: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

What native database roles do not solve

Native MySQL roles decide who can log in and which tables they can read. Still, static roles cannot check what a single SQL statement does in flight. A read-only user can still dump a whole customer table in one query. If that table holds emails, phone numbers, or card numbers, those values go to the model. Standard database views require manual work to maintain across hundreds of tables.

Static credentials also stay on the machine that runs the server process. If a developer runs the agent on a laptop, that laptop holds a live database key. Database logs only show the shared role name, not which person or prompt triggered the query. When an agent runs a bad write command, nobody gets a prompt to approve it first.

Add wire guardrails and masking beside MySQL

Database grants and TLS setup decide who can reach MySQL. Even so, an open session can run broad updates or read raw personal data from allowed tables. In practice, hoop.dev runs beside the database as a sidecar for your MySQL. It keeps your login setup and drivers as they are while checking queries on the wire.

With hoop.dev MySQL support, guardrails refuse bad queries before they run and return a readable MySQL error. Data masking rewrites matched sensitive values in result rows in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for GDPR, HIPAA, and PCI DSS programs. Read the docs and source on GitHub.

Frequently asked questions

How does the agent validate tool calls before they reach MySQL?

The agent framework checks that the model output matches the expected data types. It rejects the tool call if a required field is missing or has the wrong type. Still, type checks do not inspect the SQL text itself for unsafe commands.

Should I give the MCP server write access to MySQL?

Most teams start with read-only access to keep production data safe from mistakes. If your app must write data, create a separate role scoped to one table. You should also require a human to approve any write or delete action before it runs.

How can I protect sensitive fields like emails or card numbers in query results?

You can create custom MySQL views that leave out sensitive columns, though views take time to manage. Another approach is to use data masking that rewrites matched fields in results before they reach the agent. This keeps emails, phone numbers, and national IDs out of external model logs.

The query runs. The emails stay hidden.