Short answer: a text-to-SQL agent is a loop: read the schema, write a query, run it,
check the result, answer. You don't need to code that loop to get one. Claude, ChatGPT and Cursor
already run it well; what they lack is safe access to your database. AI2SQL gives them that as a
hosted MCP server with three tools (list_connections, describe_schema,
run_query), read-only and logged.
Text-to-SQL tool vs text-to-SQL agent
- Text-to-SQL tool: you type a question, it writes SQL, and you run it yourself.
- Text-to-SQL agent: it looks up the tables, writes the SQL, runs it, reads the error or the rows, fixes the query if needed and answers in plain language.
The agent is more useful and needs more care, because it runs SQL that nobody reviewed first. That is why the database access layer matters more than the model.
Option 1: build your own
LangChain's SQL agent, Hugging Face smolagents, agno and Vanna all give you the pieces: a model, a schema loader and a tool that runs SQL. Building makes sense when the agent is part of your own product, or you need custom retrieval over a very large schema. You then own:
- loading and trimming the schema so it fits the prompt,
- blocking writes (a read-only user plus statement checks),
- row limits and timeouts, so one bad join can't hang the database,
- keeping credentials out of the model's reach,
- logging what ran, for debugging and audits.
You can also build your own agent and still hand those parts to AI2SQL: point any MCP client at
https://builder.ai2sql.io/api/mcp with an API key. Code for the OpenAI Agents SDK, the
Claude API and the MCP TypeScript SDK is in
build an AI agent that queries SQL over MCP.
Option 2: plug in a ready one
- Add your database to AI2SQL with a read-only user (PostgreSQL, MySQL or SQL Server).
- Connect your assistant: Claude, ChatGPT or Cursor, with a sign-in instead of a password.
- Ask: "Use AI2SQL to find which product categories grew most last month." The assistant reads the schema, writes the SQL, runs it and explains the answer.
No database yet? Every account includes demo_shop, a sample store with fake customers and
orders, so you can watch the agent loop work first.
How schema changes are handled
Text-to-SQL tools go wrong after a migration when they work from an old copy of the schema: a renamed column, a new table or a dropped field leads to SQL that fails or, worse, runs against the wrong column. There are three common approaches:
- Fine-tuned or trained on a snapshot: accurate until the schema changes, then needs retraining.
- Indexed schema (embeddings or a semantic layer): scales to big schemas, but the index must be refreshed after migrations.
- Live schema read: the agent asks the database for the current tables and columns when it needs them.
AI2SQL uses the live read: describe_schema queries the database's own catalog when the
agent calls it, so a new column is visible on the next question with nothing to retrain or re-index.
If a query still fails because the schema moved mid-conversation, the error goes back to the agent,
which reads the schema again and rewrites the SQL.
Getting accurate answers
- Name things clearly.
orders.created_atbeatsord.dt1; views with clear names help a lot. - Declare foreign keys. The agent uses them to pick the right joins.
- State the business rules in the question or the assistant's instructions, such as "revenue excludes refunded orders".
- Read the SQL. Ask the agent to show the query it ran, especially for numbers that go into a report.