A Next.js application that enables natural language database queries powered by AI. Ask questions in plain English and get results displayed as interactive tables or charts.
- Natural Language Queries: Query your database using plain English instead of SQL
- Dual View Modes:
- Table View: Display query results in a structured table format
- Chart View: Visualize data with interactive bar charts
- AI-Powered SQL Generation: Uses Groq's LLM to automatically generate and execute SQL queries
- PostgreSQL Integration: Connects to PostgreSQL databases via MCP (Model Context Protocol)
- Smart Query Execution: Automatically analyzes table schemas and handles query retries on failures
- Modern UI: Built with Next.js 15, React 19, and Tailwind CSS with shadcn/ui components
- Frontend: Next.js 15, React 19, TypeScript
- UI Components: shadcn/ui (Radix UI primitives)
- Styling: Tailwind CSS 4
- AI/LLM: Groq API with Llama 3.3 70B model
- Charts: Recharts
- Database: PostgreSQL (via Neon)
- MCP Integration: Server-Sent Events (SSE) for database tool access
- Node.js 20+ installed
- A Groq API key (Get one here)
- A PostgreSQL database (the app uses Neon, but any PostgreSQL database works)
- An MCP server providing database tools (configured at
https://ai-db-query.akramansari1433.workers.dev/sse)
- Clone the repository:
git clone <repository-url>
cd ai-db-query- Install dependencies:
npm install- Create a
.env.localfile in the root directory:
DATABASE_URL="postgresql://user:password@host:port/database?sslmode=require"
GROQ_API_KEY="your_groq_api_key_here"- Start the development server:
npm run dev- Open http://localhost:3000 in your browser
- Select the "Table View" tab
- Enter a natural language query like:
- "get all users"
- "show me orders from last month"
- "list products with price greater than 100"
- Press Enter or click the "Query" button
- View results in a structured table
- Select the "Chart View" tab
- Enter a query that returns data suitable for visualization:
- "show user count by month"
- "total sales per category"
- "average rating by product"
- Press Enter or click the "Query" button
- View results as an interactive bar chart
- User Input: You type a natural language query
- AI Processing: The query is sent to Groq's Llama model via the AI SDK
- Schema Analysis: The AI first retrieves your database schema using MCP tools
- SQL Generation: Based on the schema, it generates appropriate SQL queries
- Query Execution: The SQL is executed against your PostgreSQL database via MCP
- Result Display: Data is formatted and displayed as a table or chart
This app uses the Model Context Protocol (MCP) to interact with the database through two main tools:
getTablesInfoPostgres: Retrieves database schema informationqueryDatabasePostgres: Executes SQL queries
The MCP server is accessed via Server-Sent Events (SSE) at the configured endpoint.
ai-db-query/
├── app/
│ ├── api/
│ │ └── completion/
│ │ └── route.ts # API endpoint for AI query processing
│ ├── page.tsx # Main application UI
│ └── layout.tsx # Root layout
├── components/
│ └── ui/ # shadcn/ui components
├── lib/
│ └── utils.ts # Utility functions
├── public/ # Static assets
├── .env.local # Environment variables (not in git)
└── package.json # Dependencies
Processes natural language queries and returns database results.
Request Body:
{
"prompt": "your natural language query",
"type": "table" | "chart"
}Response:
{
"success": true,
"type": "table",
"data": [...],
"columns": [...]
}| Variable | Description | Required |
|---|---|---|
DATABASE_URL |
PostgreSQL connection string | Yes |
GROQ_API_KEY |
Groq API key for LLM access | Yes |
# Start development server
npm run dev
# Build for production
npm run build
# Start production server
npm start
# Run linting
npm run lint- Push your code to GitHub
- Import your repository in Vercel
- Add environment variables (
DATABASE_URL,GROQ_API_KEY) - Deploy
This is a standard Next.js app and can be deployed to any platform supporting Node.js:
- AWS (Amplify, EC2, ECS)
- Google Cloud Run
- Railway
- Render
- Fly.io
Edit route.ts:80:
model: groq("llama-3.3-70b-versatile"),Available Groq models include:
llama-3.3-70b-versatilemixtral-8x7b-32768gemma2-9b-it
The app currently supports bar charts. To add other chart types, update the rendering logic in page.tsx:101-144.
Update the SSE URL in route.ts:16:
url: "https://your-mcp-server.com/sse",- The AI automatically retrieves schema information, but complex queries may need refinement
- Try rephrasing your query with more specific details
- Ensure your query returns at least 2 columns
- The first column is used for X-axis, second for Y-axis
- Numeric data works best for the Y-axis
- Verify your
GROQ_API_KEYis valid - Check your MCP server is accessible
- Ensure
DATABASE_URLis correctly formatted
This project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit a Pull Request.
If you encounter any issues or have questions, please open an issue on GitHub.