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.
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.
- Clone the template
- Run
npx create-mastra@latest --template text-to-sqlto scaffold the project locally.
- Run
- Add your API key
- Copy
.env.exampleto.envand fill in your OpenAI API key.
- Copy
- Start the dev server
- Run
npm run devand open localhost:4111 to try it out.
- Run
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.
- "What tables are in the database?"
- "Show me all employees at Acme Corp"
- "What's the average salary by department?"
- "Which projects are currently in progress and what's their total budget?"
- "List employees hired in 2023 sorted by salary"
- "Which company has the highest revenue?"
This template is a starting point. Here are some ideas to make it your own:
- Connect to a different database (PostgreSQL, MySQL, etc.) by swapping out the database client and adjusting the introspection and execution tools.
- Add authentication and user management to control access to the database.
- Implement more advanced SQL features like JOINs, subqueries, or transactions.
- Build a custom frontend using the Mastra Client SDK or UI libraries to create a polished user interface.
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.