How to connect a database to your AI agent
Short answer: add the database to swarmvia as a source with a read-only role, then add https://swarmvia.com/mcp to your agent. The agent reads the schema and runs SQL through swarmvia. It never receives the password, and each query is limited, attributed to a person, and logged. Works with PostgreSQL, MySQL, and JDBC databases like Snowflake and NetSuite.
Agents write better SQL when they can see your real tables. The usual way to
get there is a database MCP server with a connection string in
claude_desktop_config.json or .cursor/mcp.json. That works for one person
on one laptop. It stops working the moment a team does it.
This page shows the shape that holds up: the database is a swarmvia source, the agent never sees the password, and every query is limited, attributed, and logged.
Why not put a connection string in your MCP config?
A connection string pasted into an MCP config file is:
- On every laptop. Each person who wants the agent has a plaintext copy of the password.
- Unrotatable in practice. Rotating it means updating every copy. Offboarding one person means rotating it for everyone.
- Unattributed. The database sees one user. "Who ran this query against production?" has no answer.
- Unbounded. Twelve agents deciding at 9am to scan
ordersis a load pattern your read replica has never seen, and nothing stops it.
The same applies to every secret an agent touches, not just database passwords. See How to stop sharing credentials with AI agents.
How does an agent query a database without the password?
- You create a read-only role in the database and add the database to swarmvia as a source with that role. The credential never leaves swarmvia.
- The agent connects to one MCP URL,
https://swarmvia.com/mcp, with OAuth. You approve which sources it may use. - The agent mints a short-lived session key, reads the schema with
source_schema, and runs SQL withsource_query. - Each query waits for a slot under the source's concurrency limit, runs in a read-only transaction as your role, and lands in the source's logs with the person and agent that ran it.
Offboarding is revoking one grant. Rotation is changing one credential.
Set it up
1. Create a read-only role
The role is the security boundary. swarmvia does not rewrite or filter your SQL, so grant exactly what the agent should read and nothing else. The exact statements differ by engine:
2. Create a database source
In the dashboard, go to Sources → New source, pick Postgres database, MySQL database, or JDBC data source, and enter the connection details and the role's credentials. The database must be reachable from the public internet over TLS.
Click Test connection. It opens a real connection and tells you which stage failed (DNS, TLS, authentication, missing database) or, on success, how many tables the role can see.
3. Connect your agent
Add the MCP server once. In Claude Code:
claude mcp add --transport http swarmvia https://swarmvia.com/mcpIn Cursor, .cursor/mcp.json:
{
"mcpServers": {
"swarmvia": {
"type": "http",
"url": "https://swarmvia.com/mcp"
}
}
}There is no token to paste. The first tool call opens an approval screen where you pick the sources the agent may use. VS Code, Codex, and Claude.ai are covered in Connect your agent.
4. Ask a question
Try: "List the sources I granted, start a session on the warehouse source, and tell me how many orders shipped to Canada last month." The agent reads the schema first, so it uses your real column names.
Scripts and CI
A database source answers plain HTTPS at its endpoint, so non-agent callers use the same limits and logs.
Give the job a Control API token so it mints a short-lived access key
(sv_at_…) and refreshes it before it expires
(how), then send SQL:
curl https://warehouse.swarmvia.cloud \
-H "X-Swarmvia-Key: $ACCESS_KEY" \
-H "content-type: application/json" \
-d '{"queries": [{"sql": "select count(*) from orders"}]}'When not to use this
A database source is for agents, analysts, and scheduled analytical work: few queries, each one valuable. It is not a replacement for your application's connection string. Production app traffic needs connection reuse and sub-millisecond overhead, which is what pgbouncer, RDS Proxy, and Hyperdrive are for.