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

  1. Go to Sources → New source and choose MySQL database.
  2. Enter the public hostname, port, database, and the swarmvia_ro credentials. The host must be a hostname, not an IP address.
  3. Leave TLS on Verify certificate. PlanetScale usually requires it.
  4. 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/mcp

Codex:

codex mcp add swarmvia --url https://swarmvia.com/mcp

Approve 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"
  }'
  • schema runs USE for that database before the query.
  • Cast when the inferred type matters: CAST(? AS DATE), CAST(? AS JSON).
  • A $1 placeholder 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.

Try it on your own data

Free to start, no credit card. Connect an agent in a few minutes.

Frequently asked questions

Does it work with PlanetScale and MariaDB?
Yes. A MySQL source works with anything that speaks the MySQL wire protocol, so MySQL, MariaDB, Aurora MySQL, PlanetScale, and TiDB all work with the same user and grant shape. PlanetScale usually needs TLS verification left on.
How do query parameters work on MySQL?
Use ? placeholders and pass values in params. They are bound, not interpolated. A $1 placeholder is Postgres syntax and is treated as a literal on a MySQL source.
What grants does the MySQL user need?
Only SELECT on the databases you want the agent to see, for example GRANT SELECT ON app.* TO the user. MySQL has no separate schema usage grant.
Can the agent see which tables exist?
Yes. The source_schema tool lists the databases, tables, and columns the user can see, so the agent writes SQL against real column names instead of guessing.

In the docs

More solutions

Last updated