text2sql-agent
Enables natural language querying of DuckDB databases, with automatic schema discovery, multi-table joins, and read-only SQL execution.
This server does not currently have a deployable release on Glama. Follow the repository's setup instructions to install and configure it for your MCP client, including any required credentials and local dependencies.
Text2SQL-MCP-Agent
A modular, high-performance Text-to-SQL AI agent and Streamlit copilot powered by LangGraph, LangChain, native MCP server integration, dynamic CSV schema linking, and read-only AST safety guardrails.
Live Web App: text2sql-mcp-agent.streamlit.app
Live FastMCP Cloud Endpoint: https://xenial-moccasin-ladybug.fastmcp.app/mcp
Overview
Text2SQL-MCP-Agent bridges the gap between natural language business questions and enterprise data warehouses / dynamic CSV files. Built on modular AI system design principles and orchestrated via LangGraph StateGraph and LangChain LCEL Runnables, it converts natural language text into precise, AST-validated read-only SQL queries, executes them safely against DuckDB, and returns tabular insights alongside natural language answers.
Key Features
LangGraph & LangChain Engine: Stateful graph orchestration (
StateGraph) with nodes for schema linking, SQL drafting, semantic constraint alignment evaluation, AST validation, execution, self-correction, and answer formatting.3-Tier Routing Architecture: Automatically routes simple deterministic queries to a sub-millisecond Heuristic SQL Synthesizer, complex analytical queries to your Hugging Face Fine-Tuned Model (
Priyanka221105/text2sql-qwen2.5-duckdb/Qwen/Qwen2.5-Coder-32B-Instruct), and uses Gemini 3.6 Flash as an online backup.Semantic Constraint Alignment Guardrail: Inspects generated SQL against the user's natural language question for completeness (catching relative date boundaries like "last day of month", complex filters, or ranks) and triggers self-correction handoff to the fine-tuned model if constraints are missed.
Multi-Dialect SQL Transpilation (MySQL Default): Automatically formats and transpiles executed queries into MySQL Dialect (with on-the-fly toggling between MySQL, DuckDB, PostgreSQL, and Snowflake via
sqlglot).Automated GitHub Actions CI/CD: Integrated GitHub Actions CI workflow (
.github/workflows/ci.yml) for automated test suite execution on every push and pull request.Consolidated Clean Codebase: Streamlined down to 4 self-contained Python modules (
src/agent.py,src/engine.py,src/sandbox.py,src/app.py) for maximum human readability and zero UI clutter.Dynamic Multi-Table CSV Ingestion: Drag-and-drop multiple CSV files via Streamlit or MCP; DuckDB automatically registers each file as a separate queryable table.
Automated Multi-Table JOIN Discovery: Automatically detects shared Primary/Foreign Key relationships across tables (e.g.
orders.customer_id <-> customers.customer_id) and injects candidate join conditions into prompt context.Value-Aware Categorical Linking: Matches literal text values (e.g.
'Consumer','Seattle') against sample categorical values across database columns.AST Safety Guardrails (
SQLGlot): Statically parses SQL syntax trees to enforce single read-onlySELECTqueries and prevent SQL injection or DDL/DML mutation statements.Native Model Context Protocol (MCP): Exposes
@mcp.tool()and@mcp.resource()endpoints so Claude Desktop, Antigravity IDE, Cursor, and AI agents can query the warehouse.Streamlit Web UI (
src/app.py): Interactive web dashboard featuring CSV drag-and-drop, dynamic schema inspector, chat bar, multi-dialect SQL query viewer, active engine badge, and Plotly visual charts.
Related MCP server: MCP Analytics Server
System Design Architecture
┌──────────────────────────────────────────────────────────────────────────┐
│ MCP CLIENT HOST / STREAMLIT APP UI │
│ (Claude Desktop / Cursor / Antigravity IDE / Streamlit) │
└────────────────────────────────────┬─────────────────────────────────────┘
│
▼
┌──────────────────────────────────────────────────────────────────────────┐
│ LANGGRAPH & LANGCHAIN 3-TIER ROUTING AGENT │
│ │
│ INTENT CLASSIFIER & ROUTER │
│ ├─ Tier 1: Simple Aggregations & Summary ➔ Heuristic Synthesizer (0ms)│
│ ├─ Tier 2: Complex Joins, CTEs, MoM ➔ Hugging Face Fine-Tuned LLM │
│ └─ Tier 3: Online LLM Backup ➔ Gemini 3.6 Flash (LangChain) │
│ │
│ LANGGRAPH STATEGRAPH NODES │
│ ├─ Node 1: Value-Aware Schema & Semantic Context Linking │
│ ├─ Node 2: SQL Drafting & Exemplars Injection │
│ ├─ Node 3: Read-Only DuckDB Sandbox Execution │
│ ├─ Node 4: Semantic Constraint Alignment Guardrail │
│ ├─ Node 5: Bounded Self-Correction Retry Loop (Max 2 retries) │
│ └─ Node 6: Multi-Dialect Transpilation (MySQL) & NL Formatting │
└────────────────────────────────────┬─────────────────────────────────────┘
│
▼
┌──────────────────────────────────────────────────────────────────────────┐
│ DUCKDB READ-ONLY EXECUTION SANDBOX │
└──────────────────────────────────────────────────────────────────────────┘Quickstart Guide
1. Installation
Clone the repository and install dependencies:
git clone https://github.com/PriyankaHichkad/Text2SQL-MCP-Agent.git
cd Text2SQL-MCP-Agent
pip install -r requirements.txt2. Seed Sample Database
Generate the sample e-commerce star schema (data/sample_warehouse.db) and data/superstore.csv:
python scripts/seed_db.py3. Launch Streamlit Web App
Run the interactive copilot dashboard:
streamlit run src/app.py4. Connect to MCP Server (Cloud or Local)
Option A: Remote FastMCP Cloud Deployment (Zero Setup)
Connect Claude Desktop, Cursor, or any MCP client directly to the deployed cloud server:
Cloud MCP URL:
https://xenial-moccasin-ladybug.fastmcp.app/mcp
Claude Desktop Configuration (claude_desktop_config.json):
{
"mcpServers": {
"text2sql-cloud": {
"url": "https://xenial-moccasin-ladybug.fastmcp.app/mcp"
}
}
}Option B: Local FastMCP Server (stdio)
Run the local Model Context Protocol server:
python -m src.mcp_serverClaude Desktop Configuration (claude_desktop_config.json):
{
"mcpServers": {
"text2sql-local": {
"command": "python",
"args": ["-m", "src.mcp_server"],
"cwd": "/path/to/Text2SQL-MCP-Agent"
}
}
}Repository Structure
Text2SQL-MCP-Agent/
├── .github/
│ └── workflows/
│ └── ci.yml # GitHub Actions CI automated test workflow
├── README.md # System overview & setup guide
├── pyproject.toml # Project metadata & dependencies
├── requirements.txt # Pip dependencies
├── config/
│ ├── semantic_layer.yaml # Business metrics & macro definitions
│ └── warehouse_config.yaml # Database configuration & security guardrails
├── scripts/
│ └── seed_db.py # Data seed script for DuckDB sample database
├── src/
│ ├── __init__.py
│ ├── app.py # Streamlit Web Application entrypoint
│ ├── agent.py # LangGraph StateGraph, Intent Router & Runnable Nodes
│ ├── engine.py # Schema Catalog, Linker, Exemplars & LangChain LLM Router
│ ├── sandbox.py # SQLGlot AST Validator & Read-Only DuckDB Sandbox
│ └── mcp_server.py # FastMCP server exposing tools & resources
└── tests/
├── test_hermetic_workflow.py # Hermetic offline workflow & mock LLM tests
├── test_guardrails.py # Semantic constraint guardrail unit tests
├── test_validator.py # AST validator unit tests
├── test_sandbox.py # Query sandbox unit tests
└── test_edge_cases.py # End-to-end edge-case evaluation suiteEvals & Verification
Run the hermetic offline test suite to verify AST security guardrails, state machine transitions, self-correction loops, and execution sandbox safety without live network calls:
pytestCurrent test suite result: 20/20 tests passing in 100% hermetic offline execution.
Tools & Technologies
Python 3.10+ — Core programming language
LangChain & LangGraph — Stateful agent graph orchestration & LCEL Runnables
DuckDB — In-memory analytical database engine
Model Context Protocol (MCP) — Open protocol for AI tools & context
FastMCP — High-level Python MCP server framework
Streamlit — Interactive web application interface
SQLGlot — SQL parser, AST validator & transpiler
Google Gemini API — LLM reasoning & SQL query generation (Free Tier)
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
This server cannot be deployed
Maintenance
Related MCP Connectors
Query your warehouse or a CSV with Claude/ChatGPT over MCP, governed by table-level ACL + audit.
Query your org's data in natural language — read-only MCP access to SQL, NoSQL, files & warehouses.
Query, join, profile, clean and convert CSV/JSON/Parquet with server-side DuckDB over MCP.
Ask questions in plain language, get answers from your business database. No SQL required.
Related MCP Servers
- FlicenseAqualityDmaintenanceEnables read-only exploration and querying of PostgreSQL or MySQL databases via MCP, with schema discovery, safe SQL validation, natural language to SQL conversion, and CSV export.112-
- AlicenseAqualityDmaintenanceEnables AI agents to discover and execute analytical queries on a DuckDB dataset through typed MCP tools, with guarded read-only SQL support for complex calculations without direct database access.6MIT
- AlicenseNot gradedqualityBmaintenanceEnables AI agents to run SQL analytics directly on CSV files in place via MCP, avoiding imports and token-heavy data pasting. It provides read-only querying, schema inspection, and file listing while keeping data local.31MIT
- AlicenseAqualityCmaintenanceEnables read-only analytical access to local CSV, Parquet, JSON files or a DuckDB database through MCP, with table discovery, column profiling, guarded SQL querying, and explain plans for safe use with Claude Desktop and Claude Code.81MIT