How to connect an AI agent to a database

Don't hand the agent a connection string. Put a read-only MCP server between them: the agent reads the schema and runs SELECT queries, the password stays out of its reach, and every call is logged.

Short answer: connect the agent to the database through an MCP server, not a raw connection. With AI2SQL you add the database once with a read-only user. Chat assistants and code editors then connect with a URL and a sign-in, and your own agents connect to https://builder.ai2sql.io/api/mcp with an API key. Every agent gets the same three tools: list_connections, describe_schema and run_query.

Which URL each agent uses

Agent Connect with Auth Guide
Claude (claude.ai, Desktop, Claude Code) https://builder.ai2sql.io/api/claude-mcp OAuth sign-in Setup
ChatGPT AI2SQL app in the plugin directory OAuth sign-in Setup
Cursor, VS Code, Gemini CLI, Devin Desktop https://builder.ai2sql.io/api/chatgpt-mcp OAuth sign-in Setup
Your own agent (OpenAI Agents SDK, Claude API, LangChain, n8n, scripts) https://builder.ai2sql.io/api/mcp API key header Setup

Step 1: a database user that can only read

-- PostgreSQL
CREATE ROLE agent_ro LOGIN PASSWORD 'a-long-random-password';
GRANT USAGE ON SCHEMA public TO agent_ro;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO agent_ro;
ALTER ROLE agent_ro SET default_transaction_read_only = on;

-- MySQL
CREATE USER 'agent_ro'@'%' IDENTIFIED BY 'a-long-random-password';
GRANT SELECT ON shop.* TO 'agent_ro'@'%';

-- SQL Server
CREATE LOGIN agent_ro WITH PASSWORD = 'a-long-random-Passw0rd!';
USE AppDb;
CREATE USER agent_ro FOR LOGIN agent_ro;
ALTER ROLE db_datareader ADD MEMBER agent_ro;

Step 2: add the database to AI2SQL

Sign in at builder.ai2sql.io, open Connections and add the database with agent_ro. A database on your laptop or inside a private network connects through the desktop connector, with no inbound port to open.

Step 3: connect the agent

Claude, ChatGPT and code editors

These sign in with OAuth, so there is no key to paste. Add https://builder.ai2sql.io/api/claude-mcp as a custom connector in Claude, install the AI2SQL app in ChatGPT, or add https://builder.ai2sql.io/api/chatgpt-mcp to Cursor, VS Code or Gemini CLI. Each setup is on its own page in the table above.

Your own agent

Create an API key in the AI2SQL dashboard and point any MCP client at the API-key server:

URL:    https://builder.ai2sql.io/api/mcp
Header: x-ai2sql-key: sql_...      (or Authorization: Bearer sql_...)
Tools:  list_connections, describe_schema, run_query

Working code for the OpenAI Agents SDK, the Claude API MCP connector, the MCP TypeScript SDK and curl is in build an AI agent that queries SQL over MCP. For no-code agents, see n8n.

What keeps it safe

The reasoning behind each layer is in how to give an AI agent read-only database access.

Frequently asked

How do I connect an AI agent to a database? +

Give the agent an MCP server that sits between it and the database. With AI2SQL you add the database once with a read-only user, then point the agent at an AI2SQL MCP URL. The agent gets list_connections, describe_schema and run_query, and never holds the database password.

Why not give the agent a database connection string? +

A connection string hands the agent everything the database user can do, with no row limit, no log of what it ran and nothing to revoke except the password. An MCP gateway lets it read the schema and run one SELECT at a time, logs every call and can be cut off by revoking a token or key.

Which databases can an agent query through AI2SQL? +

PostgreSQL, MySQL and SQL Server, including managed versions such as Amazon RDS, Azure SQL, Supabase and Neon. Databases on a laptop or private network connect through the AI2SQL desktop connector. SQLite is not supported for live queries.

Can the agent write or delete data? +

No. run_query accepts one SELECT or WITH statement per call; INSERT, UPDATE, DELETE and DDL are rejected before they reach the database. Use a read-only database user as well so the database refuses writes on its own.

What does it cost? +

Signing in, reading schemas and querying the demo_shop sample work on every plan. Running queries on your own database and API keys are part of Pro and Team, which start with a 7-day trial.

Give your agent a database it can't break

Connect PostgreSQL, MySQL or SQL Server once, then point any agent at it. Read-only and logged from the first query.

Start free →

Related guides