Text-to-SQL agent: build one or plug one in

A text-to-SQL agent reads your schema, writes SQL, runs it and answers from the result. You can build one with a framework, or connect an assistant you already use to a read-only MCP server and have one working in a few minutes.

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

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:

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

  1. Add your database to AI2SQL with a read-only user (PostgreSQL, MySQL or SQL Server).
  2. Connect your assistant: Claude, ChatGPT or Cursor, with a sign-in instead of a password.
  3. 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:

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

Frequently asked

What is a text-to-SQL agent? +

A text-to-SQL agent is an AI model that turns a question into SQL, runs it against a database, looks at the result and answers, retrying when a query fails. A plain text-to-SQL tool stops after writing the query; an agent also reads the schema and runs the SQL.

Do I have to build my own text-to-SQL agent? +

No. Frameworks such as LangChain or smolagents let you build one, but you then own the schema loading, the read-only checks, the limits and the logging. Claude, ChatGPT and Cursor are already capable agents; connect them to a hosted MCP server such as AI2SQL and they can read your schema and run read-only SQL.

How do text-to-SQL tools handle changing schemas? +

It depends on where the schema comes from. Tools that index or fine-tune on a schema snapshot go stale until you refresh them. AI2SQL's describe_schema reads the live schema from the database when the agent asks, so a new column or table is visible on the next question with nothing to retrain.

How accurate are text-to-SQL agents? +

Accuracy depends mostly on the schema: clear table and column names, foreign keys and a few documented business rules matter more than the model. Always let people see the SQL that ran, and start with questions whose answers you already know.

Which databases does the AI2SQL agent setup support? +

PostgreSQL, MySQL and SQL Server for live schema reads and queries, including Amazon RDS, Azure SQL, Supabase and Neon. AI2SQL can also write SQL for other dialects such as Oracle, Snowflake and BigQuery without running it.

Turn Claude or ChatGPT into a text-to-SQL agent

Connect PostgreSQL, MySQL or SQL Server, add one connector, sign in. Read-only and logged from the first query.

Start free →

Related guides