# SQL Pilot

A LangGraph-based AI agent that connects with a custom MCP server to provide natural language database interactions. SQL Pilot enables users to query, manipulate, and manage SQLite databases through conversational AI.

## Features

- **Natural Language Queries**: Ask questions about your database in plain English
- **Database Operations**: Read, insert, update, delete, and create tables
- **Schema Management**: View table structures and relationships
- **Authentication & Security**: Built-in user authentication with consent management
- **Web Interface**: Clean Gradio-based UI for easy interaction
- **Session Memory**: Maintains conversation context and database schema awareness

## Capabilities

### Database Operations
- List all tables in the database
- Read data with filtering, limits, and ordering
- Insert new records with validation
- Update existing records with WHERE clauses
- Delete records with safety constraints
- Create new tables with custom schemas
- Drop tables (with consent)

### AI-Powered Features
- Intelligent query parsing and operation planning
- Context-aware follow-up questions
- Automatic parameter mapping and validation
- Error handling and fallback mechanisms
- Session-based memory and learning

## Quick Start

### Prerequisites
- Python 3.8+
- OpenAI API key (set in environment variables)

### Installation

1. **Clone the repository**
   ```bash
   git clone https://github.com/chandraprvkvsh/SQL-Pilot.git
   cd SQL-Pilot
   ```

2. **Install dependencies**
   ```bash
   pip install -r requirements.txt
   cd Query-MCP
   pip install -r requirements.txt
   ```

3. **Set environment variables**
   ```bash
   export OPENAI_API_KEY="your-openai-api-key"
   ```

### Running the System

1. **Start the MCP Server**
   ```bash
   cd Query-MCP (github.com/chandraprvkvsh/Query-MCP)
   python server.py
   ```

2. **Start the Web Interface** (in a new terminal)
   ```bash
   python ui.py
   ```

3. **Access the Interface**
   - Open `http://localhost:7860` in your browser
   - Upload a SQLite database or create a sample database
   - Start chatting with your database!

## Usage Examples

```
"Show me all tables in the database"
"List all users with their email addresses"
"Add a new user named 'Alice' with email 'alice@example.com'"
"Update the user with id 1 to have name 'Bob'"
"Create a new table called 'products' with columns id, name, and price"
"Show me the schema of the users table"
"What was the last operation I performed?"
```

## Architecture

- **LangGraph Agent**: Orchestrates the conversation flow and decision making
- **Query-MCP Server**: Custom MCP server handling database operations
- **FastMCP Framework**: Enables tool calling and resource management
- **Gradio UI**: Provides the web interface for user interaction
- **SQLite Database**: Stores and manages your data
- **Persistent Agent Memory**: Stores user context, session history, and schema awareness for intelligent multi-turn conversations  


## Security Features

- Consent-based destructive operations
- Parameterized queries preventing SQL injection
- Input validation and sanitization
- Session timeout and cleanup