knowledge-rag 4.5.0 → 4.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +1706 -1678
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,1678 +1,1706 @@
1
- # Knowledge RAG
2
-
3
- <div align="center">
4
-
5
- [![PyPI](https://img.shields.io/pypi/v/knowledge-rag)](https://pypi.org/project/knowledge-rag/)
6
- [![NPM](https://img.shields.io/npm/v/knowledge-rag)](https://www.npmjs.com/package/knowledge-rag)
7
- [![PyPI Downloads](https://static.pepy.tech/personalized-badge/knowledge-rag?period=total&units=INTERNATIONAL_SYSTEM&left_color=BLACK&right_color=GREEN&left_text=downloads)](https://pepy.tech/projects/knowledge-rag)
8
- ![Python](https://img.shields.io/badge/python-3.11%2B-green.svg)
9
- ![License](https://img.shields.io/badge/license-MIT-yellow.svg)
10
- ![Platform](https://img.shields.io/badge/platform-Windows%20%7C%20Linux%20%7C%20macOS-lightgrey.svg)
11
- ![GPU](https://img.shields.io/badge/GPU-NVIDIA%20CUDA-76B900.svg?logo=nvidia)
12
- [![CI](https://github.com/lyonzin/knowledge-rag/actions/workflows/ci.yml/badge.svg)](https://github.com/lyonzin/knowledge-rag/actions/workflows/ci.yml)
13
- [![CodeQL](https://github.com/lyonzin/knowledge-rag/actions/workflows/security.yml/badge.svg)](https://github.com/lyonzin/knowledge-rag/actions/workflows/security.yml)
14
- [![Quality Gate](https://github.com/lyonzin/knowledge-rag/actions/workflows/quality-gate.yml/badge.svg)](https://github.com/lyonzin/knowledge-rag/actions/workflows/quality-gate.yml)
15
- [![Glama Score](https://glama.ai/mcp/servers/lyonzin/knowledge-rag/badges/score.svg)](https://glama.ai/mcp/servers/lyonzin/knowledge-rag)
16
-
17
- ### Your docs, your machine, zero cloud. Claude Code searches them natively.
18
-
19
- Drop your PDFs, markdown, code, notebooks — **1800+ files, 39K chunks, indexed in under 3 minutes.**<br/>
20
- Hybrid search (BM25 + semantic vectors + cross-encoder reranking) through 13 MCP tools.<br/>
21
- Everything runs locally via ONNX. No Docker, no Ollama, no API keys, no data leaves your machine.
22
-
23
- ```
24
- pip install knowledge-rag → restart Claude Code → search_knowledge("your query")
25
- ```
26
-
27
- ---
28
-
29
- **13 MCP Tools** | **Hybrid Search + Reranking** | **20 File Formats** | **Optional NVIDIA GPU** | **100% Local**
30
-
31
- [What's New](#whats-new-in-v420) | [Supported Formats](#supported-formats) | [Installation](#installation) | [Configuration](#configuration) | [API Reference](#api-reference) | [Architecture](#architecture)
32
-
33
- </div>
34
-
35
- ---
36
-
37
- ## Star History
38
-
39
- <div align="center">
40
-
41
- <a href="https://www.star-history.com/?repos=lyonzin%2Fknowledge-rag&type=date&legend=top-left">
42
- <picture>
43
- <source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/chart?repos=lyonzin/knowledge-rag&type=date&theme=dark&legend=top-left" />
44
- <source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/chart?repos=lyonzin/knowledge-rag&type=date&legend=top-left" />
45
- <img alt="Star History Chart" src="https://api.star-history.com/chart?repos=lyonzin/knowledge-rag&type=date&legend=top-left" />
46
- </picture>
47
- </a>
48
-
49
- </div>
50
-
51
- ---
52
-
53
- ## What's New in v4.2.0
54
-
55
- ### Search Performance & Output Quality (v4.2.0)
56
-
57
- **128× faster BM25 search** — replaced `rank-bm25` full-corpus scan with a custom **inverted-index** implementation. Only documents containing query terms are scored, using `numpy.argpartition` for O(n) top-k selection. Adjacent chunk fetching now uses a single batched ChromaDB call instead of N round-trips, and an O(1) reverse lookup (`_source_to_docid`) eliminates linear scans.
58
-
59
- **Smarter output** — two new parameters on `search_knowledge`:
60
- - **`snippet_mode`** (default: `true`) — truncates content to ~500 characters at natural break points, reducing token consumption by ~72%. Adds `content_length` field with original size; use `get_document()` for full content.
61
- - **`min_score`** — filters results below a normalized relevance threshold (0.0–1.0). Eliminates low-quality noise from results. Response includes `filtered_by_score` count for transparency.
62
-
63
- Both parameters are fully backwards-compatible (existing callers see no change in behavior).
64
-
65
- ### Enterprise Concurrent Access — SSE/HTTP Transport (v4.0.0)
66
-
67
- The server now supports **SSE** and **streamable-http** transport modes. Instead of spawning a separate process per client (stdio), a single server process serves all clients with shared resources — 1 embedding model, 1 ChromaDB, 1 query cache.
68
-
69
- ```yaml
70
- # config.yaml
71
- server:
72
- transport: "sse" # "stdio" | "sse" | "streamable-http"
73
- host: "127.0.0.1"
74
- port: 8179
75
- ```
76
-
77
- Or via CLI: `knowledge-rag --transport sse`
78
-
79
- **Optional enterprise features** (all disabled by default):
80
- - **Rate limiting**: Sliding-window counter, configurable RPM and burst
81
- - **Prometheus metrics**: `/metrics` endpoint on separate port
82
- - **Bearer auth**: Token validation for SSE/HTTP connections
83
-
84
- All 13 MCP tools are instrumented with `@rate_limited` and `@instrument` decorators — zero overhead when features are disabled. Default transport remains **stdio** for full backwards compatibility.
85
-
86
- > **Migration**: Existing users need zero changes. SSE mode is opt-in via `server.transport: "sse"` in config.yaml. See [Configuration](#configuration) for details.
87
-
88
- ### Quality Gate — 7-Pillar PR Validation
89
-
90
- Every PR (including dependabot bumps and one-line fixes) is now evaluated against **35+ automated checks** spread across 7 pillars before any human review:
91
-
92
- | Pillar | What it enforces | Tools |
93
- |---|---|---|
94
- | **1 Security** | SAST, secrets, CVEs, supply chain | bandit, semgrep, gitleaks, pip-audit, dependency-review, Snyk, CodeQL, Socket |
95
- | **2 Stability** | Flake detection, coverage trend, test count, deterministic runs | pytest-rerunfailures, codecov ±0.5pp, test-count guard |
96
- | **3 Memory Leak** | RSS bounded under 1000-query load, no idle bloat | psutil-based baseline tests + nightly 50K-iteration soak |
97
- | **4 Versatility** | 9 OS×Python combos, 14 format parsers, 4 config presets, locale tolerance, property-based fuzzing | matrix CI on Linux+Windows+macOS × 3.11+3.12+3.13, Hypothesis |
98
- | **5 Scalability** | Performance regression > 10% blocks merge, public bench dashboard | pytest-benchmark, GH Pages chart |
99
- | **6 Versioning** | Atomic version sync, API surface diff, conventional commits, CHANGELOG enforcement, backwards compat | griffe-style AST diff, custom guards |
100
- | **7 Quality** | Type strictness, docstring coverage, complexity, dead code | mypy strict, interrogate ≥80%, radon, vulture |
101
-
102
- Plus a **nightly resilience workflow** that runs chaos failure-injection (HF down, ChromaDB corruption, watchdog crash, ONNX zero-byte replay), determinism check (full suite × 3), and mutation testing on selected modules.
103
-
104
- Read the full philosophy in [CONTRIBUTING.md](CONTRIBUTING.md). Report bugs via [SECURITY.md](SECURITY.md) or the [issue templates](.github/ISSUE_TEMPLATE/).
105
-
106
- ### Critical Hotfix — No More Silent Zero-Vector Corruption (v3.8.1)
107
-
108
- `FastEmbedEmbeddings.__call__` no longer swallows exceptions and returns `[[0.0]*dim, ...]` when the ONNX model fails to load. That bug pre-existed in master but was silent: ChromaDB happily stored zero embeddings, `count()` reported normal numbers, smart-reindex skipped them as "already indexed", and queries returned garbage similarity with no error visible. Now raises `EmbeddingModelLoadError` / `EmbeddingError` loudly. **All v3.8.0 users should upgrade.** Full details in [Changelog](#v381-2026-05-10--hotfix).
109
-
110
- ### Lazy-Loaded Embeddings — Cheaper Idle Processes (v3.8.0)
111
-
112
- The FastEmbed ONNX model (~200MB resident) now loads on the **first query**, not at startup. Idle `knowledge-rag` processes are now genuinely cheap. Why this matters: MCP stdio is one-process-per-client by protocol — multiple Claude Code windows, Claude Desktop + IDE simultaneously, or review/approval flows that open extra connections all spawn their own processes. Before v3.8.0, every one of them paid the full embedding-model cost up front. Now only processes that actually serve queries load the model. Public API is unchanged.
113
-
114
- ### Opt-In Single-Instance Guard (v3.8.0)
115
-
116
- For users who measured their setup and want a hard cap of one server per `data_dir`:
117
-
118
- ```bash
119
- export KNOWLEDGE_RAG_SINGLE_INSTANCE=1
120
- ```
121
-
122
- A second instance exits immediately with code 75. **OFF by default** so multi-client MCP usage continues to work unchanged. Stale-PID recovery + SIGINT/SIGTERM cleanup wired correctly. Full guide in [docs/single-instance.md](docs/single-instance.md). Sample MCP config in [examples/mcp-config-single-instance.json](examples/mcp-config-single-instance.json).
123
-
124
- ### 5 Ways to Install
125
-
126
- ```bash
127
- npx -y knowledge-rag # NPM — zero setup, auto-manages Python venv
128
- pip install knowledge-rag # PyPI — classic Python install
129
- curl -fsSL .../install.sh | bash # One-line installer (Linux/macOS/Windows)
130
- docker pull ghcr.io/lyonzin/knowledge-rag # Docker — models pre-downloaded
131
- git clone ... && pip install -r ... # From source
132
- ```
133
-
134
- All methods produce the same MCP server. See [Installation](#installation) for full instructions.
135
-
136
- ### Recent Highlights
137
-
138
- - **v4.0.0** — **Enterprise concurrent access**: SSE/HTTP transport (1 server → N clients), thread-safe shared state, optional rate limiting + Prometheus metrics, ChromaDB WAL mode, `--transport` CLI
139
- - **v3.9.0** — **Quality Gate** activated: 35+ automated PR checks across 7 pillars (Security, Stability, Memory Leak, Versatility, Scalability, Versioning, Quality) + nightly resilience suite (chaos, soak, determinism, mutation)
140
- - **v3.8.1** — Critical hotfix: loud-fail embeddings (no more silent zero-vector corruption); Windows CI flake erradicated (HF_HUB_OFFLINE + shell:bash + atexit wrapper)
141
- - **v3.8.0** — Lazy-load embeddings, opt-in single-instance guard, version sync across PyPI/NPM/Docker
142
- - **v3.6.0** — Multi-language code parsing (C/C++/JS/TS/XML), NPM wrapper, Docker image, automated release pipeline
143
- - **v3.5.2** — CUDA DLL auto-discovery from pip packages, graceful GPU→CPU fallback, explicit CPU provider (no CUDA noise when `gpu: false`), BASE_DIR resolution fix for editable installs
144
- - **v3.5.1** — Remove Python `<3.13` upper bound — 3.13 and 3.14 now supported
145
- - **v3.5.0** — Optional GPU acceleration, supported formats table, full README rewrite
146
- - **v3.4.3** — MCP stdout save/restore fix (v3.4.2 broke JSON-RPC responses)
147
- - **v3.4.0** — Persistent model cache, exclude patterns, Jupyter Notebook parser, inotify resilience, MetaTrader support
148
-
149
- See [Changelog](#changelog) for full history.
150
-
151
- ---
152
-
153
- ## Supported Formats
154
-
155
- | Format | Extension | Parser | Default | Notes |
156
- |--------|-----------|--------|---------|-------|
157
- | Markdown | `.md` | Section-aware (splits at `##`) | Yes | Headers preserved as chunk boundaries |
158
- | Plain Text | `.txt` | Fixed-size chunking | Yes | 1000 chars + 200 overlap |
159
- | PDF | `.pdf` | PyMuPDF extraction | Yes | Text-based PDFs only (no OCR) |
160
- | Python | `.py` | Code-aware parser | Yes | Functions/classes as chunks |
161
- | JSON | `.json` | Structure-aware | Yes | Flattened key-value extraction |
162
- | CSV | `.csv` | Row-based parser | Yes | Headers + rows as text |
163
- | Word | `.docx` | python-docx | Yes | Headings preserved as markdown |
164
- | Excel | `.xlsx` | openpyxl | Yes | Sheet-by-sheet extraction |
165
- | PowerPoint | `.pptx` | python-pptx | Yes | Slide-by-slide extraction |
166
- | Jupyter Notebook | `.ipynb` | Cell-aware parser | Yes | Markdown + code cells only, no outputs/base64 |
167
- | C Source | `.c` | Code-aware parser | Yes | Functions/structs/includes extracted |
168
- | C/C++ Header | `.h` | Code-aware parser | Yes | Function declarations/structs extracted |
169
- | C++ Source | `.cpp` | Code-aware parser | Yes | Classes/structs/includes extracted |
170
- | JavaScript | `.js` | Code-aware parser | Yes | Functions/classes/imports (ESM + CJS) |
171
- | React JSX | `.jsx` | Code-aware parser | Yes | Same as JS parser |
172
- | TypeScript | `.ts` | Code-aware parser | Yes | Functions/classes/interfaces/enums/imports |
173
- | React TSX | `.tsx` | Code-aware parser | Yes | Same as TS parser |
174
- | XML | `.xml` | XML parser | Yes | Root element and namespace extraction |
175
- | MQL4 Header | `.mqh` | Code parser | No | MetaTrader — add to `supported_formats` to enable |
176
- | MQL4 Source | `.mq4` | Code parser | No | MetaTrader — add to `supported_formats` to enable |
177
-
178
- > **Tip:** The parser dispatch is extensible. Any format mapped in `_parsers` can be enabled via `supported_formats` in config.yaml.
179
-
180
- ---
181
-
182
- ## Features
183
-
184
- | Feature | Description |
185
- |---------|-------------|
186
- | **Hybrid Search** | Semantic + BM25 keyword search with Reciprocal Rank Fusion |
187
- | **Cross-Encoder Reranker** | Xenova/ms-marco-MiniLM-L-6-v2 re-scores top candidates for precision |
188
- | **GPU Acceleration** | Optional ONNX CUDA support for 5-10x faster indexing |
189
- | **YAML Configuration** | Fully customizable via `config.yaml` with domain-specific presets |
190
- | **Query Expansion** | Configurable synonym mappings (69 security-term defaults) |
191
- | **Markdown-Aware Chunking** | `.md` files split by `##`/`###` sections instead of fixed windows |
192
- | **In-Process Embeddings** | FastEmbed ONNX Runtime (BAAI/bge-small-en-v1.5, 384D) |
193
- | **Keyword Routing** | Word-boundary aware routing for domain-specific queries |
194
- | **20 Format Parsers** | MD, TXT, PDF, PY, C, H, CPP, JS, JSX, TS, TSX, JSON, XML, CSV, DOCX, XLSX, PPTX, IPYNB + opt-in MQH/MQ4 |
195
- | **Category Organization** | Organize docs by folder, auto-tagged by path |
196
- | **Incremental Indexing** | Change detection via mtime/size — only re-indexes modified files |
197
- | **Chunk Deduplication** | SHA256 content hashing prevents duplicate chunks |
198
- | **Query Cache** | LRU cache with 5-min TTL for instant repeat queries |
199
- | **Document CRUD** | Add, update, remove documents via MCP tools |
200
- | **URL Ingestion** | Fetch URLs, strip HTML, convert to markdown, index |
201
- | **Similarity Search** | Find documents similar to a reference document |
202
- | **Retrieval Evaluation** | Built-in MRR@5 and Recall@5 metrics |
203
- | **File Watcher** | Auto-reindex on document changes via watchdog (5s debounce) |
204
- | **Exclude Patterns** | Glob-based file/directory exclusion during indexing |
205
- | **MMR Diversification** | Maximal Marginal Relevance reduces redundant results |
206
- | **Persistent Model Cache** | Embedding models cached in `models_cache/` — survives reboots |
207
- | **Auto-Migration** | Detects embedding dimension mismatch and rebuilds automatically |
208
- | **13 MCP Tools** | Full CRUD + search + evaluation via Claude Code |
209
-
210
- ---
211
-
212
- ## Architecture
213
-
214
- ### System Overview
215
-
216
- ```mermaid
217
- flowchart TB
218
- subgraph MCP["MCP SERVER (FastMCP)"]
219
- direction TB
220
- TOOLS["13 MCP Tools<br/>search | get | add | update | remove<br/>reindex | reindex_status | list | stats | url | similar | evaluate"]
221
- end
222
-
223
- subgraph SEARCH["HYBRID SEARCH ENGINE"]
224
- direction LR
225
- ROUTER["Keyword Router<br/>(word boundaries)"]
226
- SEMANTIC["Semantic Search<br/>(ChromaDB)"]
227
- BM25["BM25 Keyword<br/>(inverted-index + expansion)"]
228
- RRF["Reciprocal Rank<br/>Fusion (RRF)"]
229
- RERANK["Cross-Encoder<br/>Reranker"]
230
-
231
- ROUTER --> SEMANTIC
232
- ROUTER --> BM25
233
- SEMANTIC --> RRF
234
- BM25 --> RRF
235
- RRF --> RERANK
236
- end
237
-
238
- subgraph STORAGE["STORAGE LAYER"]
239
- direction LR
240
- CHROMA[("ChromaDB<br/>Vector Database")]
241
- COLLECTIONS["Collections<br/>security | ctf<br/>logscale | development"]
242
- CHROMA --- COLLECTIONS
243
- end
244
-
245
- subgraph EMBED["EMBEDDINGS (In-Process)"]
246
- FASTEMBED["FastEmbed ONNX<br/>BAAI/bge-small-en-v1.5<br/>(384D, CPU or GPU)"]
247
- CROSSENC["Cross-Encoder<br/>ms-marco-MiniLM-L-6-v2"]
248
- FASTEMBED --- CROSSENC
249
- end
250
-
251
- subgraph INGEST["DOCUMENT INGESTION"]
252
- PARSERS["20 Parsers<br/>MD | PDF | TXT | PY | C | H | CPP | JS | JSX | TS | TSX | JSON | XML | CSV<br/>DOCX | XLSX | PPTX | IPYNB | MQH | MQ4"]
253
- CHUNKER["Chunking<br/>MD: section-aware<br/>Other: 1000 chars + 200 overlap"]
254
- PARSERS --> CHUNKER
255
- end
256
-
257
- CLAUDE["Claude Code"] --> MCP
258
- MCP --> SEARCH
259
- SEARCH --> STORAGE
260
- STORAGE --> EMBED
261
- INGEST --> EMBED
262
- EMBED --> STORAGE
263
- ```
264
-
265
- ### Query Processing Flow
266
-
267
- ```mermaid
268
- flowchart TB
269
- QUERY["User Query<br/>'mimikatz credential dump'"] --> EXPAND
270
-
271
- subgraph EXPANSION["Query Expansion"]
272
- EXPAND["Synonym Expansion<br/>mimikatz -> mimikatz, sekurlsa, logonpasswords"]
273
- end
274
-
275
- EXPAND --> ROUTER
276
-
277
- subgraph ROUTING["Keyword Routing"]
278
- ROUTER["Keyword Router"]
279
- MATCH{"Word Boundary<br/>Match?"}
280
- CATEGORY["Filter: redteam"]
281
- NOFILTER["No Filter"]
282
-
283
- ROUTER --> MATCH
284
- MATCH -->|Yes| CATEGORY
285
- MATCH -->|No| NOFILTER
286
- end
287
-
288
- subgraph HYBRID["Hybrid Search"]
289
- direction LR
290
- SEMANTIC["Semantic Search<br/>(ChromaDB embeddings)<br/>Conceptual similarity"]
291
- BM25["BM25 Inverted-Index<br/>(posting lists + numpy top-k)<br/>Exact term matching"]
292
- end
293
-
294
- subgraph FUSION["Result Fusion + Reranking"]
295
- RRF["Reciprocal Rank Fusion<br/>score = alpha * 1/(k+rank_sem)<br/>+ (1-alpha) * 1/(k+rank_bm25)"]
296
- RERANK["Cross-Encoder Reranker<br/>Re-scores top 3x candidates<br/>query+doc pair scoring"]
297
- SORT["Sort by Reranker Score<br/>Normalize to 0-1"]
298
- ADJ["Adjacent Chunk Expansion<br/>(batch fetch ±1 chunk)"]
299
-
300
- RRF --> RERANK --> SORT --> ADJ
301
- end
302
-
303
- subgraph OUTPUT["Output Processing"]
304
- MINSCORE["min_score Filter<br/>(discard below threshold)"]
305
- SNIPPET["snippet_mode Truncation<br/>(~500 chars at natural break)"]
306
-
307
- MINSCORE --> SNIPPET
308
- end
309
-
310
- CATEGORY --> HYBRID
311
- NOFILTER --> HYBRID
312
- SEMANTIC --> RRF
313
- BM25 --> RRF
314
-
315
- ADJ --> MINSCORE
316
- SNIPPET --> RESULTS["Results<br/>search_method: hybrid|semantic|keyword<br/>score + filtered_by_score + content_length"]
317
- ```
318
-
319
- ### Document Ingestion Flow
320
-
321
- ```mermaid
322
- flowchart LR
323
- subgraph INPUT["Input"]
324
- FILES["documents/<br/>├── security/<br/>├── development/<br/>├── ctf/<br/>└── general/"]
325
- end
326
-
327
- subgraph PARSE["Parse (20 formats)"]
328
- MD["Markdown"]
329
- PDF["PDF<br/>(PyMuPDF)"]
330
- OFFICE["DOCX | XLSX<br/>PPTX | CSV"]
331
- CODE["PY | C | H | CPP | JS | JSX<br/>TS | TSX | JSON | XML | IPYNB"]
332
- end
333
-
334
- subgraph CHUNK["Chunk"]
335
- MDSPLIT["MD: Section-Aware<br/>Split at ## headers"]
336
- TXTSPLIT["Other: Fixed-Size<br/>1000 chars + 200 overlap"]
337
- DEDUP["SHA256 Dedup<br/>Skip duplicate content"]
338
- end
339
-
340
- subgraph EMBED["Embed"]
341
- FASTEMBED["FastEmbed ONNX<br/>bge-small-en-v1.5<br/>(384D, CPU or GPU)"]
342
- end
343
-
344
- subgraph STORE["Store"]
345
- CHROMADB[("ChromaDB")]
346
- BM25IDX["BM25 Index"]
347
- end
348
-
349
- FILES --> MD & PDF & OFFICE & CODE
350
- MD --> MDSPLIT
351
- PDF & OFFICE & CODE --> TXTSPLIT
352
- MDSPLIT --> DEDUP
353
- TXTSPLIT --> DEDUP
354
- DEDUP --> EMBED
355
- EMBED --> STORE
356
- ```
357
-
358
- ### hybrid_alpha Parameter Effect
359
-
360
- ```mermaid
361
- flowchart LR
362
- subgraph ALPHA["hybrid_alpha values"]
363
- A0["0.0<br/>Pure BM25<br/>Instant"]
364
- A3["0.3 (default)<br/>Keyword-heavy<br/>Fast"]
365
- A5["0.5<br/>Balanced"]
366
- A7["0.7<br/>Semantic-heavy"]
367
- A10["1.0<br/>Pure Semantic"]
368
- end
369
-
370
- subgraph USE["Best For"]
371
- U0["CVEs, tool names<br/>exact matches"]
372
- U3["Technical queries<br/>specific terms"]
373
- U5["General queries"]
374
- U7["Conceptual queries<br/>related topics"]
375
- U10["'How to...' questions<br/>conceptual search"]
376
- end
377
-
378
- A0 --- U0
379
- A3 --- U3
380
- A5 --- U5
381
- A7 --- U7
382
- A10 --- U10
383
- ```
384
-
385
- ---
386
-
387
- ## Installation
388
-
389
- ### Prerequisites
390
-
391
- - Python 3.11+
392
- - Claude Code CLI
393
- - *…or any other MCP client (Claude Desktop, Cursor, VS Code, Antigravity, opencode, Windsurf) — see [Use with other MCP clients](#use-with-other-mcp-clients)*
394
- - ~200MB disk for model cache (auto-downloaded on first run)
395
- - *Optional:* NVIDIA GPU + CUDA 12 for accelerated embeddings (see [GPU Acceleration](#gpu-acceleration) below)
396
-
397
- ### GPU Acceleration
398
-
399
- GPU mode accelerates embedding generation during indexing and search. It requires an NVIDIA GPU with CUDA 12 support. No GPU? No problem — the server runs on CPU by default and GPU is entirely optional.
400
-
401
- **Requirements:**
402
-
403
- | Component | Minimum | How to check / get it |
404
- |-----------|---------|----------------------|
405
- | NVIDIA GPU (Turing+) | RTX 20xx / 30xx / 40xx / 50xx, or Tesla T4+ | `nvidia-smi` |
406
- | NVIDIA Driver | ≥ 525 | `nvidia-smi` — [nvidia.com/drivers](https://www.nvidia.com/drivers) |
407
- | CUDA 12 runtime | Provided by pip packages below | Automatic |
408
-
409
- **Setup (2 steps):**
410
-
411
- ```bash
412
- # 1. Install GPU dependencies (onnxruntime-gpu + all CUDA 12 runtime DLLs)
413
- pip install knowledge-rag[gpu]
414
-
415
- # 2. Enable in config.yaml
416
- # models:
417
- # embedding:
418
- # gpu: true
419
- ```
420
-
421
- The `[gpu]` extra installs `onnxruntime-gpu` plus 7 NVIDIA CUDA 12 packages (`cublas`, `cudnn`, `cuda-runtime`, `cufft`, `cusparse`, `cusolver`, `curand`, `nvjitlink`) so you don't need a full CUDA Toolkit install.
422
-
423
- **Verify GPU is active:**
424
-
425
- On server startup, look for the GPU status banner:
426
- ```
427
- ============================================================
428
- GPU STATUS: ACTIVE
429
- Provider: CUDAExecutionProvider
430
- Device: NVIDIA GeForce RTX 3080 Ti
431
- VRAM: 12.0 GB
432
- ============================================================
433
- ```
434
-
435
- Or programmatically:
436
- ```bash
437
- python -c "import onnxruntime; print(onnxruntime.get_available_providers())"
438
- # Should include: 'CUDAExecutionProvider'
439
- ```
440
-
441
- > **Fallback**: If CUDA is unavailable at runtime (wrong driver, missing DLLs, no GPU), the server falls back to CPU automatically with a `[WARN]` log — it never crashes. The `gpu: true` config is a preference, not a requirement.
442
-
443
- ### Install Methods
444
-
445
- Pick one — all produce the same running server.
446
-
447
- #### Option A: NPX (fastest)
448
-
449
- Requires Node.js 16+. Handles Python venv, pip install, and version upgrades automatically.
450
-
451
- ```bash
452
- claude mcp add knowledge-rag -s user -- npx -y knowledge-rag
453
- ```
454
-
455
- That's it. On first run, `npx` creates a venv at `~/.knowledge-rag/`, installs the PyPI package, and starts the MCP server. Subsequent runs reuse the cached venv.
456
-
457
- #### Option B: One-line installer
458
-
459
- ```bash
460
- # Linux/macOS:
461
- curl -fsSL https://raw.githubusercontent.com/lyonzin/knowledge-rag/master/install.sh | bash
462
-
463
- # Windows (PowerShell):
464
- irm https://raw.githubusercontent.com/lyonzin/knowledge-rag/master/install.ps1 | iex
465
- ```
466
-
467
- Then configure Claude Code:
468
-
469
- ```bash
470
- claude mcp add knowledge-rag -s user -- ~/knowledge-rag/venv/bin/python -m mcp_server.server
471
- ```
472
-
473
- > **Windows**: `claude mcp add knowledge-rag -s user -- %USERPROFILE%\knowledge-rag\venv\Scripts\python.exe -m mcp_server.server`
474
-
475
- #### Option C: pip install
476
-
477
- ```bash
478
- mkdir ~/knowledge-rag && cd ~/knowledge-rag
479
- python3 -m venv venv && source venv/bin/activate
480
- pip install knowledge-rag
481
- knowledge-rag init # Exports config template, presets, creates documents/
482
- ```
483
-
484
- Then configure Claude Code:
485
-
486
- ```bash
487
- claude mcp add knowledge-rag -s user -- ~/knowledge-rag/venv/bin/python -m mcp_server.server
488
- ```
489
-
490
- > **Windows users**: Use `python` instead of `python3`, `venv\Scripts\activate` instead of `source venv/bin/activate`.
491
- > **Windows path**: `claude mcp add knowledge-rag -s user -- %USERPROFILE%\knowledge-rag\venv\Scripts\python.exe -m mcp_server.server`
492
-
493
- #### Option D: Clone from source
494
-
495
- ```bash
496
- git clone https://github.com/lyonzin/knowledge-rag.git ~/knowledge-rag
497
- cd ~/knowledge-rag
498
- python3 -m venv venv && source venv/bin/activate
499
- pip install -r requirements.txt
500
- ```
501
-
502
- Then configure Claude Code:
503
-
504
- ```bash
505
- claude mcp add knowledge-rag -s user -- ~/knowledge-rag/venv/bin/python -m mcp_server.server
506
- ```
507
-
508
- #### Option E: Docker
509
-
510
- ```bash
511
- docker pull ghcr.io/lyonzin/knowledge-rag:latest
512
- ```
513
-
514
- ```bash
515
- claude mcp add knowledge-rag -s user -- \
516
- docker run -i --rm \
517
- -v ~/knowledge-rag/documents:/app/documents \
518
- -v ~/knowledge-rag/data:/app/data \
519
- ghcr.io/lyonzin/knowledge-rag:latest
520
- ```
521
-
522
- Models are pre-downloaded in the image — no first-run delay.
523
-
524
- <details>
525
- <summary>Alternative: manual JSON config</summary>
526
-
527
- Add to `~/.claude.json`:
528
-
529
- **Windows:**
530
- ```json
531
- {
532
- "mcpServers": {
533
- "knowledge-rag": {
534
- "command": "C:\\Users\\YOUR_USER\\knowledge-rag\\venv\\Scripts\\python.exe",
535
- "args": ["-m", "mcp_server.server"]
536
- }
537
- }
538
- }
539
- ```
540
-
541
- **Linux / macOS:**
542
- ```json
543
- {
544
- "mcpServers": {
545
- "knowledge-rag": {
546
- "command": "/home/YOUR_USER/knowledge-rag/venv/bin/python",
547
- "args": ["-m", "mcp_server.server"]
548
- }
549
- }
550
- }
551
- ```
552
- > Replace `YOUR_USER` with your username, or use the full path from `echo $HOME`.
553
- </details>
554
-
555
- #### Option F: SSE Server Mode (multi-agent)
556
-
557
- For multi-agent setups where multiple clients query the same knowledge base simultaneously:
558
-
559
- ```bash
560
- pip install knowledge-rag[server] # Adds uvicorn for SSE/HTTP
561
- knowledge-rag --transport sse # Starts on http://127.0.0.1:8179
562
- ```
563
-
564
- Then configure each MCP client to connect via SSE:
565
-
566
- ```json
567
- {
568
- "mcpServers": {
569
- "knowledge-rag": {
570
- "type": "sse",
571
- "url": "http://127.0.0.1:8179/sse"
572
- }
573
- }
574
- }
575
- ```
576
-
577
- One server process serves all agents — shared embedding model, shared cache, shared ChromaDB. See [Configuration > Server](#server) for rate limiting, metrics, and auth options.
578
-
579
- ### Use with other MCP clients
580
-
581
- `knowledge-rag` supports both **stdio** (default, 1:1) and **SSE** (1:N) transport modes. In stdio mode, it works with any MCP-compatible client, not only Claude Code. The launch command is the same everywhere (the `python -m mcp_server.server` from whichever install method you picked); only the **config file location** and **JSON shape** differ per client.
582
-
583
- #### Clients using the standard `mcpServers` format
584
-
585
- For **Claude Desktop, Cursor, Antigravity, and Windsurf**, use the same block — only the file location changes:
586
-
587
- ```json
588
- {
589
- "mcpServers": {
590
- "knowledge-rag": {
591
- "command": "/home/YOUR_USER/knowledge-rag/venv/bin/python",
592
- "args": ["-m", "mcp_server.server"]
593
- }
594
- }
595
- }
596
- ```
597
-
598
- > **Windows**: set `command` to the full path of `venv\Scripts\python.exe`.
599
-
600
- | Client | Config file | Notes |
601
- |---|---|---|
602
- | **Claude Code** | use `claude mcp add …` (see install methods above) | The CLI writes `~/.claude.json` for you — manual edits to it aren't reliably picked up. |
603
- | **Claude Desktop** | macOS: `~/Library/Application Support/Claude/claude_desktop_config.json` · Windows: `%APPDATA%\Claude\claude_desktop_config.json` | Easiest: **Settings → Developer → Edit Config** opens the correct file (avoids the Windows Store/MSIX path quirk). |
604
- | **Cursor** | `~/.cursor/mcp.json` (global) or `.cursor/mcp.json` (per project) | — |
605
- | **Antigravity** | macOS/Linux: `~/.gemini/antigravity/mcp_config.json` · Windows: `%USERPROFILE%\.gemini\antigravity\mcp_config.json` | Open via Agent panel → **"…" → Manage MCP Servers → View raw config**. |
606
- | **Windsurf** | `~/.codeium/windsurf/mcp_config.json` (global only) | Easiest: Cascade panel → MCP → **View raw config**. |
607
-
608
- #### VS Code — uses a `servers` key
609
-
610
- VS Code (Copilot MCP) nests servers under **`servers`**, not `mcpServers`. Put this in `.vscode/mcp.json` (workspace) or the file opened by the **MCP: Open User Configuration** command:
611
-
612
- ```json
613
- {
614
- "servers": {
615
- "knowledge-rag": {
616
- "type": "stdio",
617
- "command": "/home/YOUR_USER/knowledge-rag/venv/bin/python",
618
- "args": ["-m", "mcp_server.server"]
619
- }
620
- }
621
- }
622
- ```
623
-
624
- #### opencode — uses an `mcp` key
625
-
626
- opencode nests servers under **`mcp`**, takes `command` as a single **array**, and uses `environment` instead of `env`. Put this in `opencode.json` (project root) or `~/.config/opencode/opencode.json` (global):
627
-
628
- ```jsonc
629
- {
630
- "$schema": "https://opencode.ai/config.json",
631
- "mcp": {
632
- "knowledge-rag": {
633
- "type": "local",
634
- "command": ["/home/YOUR_USER/knowledge-rag/venv/bin/python", "-m", "mcp_server.server"],
635
- "enabled": true
636
- }
637
- }
638
- }
639
- ```
640
-
641
- > **Any other MCP client**: point it at the same command + args (`…/venv/bin/python -m mcp_server.server`). If it speaks stdio MCP, knowledge-rag works — only the config file's location and key naming differ. Check your client's docs for the exact path.
642
-
643
- ### Verify
644
-
645
- ```bash
646
- claude mcp list
647
- ```
648
-
649
- On first start, the server will:
650
- 1. Download the embedding model (~50MB, cached in `models_cache/`)
651
- 2. Auto-index any documents in the `documents/` directory
652
- 3. Start watching for file changes (auto-reindex)
653
-
654
- ---
655
-
656
- ## Usage
657
-
658
- ### Adding Documents
659
-
660
- Place your documents in the `documents/` directory, organized by category:
661
-
662
- ```
663
- documents/
664
- ├── security/ # Pentest, exploit, vulnerability docs
665
- ├── development/ # Code, APIs, frameworks
666
- ├── ctf/ # CTF writeups and methodology
667
- ├── logscale/ # LogScale/LQL documentation
668
- └── general/ # Everything else
669
- ```
670
-
671
- Or add documents programmatically via MCP tools:
672
-
673
- ```python
674
- # Add from content
675
- add_document(
676
- content="# My Document\n\nContent here...",
677
- filepath="security/my-technique.md",
678
- category="security"
679
- )
680
-
681
- # Add from URL
682
- add_from_url(
683
- url="https://example.com/article",
684
- category="security",
685
- title="Custom Title"
686
- )
687
- ```
688
-
689
- ### Searching
690
-
691
- Claude uses the RAG system automatically when configured. You can also control search behavior:
692
-
693
- ```python
694
- # Pure keyword search — instant, no embedding needed
695
- search_knowledge("gtfobins suid", hybrid_alpha=0.0)
696
-
697
- # Keyword-heavy (default) — fast, slight semantic boost
698
- search_knowledge("mimikatz", hybrid_alpha=0.3)
699
-
700
- # Balanced hybrid — both engines equally weighted
701
- search_knowledge("SQL injection techniques", hybrid_alpha=0.5)
702
-
703
- # Semantic-heavy — better for conceptual queries
704
- search_knowledge("how to escalate privileges", hybrid_alpha=0.7)
705
-
706
- # Pure semantic — embedding similarity only
707
- search_knowledge("lateral movement strategies", hybrid_alpha=1.0)
708
- ```
709
-
710
- ### Indexing
711
-
712
- Documents are automatically indexed on first startup. All reindex operations run **in background** — they return immediately and you poll progress via `get_reindex_status()`:
713
-
714
- ```python
715
- # Incremental: only re-index changed files (fast)
716
- reindex_documents()
717
-
718
- # Smart reindex: detect changes + rebuild BM25
719
- reindex_documents(force=True)
720
-
721
- # Nuclear rebuild: delete everything, re-embed all (use after model change)
722
- reindex_documents(full_rebuild=True)
723
-
724
- # Poll progress (lightweight, no full stats computation)
725
- get_reindex_status()
726
- # → {"reindex": {"active": true, "percent": 56, "progress": "2090/3734", ...}}
727
- ```
728
-
729
- ### Evaluating Retrieval Quality
730
-
731
- ```python
732
- evaluate_retrieval(test_cases='[
733
- {"query": "sql injection", "expected_filepath": "security/sqli-guide.md"},
734
- {"query": "privilege escalation", "expected_filepath": "security/privesc.md"}
735
- ]')
736
- # Returns: MRR@5, Recall@5, per-query results
737
- ```
738
-
739
- ---
740
-
741
- ## API Reference
742
-
743
- ### Search & Query
744
-
745
- #### `search_knowledge`
746
-
747
- Hybrid search combining semantic search + BM25 keyword search with cross-encoder reranking.
748
-
749
- | Parameter | Type | Default | Description |
750
- |-----------|------|---------|-------------|
751
- | `query` | string | required | Search query text (1-3 keywords recommended) |
752
- | `max_results` | int | 5 | Maximum results to return (1-20) |
753
- | `category` | string | null | Filter by category |
754
- | `hybrid_alpha` | float | 0.3 | Balance: 0.0 = keyword only, 1.0 = semantic only |
755
- | `min_score` | float | 0.0 | Minimum relevance score (0.0-1.0) to include a result. Use 0.2-0.4 to cut noise |
756
- | `snippet_mode` | bool | true | Truncate content to ~500 chars at natural break points. Adds `content_length` field |
757
-
758
- **Returns:**
759
-
760
- ```json
761
- {
762
- "status": "success",
763
- "query": "mimikatz credential dump",
764
- "hybrid_alpha": 0.5,
765
- "result_count": 3,
766
- "filtered_by_score": 2,
767
- "cache_hit_rate": "0.0%",
768
- "results": [
769
- {
770
- "content": "Mimikatz can extract credentials from memory...",
771
- "source": "documents/security/credential-attacks.md",
772
- "filename": "credential-attacks.md",
773
- "category": "security",
774
- "score": 0.9823,
775
- "raw_rrf_score": 0.016393,
776
- "reranker_score": 0.987654,
777
- "semantic_rank": 2,
778
- "bm25_rank": 1,
779
- "search_method": "hybrid",
780
- "keywords": ["mimikatz", "credential", "lsass"],
781
- "routed_by": "redteam"
782
- }
783
- ]
784
- }
785
- ```
786
-
787
- **Search Method Values:**
788
- - `hybrid`: Found by both semantic and BM25 search (highest confidence)
789
- - `semantic`: Found only by semantic search
790
- - `keyword`: Found only by BM25 keyword search
791
-
792
- ---
793
-
794
- #### `get_document`
795
-
796
- Retrieve the full content of a specific document.
797
-
798
- | Parameter | Type | Description |
799
- |-----------|------|-------------|
800
- | `filepath` | string | Path to the document file |
801
-
802
- **Returns:** JSON with document content, metadata, keywords, and chunk count.
803
-
804
- ---
805
-
806
- #### `reindex_documents`
807
-
808
- Index or reindex all documents in the knowledge base. **Runs in background** — returns immediately. Poll progress via `get_reindex_status()`.
809
-
810
- | Parameter | Type | Default | Description |
811
- |-----------|------|---------|-------------|
812
- | `force` | bool | false | Smart reindex: detects changes, rebuilds BM25. Fast. |
813
- | `full_rebuild` | bool | false | Nuclear rebuild: deletes everything, re-embeds all documents. Use after model change. |
814
-
815
- **Returns:** `{"status": "started", "operation": "..."}` immediately. If already running, returns `{"status": "already_running", "progress": "1200/3734"}`.
816
-
817
- ---
818
-
819
- #### `get_reindex_status`
820
-
821
- Get the current status of a background reindex operation. Lightweight — does not compute full index statistics.
822
-
823
- **Returns (active):**
824
- ```json
825
- {
826
- "status": "success",
827
- "reindex": {
828
- "active": true,
829
- "operation": "nuclear_rebuild",
830
- "progress": "1200/3734",
831
- "percent": 32,
832
- "indexed": 1200,
833
- "skipped": 0,
834
- "errors": 0,
835
- "started_at": "2026-06-17T18:29:49"
836
- }
837
- }
838
- ```
839
-
840
- **Returns (idle):** `{"status": "success", "reindex": {"active": false}}`
841
-
842
- ---
843
-
844
- #### `list_categories`
845
-
846
- List all document categories with their document counts.
847
-
848
- **Returns:**
849
-
850
- ```json
851
- {
852
- "status": "success",
853
- "categories": {
854
- "security": 52,
855
- "development": 8,
856
- "ctf": 12,
857
- "general": 3
858
- },
859
- "total_documents": 75
860
- }
861
- ```
862
-
863
- ---
864
-
865
- #### `list_documents`
866
-
867
- List all indexed documents, optionally filtered by category.
868
-
869
- | Parameter | Type | Description |
870
- |-----------|------|-------------|
871
- | `category` | string | Optional category filter |
872
-
873
- **Returns:** JSON array of documents with id, source, category, format, chunks, and keywords.
874
-
875
- ---
876
-
877
- #### `get_index_stats`
878
-
879
- Get statistics about the knowledge base index.
880
-
881
- **Returns:**
882
-
883
- ```json
884
- {
885
- "status": "success",
886
- "stats": {
887
- "total_documents": 75,
888
- "total_chunks": 9256,
889
- "categories": {"security": 52, "development": 8},
890
- "supported_formats": [".md", ".txt", ".pdf", ".py", ".json", ".docx", ".xlsx", ".pptx", ".csv", ".ipynb"],
891
- "embedding_model": "BAAI/bge-small-en-v1.5",
892
- "embedding_dim": 384,
893
- "reranker_model": "Xenova/ms-marco-MiniLM-L-6-v2",
894
- "chunk_size": 1000,
895
- "chunk_overlap": 200,
896
- "query_cache": {
897
- "size": 12,
898
- "max_size": 100,
899
- "ttl_seconds": 300,
900
- "hits": 45,
901
- "misses": 23,
902
- "hit_rate": "66.2%"
903
- }
904
- }
905
- }
906
- ```
907
-
908
- ---
909
-
910
- ### Document Management
911
-
912
- #### `add_document`
913
-
914
- Add a new document to the knowledge base from raw content. Saves the file to the documents directory and indexes it immediately.
915
-
916
- | Parameter | Type | Default | Description |
917
- |-----------|------|---------|-------------|
918
- | `content` | string | required | Full text content of the document |
919
- | `filepath` | string | required | Relative path within documents dir (e.g., `security/new-technique.md`) |
920
- | `category` | string | "general" | Document category |
921
-
922
- ---
923
-
924
- #### `update_document`
925
-
926
- Update an existing document. Removes old chunks from the index and re-indexes with new content.
927
-
928
- | Parameter | Type | Description |
929
- |-----------|------|-------------|
930
- | `filepath` | string | Full path to the document file |
931
- | `content` | string | New content for the document |
932
-
933
- ---
934
-
935
- #### `remove_document`
936
-
937
- Remove a document from the knowledge base index. Optionally deletes the file from disk.
938
-
939
- | Parameter | Type | Default | Description |
940
- |-----------|------|---------|-------------|
941
- | `filepath` | string | required | Path to the document file |
942
- | `delete_file` | bool | false | If true, also delete the file from disk |
943
-
944
- ---
945
-
946
- #### `add_from_url`
947
-
948
- Fetch content from a URL, strip HTML (scripts, styles, nav, footer, header), convert to markdown, and add to the knowledge base.
949
-
950
- | Parameter | Type | Default | Description |
951
- |-----------|------|---------|-------------|
952
- | `url` | string | required | URL to fetch content from |
953
- | `category` | string | "general" | Document category |
954
- | `title` | string | null | Custom title (auto-detected from `<title>` tag if not provided) |
955
-
956
- ---
957
-
958
- #### `search_similar`
959
-
960
- Find documents similar to a given document using embedding similarity.
961
-
962
- | Parameter | Type | Default | Description |
963
- |-----------|------|---------|-------------|
964
- | `filepath` | string | required | Path to the reference document |
965
- | `max_results` | int | 5 | Number of similar documents to return (1-20) |
966
-
967
- ---
968
-
969
- #### `evaluate_retrieval`
970
-
971
- Evaluate retrieval quality with test queries. Useful for tuning `hybrid_alpha`, testing query expansion effectiveness, or validating after reindexing.
972
-
973
- | Parameter | Type | Description |
974
- |-----------|------|-------------|
975
- | `test_cases` | string (JSON) | Array of test cases: `[{"query": "...", "expected_filepath": "..."}, ...]` |
976
-
977
- **Metrics:**
978
- - **MRR@5** (Mean Reciprocal Rank): Average of 1/rank for expected documents. 1.0 = always first result.
979
- - **Recall@5**: Fraction of expected documents found in top 5 results. 1.0 = all found.
980
-
981
- ---
982
-
983
- ## Configuration
984
-
985
- Knowledge RAG is fully configurable via a `config.yaml` file in the project root. If no `config.yaml` exists, sensible defaults are used — the system works out of the box with zero configuration.
986
-
987
- ### Quick Start
988
-
989
- ```bash
990
- # Option 1: Use a preset
991
- cp presets/cybersecurity.yaml config.yaml # Offensive/defensive security, CTFs
992
- cp presets/developer.yaml config.yaml # Software engineering, APIs, DevOps
993
- cp presets/research.yaml config.yaml # Academic research, papers, studies
994
- cp presets/general.yaml config.yaml # Blank slate, pure semantic search
995
-
996
- # Option 2: Start from the documented template
997
- cp config.example.yaml config.yaml
998
- # Edit config.yaml to your needs
999
- ```
1000
-
1001
- Restart Claude Code after changing `config.yaml`.
1002
-
1003
- ### config.yaml Structure
1004
-
1005
- ```yaml
1006
- # Paths — where your documents live
1007
- paths:
1008
- documents_dir: "./documents" # Scanned recursively
1009
- data_dir: "./data" # Index storage
1010
- models_cache_dir: "./models_cache" # Persistent embedding model cache
1011
-
1012
- # Documents — what gets indexed and how
1013
- documents:
1014
- supported_formats: # File types to index
1015
- - .md
1016
- - .txt
1017
- - .pdf
1018
- - .docx
1019
- - .ipynb
1020
- # - .py # Uncomment to index code
1021
- exclude_patterns: # Glob patterns to skip
1022
- - "node_modules"
1023
- - ".venv"
1024
- - "__pycache__"
1025
- chunking:
1026
- chunk_size: 1000 # Max chars per chunk
1027
- chunk_overlap: 200 # Shared chars between chunks
1028
-
1029
- # Models — AI models for search (all run locally, no API keys)
1030
- models:
1031
- embedding:
1032
- model: "BAAI/bge-small-en-v1.5" # ONNX, ~33MB, auto-downloaded
1033
- dimensions: 384
1034
- gpu: false # Set true + pip install knowledge-rag[gpu]
1035
- reranker:
1036
- enabled: true # Falls back to RRF if model is unavailable
1037
- model: "Xenova/ms-marco-MiniLM-L-6-v2"
1038
- top_k_multiplier: 3 # Candidates fetched before reranking
1039
-
1040
- # Search — result limits and collection name
1041
- search:
1042
- default_results: 5
1043
- max_results: 20
1044
- collection_name: "knowledge_base" # Change for separate knowledge bases
1045
-
1046
- # Categories — auto-tag documents by folder path
1047
- # Set to {} to disable categorization entirely
1048
- category_mappings:
1049
- "security/redteam": "redteam"
1050
- "security/blueteam": "blueteam"
1051
- "notes": "notes"
1052
-
1053
- # Keyword routing — prioritize categories based on query keywords
1054
- # Set to {} for pure semantic search with no routing bias
1055
- keyword_routes:
1056
- redteam:
1057
- - pentest
1058
- - exploit
1059
- - privilege escalation
1060
-
1061
- # Query expansion — expand abbreviations for better BM25 recall
1062
- # Set to {} for no expansion (search terms used as-is)
1063
- query_expansions:
1064
- sqli:
1065
- - sql injection
1066
- - sqli
1067
- privesc:
1068
- - privilege escalation
1069
- - privesc
1070
-
1071
- # Server — enterprise features (new in v4.0.0)
1072
- server:
1073
- transport: "stdio" # "stdio" | "sse" | "streamable-http"
1074
- host: "127.0.0.1" # Bind address (SSE/HTTP only)
1075
- port: 8179 # Bind port (SSE/HTTP only)
1076
- auth:
1077
- bearer_token: "" # Set a secret to enable auth (SSE/HTTP only)
1078
- rate_limit:
1079
- enabled: false
1080
- requests_per_minute: 60
1081
- burst: 10
1082
- metrics:
1083
- enabled: false
1084
- port: 9179 # Separate port for Prometheus scraping
1085
- ```
1086
-
1087
- > See `config.example.yaml` for the fully documented template with explanations for every field.
1088
-
1089
- ### Presets
1090
-
1091
- Pre-built configurations for common use cases:
1092
-
1093
- | Preset | File | Categories | Keywords | Expansions | Best For |
1094
- |--------|------|-----------|----------|-----------|----------|
1095
- | **Cybersecurity** | `presets/cybersecurity.yaml` | 8 | 200+ | 69 | Red/Blue Team, CTFs, threat hunting, exploit dev |
1096
- | **Developer** | `presets/developer.yaml` | 9 | 150+ | 50+ | Full-stack dev, APIs, DevOps, cloud, databases |
1097
- | **Research** | `presets/research.yaml` | 9 | 100+ | 40+ | Academic papers, thesis, lab notebooks, datasets |
1098
- | **General** | `presets/general.yaml` | 0 | 0 | 0 | Blank slate — pure semantic search, no domain logic |
1099
-
1100
- **Creating your own preset**: Copy `config.example.yaml`, fill in your categories/keywords/expansions, save to `presets/your-domain.yaml`.
1101
-
1102
- ### Configuration Reference
1103
-
1104
- #### Server
1105
-
1106
- | Field | Default | Description |
1107
- |-------|---------|-------------|
1108
- | `server.transport` | `"stdio"` | Transport protocol: `"stdio"`, `"sse"`, or `"streamable-http"` |
1109
- | `server.host` | `"127.0.0.1"` | Bind address for SSE/HTTP mode |
1110
- | `server.port` | `8179` | Bind port for SSE/HTTP mode |
1111
- | `server.auth.bearer_token` | `""` (disabled) | Bearer token for SSE/HTTP auth. Empty = no auth |
1112
- | `server.rate_limit.enabled` | `false` | Enable per-client rate limiting |
1113
- | `server.rate_limit.requests_per_minute` | `60` | Max requests per minute |
1114
- | `server.rate_limit.burst` | `10` | Burst allowance above steady rate |
1115
- | `server.metrics.enabled` | `false` | Enable Prometheus `/metrics` endpoint |
1116
- | `server.metrics.port` | `9179` | Port for metrics scraping |
1117
-
1118
- In stdio mode (default), server settings are ignored. SSE/HTTP mode auto-enables the single-instance lock.
1119
-
1120
- #### Paths
1121
-
1122
- | Field | Default | Description |
1123
- |-------|---------|-------------|
1124
- | `paths.documents_dir` | `./documents` | Root folder scanned recursively for documents |
1125
- | `paths.data_dir` | `./data` | Internal storage for ChromaDB and index metadata |
1126
- | `paths.models_cache_dir` | `./models_cache` | Persistent cache for embedding models (~250MB). Survives reboots |
1127
-
1128
- Relative paths resolve from the project root. Absolute paths work too.
1129
-
1130
- #### Documents
1131
-
1132
- | Field | Default | Description |
1133
- |-------|---------|-------------|
1134
- | `documents.supported_formats` | .md .txt .pdf .py .json .docx .xlsx .pptx .csv .ipynb | File extensions to index |
1135
- | `documents.exclude_patterns` | `[]` (empty) | Glob patterns for files/dirs to skip during indexing |
1136
- | `documents.chunking.chunk_size` | 1000 | Max characters per chunk |
1137
- | `documents.chunking.chunk_overlap` | 200 | Characters shared between consecutive chunks |
1138
-
1139
- **Chunking guidelines**: Short notes → 500/100. General use → 1000/200. Long technical docs → 1500/300.
1140
-
1141
- For `.md` files, chunking splits at `##` and `###` header boundaries first. Sections larger than `chunk_size` are sub-chunked with overlap. Non-markdown files use fixed-size chunking.
1142
-
1143
- #### Models
1144
-
1145
- | Field | Default | Description |
1146
- |-------|---------|-------------|
1147
- | `models.embedding.model` | `BAAI/bge-small-en-v1.5` | Embedding model (ONNX, runs locally) |
1148
- | `models.embedding.dimensions` | 384 | Vector dimensions (must match model) |
1149
- | `models.embedding.gpu` | false | Enable CUDA GPU acceleration. See [GPU Acceleration](#gpu-acceleration) for full setup |
1150
- | `models.reranker.enabled` | true | Enable cross-encoder reranking |
1151
- | `models.reranker.model` | `Xenova/ms-marco-MiniLM-L-6-v2` | Reranker model |
1152
- | `models.reranker.top_k_multiplier` | 3 | Fetch N*multiplier candidates for reranking |
1153
-
1154
- If the reranker model is not available locally and the machine cannot download it, search now falls back to the RRF order from hybrid semantic+BM25 retrieval. This keeps `search_knowledge` available offline, but result ordering may be less precise for ambiguous queries until the reranker model is cached.
1155
-
1156
- **Embedding model options** (fastest → most accurate):
1157
- - `BAAI/bge-small-en-v1.5` — 384D, ~33MB (default)
1158
- - `BAAI/bge-base-en-v1.5` — 768D, ~130MB
1159
- - `BAAI/bge-large-en-v1.5` — 1024D, ~335MB
1160
- - `intfloat/multilingual-e5-small` — 384D, 100+ languages
1161
-
1162
- > **Warning**: Changing the embedding model after indexing requires `reindex_documents(full_rebuild=True)`.
1163
-
1164
- #### Search
1165
-
1166
- | Field | Default | Description |
1167
- |-------|---------|-------------|
1168
- | `search.default_results` | 5 | Results returned when no limit specified |
1169
- | `search.max_results` | 20 | Hard cap even if client requests more |
1170
- | `search.collection_name` | `knowledge_base` | ChromaDB collection — change for separate KBs |
1171
-
1172
- #### Categories
1173
-
1174
- Map folder paths to category names. Documents in matching folders get auto-tagged, enabling filtered searches.
1175
-
1176
- ```yaml
1177
- category_mappings:
1178
- "security/redteam": "redteam"
1179
- "security": "security"
1180
- ```
1181
-
1182
- Set `category_mappings: {}` to disable — documents are still searchable, just without category filters.
1183
-
1184
- #### Keyword Routing
1185
-
1186
- Route queries to categories based on keywords. When a query contains listed keywords, results from that category are prioritized (not filtered — other categories still appear, ranked lower).
1187
-
1188
- ```yaml
1189
- keyword_routes:
1190
- redteam:
1191
- - pentest
1192
- - exploit
1193
- - sqli
1194
- ```
1195
-
1196
- Single-word keywords use regex word boundaries (`\b`) — "api" won't match "RAPID". Multi-word keywords use substring matching.
1197
-
1198
- Set `keyword_routes: {}` for pure semantic search.
1199
-
1200
- #### Query Expansion
1201
-
1202
- Expand search terms with synonyms before BM25 search. Supports single tokens, bigrams, and full query matches.
1203
-
1204
- ```yaml
1205
- query_expansions:
1206
- sqli:
1207
- - sql injection
1208
- - sqli
1209
- k8s:
1210
- - kubernetes
1211
- - k8s
1212
- ```
1213
-
1214
- Set `query_expansions: {}` for no expansion.
1215
-
1216
- `query_expansions` is directional: only the key on the left triggers the terms on the right. If you need mutual expansion without duplicating entries, use `query_expansion_groups`.
1217
-
1218
- ```yaml
1219
- query_expansion_groups:
1220
- - ["triple barrier", "tb", "trip_barr"]
1221
- - ["profit factor", "pf"]
1222
- ```
1223
-
1224
- Each group is interpreted symmetrically, so every term expands to the rest of the group. The final internal expansion table is built by merging both sources:
1225
-
1226
- 1. `query_expansions` entries are loaded as-is.
1227
- 2. `query_expansion_groups` adds reciprocal links for every term in each group.
1228
- 3. Overlaps are merged by union with duplicate terms removed.
1229
-
1230
- This keeps backward compatibility while allowing concise synonym groups.
1231
-
1232
- ### Hybrid Search Tuning
1233
-
1234
- | hybrid_alpha | Behavior | Best For |
1235
- |--------------|----------|----------|
1236
- | 0.0 | Pure BM25 keyword | Exact terms, CVEs, tool names |
1237
- | 0.3 | Keyword-heavy **(default)** | Technical queries with specific terms |
1238
- | 0.5 | Balanced | General queries |
1239
- | 0.7 | Semantic-heavy | Conceptual queries, related topics |
1240
- | 1.0 | Pure semantic | "How to..." questions, abstract concepts |
1241
-
1242
- ---
1243
-
1244
- ## Project Structure
1245
-
1246
- ```
1247
- knowledge-rag/
1248
- ├── mcp_server/
1249
- │ ├── __init__.py # Stdout protection + version
1250
- │ ├── config.py # YAML config loader + defaults
1251
- │ ├── ingestion.py # 20 parsers, chunking, metadata extraction
1252
- │ └── server.py # MCP server, ChromaDB, BM25, reranker, 12 tools
1253
- ├── config.example.yaml # Documented config template (copy to config.yaml)
1254
- ├── config.yaml # Your active configuration (git-ignored)
1255
- ├── presets/ # Ready-to-use domain configurations
1256
- │ ├── cybersecurity.yaml
1257
- │ ├── developer.yaml
1258
- │ ├── research.yaml
1259
- │ └── general.yaml
1260
- ├── documents/ # Your documents (scanned recursively)
1261
- ├── data/
1262
- │ ├── chroma_db/ # ChromaDB vector database
1263
- │ └── index_metadata.json # Incremental indexing state
1264
- ├── models_cache/ # Persistent embedding model cache
1265
- ├── tests/ # Test suite (82 tests)
1266
- ├── install.sh # Linux/macOS installer
1267
- ├── install.ps1 # Windows installer
1268
- ├── venv/ # Python virtual environment
1269
- ├── requirements.txt
1270
- ├── pyproject.toml
1271
- ├── LICENSE
1272
- └── README.md
1273
- ```
1274
-
1275
- ---
1276
-
1277
- ## Troubleshooting
1278
-
1279
- ### Python version mismatch
1280
-
1281
- Requires Python 3.11 or newer.
1282
-
1283
- ```bash
1284
- python --version # Must be 3.11+
1285
- ```
1286
-
1287
- ### FastEmbed model download fails
1288
-
1289
- On first run, FastEmbed downloads models to `models_cache/`. If the download fails:
1290
-
1291
- ```bash
1292
- # Clear cache and retry
1293
- # Windows:
1294
- rmdir /s /q models_cache
1295
-
1296
- # Linux/macOS:
1297
- rm -rf models_cache
1298
-
1299
- # Then restart the MCP server
1300
- ```
1301
-
1302
- ### Reranker model download fails
1303
-
1304
- The reranker is lazy-loaded on the first query. If the model is not cached and the machine is offline, search continues without reranking and uses the RRF order from hybrid retrieval. To keep reranking enabled offline, run one query while online or pre-populate `models_cache/` on the target machine.
1305
-
1306
- You can still disable reranking explicitly in `config.yaml`:
1307
-
1308
- ```yaml
1309
- models:
1310
- reranker:
1311
- enabled: false
1312
- ```
1313
-
1314
- Disabling reranking reduces memory use and avoids first-query model loading. The tradeoff is lower ranking precision, especially when several chunks match the same terms but only one is the best answer.
1315
-
1316
- ### ChromaDB index crashes on startup
1317
-
1318
- Native ChromaDB failures can terminate Python before normal exception handling runs. Startup now probes ChromaDB in a child process before initializing the MCP server. If the probe crashes, the active `chroma_db/` and `index_metadata.json` are moved to `data/backups/auto-repair-*`, and the next startup can rebuild a clean index.
1319
-
1320
- The same guarded behavior is available through either console script:
1321
-
1322
- ```bash
1323
- knowledge-rag
1324
- knowledge-rag-guarded
1325
- ```
1326
-
1327
- ### Index is empty
1328
-
1329
- ```bash
1330
- # Check documents directory has files
1331
- ls documents/
1332
-
1333
- # Force reindex via Claude Code:
1334
- # reindex_documents(force=True)
1335
-
1336
- # Or nuclear rebuild if model changed:
1337
- # reindex_documents(full_rebuild=True)
1338
- ```
1339
-
1340
- ### MCP server not loading
1341
-
1342
- 1. Check `~/.claude.json` exists and has valid JSON in the `mcpServers` section
1343
- 2. Verify paths use double backslashes (`\\`) on Windows
1344
- 3. Restart Claude Code completely
1345
- 4. Run `claude mcp list` to check connection status
1346
-
1347
- ### "Failed to connect" error
1348
-
1349
- The MCP server uses stdout for JSON-RPC communication. If a library prints to stdout during init, the stream gets corrupted. v3.4.3+ includes stdout protection that prevents this. If you're on an older version, upgrade:
1350
-
1351
- ```bash
1352
- pip install --upgrade knowledge-rag
1353
- ```
1354
-
1355
- ### Slow first query
1356
-
1357
- The cross-encoder reranker model is lazy-loaded on the first query. This adds a one-time ~2-3 second delay for model download and loading. Subsequent queries are fast. If the model cannot be loaded, search falls back to RRF ordering and does not retry loading the reranker until the server restarts.
1358
-
1359
- ### Memory usage
1360
-
1361
- With ~200 documents, expect ~300-500MB RAM. The embedding model (~200MB ONNX runtime resident, lazy-loaded on first query since v3.8.0) and reranker (~25MB, lazy-loaded) are loaded into memory only when actually used. For very large knowledge bases (1000+ documents), consider enabling GPU acceleration and using exclude patterns to limit index scope.
1362
-
1363
- ### Multiple MCP clients spawn duplicate servers
1364
-
1365
- MCP stdio is one process per client by protocol — multiple Claude Code windows, Claude Desktop + IDE, etc. each spawn their own `knowledge-rag` process. Since v3.8.0 idle processes are cheap (no embedding model loaded until first query). If you've measured and want a hard cap of one server per data directory, opt in:
1366
-
1367
- ```bash
1368
- export KNOWLEDGE_RAG_SINGLE_INSTANCE=1
1369
- ```
1370
-
1371
- A second instance exits immediately with code 75. Default is OFF (multi-client friendly). Full guide: [docs/single-instance.md](docs/single-instance.md). Sample MCP config: [examples/mcp-config-single-instance.json](examples/mcp-config-single-instance.json).
1372
-
1373
- ### SSE server won't start
1374
-
1375
- ```bash
1376
- # Check if port 8179 is already in use
1377
- # Windows:
1378
- netstat -aon | findstr :8179
1379
- # Linux/macOS:
1380
- lsof -i :8179
1381
- ```
1382
-
1383
- If `uvicorn` is not found, install the server extras: `pip install knowledge-rag[server]`
1384
-
1385
- ### Can't connect to SSE server
1386
-
1387
- Verify the server is running and the URL is correct:
1388
-
1389
- ```bash
1390
- curl http://127.0.0.1:8179/sse
1391
- ```
1392
-
1393
- Common issues:
1394
- - Wrong URL: must end with `/sse` (not just the port)
1395
- - Firewall blocking the port
1396
- - Server started with a different host/port than configured in the MCP client
1397
-
1398
- ---
1399
-
1400
- ## Changelog
1401
-
1402
- ### Unreleased
1403
-
1404
- ### v4.5.0 (2026-07-06) — Hybrid Search Ranking & Routing Fix
1405
-
1406
- - **FIX**: `search_knowledge` now searches the entire index when the user omits `category_filter`. The internal `_route_by_keywords()` heuristic previously acted as a hard where-filter on both the semantic and BM25 branches, so queries whose terms happened to map to a sparsely-populated category could return two documents while thousands of relevant chunks in other categories were silently dropped. The router is now **informational only**: the `routed_by` field is still populated in every result for telemetry (public API unchanged), but the candidate set is never restricted. Users who want a hard filter still get it by passing `category_filter=...` explicitly — which continues to filter BM25 consistently with #109. (#112)
1407
- - **NEW**: Hybrid search adds a small bounded ranking boost when query terms match indexed `source` or `filename` metadata, helping navigational queries surface the canonical file for a topic instead of adjacent files that only cross-reference it. The boost is capped at ~20% of typical RRF magnitudes, so it breaks near-ties without overriding semantic or BM25 signal. Public API unchanged. (#110, thanks @Hohlas)
1408
- - **TEST**: New `TestKeywordRoutingBehavior` (3 tests) pins the routing fix — BM25 and semantic branches must both see the full corpus when no explicit filter is passed, and explicit `category_filter` still overrides the router. New `TestPathAwareRanking` covers the path-metadata boost. Baseline: 266 → 271.
1409
- - **UPGRADE NOTE**: warm `query_cache` entries from before v4.5.0 should be invalidated by restarting the server so cached responses no longer reflect the pre-fix restrictive behavior.
1410
-
1411
- ### v4.4.0 (2026-07-06) — Cross-Platform Installer & Hybrid Search Category Filter
1412
-
1413
- - **NEW**: Cross-platform, multi-LLM-client installer (`install.py`) driving both `install.sh` (Linux/macOS) and `install.ps1` (Windows) as thin wrappers. One codebase, one behavior across every OS.
1414
- - **NEW**: Auto-detects and registers `knowledge-rag` in 8 LLM clients — Claude Code, Claude Desktop, Cursor, Windsurf, VS Code (Copilot Chat), Cline, Gemini CLI, Zed — writing to each tool's canonical config path with the correct JSON schema per client (VS Code uses `servers`, Zed uses `context_servers`, everyone else uses `mcpServers`).
1415
- - **NEW**: `--for <clients>` / `--exclude <clients>` opt-in/opt-out selection, `--dry-run` preview, `--list-clients` registry inspection, `--pypi-version <ver>` pinning, `--skip-init` / `--skip-model` for fast reruns. See `python install.py --help`.
1416
- - **FIX**: `install.ps1` no longer writes MCP config to `~/.claude/mcp.json` (a stale secondary path); it now targets `~/.claude.json` — the file Claude Code actually reads — via idempotent JSON merge that preserves every existing MCP server entry with an automatic `.knowledge-rag.bak` backup.
1417
- - **FIX**: `install.ps1` gains PyPI mode (`pip install knowledge-rag`) and runs `mcp_server.server init` on install — feature parity with `install.sh`.
1418
- - **FIX**: MCP server spec no longer uses the fragile `cmd /c cd /d ... && python ...` wrapper on Windows; it emits the standard `command` + `cwd` shape supported natively by every modern client.
1419
- - **FIX**: `install.sh` guards against `sh install.sh` (bash-only features now emit a clear error instead of a cryptic syntax failure).
1420
- - **FIX**: Both scripts now correctly advertise **13 MCP tools** (was outdated at 12; `get_reindex_status` shipped in v4.3.0).
1421
- - **FIX**: Windows-side `install.ps1` prefers `winget install Python.Python.3.12 --scope user` (no admin), falls back to python.org 3.12.7 (was pinned to 3.12.0).
1422
- - **FIX**: Hybrid search now applies the active category filter to BM25 results before RRF fusion, preventing keyword-only BM25 hits from other categories from leaking into filtered searches. Both leak paths are closed: BM25 candidates are metadata-filtered before RRF (with `top_k` widened to `max_results * 20` to compensate for post-filter drop), and the fallback fetch during fusion re-checks `category` before adding a chunk to `combined_scores`. Note: the `category_filter` guard now also applies to the keyword-routed category — `_route_by_keywords()`-inferred routing filters BM25 too, consistent with the semantic branch. Users with warm `query_cache` entries should restart the server to invalidate stale results. (#109, thanks @Hohlas)
1423
- - **TEST**: New `tests/test_installer_no_data_loss.py` (22 tests) locks in the installer's zero-data-loss contract across all three JSON schemas (`mcpServers` / `servers` / `context_servers`): top-level keys preserved, sibling MCP servers byte-identical, `.knowledge-rag.bak` backup written before every mutation, idempotent second run, `--dry-run` writes nothing, atomic `os.replace` write. Baseline: 231 → 266.
1424
- - **TEST**: New `TestHybridCategoryFilter::test_bm25_results_respect_category_filter` in `tests/test_search.py` — deterministic regression covering BM25-only hits with mixed categories. Baseline: 266 → 267.
1425
-
1426
- ### v4.3.1 (2026-06-22) — Hybrid Search Fixes
1427
-
1428
- - **FIX**: Accept `"general"` as a valid category in `search_knowledge`. The parser hardcodes `"general"` as the fallback in `_detect_category` (`ingestion.py`), but the validator only built `valid_categories` from `config.keyword_routes` + `config.category_mappings.values()` — so users who customized `config.yaml` and dropped the default `"general": "general"` mapping hit `Invalid category` even though the index contained `general` documents. Validator now always tolerates `"general"`. (#98, thanks @Hohlas)
1429
- - **FIX**: Skip BM25-only search results when Chroma can no longer resolve the chunk ID. Stale BM25 indices (typically right after `remove_document` or in the window between async reindex and BM25 rebuild) returned hits whose `collection.get()` came back empty; the previous fallback inserted entries with `document=""` / `metadata={}` into the reranker, polluting results with empty matches. The pipeline now `continue`s past those, dropping the stale hit cleanly. (#98, thanks @Hohlas)
1430
- - **TEST**: Added `tests/test_pr98_regression.py` (4 tests) pinning both contracts so future refactors cannot silently revert either fix. Test count baseline: 227 → 231. (#99)
1431
- - **CI**: Bumped `[tool.mypy] python_version` from 3.11 to 3.12 to accept PEP 695 `type` statements in the numpy stub (`numpy/__init__.pyi`) which were breaking the Pillar 7 strict gate. Only affects static analysis; `requires-python = ">=3.11"` unchanged. (#100)
1432
-
1433
- ### v4.3.0 (2026-06-17) — Async Reindex, GPU CUDA 12, 13th MCP Tool
1434
-
1435
- - **NEW**: `get_reindex_status` MCP tool — lightweight reindex progress polling without computing full index stats. Returns active/idle status, percent, processed/total, errors, and last result.
1436
- - **NEW**: `reindex_documents` now runs in background via daemon thread — returns immediately with `{"status": "started"}`. Eliminates MCP timeout on large document sets (5K+ files). Concurrent calls return `already_running` with current progress.
1437
- - **NEW**: GPU acceleration with full CUDA 12 support — `onnxruntime-gpu` + 7 NVIDIA pip packages (`cublas`, `cudnn`, `cuda-runtime`, `cufft`, `cusparse`, `cusolver`, `curand`, `nvjitlink`). Server auto-detects GPU on startup with 4-step verification (providers, DLLs, nvidia-smi, session creation). Falls back to CPU gracefully.
1438
- - **NEW**: `_setup_cuda_dll_paths()` adds NVIDIA pip package DLL directories to `PATH` automatically on Windows — onnxruntime finds CUDA 12 DLLs without a full CUDA Toolkit install.
1439
- - **DEPS**: `[gpu]` extra expanded from 3 to 8 packages (added `cufft`, `cusparse`, `cusolver`, `curand`, `nvjitlink`).
1440
- - **FIX**: GPU status reporting now uses actual ONNX session creation test instead of just checking `get_available_providers()` — prevents false "GPU ACTIVE" when CUDA DLLs are missing.
1441
- - **DOCS**: GPU Acceleration section rewritten with complete requirements table, setup steps, verification instructions, and fallback behavior.
1442
- - **DOCS**: Tool reference updated — `reindex_documents` async behavior documented, `get_reindex_status` reference added.
1443
- - **TEST**: Backwards-compat baseline updated for 13 MCP tools.
1444
-
1445
- ### v4.2.0 (2026-06-17) — Search Performance & Output Quality
1446
-
1447
- - **PERF**: Custom inverted-index BM25 replaces `rank-bm25` full-corpus scan — 128× faster keyword search on 50K+ chunk corpora. Only documents containing query terms are scored via posting lists.
1448
- - **PERF**: `numpy.argpartition` for O(n) top-k selection instead of O(n log n) sort.
1449
- - **PERF**: Batched adjacent chunk fetch — single ChromaDB `collection.get()` call replaces N round-trips per result.
1450
- - **PERF**: O(1) reverse lookup via `_source_to_docid` dict eliminates linear scans of `_indexed_docs` in `search_similar`, `update_document`, `remove_document`, and `_expand_with_adjacent_chunks`.
1451
- - **NEW**: `snippet_mode` parameter on `search_knowledge` (default: `true`) — truncates content to ~500 chars at natural break points with `content_length` field. Reduces token consumption by ~72%.
1452
- - **NEW**: `min_score` parameter on `search_knowledge` (default: `0.0`) — filters results below a normalized relevance threshold. Response includes `filtered_by_score` count.
1453
- - **NEW**: `filtered_by_score` field in search response JSON for transparency.
1454
- - **DEPS**: `numpy` added as direct dependency (was transitive via fastembed); `rank-bm25` import removed from server.py.
1455
- - **TEST**: 6 new tests for `min_score` filtering and `snippet_mode` truncation.
1456
- - **TEST**: Updated backwards-compat baseline to include new `search_knowledge` parameters.
1457
-
1458
- ### v4.1.2 (2026-06-17)
1459
-
1460
- - **FIX**: `_save_metadata` dict snapshot prevents concurrent modification crash during file watcher events.
1461
- - **STYLE**: ruff format applied to server.py.
1462
-
1463
- ### v4.1.1 (2026-06-17)
1464
-
1465
- - **FIX**: All `_indexed_docs` iterations now use `list()` snapshot, preventing `dictionary changed size during iteration` crash when FileWatcher modifies the index concurrently with MCP tool calls (affects `search_knowledge`, `search_similar`, `update_document`, `remove_document`, `evaluate_retrieval`, `list_categories`, `list_documents`)
1466
-
1467
- ### v4.1.0 (2026-06-17)
1468
-
1469
- - **Added:** `query_expansion_groups` config for symmetric synonym expansion (#92)
1470
- - **Improved:** `expand_query()` now returns deterministic expansion order (set → ordered list with dedup)
1471
-
1472
- ### v4.0.1 (2026-06-16)
1473
-
1474
- - **FIX**: Orphan cleanup now runs before indexing loop, preventing chunk loss when files are moved (#90).
1475
- - **FIX**: Chunk deduplication is now per-document instead of global, preventing cross-document chunk deletion (#91).
1476
- - **FIX**: Added `on_moved` handler to `DocumentWatcher` for proper file move detection.
1477
- - **FIX**: Startup preflight probes ChromaDB in a child process and moves crashing persistent indexes to `data/backups/auto-repair-*` before MCP initialization.
1478
- - **FIX**: Reranker load failures now fall back to RRF ordering instead of failing `search_knowledge` on offline machines.
1479
- - **FIX**: Virtualenv project-root detection now handles Python symlinks that resolve to the system interpreter.
1480
- - **NEW**: `knowledge-rag-guarded` console script kept as an explicit guarded startup alias.
1481
-
1482
- ### v4.0.0 (2026-06-09) — Enterprise Concurrent Access
1483
-
1484
- - **NEW**: SSE and streamable-http transport modes — 1 server serves N clients (`server.transport: "sse"` in config.yaml or `--transport sse` CLI).
1485
- - **NEW**: Thread-safe shared state for concurrent queries — QueryCache locking, BM25 build lock, orchestrator double-checked locking.
1486
- - **NEW**: ChromaDB WAL mode enabled automatically in SSE/HTTP mode for concurrent read performance.
1487
- - **NEW**: Optional rate limiting — sliding-window counter, configurable RPM and burst, disabled by default.
1488
- - **NEW**: Optional Prometheus metrics endpoint — tool call counts, latency histograms, separate port, disabled by default.
1489
- - **NEW**: All 13 MCP tools instrumented with `@rate_limited` and `@instrument` decorators (zero-cost when disabled).
1490
- - **NEW**: `--transport` CLI override for Docker/systemd deployments.
1491
- - **NEW**: `pip install knowledge-rag[server]` optional dependency for SSE/HTTP (uvicorn).
1492
- - **CHANGED**: SSE/HTTP mode auto-enables single-instance lock (port collision prevention).
1493
- - **CHANGED**: `mcp` dependency bumped to `>=1.6.0` (SSE/streamable-http support).
1494
- - **MIGRATION**: Default transport remains `stdio` — existing users need zero changes. See config.example.yaml for SSE setup.
1495
-
1496
- ### v3.9.1 (2026-06-08)
1497
-
1498
- - **FIX**: Expand `~` in `config.yaml` path values (`documents_dir`, `data_dir`, `models_cache_dir`) via `expanduser()` on all platforms (#86).
1499
- - **FIX**: Warn when `documents_dir` resolves to a non-existent path instead of silently indexing zero files.
1500
- - **FIX**: File watcher now uses accumulate-mode debounce — bulk file copies no longer starve the reindex trigger.
1501
- - **FIX**: Concurrent `index_all()` calls are serialized via `_index_lock` to prevent ChromaDB SQLite corruption.
1502
- - **FIX**: `collection.add()` is batched (500 chunks/call) to cap memory usage during large reindex operations.
1503
- - **NEW**: `KNOWLEDGE_RAG_WATCHER_DISABLED=1` env var to disable the file watcher for troubleshooting.
1504
- - **NEW**: Progress logging every 10% for reindex operations with >100 documents.
1505
-
1506
- ### v3.9.0 (2026-05-10) — Quality Gate
1507
-
1508
- **Major governance + CI hardening release. No runtime behavior change in `mcp_server/`. Public API surface unchanged from v3.8.1.**
1509
-
1510
- - **NEW** Quality Gate workflow (`.github/workflows/quality-gate.yml`) enforcing the 7 pillars on every PR: Security, Stability, Memory Leak, Versatility, Scalability, Versioning, Quality. 35+ status checks total.
1511
- - **NEW** Nightly resilience workflow (`.github/workflows/nightly.yml`): chaos suite (failure injection), 1h soak test (50K-iteration loop), determinism check (full suite × 3), mutation testing (mutmut). Auto-opens GitHub issue on any nightly failure.
1512
- - **NEW** Performance benchmark suite under `bench/` (12 microbenchmarks, pytest-benchmark) with 10% regression gate on every PR.
1513
- - **NEW** Public performance dashboard via GitHub Pages (`.github/workflows/bench-pages.yml`) — chart of latency/throughput per commit. Dormant until repo Pages is enabled.
1514
- - **NEW** Property-based fuzzing of all parsers via Hypothesis (`tests/test_ingestion_property.py`) — 200 random examples per CI run.
1515
- - **NEW** Memory baseline regression tests (`tests/test_memory_baseline.py`, cross-platform via psutil) — RSS bounded under 1000 queries; nightly soak amplifies to 50K iterations.
1516
- - **NEW** Property/locale/format/preset matrices (`tests/test_presets.py`, `tests/test_locale.py`, `tests/test_format_smoke.py`).
1517
- - **NEW** Backwards-compatibility regression tests (`tests/test_backwards_compat.py`) — legacy YAML configs from v3.6.0 / v3.7.0 still parse; all 13 MCP tool parameter names frozen.
1518
- - **NEW** AST-based public API surface diff (`scripts/check_api_surface.py`) — any breaking change blocks merge, baseline at `.github/api-surface-baseline.json`.
1519
- - **NEW** CHANGELOG enforcement (`scripts/check_changelog.py`) — user-facing PRs must add a bullet under `## Unreleased`; bypass via `skip-changelog` label.
1520
- - **NEW** Test count anti-regression (`scripts/check_test_count.py`) — guards against silent test deletion.
1521
- - **NEW** Conventional commits required on every PR title (commitlint via `amannn/action-semantic-pull-request`).
1522
- - **NEW** mypy `--strict` rolling out per-module (currently `instance_lock.py` + `preflight.py` + `scripts/`); interrogate docstring coverage ≥ 80%; radon, vulture, PR-size guard report-only.
1523
- - **NEW** CI matrix expanded to 9 cells: Linux + Windows + **macOS** × 3.11 + 3.12 + **3.13** (all required at v3.9.0; macOS / 3.13 promoted from experimental after two clean cycles).
1524
- - **NEW** Governance docs: `CONTRIBUTING.md`, `CODE_OF_CONDUCT.md`, `SECURITY.md`, `.github/PULL_REQUEST_TEMPLATE.md`, 3 issue templates, expanded `CODEOWNERS`.
1525
- - **NEW** Pre-commit hooks: ruff, gitleaks, version-sync, conventional commits.
1526
- - **CHORE** `.github/codecov.yml` enforcing coverage trend gate (-0.5pp blocks; new code ≥ 70%).
1527
-
1528
- ### v3.8.1 (2026-05-10) — hotfix
1529
-
1530
- - **FIX (critical)**: `FastEmbedEmbeddings.__call__` no longer returns vectors of zeros when the ONNX model fails to load or `embed()` raises. The previous behavior silently corrupted the index — ChromaDB stored zero embeddings, `count()` reported normal numbers, smart-reindex skipped the bad chunks, and queries returned garbage scores with no error visible. Now raises `EmbeddingModelLoadError` / `EmbeddingError`. (#36)
1531
- - **FIX**: Sticky `_load_failed` flag — after a load failure, subsequent calls re-raise immediately instead of looping through HuggingFace download attempts (was the "frozen query" UX in v3.8.0).
1532
- - **NEW**: Sanity checks in `__call__` — embed count and dim mismatches raise `EmbeddingError` instead of silently returning malformed vectors.
1533
- - **TEST**: 7 new regression cases in `tests/test_lazy_embeddings.py`, including `test_does_not_return_zero_vectors_silently` as a guard for the whole class of bug.
1534
- - **NOTE**: This is a pre-existing bug in master, not introduced by v3.8.0. v3.8.0 lazy-load expanded the impact (failures moved to query time). All v3.8.0 users should upgrade.
1535
-
1536
- ### v3.8.0 (2026-05-10)
1537
-
1538
- - **NEW**: Lazy-load FastEmbed embedding model (~200MB ONNX runtime). Loads on first query instead of startup — idle `knowledge-rag` processes are now cheap, which matters when MCP stdio clients spawn parallel server processes (multiple Claude Code windows, Claude Desktop + IDE, etc.). Public API unchanged. (#32)
1539
- - **NEW**: Opt-in single-instance guard via `KNOWLEDGE_RAG_SINGLE_INSTANCE=1` env var. **OFF by default** — multi-client MCP usage continues to work unchanged. When enabled, a second server process for the same `data_dir` exits with code 75 (`EX_TEMPFAIL`). Includes stale-PID recovery and SIGINT/SIGTERM handlers. See [docs/single-instance.md](docs/single-instance.md). (#33, original concept by @Hohlas in #31)
1540
- - **NEW**: `examples/mcp-config-single-instance.json` — sample MCP client config for the opt-in guard.
1541
- - **DOCS**: New `docs/single-instance.md` — when to use, when NOT to use, troubleshooting, full activation reference.
1542
- - **DOCS**: README troubleshooting section for "Multiple MCP clients spawn duplicate servers" + memory-usage note for lazy embeddings.
1543
- - **CHORE**: Sync version across `pyproject.toml`, `mcp_server/__init__.py`, and `npm/package.json` (was drifting since v3.5.x).
1544
- - **CHORE**: pytest `tmp_path_retention_count=1` to avoid Windows atexit cleanup race in CI.
1545
- - **ROADMAP**: Tracked v4.0 shared-service architecture (one daemon, many thin MCP clients) as the long-term fix for multi-process resource duplication. (#34)
1546
-
1547
- ### v3.6.2 (2026-04-23)
1548
-
1549
- - **INFRA**: NPM provenance attestation (SLSA supply chain security), full README on npm page
1550
- - **DOCS**: Reorganize Installation section — add NPX and Docker install methods, update What's New to v3.6.0
1551
-
1552
- ### v3.6.0 (2026-04-23)
1553
-
1554
- - **NEW**: Multi-language code parsing — C (`.c`), C++ (`.cpp`/`.h`), JavaScript (`.js`/`.jsx`), TypeScript (`.ts`/`.tsx`) with per-language function/class/import extraction
1555
- - **NEW**: XML parser (`.xml`) — root element and namespace metadata extraction
1556
- - **NEW**: All 8 new formats default enabled — no config change needed
1557
- - **NEW**: NPM wrapper (`npx knowledge-rag`) + Docker image (`ghcr.io/lyonzin/knowledge-rag`)
1558
- - **NEW**: Automated release pipeline — PyPI (Trusted Publishing), NPM, Docker GHCR
1559
- - **IMPROVED**: Code parser reports correct `language` metadata per file type (was hardcoded to `"python"` for all code files)
1560
-
1561
- ### v3.5.2 (2026-04-16)
1562
-
1563
- - **NEW**: Auto-discovery of CUDA 12 DLLs from pip-installed NVIDIA packages — no manual PATH configuration needed
1564
- - **NEW**: Graceful GPU→CPU fallback with `[WARN]` log when CUDA init fails (missing drivers, wrong version, etc.)
1565
- - **FIX**: Explicit `CPUExecutionProvider` when `gpu: false` — eliminates noisy CUDA probe errors in logs
1566
- - **FIX**: BASE_DIR resolution now correctly prefers directories with `config.yaml` over those with only `config.example.yaml` (fixes editable installs)
1567
-
1568
- ### v3.5.1 (2026-04-16)
1569
-
1570
- - **FIX**: Removed Python upper bound constraint (`<3.13` → `>=3.11`). Python 3.13 and 3.14 now supported — onnxruntime ships wheels for both.
1571
-
1572
- ### v3.5.0 (2026-04-16)
1573
-
1574
- - **NEW**: Optional GPU acceleration for ONNX embeddings — `pip install knowledge-rag[gpu]` + `models.embedding.gpu: true` in config. 5-10x faster indexing on NVIDIA GPUs with automatic CPU fallback.
1575
- - **DOCS**: Supported formats table added to README (20 formats)
1576
-
1577
- ### v3.4.3 (2026-04-16)
1578
-
1579
- - **FIX**: Correct stdout protection via save/restore pattern — `__init__.py` saves original stdout and redirects to stderr during init, `server.py main()` restores it before `mcp.run()`. v3.4.2's global redirect broke MCP JSON-RPC response channel.
1580
-
1581
- ### v3.4.1 (2026-04-16)
1582
-
1583
- - **FIX**: `pip install knowledge-rag` now auto-detects project directory from venv location
1584
- - **NEW**: `install.sh` — Linux/macOS installer with pip and from-source modes
1585
- - **IMPROVED**: BASE_DIR resolution chain: env var → source dir → venv parent → CWD → fallback
1586
-
1587
- ### v3.4.0 (2026-04-16)
1588
-
1589
- - **NEW**: `models_cache_dir` — persistent embedding model cache, prevents re-download after reboots
1590
- - **NEW**: `exclude_patterns` — glob-based file/directory exclusion during indexing
1591
- - **NEW**: Jupyter Notebook (.ipynb) parser — extracts markdown and code cell sources only
1592
- - **NEW**: MCP stdout protection — redirects stdout to stderr before server start
1593
- - **NEW**: File watcher resilience — graceful fallback when Linux inotify limits are reached
1594
- - **NEW**: MetaTrader (.mq4, .mqh) support — opt-in code parsing
1595
- - **NEW**: 23 new tests (exclude patterns, ipynb parser, stdout protection)
1596
- - Community credit: [@Hohlas](https://github.com/Hohlas) ([PR #18](https://github.com/lyonzin/knowledge-rag/pull/18))
1597
-
1598
- ### v3.3.x
1599
-
1600
- - **v3.3.2**: Full type validation on YAML config, bounds checking, version sync
1601
- - **v3.3.1**: YAML null value crash fix, presets bundled in pip wheel, `knowledge-rag init` CLI
1602
- - **v3.3.0**: YAML configuration system, 4 domain presets, generic use support
1603
-
1604
- ### v3.2.x
1605
-
1606
- - **v3.2.4**: Symlink support with circular loop protection
1607
- - **v3.2.3**: BASE_DIR smart detection for pip installs
1608
- - **v3.2.2**: Plug-and-play pip install, `KNOWLEDGE_RAG_DIR` env var
1609
- - **v3.2.1**: Auto-recovery from corrupted ChromaDB
1610
- - **v3.2.0**: Parallel BM25 + Semantic search, adjacent chunk retrieval
1611
-
1612
- ### v3.1.x
1613
-
1614
- - **v3.1.1**: Code block protection in markdown chunker, AAR category, 14 CVE aliases
1615
- - **v3.1.0**: DOCX/XLSX/PPTX/CSV support, file watcher, MMR diversification, PyPI publish
1616
-
1617
- ### v3.0.0 (2026-03-19)
1618
-
1619
- - Replaced Ollama with FastEmbed (ONNX in-process)
1620
- - Cross-encoder reranking, markdown-aware chunking, query expansion
1621
- - 6 new MCP tools (12 total), auto-migration from v2.x
1622
-
1623
- <details>
1624
- <summary>v2.x and earlier</summary>
1625
-
1626
- - **v2.2.0**: `hybrid_alpha=0` skips Ollama, default changed from 0.5 to 0.3
1627
- - **v2.1.0**: Mermaid architecture diagrams
1628
- - **v2.0.0**: Hybrid search, RRF fusion, `hybrid_alpha` parameter
1629
- - **v1.1.0**: Incremental indexing, query cache, chunk deduplication
1630
- - **v1.0.1**: Auto-cleanup orphan folders, removed hardcoded paths
1631
- - **v1.0.0**: Initial release
1632
- </details>
1633
-
1634
- ---
1635
-
1636
- ## Contributing
1637
-
1638
- 1. Fork the repository
1639
- 2. Create a feature branch (`git checkout -b feature/amazing-feature`)
1640
- 3. Commit your changes
1641
- 4. Push to the branch (`git push origin feature/amazing-feature`)
1642
- 5. Open a Pull Request
1643
-
1644
- ---
1645
-
1646
- ## License
1647
-
1648
- This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
1649
-
1650
- ---
1651
-
1652
- ## Acknowledgments
1653
-
1654
- - [ChromaDB](https://www.trychroma.com/) — Vector database
1655
- - [FastEmbed](https://qdrant.github.io/fastembed/) — ONNX Runtime embeddings
1656
- - [FastMCP](https://github.com/anthropics/mcp) — Model Context Protocol framework
1657
- - [PyMuPDF](https://pymupdf.readthedocs.io/) — PDF parsing
1658
- - [rank-bm25](https://github.com/dorianbrown/rank_bm25) — BM25 Okapi implementation
1659
- - [Watchdog](https://github.com/gorakhargosh/watchdog) — File system monitoring
1660
- - [python-docx](https://python-docx.readthedocs.io/) / [openpyxl](https://openpyxl.readthedocs.io/) / [python-pptx](https://python-pptx.readthedocs.io/) — Office document parsing
1661
- - [PyYAML](https://pyyaml.org/) — YAML configuration parsing
1662
- - [Beautiful Soup](https://www.crummy.com/software/BeautifulSoup/) — HTML parsing for URL ingestion
1663
-
1664
- ---
1665
-
1666
- ## Author
1667
-
1668
- **Lyon.**
1669
-
1670
- Security Researcher | Developer
1671
-
1672
- ---
1673
-
1674
- <div align="center">
1675
-
1676
- **[Back to Top](#knowledge-rag)**
1677
-
1678
- </div>
1
+ # Knowledge RAG
2
+
3
+ <div align="center">
4
+
5
+ [![PyPI](https://img.shields.io/pypi/v/knowledge-rag)](https://pypi.org/project/knowledge-rag/)
6
+ [![NPM](https://img.shields.io/npm/v/knowledge-rag)](https://www.npmjs.com/package/knowledge-rag)
7
+ [![PyPI Downloads](https://static.pepy.tech/personalized-badge/knowledge-rag?period=total&units=INTERNATIONAL_SYSTEM&left_color=BLACK&right_color=GREEN&left_text=downloads)](https://pepy.tech/projects/knowledge-rag)
8
+ ![Python](https://img.shields.io/badge/python-3.11%2B-green.svg)
9
+ ![License](https://img.shields.io/badge/license-MIT-yellow.svg)
10
+ ![Platform](https://img.shields.io/badge/platform-Windows%20%7C%20Linux%20%7C%20macOS-lightgrey.svg)
11
+ ![GPU](https://img.shields.io/badge/GPU-NVIDIA%20CUDA-76B900.svg?logo=nvidia)
12
+ [![CI](https://github.com/lyonzin/knowledge-rag/actions/workflows/ci.yml/badge.svg)](https://github.com/lyonzin/knowledge-rag/actions/workflows/ci.yml)
13
+ [![CodeQL](https://github.com/lyonzin/knowledge-rag/actions/workflows/security.yml/badge.svg)](https://github.com/lyonzin/knowledge-rag/actions/workflows/security.yml)
14
+ [![Quality Gate](https://github.com/lyonzin/knowledge-rag/actions/workflows/quality-gate.yml/badge.svg)](https://github.com/lyonzin/knowledge-rag/actions/workflows/quality-gate.yml)
15
+ [![Glama Score](https://glama.ai/mcp/servers/lyonzin/knowledge-rag/badges/score.svg)](https://glama.ai/mcp/servers/lyonzin/knowledge-rag)
16
+ [![OpenSSF Best Practices](https://bestpractices.coreinfrastructure.org/projects/13864/badge)](https://bestpractices.coreinfrastructure.org/projects/13864)
17
+
18
+ ### Your docs, your machine, zero cloud. Claude Code searches them natively.
19
+
20
+ Drop your PDFs, markdown, code, notebooks — **1800+ files, 39K chunks, indexed in under 3 minutes.**<br/>
21
+ Hybrid search (BM25 + semantic vectors + cross-encoder reranking) through 13 MCP tools.<br/>
22
+ Everything runs locally via ONNX. No Docker, no Ollama, no API keys, no data leaves your machine.
23
+
24
+ ```
25
+ pip install knowledge-rag → restart Claude Code → search_knowledge("your query")
26
+ ```
27
+
28
+ ---
29
+
30
+ **13 MCP Tools** | **Hybrid Search + Reranking** | **20 File Formats** | **Optional NVIDIA GPU** | **100% Local**
31
+
32
+ [What's New](#whats-new-in-v420) | [Supported Formats](#supported-formats) | [Installation](#installation) | [Configuration](#configuration) | [API Reference](#api-reference) | [Architecture](#architecture)
33
+
34
+ </div>
35
+
36
+ ---
37
+
38
+ ## Star History
39
+
40
+ <div align="center">
41
+
42
+ <a href="https://www.star-history.com/?repos=lyonzin%2Fknowledge-rag&type=date&legend=top-left">
43
+ <img alt="Star History Chart" src="./docs/star-history.svg" width="720" />
44
+ </a>
45
+
46
+ <sub>Chart updated daily by <a href="./.github/workflows/update-star-history.yml">GitHub Action</a></sub>
47
+
48
+ </div>
49
+
50
+ ---
51
+
52
+ ## What's New in v4.2.0
53
+
54
+ ### Search Performance & Output Quality (v4.2.0)
55
+
56
+ **128× faster BM25 search** — replaced `rank-bm25` full-corpus scan with a custom **inverted-index** implementation. Only documents containing query terms are scored, using `numpy.argpartition` for O(n) top-k selection. Adjacent chunk fetching now uses a single batched ChromaDB call instead of N round-trips, and an O(1) reverse lookup (`_source_to_docid`) eliminates linear scans.
57
+
58
+ **Smarter output** — two new parameters on `search_knowledge`:
59
+ - **`snippet_mode`** (default: `true`) — truncates content to ~500 characters at natural break points, reducing token consumption by ~72%. Adds `content_length` field with original size; use `get_document()` for full content.
60
+ - **`min_score`** — filters results below a normalized relevance threshold (0.0–1.0). Eliminates low-quality noise from results. Response includes `filtered_by_score` count for transparency.
61
+
62
+ Both parameters are fully backwards-compatible (existing callers see no change in behavior).
63
+
64
+ ### Enterprise Concurrent Access — SSE/HTTP Transport (v4.0.0)
65
+
66
+ The server now supports **SSE** and **streamable-http** transport modes. Instead of spawning a separate process per client (stdio), a single server process serves all clients with shared resources — 1 embedding model, 1 ChromaDB, 1 query cache.
67
+
68
+ ```yaml
69
+ # config.yaml
70
+ server:
71
+ transport: "sse" # "stdio" | "sse" | "streamable-http"
72
+ host: "127.0.0.1"
73
+ port: 8179
74
+ ```
75
+
76
+ Or via CLI: `knowledge-rag --transport sse`
77
+
78
+ **Optional enterprise features** (all disabled by default):
79
+ - **Rate limiting**: Sliding-window counter, configurable RPM and burst
80
+ - **Prometheus metrics**: `/metrics` endpoint on separate port
81
+ - **Bearer auth**: Token validation for SSE/HTTP connections
82
+
83
+ All 13 MCP tools are instrumented with `@rate_limited` and `@instrument` decorators — zero overhead when features are disabled. Default transport remains **stdio** for full backwards compatibility.
84
+
85
+ > **Migration**: Existing users need zero changes. SSE mode is opt-in via `server.transport: "sse"` in config.yaml. See [Configuration](#configuration) for details.
86
+
87
+ ### Quality Gate — 7-Pillar PR Validation
88
+
89
+ Every PR (including dependabot bumps and one-line fixes) is now evaluated against **35+ automated checks** spread across 7 pillars before any human review:
90
+
91
+ | Pillar | What it enforces | Tools |
92
+ |---|---|---|
93
+ | **1 Security** | SAST, secrets, CVEs, supply chain | bandit, semgrep, gitleaks, pip-audit, dependency-review, Snyk, CodeQL, Socket |
94
+ | **2 Stability** | Flake detection, coverage trend, test count, deterministic runs | pytest-rerunfailures, codecov ±0.5pp, test-count guard |
95
+ | **3 Memory Leak** | RSS bounded under 1000-query load, no idle bloat | psutil-based baseline tests + nightly 50K-iteration soak |
96
+ | **4 Versatility** | 9 OS×Python combos, 14 format parsers, 4 config presets, locale tolerance, property-based fuzzing | matrix CI on Linux+Windows+macOS × 3.11+3.12+3.13, Hypothesis |
97
+ | **5 Scalability** | Performance regression > 10% blocks merge, public bench dashboard | pytest-benchmark, GH Pages chart |
98
+ | **6 Versioning** | Atomic version sync, API surface diff, conventional commits, CHANGELOG enforcement, backwards compat | griffe-style AST diff, custom guards |
99
+ | **7 Quality** | Type strictness, docstring coverage, complexity, dead code | mypy strict, interrogate ≥80%, radon, vulture |
100
+
101
+ Plus a **nightly resilience workflow** that runs chaos failure-injection (HF down, ChromaDB corruption, watchdog crash, ONNX zero-byte replay), determinism check (full suite × 3), and mutation testing on selected modules.
102
+
103
+ Read the full philosophy in [CONTRIBUTING.md](CONTRIBUTING.md). Report bugs via [SECURITY.md](SECURITY.md) or the [issue templates](.github/ISSUE_TEMPLATE/).
104
+
105
+ ### Critical Hotfix — No More Silent Zero-Vector Corruption (v3.8.1)
106
+
107
+ `FastEmbedEmbeddings.__call__` no longer swallows exceptions and returns `[[0.0]*dim, ...]` when the ONNX model fails to load. That bug pre-existed in master but was silent: ChromaDB happily stored zero embeddings, `count()` reported normal numbers, smart-reindex skipped them as "already indexed", and queries returned garbage similarity with no error visible. Now raises `EmbeddingModelLoadError` / `EmbeddingError` loudly. **All v3.8.0 users should upgrade.** Full details in [Changelog](#v381-2026-05-10--hotfix).
108
+
109
+ ### Lazy-Loaded Embeddings — Cheaper Idle Processes (v3.8.0)
110
+
111
+ The FastEmbed ONNX model (~200MB resident) now loads on the **first query**, not at startup. Idle `knowledge-rag` processes are now genuinely cheap. Why this matters: MCP stdio is one-process-per-client by protocol — multiple Claude Code windows, Claude Desktop + IDE simultaneously, or review/approval flows that open extra connections all spawn their own processes. Before v3.8.0, every one of them paid the full embedding-model cost up front. Now only processes that actually serve queries load the model. Public API is unchanged.
112
+
113
+ ### Opt-In Single-Instance Guard (v3.8.0)
114
+
115
+ For users who measured their setup and want a hard cap of one server per `data_dir`:
116
+
117
+ ```bash
118
+ export KNOWLEDGE_RAG_SINGLE_INSTANCE=1
119
+ ```
120
+
121
+ A second instance exits immediately with code 75. **OFF by default** so multi-client MCP usage continues to work unchanged. Stale-PID recovery + SIGINT/SIGTERM cleanup wired correctly. Full guide in [docs/single-instance.md](docs/single-instance.md). Sample MCP config in [examples/mcp-config-single-instance.json](examples/mcp-config-single-instance.json).
122
+
123
+ ### 5 Ways to Install
124
+
125
+ ```bash
126
+ npx -y knowledge-rag # NPM — zero setup, auto-manages Python venv
127
+ pip install knowledge-rag # PyPI — classic Python install
128
+ curl -fsSL .../install.sh | bash # One-line installer (Linux/macOS/Windows)
129
+ docker pull ghcr.io/lyonzin/knowledge-rag # Docker — models pre-downloaded
130
+ git clone ... && pip install -r ... # From source
131
+ ```
132
+
133
+ All methods produce the same MCP server. See [Installation](#installation) for full instructions.
134
+
135
+ ### Recent Highlights
136
+
137
+ - **v4.0.0** — **Enterprise concurrent access**: SSE/HTTP transport (1 server → N clients), thread-safe shared state, optional rate limiting + Prometheus metrics, ChromaDB WAL mode, `--transport` CLI
138
+ - **v3.9.0** — **Quality Gate** activated: 35+ automated PR checks across 7 pillars (Security, Stability, Memory Leak, Versatility, Scalability, Versioning, Quality) + nightly resilience suite (chaos, soak, determinism, mutation)
139
+ - **v3.8.1** — Critical hotfix: loud-fail embeddings (no more silent zero-vector corruption); Windows CI flake erradicated (HF_HUB_OFFLINE + shell:bash + atexit wrapper)
140
+ - **v3.8.0** — Lazy-load embeddings, opt-in single-instance guard, version sync across PyPI/NPM/Docker
141
+ - **v3.6.0** — Multi-language code parsing (C/C++/JS/TS/XML), NPM wrapper, Docker image, automated release pipeline
142
+ - **v3.5.2** — CUDA DLL auto-discovery from pip packages, graceful GPU→CPU fallback, explicit CPU provider (no CUDA noise when `gpu: false`), BASE_DIR resolution fix for editable installs
143
+ - **v3.5.1** — Remove Python `<3.13` upper bound — 3.13 and 3.14 now supported
144
+ - **v3.5.0** — Optional GPU acceleration, supported formats table, full README rewrite
145
+ - **v3.4.3** — MCP stdout save/restore fix (v3.4.2 broke JSON-RPC responses)
146
+ - **v3.4.0** — Persistent model cache, exclude patterns, Jupyter Notebook parser, inotify resilience, MetaTrader support
147
+
148
+ See [Changelog](#changelog) for full history.
149
+
150
+ ---
151
+
152
+ ## Supported Formats
153
+
154
+ | Format | Extension | Parser | Default | Notes |
155
+ |--------|-----------|--------|---------|-------|
156
+ | Markdown | `.md` | Section-aware (splits at `##`) | Yes | Headers preserved as chunk boundaries |
157
+ | Plain Text | `.txt` | Fixed-size chunking | Yes | 1000 chars + 200 overlap |
158
+ | PDF | `.pdf` | PyMuPDF extraction | Yes | Text-based PDFs only (no OCR) |
159
+ | Python | `.py` | Code-aware parser | Yes | Functions/classes as chunks |
160
+ | JSON | `.json` | Structure-aware | Yes | Flattened key-value extraction |
161
+ | CSV | `.csv` | Row-based parser | Yes | Headers + rows as text |
162
+ | Word | `.docx` | python-docx | Yes | Headings preserved as markdown |
163
+ | Excel | `.xlsx` | openpyxl | Yes | Sheet-by-sheet extraction |
164
+ | PowerPoint | `.pptx` | python-pptx | Yes | Slide-by-slide extraction |
165
+ | Jupyter Notebook | `.ipynb` | Cell-aware parser | Yes | Markdown + code cells only, no outputs/base64 |
166
+ | C Source | `.c` | Code-aware parser | Yes | Functions/structs/includes extracted |
167
+ | C/C++ Header | `.h` | Code-aware parser | Yes | Function declarations/structs extracted |
168
+ | C++ Source | `.cpp` | Code-aware parser | Yes | Classes/structs/includes extracted |
169
+ | JavaScript | `.js` | Code-aware parser | Yes | Functions/classes/imports (ESM + CJS) |
170
+ | React JSX | `.jsx` | Code-aware parser | Yes | Same as JS parser |
171
+ | TypeScript | `.ts` | Code-aware parser | Yes | Functions/classes/interfaces/enums/imports |
172
+ | React TSX | `.tsx` | Code-aware parser | Yes | Same as TS parser |
173
+ | XML | `.xml` | XML parser | Yes | Root element and namespace extraction |
174
+ | MQL4 Header | `.mqh` | Code parser | No | MetaTrader — add to `supported_formats` to enable |
175
+ | MQL4 Source | `.mq4` | Code parser | No | MetaTrader — add to `supported_formats` to enable |
176
+
177
+ > **Tip:** The parser dispatch is extensible. Any format mapped in `_parsers` can be enabled via `supported_formats` in config.yaml.
178
+
179
+ ---
180
+
181
+ ## Features
182
+
183
+ | Feature | Description |
184
+ |---------|-------------|
185
+ | **Hybrid Search** | Semantic + BM25 keyword search with Reciprocal Rank Fusion |
186
+ | **Cross-Encoder Reranker** | Xenova/ms-marco-MiniLM-L-6-v2 re-scores top candidates for precision |
187
+ | **GPU Acceleration** | Optional ONNX CUDA support for 5-10x faster indexing |
188
+ | **YAML Configuration** | Fully customizable via `config.yaml` with domain-specific presets |
189
+ | **Query Expansion** | Configurable synonym mappings (69 security-term defaults) |
190
+ | **Markdown-Aware Chunking** | `.md` files split by `##`/`###` sections instead of fixed windows |
191
+ | **In-Process Embeddings** | FastEmbed ONNX Runtime (BAAI/bge-small-en-v1.5, 384D) |
192
+ | **Keyword Routing** | Word-boundary aware routing for domain-specific queries |
193
+ | **20 Format Parsers** | MD, TXT, PDF, PY, C, H, CPP, JS, JSX, TS, TSX, JSON, XML, CSV, DOCX, XLSX, PPTX, IPYNB + opt-in MQH/MQ4 |
194
+ | **Category Organization** | Organize docs by folder, auto-tagged by path |
195
+ | **Incremental Indexing** | Change detection via mtime/size — only re-indexes modified files |
196
+ | **Chunk Deduplication** | SHA256 content hashing prevents duplicate chunks |
197
+ | **Query Cache** | LRU cache with 5-min TTL for instant repeat queries |
198
+ | **Document CRUD** | Add, update, remove documents via MCP tools |
199
+ | **URL Ingestion** | Fetch URLs, strip HTML, convert to markdown, index |
200
+ | **Similarity Search** | Find documents similar to a reference document |
201
+ | **Retrieval Evaluation** | Built-in MRR@5 and Recall@5 metrics |
202
+ | **File Watcher** | Auto-reindex on document changes via watchdog (5s debounce) |
203
+ | **Exclude Patterns** | Glob-based file/directory exclusion during indexing |
204
+ | **MMR Diversification** | Maximal Marginal Relevance reduces redundant results |
205
+ | **Persistent Model Cache** | Embedding models cached in `models_cache/` — survives reboots |
206
+ | **Auto-Migration** | Detects embedding dimension mismatch and rebuilds automatically |
207
+ | **13 MCP Tools** | Full CRUD + search + evaluation via Claude Code |
208
+
209
+ ---
210
+
211
+ ## Architecture
212
+
213
+ ### System Overview
214
+
215
+ ```mermaid
216
+ flowchart TB
217
+ subgraph MCP["MCP SERVER (FastMCP)"]
218
+ direction TB
219
+ TOOLS["13 MCP Tools<br/>search | get | add | update | remove<br/>reindex | reindex_status | list | stats | url | similar | evaluate"]
220
+ end
221
+
222
+ subgraph SEARCH["HYBRID SEARCH ENGINE"]
223
+ direction LR
224
+ ROUTER["Keyword Router<br/>(word boundaries)"]
225
+ SEMANTIC["Semantic Search<br/>(ChromaDB)"]
226
+ BM25["BM25 Keyword<br/>(inverted-index + expansion)"]
227
+ RRF["Reciprocal Rank<br/>Fusion (RRF)"]
228
+ RERANK["Cross-Encoder<br/>Reranker"]
229
+
230
+ ROUTER --> SEMANTIC
231
+ ROUTER --> BM25
232
+ SEMANTIC --> RRF
233
+ BM25 --> RRF
234
+ RRF --> RERANK
235
+ end
236
+
237
+ subgraph STORAGE["STORAGE LAYER"]
238
+ direction LR
239
+ CHROMA[("ChromaDB<br/>Vector Database")]
240
+ COLLECTIONS["Collections<br/>security | ctf<br/>logscale | development"]
241
+ CHROMA --- COLLECTIONS
242
+ end
243
+
244
+ subgraph EMBED["EMBEDDINGS (In-Process)"]
245
+ FASTEMBED["FastEmbed ONNX<br/>BAAI/bge-small-en-v1.5<br/>(384D, CPU or GPU)"]
246
+ CROSSENC["Cross-Encoder<br/>ms-marco-MiniLM-L-6-v2"]
247
+ FASTEMBED --- CROSSENC
248
+ end
249
+
250
+ subgraph INGEST["DOCUMENT INGESTION"]
251
+ PARSERS["20 Parsers<br/>MD | PDF | TXT | PY | C | H | CPP | JS | JSX | TS | TSX | JSON | XML | CSV<br/>DOCX | XLSX | PPTX | IPYNB | MQH | MQ4"]
252
+ CHUNKER["Chunking<br/>MD: section-aware<br/>Other: 1000 chars + 200 overlap"]
253
+ PARSERS --> CHUNKER
254
+ end
255
+
256
+ CLAUDE["Claude Code"] --> MCP
257
+ MCP --> SEARCH
258
+ SEARCH --> STORAGE
259
+ STORAGE --> EMBED
260
+ INGEST --> EMBED
261
+ EMBED --> STORAGE
262
+ ```
263
+
264
+ ### Query Processing Flow
265
+
266
+ ```mermaid
267
+ flowchart TB
268
+ QUERY["User Query<br/>'mimikatz credential dump'"] --> EXPAND
269
+
270
+ subgraph EXPANSION["Query Expansion"]
271
+ EXPAND["Synonym Expansion<br/>mimikatz -> mimikatz, sekurlsa, logonpasswords"]
272
+ end
273
+
274
+ EXPAND --> ROUTER
275
+
276
+ subgraph ROUTING["Keyword Routing"]
277
+ ROUTER["Keyword Router"]
278
+ MATCH{"Word Boundary<br/>Match?"}
279
+ CATEGORY["Filter: redteam"]
280
+ NOFILTER["No Filter"]
281
+
282
+ ROUTER --> MATCH
283
+ MATCH -->|Yes| CATEGORY
284
+ MATCH -->|No| NOFILTER
285
+ end
286
+
287
+ subgraph HYBRID["Hybrid Search"]
288
+ direction LR
289
+ SEMANTIC["Semantic Search<br/>(ChromaDB embeddings)<br/>Conceptual similarity"]
290
+ BM25["BM25 Inverted-Index<br/>(posting lists + numpy top-k)<br/>Exact term matching"]
291
+ end
292
+
293
+ subgraph FUSION["Result Fusion + Reranking"]
294
+ RRF["Reciprocal Rank Fusion<br/>score = alpha * 1/(k+rank_sem)<br/>+ (1-alpha) * 1/(k+rank_bm25)"]
295
+ RERANK["Cross-Encoder Reranker<br/>Re-scores top 3x candidates<br/>query+doc pair scoring"]
296
+ SORT["Sort by Reranker Score<br/>Normalize to 0-1"]
297
+ ADJ["Adjacent Chunk Expansion<br/>(batch fetch ±1 chunk)"]
298
+
299
+ RRF --> RERANK --> SORT --> ADJ
300
+ end
301
+
302
+ subgraph OUTPUT["Output Processing"]
303
+ MINSCORE["min_score Filter<br/>(discard below threshold)"]
304
+ SNIPPET["snippet_mode Truncation<br/>(~500 chars at natural break)"]
305
+
306
+ MINSCORE --> SNIPPET
307
+ end
308
+
309
+ CATEGORY --> HYBRID
310
+ NOFILTER --> HYBRID
311
+ SEMANTIC --> RRF
312
+ BM25 --> RRF
313
+
314
+ ADJ --> MINSCORE
315
+ SNIPPET --> RESULTS["Results<br/>search_method: hybrid|semantic|keyword<br/>score + filtered_by_score + content_length"]
316
+ ```
317
+
318
+ ### Document Ingestion Flow
319
+
320
+ ```mermaid
321
+ flowchart LR
322
+ subgraph INPUT["Input"]
323
+ FILES["documents/<br/>├── security/<br/>├── development/<br/>├── ctf/<br/>└── general/"]
324
+ end
325
+
326
+ subgraph PARSE["Parse (20 formats)"]
327
+ MD["Markdown"]
328
+ PDF["PDF<br/>(PyMuPDF)"]
329
+ OFFICE["DOCX | XLSX<br/>PPTX | CSV"]
330
+ CODE["PY | C | H | CPP | JS | JSX<br/>TS | TSX | JSON | XML | IPYNB"]
331
+ end
332
+
333
+ subgraph CHUNK["Chunk"]
334
+ MDSPLIT["MD: Section-Aware<br/>Split at ## headers"]
335
+ TXTSPLIT["Other: Fixed-Size<br/>1000 chars + 200 overlap"]
336
+ DEDUP["SHA256 Dedup<br/>Skip duplicate content"]
337
+ end
338
+
339
+ subgraph EMBED["Embed"]
340
+ FASTEMBED["FastEmbed ONNX<br/>bge-small-en-v1.5<br/>(384D, CPU or GPU)"]
341
+ end
342
+
343
+ subgraph STORE["Store"]
344
+ CHROMADB[("ChromaDB")]
345
+ BM25IDX["BM25 Index"]
346
+ end
347
+
348
+ FILES --> MD & PDF & OFFICE & CODE
349
+ MD --> MDSPLIT
350
+ PDF & OFFICE & CODE --> TXTSPLIT
351
+ MDSPLIT --> DEDUP
352
+ TXTSPLIT --> DEDUP
353
+ DEDUP --> EMBED
354
+ EMBED --> STORE
355
+ ```
356
+
357
+ ### hybrid_alpha Parameter Effect
358
+
359
+ ```mermaid
360
+ flowchart LR
361
+ subgraph ALPHA["hybrid_alpha values"]
362
+ A0["0.0<br/>Pure BM25<br/>Instant"]
363
+ A3["0.3 (default)<br/>Keyword-heavy<br/>Fast"]
364
+ A5["0.5<br/>Balanced"]
365
+ A7["0.7<br/>Semantic-heavy"]
366
+ A10["1.0<br/>Pure Semantic"]
367
+ end
368
+
369
+ subgraph USE["Best For"]
370
+ U0["CVEs, tool names<br/>exact matches"]
371
+ U3["Technical queries<br/>specific terms"]
372
+ U5["General queries"]
373
+ U7["Conceptual queries<br/>related topics"]
374
+ U10["'How to...' questions<br/>conceptual search"]
375
+ end
376
+
377
+ A0 --- U0
378
+ A3 --- U3
379
+ A5 --- U5
380
+ A7 --- U7
381
+ A10 --- U10
382
+ ```
383
+
384
+ ---
385
+
386
+ ## Installation
387
+
388
+ ### Prerequisites
389
+
390
+ - Python 3.11+
391
+ - Claude Code CLI
392
+ - *…or any other MCP client (Claude Desktop, Cursor, VS Code, Antigravity, opencode, Windsurf) — see [Use with other MCP clients](#use-with-other-mcp-clients)*
393
+ - ~200MB disk for model cache (auto-downloaded on first run)
394
+ - *Optional:* NVIDIA GPU + CUDA 12 for accelerated embeddings (see [GPU Acceleration](#gpu-acceleration) below)
395
+
396
+ ### GPU Acceleration
397
+
398
+ GPU mode accelerates embedding generation during indexing and search. It requires an NVIDIA GPU with CUDA 12 support. No GPU? No problem — the server runs on CPU by default and GPU is entirely optional.
399
+
400
+ **Requirements:**
401
+
402
+ | Component | Minimum | How to check / get it |
403
+ |-----------|---------|----------------------|
404
+ | NVIDIA GPU (Turing+) | RTX 20xx / 30xx / 40xx / 50xx, or Tesla T4+ | `nvidia-smi` |
405
+ | NVIDIA Driver | ≥ 525 | `nvidia-smi` — [nvidia.com/drivers](https://www.nvidia.com/drivers) |
406
+ | CUDA 12 runtime | Provided by pip packages below | Automatic |
407
+
408
+ **Setup (2 steps):**
409
+
410
+ ```bash
411
+ # 1. Install GPU dependencies (onnxruntime-gpu + all CUDA 12 runtime DLLs)
412
+ pip install knowledge-rag[gpu]
413
+
414
+ # 2. Enable in config.yaml
415
+ # models:
416
+ # embedding:
417
+ # gpu: true
418
+ ```
419
+
420
+ The `[gpu]` extra installs `onnxruntime-gpu` plus 7 NVIDIA CUDA 12 packages (`cublas`, `cudnn`, `cuda-runtime`, `cufft`, `cusparse`, `cusolver`, `curand`, `nvjitlink`) so you don't need a full CUDA Toolkit install.
421
+
422
+ **Verify GPU is active:**
423
+
424
+ On server startup, look for the GPU status banner:
425
+ ```
426
+ ============================================================
427
+ GPU STATUS: ACTIVE
428
+ Provider: CUDAExecutionProvider
429
+ Device: NVIDIA GeForce RTX 3080 Ti
430
+ VRAM: 12.0 GB
431
+ ============================================================
432
+ ```
433
+
434
+ Or programmatically:
435
+ ```bash
436
+ python -c "import onnxruntime; print(onnxruntime.get_available_providers())"
437
+ # Should include: 'CUDAExecutionProvider'
438
+ ```
439
+
440
+ > **Fallback**: If CUDA is unavailable at runtime (wrong driver, missing DLLs, no GPU), the server falls back to CPU automatically with a `[WARN]` log — it never crashes. The `gpu: true` config is a preference, not a requirement.
441
+
442
+ ### Install Methods
443
+
444
+ Pick one — all produce the same running server.
445
+
446
+ #### Option A: NPX (fastest)
447
+
448
+ Requires Node.js 16+. Handles Python venv, pip install, and version upgrades automatically.
449
+
450
+ ```bash
451
+ claude mcp add knowledge-rag -s user -- npx -y knowledge-rag
452
+ ```
453
+
454
+ That's it. On first run, `npx` creates a venv at `~/.knowledge-rag/`, installs the PyPI package, and starts the MCP server. Subsequent runs reuse the cached venv.
455
+
456
+ #### Option B: One-line installer
457
+
458
+ ```bash
459
+ # Linux/macOS:
460
+ curl -fsSL https://raw.githubusercontent.com/lyonzin/knowledge-rag/master/install.sh | bash
461
+
462
+ # Windows (PowerShell):
463
+ irm https://raw.githubusercontent.com/lyonzin/knowledge-rag/master/install.ps1 | iex
464
+ ```
465
+
466
+ Then configure Claude Code:
467
+
468
+ ```bash
469
+ claude mcp add knowledge-rag -s user -- ~/knowledge-rag/venv/bin/python -m mcp_server.server
470
+ ```
471
+
472
+ > **Windows**: `claude mcp add knowledge-rag -s user -- %USERPROFILE%\knowledge-rag\venv\Scripts\python.exe -m mcp_server.server`
473
+
474
+ #### Option C: pip install
475
+
476
+ ```bash
477
+ mkdir ~/knowledge-rag && cd ~/knowledge-rag
478
+ python3 -m venv venv && source venv/bin/activate
479
+ pip install knowledge-rag
480
+ knowledge-rag init # Exports config template, presets, creates documents/
481
+ ```
482
+
483
+ Then configure Claude Code:
484
+
485
+ ```bash
486
+ claude mcp add knowledge-rag -s user -- ~/knowledge-rag/venv/bin/python -m mcp_server.server
487
+ ```
488
+
489
+ > **Windows users**: Use `python` instead of `python3`, `venv\Scripts\activate` instead of `source venv/bin/activate`.
490
+ > **Windows path**: `claude mcp add knowledge-rag -s user -- %USERPROFILE%\knowledge-rag\venv\Scripts\python.exe -m mcp_server.server`
491
+
492
+ #### Option D: Clone from source
493
+
494
+ ```bash
495
+ git clone https://github.com/lyonzin/knowledge-rag.git ~/knowledge-rag
496
+ cd ~/knowledge-rag
497
+ python3 -m venv venv && source venv/bin/activate
498
+ pip install -r requirements.txt
499
+ ```
500
+
501
+ Then configure Claude Code:
502
+
503
+ ```bash
504
+ claude mcp add knowledge-rag -s user -- ~/knowledge-rag/venv/bin/python -m mcp_server.server
505
+ ```
506
+
507
+ #### Option E: Docker
508
+
509
+ ```bash
510
+ docker pull ghcr.io/lyonzin/knowledge-rag:latest
511
+ ```
512
+
513
+ ```bash
514
+ claude mcp add knowledge-rag -s user -- \
515
+ docker run -i --rm \
516
+ -v ~/knowledge-rag/documents:/app/documents \
517
+ -v ~/knowledge-rag/data:/app/data \
518
+ ghcr.io/lyonzin/knowledge-rag:latest
519
+ ```
520
+
521
+ Models are pre-downloaded in the image — no first-run delay.
522
+
523
+ <details>
524
+ <summary>Alternative: manual JSON config</summary>
525
+
526
+ Add to `~/.claude.json`:
527
+
528
+ **Windows:**
529
+ ```json
530
+ {
531
+ "mcpServers": {
532
+ "knowledge-rag": {
533
+ "command": "C:\\Users\\YOUR_USER\\knowledge-rag\\venv\\Scripts\\python.exe",
534
+ "args": ["-m", "mcp_server.server"]
535
+ }
536
+ }
537
+ }
538
+ ```
539
+
540
+ **Linux / macOS:**
541
+ ```json
542
+ {
543
+ "mcpServers": {
544
+ "knowledge-rag": {
545
+ "command": "/home/YOUR_USER/knowledge-rag/venv/bin/python",
546
+ "args": ["-m", "mcp_server.server"]
547
+ }
548
+ }
549
+ }
550
+ ```
551
+ > Replace `YOUR_USER` with your username, or use the full path from `echo $HOME`.
552
+ </details>
553
+
554
+ #### Option F: SSE Server Mode (multi-agent)
555
+
556
+ For multi-agent setups where multiple clients query the same knowledge base simultaneously:
557
+
558
+ ```bash
559
+ pip install knowledge-rag[server] # Adds uvicorn for SSE/HTTP
560
+ knowledge-rag --transport sse # Starts on http://127.0.0.1:8179
561
+ ```
562
+
563
+ Then configure each MCP client to connect via SSE:
564
+
565
+ ```json
566
+ {
567
+ "mcpServers": {
568
+ "knowledge-rag": {
569
+ "type": "sse",
570
+ "url": "http://127.0.0.1:8179/sse"
571
+ }
572
+ }
573
+ }
574
+ ```
575
+
576
+ One server process serves all agents — shared embedding model, shared cache, shared ChromaDB. See [Configuration > Server](#server) for rate limiting, metrics, and auth options.
577
+
578
+ ### Use with other MCP clients
579
+
580
+ `knowledge-rag` supports both **stdio** (default, 1:1) and **SSE** (1:N) transport modes. In stdio mode, it works with any MCP-compatible client, not only Claude Code. The launch command is the same everywhere (the `python -m mcp_server.server` from whichever install method you picked); only the **config file location** and **JSON shape** differ per client.
581
+
582
+ #### Clients using the standard `mcpServers` format
583
+
584
+ For **Claude Desktop, Cursor, Antigravity, and Windsurf**, use the same block — only the file location changes:
585
+
586
+ ```json
587
+ {
588
+ "mcpServers": {
589
+ "knowledge-rag": {
590
+ "command": "/home/YOUR_USER/knowledge-rag/venv/bin/python",
591
+ "args": ["-m", "mcp_server.server"]
592
+ }
593
+ }
594
+ }
595
+ ```
596
+
597
+ > **Windows**: set `command` to the full path of `venv\Scripts\python.exe`.
598
+
599
+ | Client | Config file | Notes |
600
+ |---|---|---|
601
+ | **Claude Code** | use `claude mcp add …` (see install methods above) | The CLI writes `~/.claude.json` for you — manual edits to it aren't reliably picked up. |
602
+ | **Claude Desktop** | macOS: `~/Library/Application Support/Claude/claude_desktop_config.json` · Windows: `%APPDATA%\Claude\claude_desktop_config.json` | Easiest: **Settings → Developer → Edit Config** opens the correct file (avoids the Windows Store/MSIX path quirk). |
603
+ | **Cursor** | `~/.cursor/mcp.json` (global) or `.cursor/mcp.json` (per project) | — |
604
+ | **Antigravity** | macOS/Linux: `~/.gemini/antigravity/mcp_config.json` · Windows: `%USERPROFILE%\.gemini\antigravity\mcp_config.json` | Open via Agent panel → **"…" → Manage MCP Servers → View raw config**. |
605
+ | **Windsurf** | `~/.codeium/windsurf/mcp_config.json` (global only) | Easiest: Cascade panel → MCP → **View raw config**. |
606
+
607
+ #### VS Code — uses a `servers` key
608
+
609
+ VS Code (Copilot MCP) nests servers under **`servers`**, not `mcpServers`. Put this in `.vscode/mcp.json` (workspace) or the file opened by the **MCP: Open User Configuration** command:
610
+
611
+ ```json
612
+ {
613
+ "servers": {
614
+ "knowledge-rag": {
615
+ "type": "stdio",
616
+ "command": "/home/YOUR_USER/knowledge-rag/venv/bin/python",
617
+ "args": ["-m", "mcp_server.server"]
618
+ }
619
+ }
620
+ }
621
+ ```
622
+
623
+ #### opencode — uses an `mcp` key
624
+
625
+ opencode nests servers under **`mcp`**, takes `command` as a single **array**, and uses `environment` instead of `env`. Put this in `opencode.json` (project root) or `~/.config/opencode/opencode.json` (global):
626
+
627
+ ```jsonc
628
+ {
629
+ "$schema": "https://opencode.ai/config.json",
630
+ "mcp": {
631
+ "knowledge-rag": {
632
+ "type": "local",
633
+ "command": ["/home/YOUR_USER/knowledge-rag/venv/bin/python", "-m", "mcp_server.server"],
634
+ "enabled": true
635
+ }
636
+ }
637
+ }
638
+ ```
639
+
640
+ > **Any other MCP client**: point it at the same command + args (`…/venv/bin/python -m mcp_server.server`). If it speaks stdio MCP, knowledge-rag works — only the config file's location and key naming differ. Check your client's docs for the exact path.
641
+
642
+ ### Verify
643
+
644
+ ```bash
645
+ claude mcp list
646
+ ```
647
+
648
+ On first start, the server will:
649
+ 1. Download the embedding model (~50MB, cached in `models_cache/`)
650
+ 2. Auto-index any documents in the `documents/` directory
651
+ 3. Start watching for file changes (auto-reindex)
652
+
653
+ ---
654
+
655
+ ## Usage
656
+
657
+ ### Adding Documents
658
+
659
+ Place your documents in the `documents/` directory, organized by category:
660
+
661
+ ```
662
+ documents/
663
+ ├── security/ # Pentest, exploit, vulnerability docs
664
+ ├── development/ # Code, APIs, frameworks
665
+ ├── ctf/ # CTF writeups and methodology
666
+ ├── logscale/ # LogScale/LQL documentation
667
+ └── general/ # Everything else
668
+ ```
669
+
670
+ Or add documents programmatically via MCP tools:
671
+
672
+ ```python
673
+ # Add from content
674
+ add_document(
675
+ content="# My Document\n\nContent here...",
676
+ filepath="security/my-technique.md",
677
+ category="security"
678
+ )
679
+
680
+ # Add from URL
681
+ add_from_url(
682
+ url="https://example.com/article",
683
+ category="security",
684
+ title="Custom Title"
685
+ )
686
+ ```
687
+
688
+ ### Searching
689
+
690
+ Claude uses the RAG system automatically when configured. You can also control search behavior:
691
+
692
+ ```python
693
+ # Pure keyword search — instant, no embedding needed
694
+ search_knowledge("gtfobins suid", hybrid_alpha=0.0)
695
+
696
+ # Keyword-heavy (default) — fast, slight semantic boost
697
+ search_knowledge("mimikatz", hybrid_alpha=0.3)
698
+
699
+ # Balanced hybrid — both engines equally weighted
700
+ search_knowledge("SQL injection techniques", hybrid_alpha=0.5)
701
+
702
+ # Semantic-heavy — better for conceptual queries
703
+ search_knowledge("how to escalate privileges", hybrid_alpha=0.7)
704
+
705
+ # Pure semantic — embedding similarity only
706
+ search_knowledge("lateral movement strategies", hybrid_alpha=1.0)
707
+ ```
708
+
709
+ ### Indexing
710
+
711
+ Documents are automatically indexed on first startup. All reindex operations run **in background** — they return immediately and you poll progress via `get_reindex_status()`:
712
+
713
+ ```python
714
+ # Incremental: only re-index changed files (fast)
715
+ reindex_documents()
716
+
717
+ # Smart reindex: detect changes + rebuild BM25
718
+ reindex_documents(force=True)
719
+
720
+ # Nuclear rebuild: delete everything, re-embed all (use after model change)
721
+ reindex_documents(full_rebuild=True)
722
+
723
+ # Poll progress (lightweight, no full stats computation)
724
+ get_reindex_status()
725
+ # → {"reindex": {"active": true, "percent": 56, "progress": "2090/3734", ...}}
726
+ ```
727
+
728
+ ### Evaluating Retrieval Quality
729
+
730
+ ```python
731
+ evaluate_retrieval(test_cases='[
732
+ {"query": "sql injection", "expected_filepath": "security/sqli-guide.md"},
733
+ {"query": "privilege escalation", "expected_filepath": "security/privesc.md"}
734
+ ]')
735
+ # Returns: MRR@5, Recall@5, per-query results
736
+ ```
737
+
738
+ ---
739
+
740
+ ## API Reference
741
+
742
+ ### Search & Query
743
+
744
+ #### `search_knowledge`
745
+
746
+ Hybrid search combining semantic search + BM25 keyword search with cross-encoder reranking.
747
+
748
+ | Parameter | Type | Default | Description |
749
+ |-----------|------|---------|-------------|
750
+ | `query` | string | required | Search query text (1-3 keywords recommended) |
751
+ | `max_results` | int | 5 | Maximum results to return (1-20) |
752
+ | `category` | string | null | Filter by category |
753
+ | `hybrid_alpha` | float | 0.3 | Balance: 0.0 = keyword only, 1.0 = semantic only |
754
+ | `min_score` | float | 0.0 | Minimum relevance score (0.0-1.0) to include a result. Use 0.2-0.4 to cut noise |
755
+ | `snippet_mode` | bool | true | Truncate content to ~500 chars at natural break points. Adds `content_length` field |
756
+
757
+ **Returns:**
758
+
759
+ ```json
760
+ {
761
+ "status": "success",
762
+ "query": "mimikatz credential dump",
763
+ "hybrid_alpha": 0.5,
764
+ "result_count": 3,
765
+ "filtered_by_score": 2,
766
+ "cache_hit_rate": "0.0%",
767
+ "results": [
768
+ {
769
+ "content": "Mimikatz can extract credentials from memory...",
770
+ "source": "documents/security/credential-attacks.md",
771
+ "filename": "credential-attacks.md",
772
+ "category": "security",
773
+ "score": 0.9823,
774
+ "raw_rrf_score": 0.016393,
775
+ "reranker_score": 0.987654,
776
+ "semantic_rank": 2,
777
+ "bm25_rank": 1,
778
+ "search_method": "hybrid",
779
+ "keywords": ["mimikatz", "credential", "lsass"],
780
+ "routed_by": "redteam"
781
+ }
782
+ ]
783
+ }
784
+ ```
785
+
786
+ **Search Method Values:**
787
+ - `hybrid`: Found by both semantic and BM25 search (highest confidence)
788
+ - `semantic`: Found only by semantic search
789
+ - `keyword`: Found only by BM25 keyword search
790
+
791
+ ---
792
+
793
+ #### `get_document`
794
+
795
+ Retrieve the full content of a specific document.
796
+
797
+ | Parameter | Type | Description |
798
+ |-----------|------|-------------|
799
+ | `filepath` | string | Path to the document file |
800
+
801
+ **Returns:** JSON with document content, metadata, keywords, and chunk count.
802
+
803
+ ---
804
+
805
+ #### `reindex_documents`
806
+
807
+ Index or reindex all documents in the knowledge base. **Runs in background** — returns immediately. Poll progress via `get_reindex_status()`.
808
+
809
+ | Parameter | Type | Default | Description |
810
+ |-----------|------|---------|-------------|
811
+ | `force` | bool | false | Smart reindex: detects changes, rebuilds BM25. Fast. |
812
+ | `full_rebuild` | bool | false | Nuclear rebuild: deletes everything, re-embeds all documents. Use after model change. |
813
+
814
+ **Returns:** `{"status": "started", "operation": "..."}` immediately. If already running, returns `{"status": "already_running", "progress": "1200/3734"}`.
815
+
816
+ ---
817
+
818
+ #### `get_reindex_status`
819
+
820
+ Get the current status of a background reindex operation. Lightweight — does not compute full index statistics.
821
+
822
+ **Returns (active):**
823
+ ```json
824
+ {
825
+ "status": "success",
826
+ "reindex": {
827
+ "active": true,
828
+ "operation": "nuclear_rebuild",
829
+ "progress": "1200/3734",
830
+ "percent": 32,
831
+ "indexed": 1200,
832
+ "skipped": 0,
833
+ "errors": 0,
834
+ "started_at": "2026-06-17T18:29:49"
835
+ }
836
+ }
837
+ ```
838
+
839
+ **Returns (idle):** `{"status": "success", "reindex": {"active": false}}`
840
+
841
+ ---
842
+
843
+ #### `list_categories`
844
+
845
+ List all document categories with their document counts.
846
+
847
+ **Returns:**
848
+
849
+ ```json
850
+ {
851
+ "status": "success",
852
+ "categories": {
853
+ "security": 52,
854
+ "development": 8,
855
+ "ctf": 12,
856
+ "general": 3
857
+ },
858
+ "total_documents": 75
859
+ }
860
+ ```
861
+
862
+ ---
863
+
864
+ #### `list_documents`
865
+
866
+ List all indexed documents, optionally filtered by category.
867
+
868
+ | Parameter | Type | Description |
869
+ |-----------|------|-------------|
870
+ | `category` | string | Optional category filter |
871
+
872
+ **Returns:** JSON array of documents with id, source, category, format, chunks, and keywords.
873
+
874
+ ---
875
+
876
+ #### `get_index_stats`
877
+
878
+ Get statistics about the knowledge base index.
879
+
880
+ **Returns:**
881
+
882
+ ```json
883
+ {
884
+ "status": "success",
885
+ "stats": {
886
+ "total_documents": 75,
887
+ "total_chunks": 9256,
888
+ "categories": {"security": 52, "development": 8},
889
+ "supported_formats": [".md", ".txt", ".pdf", ".py", ".json", ".docx", ".xlsx", ".pptx", ".csv", ".ipynb"],
890
+ "embedding_model": "BAAI/bge-small-en-v1.5",
891
+ "embedding_dim": 384,
892
+ "reranker_model": "Xenova/ms-marco-MiniLM-L-6-v2",
893
+ "chunk_size": 1000,
894
+ "chunk_overlap": 200,
895
+ "query_cache": {
896
+ "size": 12,
897
+ "max_size": 100,
898
+ "ttl_seconds": 300,
899
+ "hits": 45,
900
+ "misses": 23,
901
+ "hit_rate": "66.2%"
902
+ }
903
+ }
904
+ }
905
+ ```
906
+
907
+ ---
908
+
909
+ ### Document Management
910
+
911
+ #### `add_document`
912
+
913
+ Add a new document to the knowledge base from raw content. Saves the file to the documents directory and indexes it immediately.
914
+
915
+ | Parameter | Type | Default | Description |
916
+ |-----------|------|---------|-------------|
917
+ | `content` | string | required | Full text content of the document |
918
+ | `filepath` | string | required | Relative path within documents dir (e.g., `security/new-technique.md`) |
919
+ | `category` | string | "general" | Document category |
920
+
921
+ ---
922
+
923
+ #### `update_document`
924
+
925
+ Update an existing document. Removes old chunks from the index and re-indexes with new content.
926
+
927
+ | Parameter | Type | Description |
928
+ |-----------|------|-------------|
929
+ | `filepath` | string | Full path to the document file |
930
+ | `content` | string | New content for the document |
931
+
932
+ ---
933
+
934
+ #### `remove_document`
935
+
936
+ Remove a document from the knowledge base index. Optionally deletes the file from disk.
937
+
938
+ | Parameter | Type | Default | Description |
939
+ |-----------|------|---------|-------------|
940
+ | `filepath` | string | required | Path to the document file |
941
+ | `delete_file` | bool | false | If true, also delete the file from disk |
942
+
943
+ ---
944
+
945
+ #### `add_from_url`
946
+
947
+ Fetch content from a URL, strip HTML (scripts, styles, nav, footer, header), convert to markdown, and add to the knowledge base.
948
+
949
+ | Parameter | Type | Default | Description |
950
+ |-----------|------|---------|-------------|
951
+ | `url` | string | required | URL to fetch content from |
952
+ | `category` | string | "general" | Document category |
953
+ | `title` | string | null | Custom title (auto-detected from `<title>` tag if not provided) |
954
+
955
+ ---
956
+
957
+ #### `search_similar`
958
+
959
+ Find documents similar to a given document using embedding similarity.
960
+
961
+ | Parameter | Type | Default | Description |
962
+ |-----------|------|---------|-------------|
963
+ | `filepath` | string | required | Path to the reference document |
964
+ | `max_results` | int | 5 | Number of similar documents to return (1-20) |
965
+
966
+ ---
967
+
968
+ #### `evaluate_retrieval`
969
+
970
+ Evaluate retrieval quality with test queries. Useful for tuning `hybrid_alpha`, testing query expansion effectiveness, or validating after reindexing.
971
+
972
+ | Parameter | Type | Description |
973
+ |-----------|------|-------------|
974
+ | `test_cases` | string (JSON) | Array of test cases: `[{"query": "...", "expected_filepath": "..."}, ...]` |
975
+
976
+ **Metrics:**
977
+ - **MRR@5** (Mean Reciprocal Rank): Average of 1/rank for expected documents. 1.0 = always first result.
978
+ - **Recall@5**: Fraction of expected documents found in top 5 results. 1.0 = all found.
979
+
980
+ ---
981
+
982
+ ## Configuration
983
+
984
+ Knowledge RAG is fully configurable via a `config.yaml` file in the project root. If no `config.yaml` exists, sensible defaults are used — the system works out of the box with zero configuration.
985
+
986
+ ### Quick Start
987
+
988
+ ```bash
989
+ # Option 1: Use a preset
990
+ cp presets/cybersecurity.yaml config.yaml # Offensive/defensive security, CTFs
991
+ cp presets/developer.yaml config.yaml # Software engineering, APIs, DevOps
992
+ cp presets/research.yaml config.yaml # Academic research, papers, studies
993
+ cp presets/general.yaml config.yaml # Blank slate, pure semantic search
994
+
995
+ # Option 2: Start from the documented template
996
+ cp config.example.yaml config.yaml
997
+ # Edit config.yaml to your needs
998
+ ```
999
+
1000
+ Restart Claude Code after changing `config.yaml`.
1001
+
1002
+ ### config.yaml Structure
1003
+
1004
+ ```yaml
1005
+ # Paths — where your documents live
1006
+ paths:
1007
+ documents_dir: "./documents" # Scanned recursively
1008
+ data_dir: "./data" # Index storage
1009
+ models_cache_dir: "./models_cache" # Persistent embedding model cache
1010
+
1011
+ # Documents — what gets indexed and how
1012
+ documents:
1013
+ supported_formats: # File types to index
1014
+ - .md
1015
+ - .txt
1016
+ - .pdf
1017
+ - .docx
1018
+ - .ipynb
1019
+ # - .py # Uncomment to index code
1020
+ exclude_patterns: # Glob patterns to skip
1021
+ - "node_modules"
1022
+ - ".venv"
1023
+ - "__pycache__"
1024
+ chunking:
1025
+ chunk_size: 1000 # Max chars per chunk
1026
+ chunk_overlap: 200 # Shared chars between chunks
1027
+
1028
+ # Models — AI models for search (all run locally, no API keys)
1029
+ models:
1030
+ embedding:
1031
+ model: "BAAI/bge-small-en-v1.5" # ONNX, ~33MB, auto-downloaded
1032
+ dimensions: 384
1033
+ gpu: false # Set true + pip install knowledge-rag[gpu]
1034
+ reranker:
1035
+ enabled: true # Falls back to RRF if model is unavailable
1036
+ model: "Xenova/ms-marco-MiniLM-L-6-v2"
1037
+ top_k_multiplier: 3 # Candidates fetched before reranking
1038
+
1039
+ # Search — result limits and collection name
1040
+ search:
1041
+ default_results: 5
1042
+ max_results: 20
1043
+ collection_name: "knowledge_base" # Change for separate knowledge bases
1044
+
1045
+ # Categories — auto-tag documents by folder path
1046
+ # Set to {} to disable categorization entirely
1047
+ category_mappings:
1048
+ "security/redteam": "redteam"
1049
+ "security/blueteam": "blueteam"
1050
+ "notes": "notes"
1051
+
1052
+ # Keyword routing — prioritize categories based on query keywords
1053
+ # Set to {} for pure semantic search with no routing bias
1054
+ keyword_routes:
1055
+ redteam:
1056
+ - pentest
1057
+ - exploit
1058
+ - privilege escalation
1059
+
1060
+ # Query expansion — expand abbreviations for better BM25 recall
1061
+ # Set to {} for no expansion (search terms used as-is)
1062
+ query_expansions:
1063
+ sqli:
1064
+ - sql injection
1065
+ - sqli
1066
+ privesc:
1067
+ - privilege escalation
1068
+ - privesc
1069
+
1070
+ # Server — enterprise features (new in v4.0.0)
1071
+ server:
1072
+ transport: "stdio" # "stdio" | "sse" | "streamable-http"
1073
+ host: "127.0.0.1" # Bind address (SSE/HTTP only)
1074
+ port: 8179 # Bind port (SSE/HTTP only)
1075
+ auth:
1076
+ bearer_token: "" # Set a secret to enable auth (SSE/HTTP only)
1077
+ rate_limit:
1078
+ enabled: false
1079
+ requests_per_minute: 60
1080
+ burst: 10
1081
+ metrics:
1082
+ enabled: false
1083
+ port: 9179 # Separate port for Prometheus scraping
1084
+ ```
1085
+
1086
+ > See `config.example.yaml` for the fully documented template with explanations for every field.
1087
+
1088
+ ### Presets
1089
+
1090
+ Pre-built configurations for common use cases:
1091
+
1092
+ | Preset | File | Categories | Keywords | Expansions | Best For |
1093
+ |--------|------|-----------|----------|-----------|----------|
1094
+ | **Cybersecurity** | `presets/cybersecurity.yaml` | 8 | 200+ | 69 | Red/Blue Team, CTFs, threat hunting, exploit dev |
1095
+ | **Developer** | `presets/developer.yaml` | 9 | 150+ | 50+ | Full-stack dev, APIs, DevOps, cloud, databases |
1096
+ | **Research** | `presets/research.yaml` | 9 | 100+ | 40+ | Academic papers, thesis, lab notebooks, datasets |
1097
+ | **General** | `presets/general.yaml` | 0 | 0 | 0 | Blank slate — pure semantic search, no domain logic |
1098
+
1099
+ **Creating your own preset**: Copy `config.example.yaml`, fill in your categories/keywords/expansions, save to `presets/your-domain.yaml`.
1100
+
1101
+ ### Configuration Reference
1102
+
1103
+ #### Server
1104
+
1105
+ | Field | Default | Description |
1106
+ |-------|---------|-------------|
1107
+ | `server.transport` | `"stdio"` | Transport protocol: `"stdio"`, `"sse"`, or `"streamable-http"` |
1108
+ | `server.host` | `"127.0.0.1"` | Bind address for SSE/HTTP mode |
1109
+ | `server.port` | `8179` | Bind port for SSE/HTTP mode |
1110
+ | `server.auth.bearer_token` | `""` (disabled) | Bearer token for SSE/HTTP auth. Empty = no auth |
1111
+ | `server.rate_limit.enabled` | `false` | Enable per-client rate limiting |
1112
+ | `server.rate_limit.requests_per_minute` | `60` | Max requests per minute |
1113
+ | `server.rate_limit.burst` | `10` | Burst allowance above steady rate |
1114
+ | `server.metrics.enabled` | `false` | Enable Prometheus `/metrics` endpoint |
1115
+ | `server.metrics.port` | `9179` | Port for metrics scraping |
1116
+
1117
+ In stdio mode (default), server settings are ignored. SSE/HTTP mode auto-enables the single-instance lock.
1118
+
1119
+ #### Paths
1120
+
1121
+ | Field | Default | Description |
1122
+ |-------|---------|-------------|
1123
+ | `paths.documents_dir` | `./documents` | Root folder scanned recursively for documents |
1124
+ | `paths.data_dir` | `./data` | Internal storage for ChromaDB and index metadata |
1125
+ | `paths.models_cache_dir` | `./models_cache` | Persistent cache for embedding models (~250MB). Survives reboots |
1126
+
1127
+ Relative paths resolve from the project root. Absolute paths work too.
1128
+
1129
+ #### Documents
1130
+
1131
+ | Field | Default | Description |
1132
+ |-------|---------|-------------|
1133
+ | `documents.supported_formats` | .md .txt .pdf .py .json .docx .xlsx .pptx .csv .ipynb | File extensions to index |
1134
+ | `documents.exclude_patterns` | `[]` (empty) | Glob patterns for files/dirs to skip during indexing |
1135
+ | `documents.chunking.chunk_size` | 1000 | Max characters per chunk |
1136
+ | `documents.chunking.chunk_overlap` | 200 | Characters shared between consecutive chunks |
1137
+
1138
+ **Chunking guidelines**: Short notes → 500/100. General use → 1000/200. Long technical docs → 1500/300.
1139
+
1140
+ For `.md` files, chunking splits at `##` and `###` header boundaries first. Sections larger than `chunk_size` are sub-chunked with overlap. Non-markdown files use fixed-size chunking.
1141
+
1142
+ #### Models
1143
+
1144
+ | Field | Default | Description |
1145
+ |-------|---------|-------------|
1146
+ | `models.embedding.model` | `BAAI/bge-small-en-v1.5` | Embedding model (ONNX, runs locally) |
1147
+ | `models.embedding.dimensions` | 384 | Vector dimensions (must match model) |
1148
+ | `models.embedding.gpu` | false | Enable CUDA GPU acceleration. See [GPU Acceleration](#gpu-acceleration) for full setup |
1149
+ | `models.reranker.enabled` | true | Enable cross-encoder reranking |
1150
+ | `models.reranker.model` | `Xenova/ms-marco-MiniLM-L-6-v2` | Reranker model |
1151
+ | `models.reranker.top_k_multiplier` | 3 | Fetch N*multiplier candidates for reranking |
1152
+
1153
+ If the reranker model is not available locally and the machine cannot download it, search now falls back to the RRF order from hybrid semantic+BM25 retrieval. This keeps `search_knowledge` available offline, but result ordering may be less precise for ambiguous queries until the reranker model is cached.
1154
+
1155
+ **Embedding model options** (fastest → most accurate):
1156
+ - `BAAI/bge-small-en-v1.5` — 384D, ~33MB (default)
1157
+ - `BAAI/bge-base-en-v1.5` — 768D, ~130MB
1158
+ - `BAAI/bge-large-en-v1.5` — 1024D, ~335MB
1159
+ - `intfloat/multilingual-e5-small` — 384D, 100+ languages
1160
+
1161
+ > **Warning**: Changing the embedding model after indexing requires `reindex_documents(full_rebuild=True)`.
1162
+
1163
+ #### Search
1164
+
1165
+ | Field | Default | Description |
1166
+ |-------|---------|-------------|
1167
+ | `search.default_results` | 5 | Results returned when no limit specified |
1168
+ | `search.max_results` | 20 | Hard cap even if client requests more |
1169
+ | `search.collection_name` | `knowledge_base` | ChromaDB collection — change for separate KBs |
1170
+
1171
+ #### Categories
1172
+
1173
+ Map folder paths to category names. Documents in matching folders get auto-tagged, enabling filtered searches.
1174
+
1175
+ ```yaml
1176
+ category_mappings:
1177
+ "security/redteam": "redteam"
1178
+ "security": "security"
1179
+ ```
1180
+
1181
+ Set `category_mappings: {}` to disable — documents are still searchable, just without category filters.
1182
+
1183
+ #### Keyword Routing
1184
+
1185
+ Route queries to categories based on keywords. When a query contains listed keywords, results from that category are prioritized (not filtered — other categories still appear, ranked lower).
1186
+
1187
+ ```yaml
1188
+ keyword_routes:
1189
+ redteam:
1190
+ - pentest
1191
+ - exploit
1192
+ - sqli
1193
+ ```
1194
+
1195
+ Single-word keywords use regex word boundaries (`\b`) — "api" won't match "RAPID". Multi-word keywords use substring matching.
1196
+
1197
+ Set `keyword_routes: {}` for pure semantic search.
1198
+
1199
+ #### Query Expansion
1200
+
1201
+ Expand search terms with synonyms before BM25 search. Supports single tokens, bigrams, and full query matches.
1202
+
1203
+ ```yaml
1204
+ query_expansions:
1205
+ sqli:
1206
+ - sql injection
1207
+ - sqli
1208
+ k8s:
1209
+ - kubernetes
1210
+ - k8s
1211
+ ```
1212
+
1213
+ Set `query_expansions: {}` for no expansion.
1214
+
1215
+ `query_expansions` is directional: only the key on the left triggers the terms on the right. If you need mutual expansion without duplicating entries, use `query_expansion_groups`.
1216
+
1217
+ ```yaml
1218
+ query_expansion_groups:
1219
+ - ["triple barrier", "tb", "trip_barr"]
1220
+ - ["profit factor", "pf"]
1221
+ ```
1222
+
1223
+ Each group is interpreted symmetrically, so every term expands to the rest of the group. The final internal expansion table is built by merging both sources:
1224
+
1225
+ 1. `query_expansions` entries are loaded as-is.
1226
+ 2. `query_expansion_groups` adds reciprocal links for every term in each group.
1227
+ 3. Overlaps are merged by union with duplicate terms removed.
1228
+
1229
+ This keeps backward compatibility while allowing concise synonym groups.
1230
+
1231
+ ### Hybrid Search Tuning
1232
+
1233
+ | hybrid_alpha | Behavior | Best For |
1234
+ |--------------|----------|----------|
1235
+ | 0.0 | Pure BM25 keyword | Exact terms, CVEs, tool names |
1236
+ | 0.3 | Keyword-heavy **(default)** | Technical queries with specific terms |
1237
+ | 0.5 | Balanced | General queries |
1238
+ | 0.7 | Semantic-heavy | Conceptual queries, related topics |
1239
+ | 1.0 | Pure semantic | "How to..." questions, abstract concepts |
1240
+
1241
+ ---
1242
+
1243
+ ## Project Structure
1244
+
1245
+ ```
1246
+ knowledge-rag/
1247
+ ├── mcp_server/
1248
+ │ ├── __init__.py # Stdout protection + version
1249
+ │ ├── config.py # YAML config loader + defaults
1250
+ │ ├── ingestion.py # 20 parsers, chunking, metadata extraction
1251
+ │ └── server.py # MCP server, ChromaDB, BM25, reranker, 12 tools
1252
+ ├── config.example.yaml # Documented config template (copy to config.yaml)
1253
+ ├── config.yaml # Your active configuration (git-ignored)
1254
+ ├── presets/ # Ready-to-use domain configurations
1255
+ │ ├── cybersecurity.yaml
1256
+ │ ├── developer.yaml
1257
+ │ ├── research.yaml
1258
+ │ └── general.yaml
1259
+ ├── documents/ # Your documents (scanned recursively)
1260
+ ├── data/
1261
+ │ ├── chroma_db/ # ChromaDB vector database
1262
+ │ └── index_metadata.json # Incremental indexing state
1263
+ ├── models_cache/ # Persistent embedding model cache
1264
+ ├── tests/ # Test suite (82 tests)
1265
+ ├── install.sh # Linux/macOS installer
1266
+ ├── install.ps1 # Windows installer
1267
+ ├── venv/ # Python virtual environment
1268
+ ├── requirements.txt
1269
+ ├── pyproject.toml
1270
+ ├── LICENSE
1271
+ └── README.md
1272
+ ```
1273
+
1274
+ ---
1275
+
1276
+ ## Troubleshooting
1277
+
1278
+ ### Python version mismatch
1279
+
1280
+ Requires Python 3.11 or newer.
1281
+
1282
+ ```bash
1283
+ python --version # Must be 3.11+
1284
+ ```
1285
+
1286
+ ### FastEmbed model download fails
1287
+
1288
+ On first run, FastEmbed downloads models to `models_cache/`. If the download fails:
1289
+
1290
+ ```bash
1291
+ # Clear cache and retry
1292
+ # Windows:
1293
+ rmdir /s /q models_cache
1294
+
1295
+ # Linux/macOS:
1296
+ rm -rf models_cache
1297
+
1298
+ # Then restart the MCP server
1299
+ ```
1300
+
1301
+ ### Reranker model download fails
1302
+
1303
+ The reranker is lazy-loaded on the first query. If the model is not cached and the machine is offline, search continues without reranking and uses the RRF order from hybrid retrieval. To keep reranking enabled offline, run one query while online or pre-populate `models_cache/` on the target machine.
1304
+
1305
+ You can still disable reranking explicitly in `config.yaml`:
1306
+
1307
+ ```yaml
1308
+ models:
1309
+ reranker:
1310
+ enabled: false
1311
+ ```
1312
+
1313
+ Disabling reranking reduces memory use and avoids first-query model loading. The tradeoff is lower ranking precision, especially when several chunks match the same terms but only one is the best answer.
1314
+
1315
+ ### ChromaDB index crashes on startup
1316
+
1317
+ Native ChromaDB failures can terminate Python before normal exception handling runs. Startup now probes ChromaDB in a child process before initializing the MCP server. If the probe crashes, the active `chroma_db/` and `index_metadata.json` are moved to `data/backups/auto-repair-*`, and the next startup can rebuild a clean index.
1318
+
1319
+ The same guarded behavior is available through either console script:
1320
+
1321
+ ```bash
1322
+ knowledge-rag
1323
+ knowledge-rag-guarded
1324
+ ```
1325
+
1326
+ ### Index is empty
1327
+
1328
+ ```bash
1329
+ # Check documents directory has files
1330
+ ls documents/
1331
+
1332
+ # Force reindex via Claude Code:
1333
+ # reindex_documents(force=True)
1334
+
1335
+ # Or nuclear rebuild if model changed:
1336
+ # reindex_documents(full_rebuild=True)
1337
+ ```
1338
+
1339
+ ### MCP server not loading
1340
+
1341
+ 1. Check `~/.claude.json` exists and has valid JSON in the `mcpServers` section
1342
+ 2. Verify paths use double backslashes (`\\`) on Windows
1343
+ 3. Restart Claude Code completely
1344
+ 4. Run `claude mcp list` to check connection status
1345
+
1346
+ ### "Failed to connect" error
1347
+
1348
+ The MCP server uses stdout for JSON-RPC communication. If a library prints to stdout during init, the stream gets corrupted. v3.4.3+ includes stdout protection that prevents this. If you're on an older version, upgrade:
1349
+
1350
+ ```bash
1351
+ pip install --upgrade knowledge-rag
1352
+ ```
1353
+
1354
+ ### Slow first query
1355
+
1356
+ The cross-encoder reranker model is lazy-loaded on the first query. This adds a one-time ~2-3 second delay for model download and loading. Subsequent queries are fast. If the model cannot be loaded, search falls back to RRF ordering and does not retry loading the reranker until the server restarts.
1357
+
1358
+ ### Memory usage
1359
+
1360
+ With ~200 documents, expect ~300-500MB RAM. The embedding model (~200MB ONNX runtime resident, lazy-loaded on first query since v3.8.0) and reranker (~25MB, lazy-loaded) are loaded into memory only when actually used. For very large knowledge bases (1000+ documents), consider enabling GPU acceleration and using exclude patterns to limit index scope.
1361
+
1362
+ ### Multiple MCP clients spawn duplicate servers
1363
+
1364
+ MCP stdio is one process per client by protocol — multiple Claude Code windows, Claude Desktop + IDE, etc. each spawn their own `knowledge-rag` process. Since v3.8.0 idle processes are cheap (no embedding model loaded until first query). If you've measured and want a hard cap of one server per data directory, opt in:
1365
+
1366
+ ```bash
1367
+ export KNOWLEDGE_RAG_SINGLE_INSTANCE=1
1368
+ ```
1369
+
1370
+ A second instance exits immediately with code 75. Default is OFF (multi-client friendly). Full guide: [docs/single-instance.md](docs/single-instance.md). Sample MCP config: [examples/mcp-config-single-instance.json](examples/mcp-config-single-instance.json).
1371
+
1372
+ ### SSE server won't start
1373
+
1374
+ ```bash
1375
+ # Check if port 8179 is already in use
1376
+ # Windows:
1377
+ netstat -aon | findstr :8179
1378
+ # Linux/macOS:
1379
+ lsof -i :8179
1380
+ ```
1381
+
1382
+ If `uvicorn` is not found, install the server extras: `pip install knowledge-rag[server]`
1383
+
1384
+ ### Can't connect to SSE server
1385
+
1386
+ Verify the server is running and the URL is correct:
1387
+
1388
+ ```bash
1389
+ curl http://127.0.0.1:8179/sse
1390
+ ```
1391
+
1392
+ Common issues:
1393
+ - Wrong URL: must end with `/sse` (not just the port)
1394
+ - Firewall blocking the port
1395
+ - Server started with a different host/port than configured in the MCP client
1396
+
1397
+ ---
1398
+
1399
+ ## Changelog
1400
+
1401
+ ### Unreleased
1402
+
1403
+ ### v4.6.0 (2026-07-30) — MCP spec 2026-07-28 via Anthropic Tier 1 SDK (mcp 2.x)
1404
+
1405
+ - **NEW (protocol)**: adopt the [MCP spec `2026-07-28`](https://blog.modelcontextprotocol.io/posts/2026-07-28/) — stateless request/response core, per-request `_meta.io.modelcontextprotocol/protocolVersion` + `clientCapabilities`, `serverInfo` on responses, retirement of the `initialize`/`initialized` handshake and the `Mcp-Session-Id` header. The MCP client Claude Code (and every Tier 1 client) negotiates the spec version per request, so this release stays backwards-compatible for clients speaking earlier spec revisions.
1406
+ - **NEW (deps)**: bump `mcp` from `>=1.6.0,<2.0.0` to `>=2.0.0,<3.0.0`. Anthropic published `mcp 2.0.0` on 2026-07-28 as the Tier 1 Python SDK — it speaks the new spec natively and is the direct upstream reference. Delivers the follow-up promised in v4.5.0-post — no longer trapped on `mcp.server.fastmcp` which upstream `mcp 2.0.0` renamed to `mcp.server.MCPServer`.
1407
+ - **CHANGE (imports)**: `mcp_server/server.py` now imports `MCPServer` from `mcp.server` instead of `FastMCP` from `mcp.server.fastmcp`. `@mcp.tool()` / `@mcp.resource()` / `@mcp.prompt()` decorator ergonomics are unchanged — only the class name and import path move. All 13 MCP tool signatures, all tool names, and every public function under `mcp_server.*` are **frozen and unchanged** — `check_api_surface.py --check` passes clean, `test_backwards_compat.py` stays green.
1408
+ - **CHANGE (constructor)**: `MCPServer(...)` (like FastMCP 3.x) no longer accepts `host` / `port` on the constructor; those parameters now belong to the transport and are passed to `mcp.run(transport=..., host=..., port=...)` on the non-stdio branches. stdio users see zero behaviour change.
1409
+ - **CHANGE (server identity)**: `MCPServer(name="knowledge-rag", version="4.6.0")` — server now advertises its version explicitly in `serverInfo`, matching the `2026-07-28` spec.
1410
+ - **NON-GOAL — why not `fastmcp` (jlowin)**: an initial attempt via the third-party standalone `fastmcp` package added ~20 transitive dependencies (from its `[client,server]` extras) and pushed `test_bench_orchestrator_idle_rss` +21.9% and `test_bench_query_cache_5000_entries` +17.1% — a real regression, not jitter. Cutting the extras to `fastmcp-slim[mcp]` broke `@mcp.tool()` at runtime (`ImportError: FastMCP server support is not installed`). Anthropic's official `mcp 2.x` is the correct entry point.
1411
+ - **VERSION**: 4.5.1 → **4.6.0** (MINOR — new spec adoption via Tier 1 SDK; every public API + every MCP tool signature preserved).
1412
+
1413
+ ### v4.5.1 (2026-07-28) — Fase 1 Security Hardening (library, standalone)
1414
+
1415
+ - **NEW (security)**: Fase 1 Security Hardening — a standalone `mcp_server.security` library ships with the tools needed to close three attack classes against untrusted document ingestion. **Nothing in the existing call graph changes** in v4.5.1: `security.py` is imported by no other module in this release, so byte-identical behaviour is preserved for every user. Integration with `add_document` / `add_from_url` / `parse_file` / MCP HTTP transports lands in v4.6.0 (14 tests in `tests/security/` are marked `xfail` accordingly — they turn green once the callers are wired up). Users motivated to adopt hardening today can `from mcp_server.security import ...` explicitly. Contract per-defense:
1416
+ - **CWE-22 / CWE-59** path traversal + symlink escape: `validate_path_within(base, candidate)` + `is_path_within(base, candidate)` + `PathEscapeError`. Rejects `..`, absolute-outside-base, and symlinks whose target leaves the corpus root.
1417
+ - **OWASP LLM01:2025** prompt injection: `neutralize_injection_sentinels` + `sanitize_external_content` + `wrap_external_content` + `detect_external_marker`. Wraps content fetched from URLs / dropped files with tagged sentinels the downstream LLM can distinguish from operator-authored context.
1418
+ - **CWE-287** unauthenticated HTTP transport: `BearerAuthMiddleware` + `bearer_token_matches` (constant-time comparison) + `extract_bearer_token`. Attach to a Starlette / FastAPI app; stdio transport bypasses auth by construction (no network surface).
1419
+ - **NEW**: `docs/adr/0001-fase1-security-hardening.md` — ADR capturing threat model + v4.6.0 integration roadmap.
1420
+ - **NEW**: `.github/SECURITY.md` — GitHub vulnerability reporting policy pointer + `.github/openssf-best-practices.md` (evidence pack for OpenSSF Best Practices badge registration).
1421
+ - **NEW**: `tests/security/` (71 tests total; 57 library unit tests green in v4.5.1, 14 integration tests `xfail` pending v4.6.0 wire-up, `strict=False` so they auto-turn-green after integration) + `tests/test_security_docs.py` (9 assertions pinning `SECURITY.md`, README badge block, dependabot weekly pip schedule).
1422
+ - **DOCS**: README carries the OpenSSF Best Practices badge (placeholder project ID until registration completes).
1423
+ - **DEPS**: `dependabot.yml` pip schedule tightened `monthly` → `weekly` (OpenSSF-recommended floor for language ecosystems).
1424
+ - **VERSION**: 4.5.0 → **4.5.1** (PATCH — additive library only, zero behaviour change on the default install).
1425
+
1426
+ ### v4.5.0-post (rolling fixes since v4.5.0 release)
1427
+
1428
+ - **FIX (deps)**: pin `mcp<2.0.0` in both `requirements.txt` and `pyproject.toml` after the upstream `mcp` package published 2.0.0, which drops/relocates `mcp.server.fastmcp`. Every `mcp_server.server` import chain — and therefore every test module — was failing collection with `ModuleNotFoundError: No module named 'mcp.server.fastmcp'` on fresh CI runners across all 9 OS×Python cells. Pin restores green until we migrate to the new 2.x FastMCP entry point in a follow-up. Root cause: `requirements.txt` had `mcp>=1.0.0` unbounded, so `pip install -r requirements.txt` in CI ignored the pyproject constraint. (#122)
1429
+ - **FIX**: `_ensure_bm25_index` no longer traps the guard flag `_bm25_initialized` in a stuck-True state when the server boots against an empty ChromaDB collection. Prior behavior set the flag unconditionally at the end of the guarded block, so a fresh-install bootup or a metadata-mapping loss left BM25 permanently uninitialized: subsequent `add_document()` calls populate `bm25_index.corpus` but do not rebuild the inverted index, and `_ensure_bm25_index` — the only path that does — short-circuited on the stale flag. All keyword-only searches returned zero results; hybrid searches returned only semantic hits with `bm25_rank: null`. The flag is now set only after a build that actually produced an index; empty collections and exceptions leave it False so the next call retries. (#114)
1430
+ - **TEST**: New `TestEmptyBootupDoesNotTrapGuardFlag` (5 tests) pins the contract — empty-collection boot leaves flag False, populated collection marks True, empty-then-populated recovery works, empty `get()` result leaves False, and `build_index()` exceptions leave False for retry. Baseline: 271 → 276.
1431
+
1432
+ ### v4.5.0 (2026-07-06) — Hybrid Search Ranking & Routing Fix
1433
+
1434
+ - **FIX**: `search_knowledge` now searches the entire index when the user omits `category_filter`. The internal `_route_by_keywords()` heuristic previously acted as a hard where-filter on both the semantic and BM25 branches, so queries whose terms happened to map to a sparsely-populated category could return two documents while thousands of relevant chunks in other categories were silently dropped. The router is now **informational only**: the `routed_by` field is still populated in every result for telemetry (public API unchanged), but the candidate set is never restricted. Users who want a hard filter still get it by passing `category_filter=...` explicitly — which continues to filter BM25 consistently with #109. (#112)
1435
+ - **NEW**: Hybrid search adds a small bounded ranking boost when query terms match indexed `source` or `filename` metadata, helping navigational queries surface the canonical file for a topic instead of adjacent files that only cross-reference it. The boost is capped at ~20% of typical RRF magnitudes, so it breaks near-ties without overriding semantic or BM25 signal. Public API unchanged. (#110, thanks @Hohlas)
1436
+ - **TEST**: New `TestKeywordRoutingBehavior` (3 tests) pins the routing fix — BM25 and semantic branches must both see the full corpus when no explicit filter is passed, and explicit `category_filter` still overrides the router. New `TestPathAwareRanking` covers the path-metadata boost. Baseline: 266 → 271.
1437
+ - **UPGRADE NOTE**: warm `query_cache` entries from before v4.5.0 should be invalidated by restarting the server so cached responses no longer reflect the pre-fix restrictive behavior.
1438
+
1439
+ ### v4.4.0 (2026-07-06) — Cross-Platform Installer & Hybrid Search Category Filter
1440
+
1441
+ - **NEW**: Cross-platform, multi-LLM-client installer (`install.py`) driving both `install.sh` (Linux/macOS) and `install.ps1` (Windows) as thin wrappers. One codebase, one behavior across every OS.
1442
+ - **NEW**: Auto-detects and registers `knowledge-rag` in 8 LLM clients — Claude Code, Claude Desktop, Cursor, Windsurf, VS Code (Copilot Chat), Cline, Gemini CLI, Zed — writing to each tool's canonical config path with the correct JSON schema per client (VS Code uses `servers`, Zed uses `context_servers`, everyone else uses `mcpServers`).
1443
+ - **NEW**: `--for <clients>` / `--exclude <clients>` opt-in/opt-out selection, `--dry-run` preview, `--list-clients` registry inspection, `--pypi-version <ver>` pinning, `--skip-init` / `--skip-model` for fast reruns. See `python install.py --help`.
1444
+ - **FIX**: `install.ps1` no longer writes MCP config to `~/.claude/mcp.json` (a stale secondary path); it now targets `~/.claude.json` — the file Claude Code actually reads — via idempotent JSON merge that preserves every existing MCP server entry with an automatic `.knowledge-rag.bak` backup.
1445
+ - **FIX**: `install.ps1` gains PyPI mode (`pip install knowledge-rag`) and runs `mcp_server.server init` on install — feature parity with `install.sh`.
1446
+ - **FIX**: MCP server spec no longer uses the fragile `cmd /c cd /d ... && python ...` wrapper on Windows; it emits the standard `command` + `cwd` shape supported natively by every modern client.
1447
+ - **FIX**: `install.sh` guards against `sh install.sh` (bash-only features now emit a clear error instead of a cryptic syntax failure).
1448
+ - **FIX**: Both scripts now correctly advertise **13 MCP tools** (was outdated at 12; `get_reindex_status` shipped in v4.3.0).
1449
+ - **FIX**: Windows-side `install.ps1` prefers `winget install Python.Python.3.12 --scope user` (no admin), falls back to python.org 3.12.7 (was pinned to 3.12.0).
1450
+ - **FIX**: Hybrid search now applies the active category filter to BM25 results before RRF fusion, preventing keyword-only BM25 hits from other categories from leaking into filtered searches. Both leak paths are closed: BM25 candidates are metadata-filtered before RRF (with `top_k` widened to `max_results * 20` to compensate for post-filter drop), and the fallback fetch during fusion re-checks `category` before adding a chunk to `combined_scores`. Note: the `category_filter` guard now also applies to the keyword-routed category — `_route_by_keywords()`-inferred routing filters BM25 too, consistent with the semantic branch. Users with warm `query_cache` entries should restart the server to invalidate stale results. (#109, thanks @Hohlas)
1451
+ - **TEST**: New `tests/test_installer_no_data_loss.py` (22 tests) locks in the installer's zero-data-loss contract across all three JSON schemas (`mcpServers` / `servers` / `context_servers`): top-level keys preserved, sibling MCP servers byte-identical, `.knowledge-rag.bak` backup written before every mutation, idempotent second run, `--dry-run` writes nothing, atomic `os.replace` write. Baseline: 231 → 266.
1452
+ - **TEST**: New `TestHybridCategoryFilter::test_bm25_results_respect_category_filter` in `tests/test_search.py` — deterministic regression covering BM25-only hits with mixed categories. Baseline: 266 → 267.
1453
+
1454
+ ### v4.3.1 (2026-06-22) — Hybrid Search Fixes
1455
+
1456
+ - **FIX**: Accept `"general"` as a valid category in `search_knowledge`. The parser hardcodes `"general"` as the fallback in `_detect_category` (`ingestion.py`), but the validator only built `valid_categories` from `config.keyword_routes` + `config.category_mappings.values()` — so users who customized `config.yaml` and dropped the default `"general": "general"` mapping hit `Invalid category` even though the index contained `general` documents. Validator now always tolerates `"general"`. (#98, thanks @Hohlas)
1457
+ - **FIX**: Skip BM25-only search results when Chroma can no longer resolve the chunk ID. Stale BM25 indices (typically right after `remove_document` or in the window between async reindex and BM25 rebuild) returned hits whose `collection.get()` came back empty; the previous fallback inserted entries with `document=""` / `metadata={}` into the reranker, polluting results with empty matches. The pipeline now `continue`s past those, dropping the stale hit cleanly. (#98, thanks @Hohlas)
1458
+ - **TEST**: Added `tests/test_pr98_regression.py` (4 tests) pinning both contracts so future refactors cannot silently revert either fix. Test count baseline: 227 → 231. (#99)
1459
+ - **CI**: Bumped `[tool.mypy] python_version` from 3.11 to 3.12 to accept PEP 695 `type` statements in the numpy stub (`numpy/__init__.pyi`) which were breaking the Pillar 7 strict gate. Only affects static analysis; `requires-python = ">=3.11"` unchanged. (#100)
1460
+
1461
+ ### v4.3.0 (2026-06-17) — Async Reindex, GPU CUDA 12, 13th MCP Tool
1462
+
1463
+ - **NEW**: `get_reindex_status` MCP tool — lightweight reindex progress polling without computing full index stats. Returns active/idle status, percent, processed/total, errors, and last result.
1464
+ - **NEW**: `reindex_documents` now runs in background via daemon thread — returns immediately with `{"status": "started"}`. Eliminates MCP timeout on large document sets (5K+ files). Concurrent calls return `already_running` with current progress.
1465
+ - **NEW**: GPU acceleration with full CUDA 12 support — `onnxruntime-gpu` + 7 NVIDIA pip packages (`cublas`, `cudnn`, `cuda-runtime`, `cufft`, `cusparse`, `cusolver`, `curand`, `nvjitlink`). Server auto-detects GPU on startup with 4-step verification (providers, DLLs, nvidia-smi, session creation). Falls back to CPU gracefully.
1466
+ - **NEW**: `_setup_cuda_dll_paths()` adds NVIDIA pip package DLL directories to `PATH` automatically on Windows — onnxruntime finds CUDA 12 DLLs without a full CUDA Toolkit install.
1467
+ - **DEPS**: `[gpu]` extra expanded from 3 to 8 packages (added `cufft`, `cusparse`, `cusolver`, `curand`, `nvjitlink`).
1468
+ - **FIX**: GPU status reporting now uses actual ONNX session creation test instead of just checking `get_available_providers()` — prevents false "GPU ACTIVE" when CUDA DLLs are missing.
1469
+ - **DOCS**: GPU Acceleration section rewritten with complete requirements table, setup steps, verification instructions, and fallback behavior.
1470
+ - **DOCS**: Tool reference updated — `reindex_documents` async behavior documented, `get_reindex_status` reference added.
1471
+ - **TEST**: Backwards-compat baseline updated for 13 MCP tools.
1472
+
1473
+ ### v4.2.0 (2026-06-17) — Search Performance & Output Quality
1474
+
1475
+ - **PERF**: Custom inverted-index BM25 replaces `rank-bm25` full-corpus scan — 128× faster keyword search on 50K+ chunk corpora. Only documents containing query terms are scored via posting lists.
1476
+ - **PERF**: `numpy.argpartition` for O(n) top-k selection instead of O(n log n) sort.
1477
+ - **PERF**: Batched adjacent chunk fetch — single ChromaDB `collection.get()` call replaces N round-trips per result.
1478
+ - **PERF**: O(1) reverse lookup via `_source_to_docid` dict eliminates linear scans of `_indexed_docs` in `search_similar`, `update_document`, `remove_document`, and `_expand_with_adjacent_chunks`.
1479
+ - **NEW**: `snippet_mode` parameter on `search_knowledge` (default: `true`) — truncates content to ~500 chars at natural break points with `content_length` field. Reduces token consumption by ~72%.
1480
+ - **NEW**: `min_score` parameter on `search_knowledge` (default: `0.0`) — filters results below a normalized relevance threshold. Response includes `filtered_by_score` count.
1481
+ - **NEW**: `filtered_by_score` field in search response JSON for transparency.
1482
+ - **DEPS**: `numpy` added as direct dependency (was transitive via fastembed); `rank-bm25` import removed from server.py.
1483
+ - **TEST**: 6 new tests for `min_score` filtering and `snippet_mode` truncation.
1484
+ - **TEST**: Updated backwards-compat baseline to include new `search_knowledge` parameters.
1485
+
1486
+ ### v4.1.2 (2026-06-17)
1487
+
1488
+ - **FIX**: `_save_metadata` dict snapshot prevents concurrent modification crash during file watcher events.
1489
+ - **STYLE**: ruff format applied to server.py.
1490
+
1491
+ ### v4.1.1 (2026-06-17)
1492
+
1493
+ - **FIX**: All `_indexed_docs` iterations now use `list()` snapshot, preventing `dictionary changed size during iteration` crash when FileWatcher modifies the index concurrently with MCP tool calls (affects `search_knowledge`, `search_similar`, `update_document`, `remove_document`, `evaluate_retrieval`, `list_categories`, `list_documents`)
1494
+
1495
+ ### v4.1.0 (2026-06-17)
1496
+
1497
+ - **Added:** `query_expansion_groups` config for symmetric synonym expansion (#92)
1498
+ - **Improved:** `expand_query()` now returns deterministic expansion order (set → ordered list with dedup)
1499
+
1500
+ ### v4.0.1 (2026-06-16)
1501
+
1502
+ - **FIX**: Orphan cleanup now runs before indexing loop, preventing chunk loss when files are moved (#90).
1503
+ - **FIX**: Chunk deduplication is now per-document instead of global, preventing cross-document chunk deletion (#91).
1504
+ - **FIX**: Added `on_moved` handler to `DocumentWatcher` for proper file move detection.
1505
+ - **FIX**: Startup preflight probes ChromaDB in a child process and moves crashing persistent indexes to `data/backups/auto-repair-*` before MCP initialization.
1506
+ - **FIX**: Reranker load failures now fall back to RRF ordering instead of failing `search_knowledge` on offline machines.
1507
+ - **FIX**: Virtualenv project-root detection now handles Python symlinks that resolve to the system interpreter.
1508
+ - **NEW**: `knowledge-rag-guarded` console script kept as an explicit guarded startup alias.
1509
+
1510
+ ### v4.0.0 (2026-06-09) — Enterprise Concurrent Access
1511
+
1512
+ - **NEW**: SSE and streamable-http transport modes — 1 server serves N clients (`server.transport: "sse"` in config.yaml or `--transport sse` CLI).
1513
+ - **NEW**: Thread-safe shared state for concurrent queries — QueryCache locking, BM25 build lock, orchestrator double-checked locking.
1514
+ - **NEW**: ChromaDB WAL mode enabled automatically in SSE/HTTP mode for concurrent read performance.
1515
+ - **NEW**: Optional rate limiting — sliding-window counter, configurable RPM and burst, disabled by default.
1516
+ - **NEW**: Optional Prometheus metrics endpoint — tool call counts, latency histograms, separate port, disabled by default.
1517
+ - **NEW**: All 13 MCP tools instrumented with `@rate_limited` and `@instrument` decorators (zero-cost when disabled).
1518
+ - **NEW**: `--transport` CLI override for Docker/systemd deployments.
1519
+ - **NEW**: `pip install knowledge-rag[server]` optional dependency for SSE/HTTP (uvicorn).
1520
+ - **CHANGED**: SSE/HTTP mode auto-enables single-instance lock (port collision prevention).
1521
+ - **CHANGED**: `mcp` dependency bumped to `>=1.6.0` (SSE/streamable-http support).
1522
+ - **MIGRATION**: Default transport remains `stdio` — existing users need zero changes. See config.example.yaml for SSE setup.
1523
+
1524
+ ### v3.9.1 (2026-06-08)
1525
+
1526
+ - **FIX**: Expand `~` in `config.yaml` path values (`documents_dir`, `data_dir`, `models_cache_dir`) via `expanduser()` on all platforms (#86).
1527
+ - **FIX**: Warn when `documents_dir` resolves to a non-existent path instead of silently indexing zero files.
1528
+ - **FIX**: File watcher now uses accumulate-mode debounce — bulk file copies no longer starve the reindex trigger.
1529
+ - **FIX**: Concurrent `index_all()` calls are serialized via `_index_lock` to prevent ChromaDB SQLite corruption.
1530
+ - **FIX**: `collection.add()` is batched (500 chunks/call) to cap memory usage during large reindex operations.
1531
+ - **NEW**: `KNOWLEDGE_RAG_WATCHER_DISABLED=1` env var to disable the file watcher for troubleshooting.
1532
+ - **NEW**: Progress logging every 10% for reindex operations with >100 documents.
1533
+
1534
+ ### v3.9.0 (2026-05-10) — Quality Gate
1535
+
1536
+ **Major governance + CI hardening release. No runtime behavior change in `mcp_server/`. Public API surface unchanged from v3.8.1.**
1537
+
1538
+ - **NEW** Quality Gate workflow (`.github/workflows/quality-gate.yml`) enforcing the 7 pillars on every PR: Security, Stability, Memory Leak, Versatility, Scalability, Versioning, Quality. 35+ status checks total.
1539
+ - **NEW** Nightly resilience workflow (`.github/workflows/nightly.yml`): chaos suite (failure injection), 1h soak test (50K-iteration loop), determinism check (full suite × 3), mutation testing (mutmut). Auto-opens GitHub issue on any nightly failure.
1540
+ - **NEW** Performance benchmark suite under `bench/` (12 microbenchmarks, pytest-benchmark) with 10% regression gate on every PR.
1541
+ - **NEW** Public performance dashboard via GitHub Pages (`.github/workflows/bench-pages.yml`) — chart of latency/throughput per commit. Dormant until repo Pages is enabled.
1542
+ - **NEW** Property-based fuzzing of all parsers via Hypothesis (`tests/test_ingestion_property.py`) — 200 random examples per CI run.
1543
+ - **NEW** Memory baseline regression tests (`tests/test_memory_baseline.py`, cross-platform via psutil) — RSS bounded under 1000 queries; nightly soak amplifies to 50K iterations.
1544
+ - **NEW** Property/locale/format/preset matrices (`tests/test_presets.py`, `tests/test_locale.py`, `tests/test_format_smoke.py`).
1545
+ - **NEW** Backwards-compatibility regression tests (`tests/test_backwards_compat.py`) — legacy YAML configs from v3.6.0 / v3.7.0 still parse; all 13 MCP tool parameter names frozen.
1546
+ - **NEW** AST-based public API surface diff (`scripts/check_api_surface.py`) — any breaking change blocks merge, baseline at `.github/api-surface-baseline.json`.
1547
+ - **NEW** CHANGELOG enforcement (`scripts/check_changelog.py`) — user-facing PRs must add a bullet under `## Unreleased`; bypass via `skip-changelog` label.
1548
+ - **NEW** Test count anti-regression (`scripts/check_test_count.py`) — guards against silent test deletion.
1549
+ - **NEW** Conventional commits required on every PR title (commitlint via `amannn/action-semantic-pull-request`).
1550
+ - **NEW** mypy `--strict` rolling out per-module (currently `instance_lock.py` + `preflight.py` + `scripts/`); interrogate docstring coverage ≥ 80%; radon, vulture, PR-size guard report-only.
1551
+ - **NEW** CI matrix expanded to 9 cells: Linux + Windows + **macOS** × 3.11 + 3.12 + **3.13** (all required at v3.9.0; macOS / 3.13 promoted from experimental after two clean cycles).
1552
+ - **NEW** Governance docs: `CONTRIBUTING.md`, `CODE_OF_CONDUCT.md`, `SECURITY.md`, `.github/PULL_REQUEST_TEMPLATE.md`, 3 issue templates, expanded `CODEOWNERS`.
1553
+ - **NEW** Pre-commit hooks: ruff, gitleaks, version-sync, conventional commits.
1554
+ - **CHORE** `.github/codecov.yml` enforcing coverage trend gate (-0.5pp blocks; new code ≥ 70%).
1555
+
1556
+ ### v3.8.1 (2026-05-10) — hotfix
1557
+
1558
+ - **FIX (critical)**: `FastEmbedEmbeddings.__call__` no longer returns vectors of zeros when the ONNX model fails to load or `embed()` raises. The previous behavior silently corrupted the index — ChromaDB stored zero embeddings, `count()` reported normal numbers, smart-reindex skipped the bad chunks, and queries returned garbage scores with no error visible. Now raises `EmbeddingModelLoadError` / `EmbeddingError`. (#36)
1559
+ - **FIX**: Sticky `_load_failed` flag — after a load failure, subsequent calls re-raise immediately instead of looping through HuggingFace download attempts (was the "frozen query" UX in v3.8.0).
1560
+ - **NEW**: Sanity checks in `__call__` — embed count and dim mismatches raise `EmbeddingError` instead of silently returning malformed vectors.
1561
+ - **TEST**: 7 new regression cases in `tests/test_lazy_embeddings.py`, including `test_does_not_return_zero_vectors_silently` as a guard for the whole class of bug.
1562
+ - **NOTE**: This is a pre-existing bug in master, not introduced by v3.8.0. v3.8.0 lazy-load expanded the impact (failures moved to query time). All v3.8.0 users should upgrade.
1563
+
1564
+ ### v3.8.0 (2026-05-10)
1565
+
1566
+ - **NEW**: Lazy-load FastEmbed embedding model (~200MB ONNX runtime). Loads on first query instead of startup — idle `knowledge-rag` processes are now cheap, which matters when MCP stdio clients spawn parallel server processes (multiple Claude Code windows, Claude Desktop + IDE, etc.). Public API unchanged. (#32)
1567
+ - **NEW**: Opt-in single-instance guard via `KNOWLEDGE_RAG_SINGLE_INSTANCE=1` env var. **OFF by default** — multi-client MCP usage continues to work unchanged. When enabled, a second server process for the same `data_dir` exits with code 75 (`EX_TEMPFAIL`). Includes stale-PID recovery and SIGINT/SIGTERM handlers. See [docs/single-instance.md](docs/single-instance.md). (#33, original concept by @Hohlas in #31)
1568
+ - **NEW**: `examples/mcp-config-single-instance.json` — sample MCP client config for the opt-in guard.
1569
+ - **DOCS**: New `docs/single-instance.md` — when to use, when NOT to use, troubleshooting, full activation reference.
1570
+ - **DOCS**: README troubleshooting section for "Multiple MCP clients spawn duplicate servers" + memory-usage note for lazy embeddings.
1571
+ - **CHORE**: Sync version across `pyproject.toml`, `mcp_server/__init__.py`, and `npm/package.json` (was drifting since v3.5.x).
1572
+ - **CHORE**: pytest `tmp_path_retention_count=1` to avoid Windows atexit cleanup race in CI.
1573
+ - **ROADMAP**: Tracked v4.0 shared-service architecture (one daemon, many thin MCP clients) as the long-term fix for multi-process resource duplication. (#34)
1574
+
1575
+ ### v3.6.2 (2026-04-23)
1576
+
1577
+ - **INFRA**: NPM provenance attestation (SLSA supply chain security), full README on npm page
1578
+ - **DOCS**: Reorganize Installation section — add NPX and Docker install methods, update What's New to v3.6.0
1579
+
1580
+ ### v3.6.0 (2026-04-23)
1581
+
1582
+ - **NEW**: Multi-language code parsing — C (`.c`), C++ (`.cpp`/`.h`), JavaScript (`.js`/`.jsx`), TypeScript (`.ts`/`.tsx`) with per-language function/class/import extraction
1583
+ - **NEW**: XML parser (`.xml`) — root element and namespace metadata extraction
1584
+ - **NEW**: All 8 new formats default enabled — no config change needed
1585
+ - **NEW**: NPM wrapper (`npx knowledge-rag`) + Docker image (`ghcr.io/lyonzin/knowledge-rag`)
1586
+ - **NEW**: Automated release pipeline — PyPI (Trusted Publishing), NPM, Docker GHCR
1587
+ - **IMPROVED**: Code parser reports correct `language` metadata per file type (was hardcoded to `"python"` for all code files)
1588
+
1589
+ ### v3.5.2 (2026-04-16)
1590
+
1591
+ - **NEW**: Auto-discovery of CUDA 12 DLLs from pip-installed NVIDIA packages — no manual PATH configuration needed
1592
+ - **NEW**: Graceful GPU→CPU fallback with `[WARN]` log when CUDA init fails (missing drivers, wrong version, etc.)
1593
+ - **FIX**: Explicit `CPUExecutionProvider` when `gpu: false` — eliminates noisy CUDA probe errors in logs
1594
+ - **FIX**: BASE_DIR resolution now correctly prefers directories with `config.yaml` over those with only `config.example.yaml` (fixes editable installs)
1595
+
1596
+ ### v3.5.1 (2026-04-16)
1597
+
1598
+ - **FIX**: Removed Python upper bound constraint (`<3.13` → `>=3.11`). Python 3.13 and 3.14 now supported — onnxruntime ships wheels for both.
1599
+
1600
+ ### v3.5.0 (2026-04-16)
1601
+
1602
+ - **NEW**: Optional GPU acceleration for ONNX embeddings — `pip install knowledge-rag[gpu]` + `models.embedding.gpu: true` in config. 5-10x faster indexing on NVIDIA GPUs with automatic CPU fallback.
1603
+ - **DOCS**: Supported formats table added to README (20 formats)
1604
+
1605
+ ### v3.4.3 (2026-04-16)
1606
+
1607
+ - **FIX**: Correct stdout protection via save/restore pattern — `__init__.py` saves original stdout and redirects to stderr during init, `server.py main()` restores it before `mcp.run()`. v3.4.2's global redirect broke MCP JSON-RPC response channel.
1608
+
1609
+ ### v3.4.1 (2026-04-16)
1610
+
1611
+ - **FIX**: `pip install knowledge-rag` now auto-detects project directory from venv location
1612
+ - **NEW**: `install.sh` — Linux/macOS installer with pip and from-source modes
1613
+ - **IMPROVED**: BASE_DIR resolution chain: env var → source dir → venv parent → CWD → fallback
1614
+
1615
+ ### v3.4.0 (2026-04-16)
1616
+
1617
+ - **NEW**: `models_cache_dir` — persistent embedding model cache, prevents re-download after reboots
1618
+ - **NEW**: `exclude_patterns` — glob-based file/directory exclusion during indexing
1619
+ - **NEW**: Jupyter Notebook (.ipynb) parser — extracts markdown and code cell sources only
1620
+ - **NEW**: MCP stdout protection — redirects stdout to stderr before server start
1621
+ - **NEW**: File watcher resilience — graceful fallback when Linux inotify limits are reached
1622
+ - **NEW**: MetaTrader (.mq4, .mqh) support — opt-in code parsing
1623
+ - **NEW**: 23 new tests (exclude patterns, ipynb parser, stdout protection)
1624
+ - Community credit: [@Hohlas](https://github.com/Hohlas) ([PR #18](https://github.com/lyonzin/knowledge-rag/pull/18))
1625
+
1626
+ ### v3.3.x
1627
+
1628
+ - **v3.3.2**: Full type validation on YAML config, bounds checking, version sync
1629
+ - **v3.3.1**: YAML null value crash fix, presets bundled in pip wheel, `knowledge-rag init` CLI
1630
+ - **v3.3.0**: YAML configuration system, 4 domain presets, generic use support
1631
+
1632
+ ### v3.2.x
1633
+
1634
+ - **v3.2.4**: Symlink support with circular loop protection
1635
+ - **v3.2.3**: BASE_DIR smart detection for pip installs
1636
+ - **v3.2.2**: Plug-and-play pip install, `KNOWLEDGE_RAG_DIR` env var
1637
+ - **v3.2.1**: Auto-recovery from corrupted ChromaDB
1638
+ - **v3.2.0**: Parallel BM25 + Semantic search, adjacent chunk retrieval
1639
+
1640
+ ### v3.1.x
1641
+
1642
+ - **v3.1.1**: Code block protection in markdown chunker, AAR category, 14 CVE aliases
1643
+ - **v3.1.0**: DOCX/XLSX/PPTX/CSV support, file watcher, MMR diversification, PyPI publish
1644
+
1645
+ ### v3.0.0 (2026-03-19)
1646
+
1647
+ - Replaced Ollama with FastEmbed (ONNX in-process)
1648
+ - Cross-encoder reranking, markdown-aware chunking, query expansion
1649
+ - 6 new MCP tools (12 total), auto-migration from v2.x
1650
+
1651
+ <details>
1652
+ <summary>v2.x and earlier</summary>
1653
+
1654
+ - **v2.2.0**: `hybrid_alpha=0` skips Ollama, default changed from 0.5 to 0.3
1655
+ - **v2.1.0**: Mermaid architecture diagrams
1656
+ - **v2.0.0**: Hybrid search, RRF fusion, `hybrid_alpha` parameter
1657
+ - **v1.1.0**: Incremental indexing, query cache, chunk deduplication
1658
+ - **v1.0.1**: Auto-cleanup orphan folders, removed hardcoded paths
1659
+ - **v1.0.0**: Initial release
1660
+ </details>
1661
+
1662
+ ---
1663
+
1664
+ ## Contributing
1665
+
1666
+ 1. Fork the repository
1667
+ 2. Create a feature branch (`git checkout -b feature/amazing-feature`)
1668
+ 3. Commit your changes
1669
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
1670
+ 5. Open a Pull Request
1671
+
1672
+ ---
1673
+
1674
+ ## License
1675
+
1676
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
1677
+
1678
+ ---
1679
+
1680
+ ## Acknowledgments
1681
+
1682
+ - [ChromaDB](https://www.trychroma.com/) — Vector database
1683
+ - [FastEmbed](https://qdrant.github.io/fastembed/) — ONNX Runtime embeddings
1684
+ - [FastMCP](https://github.com/anthropics/mcp) — Model Context Protocol framework
1685
+ - [PyMuPDF](https://pymupdf.readthedocs.io/) — PDF parsing
1686
+ - [rank-bm25](https://github.com/dorianbrown/rank_bm25) — BM25 Okapi implementation
1687
+ - [Watchdog](https://github.com/gorakhargosh/watchdog) — File system monitoring
1688
+ - [python-docx](https://python-docx.readthedocs.io/) / [openpyxl](https://openpyxl.readthedocs.io/) / [python-pptx](https://python-pptx.readthedocs.io/) — Office document parsing
1689
+ - [PyYAML](https://pyyaml.org/) — YAML configuration parsing
1690
+ - [Beautiful Soup](https://www.crummy.com/software/BeautifulSoup/) — HTML parsing for URL ingestion
1691
+
1692
+ ---
1693
+
1694
+ ## Author
1695
+
1696
+ **Lyon.**
1697
+
1698
+ Security Researcher | Developer
1699
+
1700
+ ---
1701
+
1702
+ <div align="center">
1703
+
1704
+ **[Back to Top](#knowledge-rag)**
1705
+
1706
+ </div>