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
- Database: the agent's user can only read.
- Gateway: one
SELECTorWITHper call; writes and DDL are rejected before they reach the database. - Limits: results are capped at 1,000 rows, and long queries time out.
- Secrets: the agent holds a token or key you can revoke, never the database password.
- Audit: every call is in Agent activity, including blocked attempts. Use one key per agent to see which one ran what.
The reasoning behind each layer is in how to give an AI agent read-only database access.