How to connect MySQL to your AI agent
Short answer: create a MySQL user with SELECT on the databases you want to share, add MySQL to swarmvia as a database source with that user, and add https://swarmvia.com/mcp to Claude, Cursor, or Codex. The agent reads the schema and runs read-only SQL with ? parameters, and never sees the password. Works with MariaDB, Aurora MySQL, PlanetScale, and TiDB.
You want Claude, Cursor, or Codex to query your MySQL data, using real table and column names instead of guesses. You do not want to hand each agent the database password.
This guide adds MySQL to swarmvia as a source and connects your agent to it. The agent gets the schema and read-only queries. The credential never leaves swarmvia, and every query is limited and logged. For the same approach to API keys and MCP tokens, see How to stop sharing credentials with AI agents.
It works with anything that speaks the MySQL wire protocol: MySQL, MariaDB, Amazon Aurora MySQL, PlanetScale, and TiDB.
Create a read-only user
Create a user with SELECT on the databases you want the agent to see:
CREATE USER 'swarmvia_ro'@'%' IDENTIFIED BY '…';
GRANT SELECT ON app.* TO 'swarmvia_ro'@'%';
FLUSH PRIVILEGES;MySQL has no separate schema usage grant. GRANT SELECT ON app.* is what lets
the user see the tables. To expose only part of a database, grant SELECT on
specific tables, or on a view that leaves out the columns you do not want
shared:
CREATE VIEW app.customers_public AS
SELECT id, country, created_at FROM app.customers;
GRANT SELECT ON app.customers_public TO 'swarmvia_ro'@'%';The user's grants are the security boundary. swarmvia runs every query in a read-only transaction, but it does not rewrite or filter SQL.
Create the source
- Go to Sources → New source and choose MySQL database.
- Enter the public hostname, port, database, and the
swarmvia_rocredentials. The host must be a hostname, not an IP address. - Leave TLS on Verify certificate. PlanetScale usually requires it.
- Click Test connection. It reports which stage failed, or how many tables the user can see.
The database must be reachable from the public internet over TLS.
Connect the agent
Add one remote MCP server. Claude Code:
claude mcp add --transport http swarmvia https://swarmvia.com/mcpCodex:
codex mcp add swarmvia --url https://swarmvia.com/mcpApprove the grant in the browser and pick the MySQL source. Cursor, VS Code, and Claude.ai are covered in Connect your agent.
The agent reads the schema with source_schema, sizes tables with
source_count_rows, and runs SQL with source_query.
MySQL query details
Parameters use ? placeholders and are bound, not interpolated:
curl https://shop-db.swarmvia.cloud \
-H "X-Swarmvia-Key: $ACCESS_KEY" \
-H "content-type: application/json" \
-d '{
"queries": [
{
"sql": "select status, count(*) as n from orders where created_at > ? group by status",
"params": ["2026-01-01"]
}
],
"schema": "app"
}'schemarunsUSEfor that database before the query.- Cast when the inferred type matters:
CAST(? AS DATE),CAST(? AS JSON). - A
$1placeholder is Postgres syntax. On a MySQL source it is a literal. - Up to 5 statements per request, each isolated: one failing does not break the others.
Agents send the same body through source_query, so this is also what to
check when an agent's query fails.
Why not just share the MySQL password?
- The password in one place. Rotate it once, not on every laptop.
- Attribution. Logs show each statement, rows returned, and the key that ran it.
- A concurrency limit. Your primary or replica sees at most as many agent queries as you allow.
- Instant offboarding. Disconnect one agent under Settings → Connected agents.
See Database sources for limits, caps, and error codes.