templates/template-text-to-sql/README.md
A Mastra template that lets you query a local SQLite database using natural language. An AI agent introspects the schema, converts your questions to SQL, and executes the queries. Built with Mastra.
Text-to-SQL is a classic problem that showcases the power of AI agents to understand complex schemas, reason about data, and generate executable code. This template demonstrates how to build a conversational SQL agent using Mastra's tools and architecture. It's a great starting point for anyone looking to create AI agents that interact with databases, whether for analytics, customer support, or internal tools.
<video controls width="640" height="360" src="https://res.cloudinary.com/mastra-assets/video/upload/v1772538183/template-text-to-sql_pgibmm.mp4"></video>
This demo runs in Mastra Studio, but you can connect this agent to your React, Next.js, or Vue app using the Mastra Client SDK or agentic UI libraries like AI SDK UI, CopilotKit, or Assistant UI.
npx create-mastra@latest --template text-to-sql to scaffold the project locally..env.example to .env and fill in your OpenAI API key.npm run dev and open localhost:4111 to try it out.Open Studio and start chatting with the SQL agent. The agent can introspect the database schema, convert your natural language questions into SQL queries, and return the results.
Need a database to try? The template comes with a pre-seeded SQLite database (
data.db) containing sample company and employee data.
This template is a starting point. Here are some ideas to make it your own:
Mastra templates are ready-to-use projects that show off what you can build — clone one, poke around, and make it yours. They live in the Mastra monorepo and are automatically synced to standalone repositories for easier cloning.
Want to contribute? See CONTRIBUTING.md.