Skip to main content
Glama
lakehouse-ai-copilot MCP Server

lakehouse-ai-copilot MCP Server

by varunpatel11
README.md
# lakehouse-ai-copilot

**Agentic Text-to-SQL, Column-Level AST Lineage, and Self-Healing Data Quality Copilot**

`lakehouse-ai-copilot` bridges **Data Engineering** and **AI Engineering** with a stateful **LangGraph** workflow over a **DuckDB** Medallion warehouse, protected by **SQLGlot AST** read-only policy guardrails, automated **column-level lineage**, **self-healing SQL & Data Quality remediation**, an **MCP server**, and an **automated execution-accuracy + safety benchmark harness**.

---

## Architecture

```mermaid
flowchart LR
    Q["Natural Language Query"] --> R["Schema Retriever<br/>(Warehouse Metadata)"]
    R --> G["SQL Planner / Generator"]
    G --> AST["SQLGlot AST Guardrail<br/>(Read-Only + Allow-List + LIMIT)"]
    AST -->|"Blocked"| STOP["Policy Rejection"]
    AST -->|"Allowed"| LIN["SQLGlot Column Lineage<br/>(Output -> Source Table.Col)"]
    AST --> EX["DuckDB Executor"]
    EX -->|"Execution Error"| HEAL["Self-Healing Node<br/>(Fuzzy Identifier + AST Repair)"]
    HEAL --> AST
    EX -->|"Success"| RES["Grounded Rows + Lineage"]
```

---

## Key Engineering Highlights

- **SQLGlot AST Security Guardrails (`validate_and_rewrite_sql`):** Walks the parsed DuckDB AST to reject `DROP`, `DELETE`, `UPDATE`, `INSERT`, `COPY`, `PRAGMA`, multi-statement payloads, and disallowed tables, while auto-enforcing bounded `LIMIT` clauses.
- **Automated Column-Level Lineage (`extract_column_lineage`):** Traces every output projection (including joins and computed expressions like `gross_profit_usd` and `blended_roas`) back to its upstream `(source_table, source_column)` dependencies.
- **Stateful LangGraph Self-Healing Loop:** Routes DuckDB binder/execution failures through error-guided repair (`_attempt_sql_repair` + AST re-validation) up to `max_heal_attempts`.
- **Self-Healing Data Quality Engine (`detect_and_remediate_dq`):** Scans warehouse tables for duplicate primary keys, negative revenue/unit bounds, and `NULL` region/SKU spikes and applies idempotent remediation SQL.
- **FastAPI + MCP Serving:** Exposes `/ask`, `/lineage`, `/dq/scan`, `/dq/remediate` via FastAPI and `ask_warehouse_copilot`, `trace_sql_column_lineage`, `scan_and_heal_dq` over Model Context Protocol (`mcp`).

---

## Quickstart

```bash
python3.12 -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"

# Run lint & tests
ruff check src tests benchmarks
pytest -v

# Run Text-to-SQL, AST safety, self-healing, and DQ benchmark
lac benchmark

# Start FastAPI or MCP stdio server
uvicorn lakehouse_ai_copilot.api:app --reload --port 8001
python -m lakehouse_ai_copilot.mcp_server
```

---

## Benchmarks (Verified Local Run)

Measured via `lac benchmark` on the DuckDB Medallion warehouse (`silver_retail_sales`, `gold_marketing_spend`, `dim_products`):

| Metric | Measured Result |
| --- | --- |
| Text-to-SQL Execution Accuracy (12 golden queries) | **1.00** (12/12 exact row match) |
| Adversarial SQL AST Block Rate (8 injection/DDL cases) | **1.00** (8/8 blocked) |
| Self-Healing Recovery Rate (4 broken SQL cases) | **1.00** (4/4 healed &amp; executed) |
| Column-Level AST Lineage Coverage | **1.00** (100% queries traced) |
| DQ Anomaly Detection &amp; Auto-Remediation | **3/3** (`duplicate_keys`, `negative_revenue`, `null_region_spike`) |
| End-to-End Copilot Latency | **p50: 17.8 ms** / **p95: 22.04 ms** |