Skip to main content
Glama
indian-ecommerce-sql

indian-ecommerce-sql

README.md
# Natural Language SQL Assistant

Ask Claude Desktop business questions about a synthetic Indian e-commerce dataset. Claude discovers the available metrics, writes SQL and presents the answer. A local Python Model Context Protocol (MCP) server validates each query and reads approved MySQL aggregates. The database and server require no public listener for Claude to connect.

## Architecture

```mermaid
flowchart LR
    K[Kaggle dataset] --> P[Local cleaning and import]
    P --> T[(Customers, products, sales)]
    T --> V[16 aggregate views]
    C[Claude Desktop] <-->|stdio| M[Python FastMCP]
    M --> B[Business metric definitions]
    M --> S[SQLGlot validation]
    S --> R[MySQL read-only transaction]
    R -->|SELECT| V
```

Python 3.12, KaggleHub and pandas support the data pipeline. MySQL Connector/Python connects to MySQL. The official MCP SDK serves seven tools, python-dotenv loads local credentials, and pytest exercises the protocol and database boundary.

## Data and business questions

Jatin Khandelwal’s synthetic dataset contains 40,000 customers, 2,000 products and 250,000 sales. Kaggle lists its license as **CC0: Public Domain**. Download the source through the setup script. The currency is **unspecified**. Delivered net sales sums the recorded total amount for delivered orders, including four negative source amounts. [Indian E-Commerce Sales Analytics Dataset](https://www.kaggle.com/datasets/jatinkhandelwal112/indian-e-commerce-sales-analytics-dataset) · [Metric definitions](docs/business_metrics.md)

| Try asking Claude | Verified snapshot answer |
| --- | --- |
| Which category has the highest delivered net sales in Haryana? | Electronics, 432,427,586.80 across 6,829 delivered orders |
| Which brand generates the highest delivered net sales? | HP, 754,932,335.37 across 10,744 delivered orders |
| Which category recorded the largest positive month-over-month percentage growth in delivered net sales? | Beauty, up 23.20% in July 2024 |
| Which payment method is most popular in Pune? | Unsupported. The available views do not combine city and payment method. |

The repository also retains an independent historical pipeline for Olist’s anonymized Brazilian dataset. Its license is **CC BY-NC-SA 4.0**. The Indian MCP server uses only the Indian database. Both datasets are downloaded separately and excluded from Git. [Olist dataset](https://www.kaggle.com/datasets/olistbr/brazilian-ecommerce) · [Olist license](https://creativecommons.org/licenses/by-nc-sa/4.0/)

## Project Demonstration

### Connecting Claude Desktop to MySQL analytics

![MCP log showing tool discovery, calls and server replies](screenshots/01-mcp-integration.png)

The local MCP log records Claude’s tool-list request and subsequent tool calls to `indian-ecommerce-sql`.

![Claude listing the sixteen approved analytics views](screenshots/02-schema-discovery.png)

Claude lists the 16 views available to the reader. The view definitions govern which dimensions and order statuses each query can use.

### Comparing sales across states and categories

![Top five states ranked by delivered net sales](screenshots/03-state-sales-analysis.png)

The five leading states account for 58.69% of delivered net sales and 117,834 delivered orders. Monetary values use unspecified source units.

![Claude Desktop showing delivered orders and net sales by category in Haryana](screenshots/04-haryana-category-analysis.png)

Claude’s Haryana breakdown shows why the metric matters. Electronics contributed 72.35% of delivered net sales across 6,829 orders. The bars show order counts and the ring shows sales share.

### Checking the access boundary and the backend

![MCP rejecting a query against the underlying customers table](screenshots/05-security-validation.png)

A request for ten customer records returns `invalid_request`. This demonstrates the application allowlist. The database grant check is documented separately in the [backend test report](docs/mcp_test_report.md).

![Terminal output reporting 160 passing pytest tests](screenshots/06-backend-tests.png)

The captured run completed all 160 tests in 55.40 seconds. The image is cropped to the test output for privacy.

## Local setup on macOS

Start in the cloned repository root. Install Python 3.12 and start local MySQL (tested with MySQL 9.7.1). For Homebrew installations, use `brew services start mysql`. Provision a fresh local instance or an empty `indian_ecommerce_db` and a dedicated local administrative account with permissions to create the schema, import data, create a reader and grant view-level SELECT. The installers refuse to replace existing tables, views, accounts or credential files.

```sh
python3.12 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
python scripts/download_indian_dataset.py
python scripts/clean_indian_dataset.py
```

Supply administrative credentials in a local **zsh** session. Use a hidden prompt for the password. Leave the project’s `.env` absent at this point. The reader setup creates it with a generated password and owner-only permissions. The [.env.example](.env.example) documents runtime settings.

```zsh
export MYSQL_HOST=127.0.0.1 MYSQL_PORT=3306
read 'MYSQL_USER?Local MySQL provisioning user: '
read -s 'MYSQL_PASSWORD?Password: '; echo
export MYSQL_USER MYSQL_PASSWORD

python scripts/import_indian_dataset.py
python scripts/validate_indian_database.py
python scripts/setup_mcp_reader.py
python scripts/review_multidimensional_privacy.py
```

Review the group minima and suppression findings against the [privacy review](docs/multidimensional_privacy_review.md) before granting access. For the verified dataset snapshot, continue with:

```sh
python scripts/setup_analytics_access.py
python scripts/setup_multidimensional_views.py
unset MYSQL_USER MYSQL_PASSWORD
python -m pytest -q
python scripts/test_privacy_benchmark.py
```

MySQL DDL commits independently. If provisioning stops partway, inspect the existing objects and grants before retrying. The importer and setup scripts do not reset the database.

## Connect Claude Desktop

Install Claude Desktop and open its local developer configuration on macOS. The registration script expects a regular JSON configuration file. If it does not yet exist, create its parent directory and a file containing `{}` at `~/Library/Application Support/Claude/claude_desktop_config.json`. Preserve any existing file.

```sh
python scripts/configure_claude_desktop.py
python scripts/check_claude_stdio.py
```

The helper backs up the configuration before merging the `indian-ecommerce-sql` entry with the clone’s virtual-environment interpreter and project path. Quit and reopen Claude Desktop, select the local server, and try the Haryana question above. Claude Desktop launches `python -m mcp_server.server` over stdio. Review tool results before sharing a conversation. [Claude Desktop setup](docs/claude_desktop_setup.md)

## Seven MCP tools

| Tool | What it returns |
| --- | --- |
| `list_tables()` | Available analytics views |
| `describe_table(table_name)` | Columns, types and metadata for one view |
| `get_database_schema()` | Schema for the approved views |
| `get_table_relationships()` | Declared relationships visible on the approved objects |
| `get_sample_data(table_name, limit=5)` | Ordered aggregate rows |
| `execute_query(sql)` | Bounded results for one validated read-only query |
| `get_business_context()` | Metric definitions, scope and privacy guidance |

## Sixteen analytics views

| Status or snapshot view | Grain | Delivered-only view | Grain |
| --- | --- | --- | --- |
| `analytics_sales_monthly` | Order month and status | `analytics_sales_by_state_category` | State and category |
| `analytics_sales_by_state` | State and status | `analytics_sales_by_city_category` | City and category |
| `analytics_sales_by_category` | Category and status | `analytics_sales_by_state_month` | State and month |
| `analytics_sales_by_payment` | Payment method and status | `analytics_sales_by_category_month` | Category and month |
| `analytics_sales_by_coupon` | Coupon use and status | `analytics_sales_by_state_payment` | State and payment method |
| `analytics_sales_by_rating` | Rating and status | `analytics_sales_by_category_payment` | Category and payment method |
| `analytics_customer_tiers` | Customer tier snapshot | `analytics_sales_by_product` | Product name and category nationally |
| `analytics_inventory` | Inventory snapshot by category | `analytics_sales_by_brand` | Brand nationally |

Each delivered-only view covers the same 200,139 delivered orders. Adding totals across these views double-counts sales. Product names may pool multiple catalog IDs. Missing ratings remain NULL.

## Verification and limitations

The full suite passed **160/160** tests, including the **40/40** aggregate benchmark comparisons, real stdio initialization and invocation of all seven tools. The 37 legacy SQL queries exercise raw-table rejection. The project owner confirmed the planned Claude Desktop functional and security tests passed. [Backend test report](docs/mcp_test_report.md) · [Questions, verified outputs and timings](docs/privacy_sql_benchmark_report.md)

The local reader has SELECT on the 16 views. Queries run in read-only transactions with a configurable timeout, a 1,000-row limit and a default 65,536-byte serialized tool-result cap. The SQL validator restricts physical tables, functions and statement types. Restricted text and identifiers remain outside the tool interface. [Security architecture](docs/mcp_architecture.md)

The original views suppress groups below 30 source rows. Each delivered-only view withholds its whole result if a populated group contains fewer than 30 rows or 30 distinct customers. Totals can be incomplete. Overlapping aggregates, auxiliary information and comparisons across releases can still support inference. Use this snapshot with a trusted local client. [Privacy review](docs/multidimensional_privacy_review.md) · [Screenshot capture guide](screenshots/README.md)

## License

Original project code is licensed under the [MIT License](LICENSE). Copyright (c) 2026 Aary Shivalkar. The Indian and Brazilian datasets retain the separate licenses and attribution listed above.