acme-knowledge
README.md
# Unified Local MCP Server (`acme-knowledge`)
A local **Model Context Protocol (MCP)** server over `stdio` combining **Document RAG** (ChromaDB + local `all-MiniLM-L6-v2` embeddings), **read-only Text-to-SQL** (SQLite), and **fixed-function analytics, charting, EDA, and data export**—with zero server-side LLM API keys required.
---
## Architecture
```text
data/sales_data.xlsx ──► load_sql.py ──► data/sales.db (SQLite, runtime read-only)
│
data/docs/*.docx ──────► ingest.py ◄───────────┘
│
▼
chroma_db/ (excel_data & docs_data collections)
│
MCP Client (LLM) ◄──stdio──► server.py (10 tools)
├── retrieval.py (ChromaDB RAG & parameterized SQL KPIs)
└── analytics.py (charts, EDA, trend analysis, CSV/XLSX export)
```
* **Zero internal LLM keys:** Embeddings (`all-MiniLM-L6-v2`) run locally, and SQL/tool selection is performed by the connected MCP client's LLM.
* **Fast startup & clean STDIO:** Heavy dependencies (`chromadb`, `sentence-transformers`, `pandas`, `numpy`, `matplotlib`) are lazily imported on first tool use; all logs go strictly to `stderr`.
* **No arbitrary code execution:** All analytics and chart generation use fixed, schema-whitelisted Python operations (`analytics.py`) instead of `exec()`/`eval()`.
---
## Implemented MCP Tools (10)
| Tool | Category | Purpose |
| :--- | :--- | :--- |
| `list_data_sources` | Discovery | Lists SQLite tables, row counts, valid `Region`/`Month`/`Product` values, `.docx` files, and ChromaDB collections. |
| `get_database_schema` | Text-to-SQL | Returns every table's columns, SQLite types, and 3 sample rows. Call before writing SQL. |
| `run_sql_query` | Text-to-SQL | Executes a validated single read-only `SELECT` query (50-row cap, 10s timeout) and shows the exact SQL executed. |
| `sales_calculator` | KPIs | Parameterized SQL totals (`sales_totals`) and target attainment % (`target_attainment`). |
| `search_documents` | Document RAG | Semantic search over `data/docs/*.docx` (400-token chunks, 50-token overlap, top-k clamped 1–5). |
| `search_excel_rows` | Row Lookup | Semantic search over database rows indexed in `excel_data` (top-k clamped 1–5). |
| `create_chart` | Visualization | Generates `bar`, `line`, `pie`, or `histogram` PNG charts in `outputs/charts/`, returning path, inline image, and data table. |
| `run_eda` | Statistics / EDA | Runs `summary`, `column_profile`, `correlation` (with optional heatmap PNG), or IQR `outliers` on a table. |
| `run_analysis` | Trends | Computes month-over-month `trend` (with least-squares slope & `< 8` months warning) or `compare_periods`. |
| `export_data` | Export | Exports validated read-only `SELECT` results (up to 10,000 rows) to `.csv` or `.xlsx` in `outputs/exports/`. |
---
## Security Controls
* **Read-only SQLite:** Runtime DB access uses `sqlite3.connect("file:...?mode=ro", uri=True)` with a 10-second `conn.interrupt()` timeout.
* **Schema Whitelisting & Parameter Binding:** Table and column identifiers in `analytics.py` and `retrieval.py` are validated against cached DB metadata and quoted; filter values are validated against distinct DB values and bound via `?` parameters.
* **SQL Guardrails:** `run_sql_query` and `export_data` reject comments (`--`, `/*`, `*/`), non-`SELECT` statements, multi-statement `;`, and `DROP`, `DELETE`, `UPDATE`, `INSERT`, `ALTER`, `ATTACH`, `PRAGMA`, and `CREATE`.
* **Path Confinement & Retention:** Output filenames are server-generated under `outputs/{charts,eda,exports}/` with title sanitization and automatic pruning to the newest 100 files per subfolder.
---
## Quickstart (Windows)
### 1. Create virtual environment & install dependencies
```powershell
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r requirements.txt
```
### 2. Build the local SQLite database & ChromaDB index
```powershell
python refresh_data.py
```
*(Runs `load_sql.py` to build `data/sales.db` from `data/sales_data.xlsx`, then `ingest.py` to populate `chroma_db/`.)*
> **Tip:** Copy `.env.example` to `.env` or set `HF_HUB_OFFLINE=1` after the first run so `all-MiniLM-L6-v2` loads from local cache without network checks.
### 3. Configure your MCP Client
See [mcp_config.example.json](mcp_config.example.json):
```json
{
"mcpServers": {
"acme-knowledge": {
"command": "<PATH_TO_REPO>/.venv/Scripts/python.exe",
"args": ["<PATH_TO_REPO>/server.py"],
"env": {
"HF_HUB_OFFLINE": "1"
}
}
}
}
```
### 4. Run Tests
```powershell
$env:HF_HUB_OFFLINE="1"
.\.venv\Scripts\python.exe test_retrieval.py
.\.venv\Scripts\python.exe test_analytics.py
.\.venv\Scripts\python.exe test_client.py
```
---
## Project Layout
```text
SQL_MCP/
├── server.py # MCP stdio server & 10 tool wrappers
├── retrieval.py # ChromaDB retrieval & parameterized SQL KPIs
├── analytics.py # Fixed-function charts, EDA, trend analysis & exports
├── load_sql.py # Excel -> SQLite loader (offline)
├── ingest.py # SQLite & DOCX -> ChromaDB indexer (offline)
├── refresh_data.py # Runs load_sql.py then ingest.py
├── requirements.txt # Python dependencies
├── test_client.py # MCP stdio end-to-end test (10 tools)
├── test_analytics.py # Tests for create_chart, run_eda, run_analysis, export_data
├── test_retrieval.py # Known-answer & vector retrieval tests
├── mcp_config.example.json # Example MCP client configuration
├── .env.example # Optional environment variable reference
├── LICENSE # MIT License
└── data/
├── sales_data.xlsx # Sample synthetic Excel dataset
└── docs/ # Sample internal Word documents (.docx)
```
---
## License
MIT — see [LICENSE](LICENSE).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues