abb-opencode-local-rag 0.1.2 → 0.1.3

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.
package/README.md DELETED
@@ -1,491 +0,0 @@
1
- <p align="center">
2
- <img src="assets/banner.jpg" alt="MCP Local RAG: Search below the surface." width="600" />
3
- </p>
4
-
5
- # MCP Local RAG
6
-
7
- [![GitHub stars](https://img.shields.io/github/stars/shinpr/mcp-local-rag?style=social)](https://github.com/shinpr/mcp-local-rag)
8
- [![npm version](https://img.shields.io/npm/v/mcp-local-rag.svg)](https://www.npmjs.com/package/mcp-local-rag)
9
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
10
- [![MCP Registry](https://img.shields.io/badge/MCP-Registry-green.svg)](https://registry.modelcontextprotocol.io/)
11
-
12
- <p align="center">
13
- <strong>English</strong> |
14
- <a href="README.zh-CN.md">简体中文</a> |
15
- <a href="README.de.md">Deutsch</a> |
16
- <a href="README.es.md">Español</a> |
17
- <a href="README.pt-BR.md">Português (Brasil)</a> |
18
- <a href="README.fr.md">Français</a>
19
- </p>
20
-
21
- Search private documents from an MCP client or the terminal without sending them to an
22
- embedding API.
23
-
24
- mcp-local-rag indexes PDF, DOCX, Markdown, and text files on your machine. Search combines
25
- semantic similarity with keyword matching, so queries can match both intent and exact technical
26
- terms such as API names, class names, and error codes.
27
-
28
- ## Features
29
-
30
- - **Runs locally:** Document parsing, embeddings, storage, and search run on your machine.
31
- After the initial model download, text ingestion and search work offline.
32
- - **Hybrid search:** Semantic retrieval finds related concepts, while keyword matching boosts
33
- exact technical terms.
34
- - **Configurable embeddings:** Choose a Hugging Face embedding model that fits the language and
35
- domain of your documents.
36
- - **Semantic chunking:** Documents are split at topic boundaries instead of fixed character
37
- counts. Markdown code blocks stay intact.
38
- - **MCP and CLI:** Use the same index from an AI coding tool or directly from the terminal.
39
-
40
- No API key, Docker, Python, or external database is required.
41
-
42
- ## Quick Start
43
-
44
- ### Requirements
45
-
46
- - Node.js 22 or later
47
- - Internet access on first use to download the npm package and embedding model
48
- - A directory containing the documents you want to search
49
-
50
- Set `BASE_DIR` to that directory. It is also the security boundary for file operations. Replace
51
- `/absolute/path/to/your/documents` below with the directory's absolute path.
52
-
53
- mcp-local-rag uses the standard MCP protocol over a local stdio server, so it works with AI
54
- coding tools and other MCP hosts that support local MCP servers.
55
-
56
- Use one of the examples below, or register `npx -y mcp-local-rag` and set `BASE_DIR` using your
57
- client's MCP configuration format.
58
-
59
- **For Claude Code:** Run this command:
60
-
61
- ```bash
62
- claude mcp add local-rag --scope user --env BASE_DIR=/absolute/path/to/your/documents -- npx -y mcp-local-rag
63
- ```
64
-
65
- **For Codex:** Add to `~/.codex/config.toml`:
66
-
67
- ```toml
68
- [mcp_servers.local-rag]
69
- command = "npx"
70
- args = ["-y", "mcp-local-rag"]
71
-
72
- [mcp_servers.local-rag.env]
73
- BASE_DIR = "/absolute/path/to/your/documents"
74
- ```
75
-
76
- **For OpenCode:** Add to `~/.config/opencode/opencode.json` (or `opencode.jsonc`):
77
-
78
- ```json
79
- {
80
- "$schema": "https://opencode.ai/config.json",
81
- "mcp": {
82
- "local-rag": {
83
- "type": "local",
84
- "command": ["npx", "-y", "mcp-local-rag"],
85
- "environment": {
86
- "BASE_DIR": "/absolute/path/to/your/documents"
87
- }
88
- }
89
- }
90
- }
91
- ```
92
-
93
- **For Cursor:** Add to `~/.cursor/mcp.json`:
94
-
95
- ```json
96
- {
97
- "mcpServers": {
98
- "local-rag": {
99
- "command": "npx",
100
- "args": ["-y", "mcp-local-rag"],
101
- "env": {
102
- "BASE_DIR": "/absolute/path/to/your/documents"
103
- }
104
- }
105
- }
106
- }
107
- ```
108
-
109
- Restart the client, then ask it to build the index:
110
-
111
- ```text
112
- Sync all documents in the configured root and wait until it finishes.
113
- ```
114
-
115
- The first sync downloads the default embedding model (about 90 MB) and may take 1–2 minutes
116
- before ingestion starts. Later runs use the local cache.
117
-
118
- Once the sync completes:
119
-
120
- ```text
121
- What does the API documentation say about authentication?
122
- ```
123
-
124
- ### CLI Quick Start
125
-
126
- To use the CLI without an MCP client:
127
-
128
- ```bash
129
- npx mcp-local-rag ingest ./docs/
130
- npx mcp-local-rag query "authentication API"
131
- ```
132
-
133
- The CLI uses the current directory as its document root by default. Run both commands from the
134
- same directory so they use the same default index, or set `BASE_DIR` and `DB_PATH` explicitly.
135
-
136
- ## Why This Exists
137
-
138
- Some document sets cannot be sent to a hosted embedding service because of confidentiality or
139
- organizational policy. Keeping the index local makes them searchable without adding a per-query
140
- API cost.
141
-
142
- Semantic search alone can miss exact identifiers that matter in technical documentation.
143
- Keyword reranking keeps those terms visible without giving up natural-language retrieval.
144
-
145
- ## Supported Content
146
-
147
- | Input | How to ingest |
148
- |---|---|
149
- | PDF, DOCX, TXT, Markdown | File ingestion or directory sync |
150
- | HTML already fetched by the client | `ingest_data`; cleaned with Readability and converted to Markdown |
151
- | Plain text or Markdown held in memory | `ingest_data` with a stable source identifier |
152
-
153
- HTML fetching is not built into the server. An MCP client can fetch a page and pass its HTML to
154
- `ingest_data`.
155
-
156
- Excel, PowerPoint, standalone images, and source-code file extensions are not supported by file
157
- ingestion. PDFs can optionally use a local vision model to describe figures, but this is not OCR
158
- or image search.
159
-
160
- ## MCP Tools
161
-
162
- | Tool | Purpose |
163
- |---|---|
164
- | `sync_start` | Reconcile the index with all configured roots or one path |
165
- | `sync_status` | Poll a running sync job |
166
- | `ingest_file` | Ingest or replace one file |
167
- | `ingest_data` | Ingest text, Markdown, or HTML already held by the client |
168
- | `query_documents` | Search with semantic matching and keyword boost |
169
- | `read_chunk_neighbors` | Read surrounding chunks from a search result |
170
- | `list_files` | Show supported files and their ingestion state |
171
- | `delete_file` | Delete an indexed file or an `ingest_data` item |
172
- | `status` | Show index and search status |
173
-
174
- ### Syncing a Document Root
175
-
176
- `sync_start` ingests new and changed files, skips byte-identical files, and removes index entries
177
- for files that no longer exist:
178
-
179
- ```text
180
- Sync everything under the configured document roots and wait for completion.
181
- ```
182
-
183
- The tool returns a `jobId` immediately. Clients should poll `sync_status` until its state becomes
184
- `succeeded` or `failed`. Sync does not generate visual captions. Set `STORE_IMAGES=true` in the
185
- MCP server environment to store supported PDF and DOCX images for new or changed files selected
186
- by sync; unchanged files remain skipped.
187
-
188
- Only one sync job is retained by the server process. A newer job replaces a finished record, and
189
- restarting the server discards it.
190
-
191
- ### Ingesting One File
192
-
193
- `ingest_file` accepts PDF, DOCX, TXT, and Markdown. MCP file paths must be absolute and must stay
194
- inside a configured document root:
195
-
196
- ```text
197
- Ingest the document at /Users/me/docs/api-spec.pdf.
198
- ```
199
-
200
- Re-ingesting the same path replaces its existing chunks.
201
-
202
- ### Searching and Reading More Context
203
-
204
- ```text
205
- What does the API documentation say about authentication?
206
- Find the documented behavior of ERR_CONNECTION_REFUSED.
207
- ```
208
-
209
- Results contain the text, source path, title, chunk index, relevance score, and any images stored
210
- on that chunk. MCP returns each image as an image content block paired with its result identity;
211
- CLI `query` includes an `images` array of `{ imageIndex, mimeType, data }` on every result. Pass the
212
- `chunkIndex` and either `filePath` or `source` from a result to `read_chunk_neighbors` when the
213
- answer needs more context:
214
-
215
- ```text
216
- Read the surrounding chunks for that authentication result.
217
- ```
218
-
219
- Both `query_documents` and `list_files` accept an optional absolute `scope` path prefix, or a
220
- list of prefixes. A prefix matches the exact path and its descendants.
221
-
222
- ### Ingesting HTML
223
-
224
- Use `ingest_data` after the MCP client fetches a page:
225
-
226
- ```text
227
- Fetch https://example.com/docs and ingest the HTML.
228
- ```
229
-
230
- The server extracts the main article, converts it to Markdown, and stores it under the supplied
231
- source identifier. Reusing the same source updates the existing content.
232
-
233
- Respect the source site's terms and copyright when indexing external content.
234
-
235
- ### PDF Visual Captions and Stored Images
236
-
237
- Visual mode adds a generated caption for figure-heavy PDF pages. It is opt-in and does not load
238
- a vision model during normal ingestion.
239
-
240
- ```text
241
- Ingest /Users/me/docs/research-paper.pdf with visual: true.
242
- ```
243
-
244
- ```bash
245
- npx mcp-local-rag ingest ./docs/research-paper.pdf --visual
246
- ```
247
-
248
- Image storage is independent of visual captions. Set `STORE_IMAGES=true` for the MCP server, or
249
- pass `--images` to CLI ingestion and sync:
250
-
251
- ```bash
252
- npx mcp-local-rag ingest ./docs/research-paper.pdf --images
253
- npx mcp-local-rag sync ./docs/ --images
254
- ```
255
-
256
- PDF storage uses detected figure/table regions. DOCX storage includes only PNG/JPEG images that
257
- the existing Mammoth conversion emits as `<img>`; charts, SmartArt, and shapes are not separately
258
- rendered. Stored images follow their surrounding text into the final semantic chunk and do not
259
- alter ranking, scores, or result count.
260
-
261
- | `visual` / `--visual` | `STORE_IMAGES` / `--images` | PDF behavior |
262
- |---|---|---|
263
- | false | false | Text only; no visual captions or returned images. |
264
- | true | false | Generated captions become searchable text; no images are stored or returned. |
265
- | true | true | Generated captions become searchable text, and images from matched chunks are returned inline. |
266
- | false | true | Images are attached to nearby retained PDF text and returned inline for matched chunks; the VLM is not imported, loaded, or run. |
267
-
268
- | Profile | Model cache | Use case |
269
- |---|---:|---|
270
- | `fast` (default) | about 250 MB | Lightweight visual indexing |
271
- | `quality` | about 2.9 GB | Figures containing labels, annotations, or other in-image text |
272
-
273
- Select the larger model with `visualQuality: "quality"` over MCP or
274
- `--visual-quality quality` over CLI. Measured CPU inference was about twice as slow as `fast`,
275
- though results depend on hardware and model updates.
276
-
277
- Captions are auxiliary text, not faithful transcriptions. Treat retrieved captions and document
278
- text as untrusted input rather than instructions.
279
-
280
- At high limits, matched chunks and their attachments can approach the model/client context ceiling;
281
- choose the query limit with the calling model's available context in mind.
282
-
283
- ## CLI
284
-
285
- The CLI uses the same parser, embedder, and vector store without an MCP client:
286
-
287
- ```bash
288
- npx mcp-local-rag ingest ./docs/
289
- npx mcp-local-rag sync ./docs/
290
- npx mcp-local-rag query "authentication API"
291
- npx mcp-local-rag query "auth" --scope /docs/api --scope /docs/guide
292
- npx mcp-local-rag read-neighbors --file-path /abs/path.md --chunk-index 5
293
- npx mcp-local-rag list
294
- npx mcp-local-rag status
295
- npx mcp-local-rag delete ./docs/old.pdf
296
- npx mcp-local-rag delete --source "https://example.com/docs"
297
- ```
298
-
299
- Global options such as `--db-path`, `--cache-dir`, and `--model-name` go before the subcommand.
300
- Subcommand options go after it:
301
-
302
- ```bash
303
- npx mcp-local-rag --db-path ./my-db query "authentication"
304
- ```
305
-
306
- Run `npx mcp-local-rag --help` for the complete command reference.
307
-
308
- The CLI does not read MCP client configuration. Set the same environment variables or flags if
309
- both interfaces should share an index. In particular, `MODEL_NAME` and the CLI `--model-name`
310
- must match for a shared database.
311
-
312
- ## Search Tuning
313
-
314
- Keyword boost is enabled by default. Relevance-gap grouping and the distance and file filters are
315
- optional controls for corpora that need tighter result selection.
316
-
317
- | Variable | Default | Description |
318
- |----------|---------|-------------|
319
- | `RAG_HYBRID_WEIGHT` | `0.6` | Keyword boost factor (0.0–1.0). 0 disables keyword reranking; 1 applies the maximum boost. |
320
- | `RAG_GROUPING` | (not set) | `similar` keeps the first relevance group; `related` keeps up to two, using significant vector-distance gaps as boundaries. |
321
- | `RAG_MAX_DISTANCE` | (not set) | Filter out low-relevance results (e.g., `0.5`). |
322
- | `RAG_MAX_FILES` | (not set) | Limit results to top N files (e.g., `1` for single best file). |
323
-
324
- For API specifications and other documents containing many identifiers, a stronger keyword
325
- weight can improve exact-term ranking:
326
-
327
- ```json
328
- "env": {
329
- "RAG_HYBRID_WEIGHT": "0.7"
330
- }
331
- ```
332
-
333
- - `0.7`: slightly stronger exact-term reranking than the default
334
- - `1.0`: maximum keyword boost
335
-
336
- ## How It Works
337
-
338
- During ingestion:
339
-
340
- 1. The parser extracts text for the input format.
341
- 2. The semantic chunker finds topic boundaries and preserves Markdown code blocks.
342
- 3. Transformers.js creates embeddings locally.
343
- 4. LanceDB stores the chunks, metadata, vectors, and full-text index.
344
-
345
- During search:
346
-
347
- 1. The query is embedded with the same model.
348
- 2. Vector search retrieves semantically related chunks.
349
- 3. Optional distance and relevance-group filters narrow the candidates when configured.
350
- 4. Full-text matches boost exact query terms.
351
-
352
- ## Agent Skills
353
-
354
- [Agent Skills](https://agentskills.io/) provide query and ingestion guidance for AI assistants:
355
-
356
- ```bash
357
- npx mcp-local-rag skills install --claude-code
358
- npx mcp-local-rag skills install --claude-code --global
359
- npx mcp-local-rag skills install --codex
360
- ```
361
-
362
- Installed skills cover query formulation, result refinement, and HTML ingestion. Ask the
363
- assistant to use the mcp-local-rag skill explicitly if it does not activate automatically.
364
-
365
- ## Configuration
366
-
367
- The MCP server reads environment variables. The CLI accepts the listed global environment
368
- variables and flags; image storage on CLI ingestion and sync is enabled only with `--images`.
369
-
370
- | Environment Variable | CLI Flag | Default | Description |
371
- |---------------------|----------|---------|-------------|
372
- | `BASE_DIR` | `--base-dir` | Current directory | One document root; the CLI flag is repeatable on `ingest`, `list`, and `sync` |
373
- | `BASE_DIRS` | N/A | (unset) | JSON array of document roots; takes precedence over `BASE_DIR` |
374
- | `DB_PATH` | `--db-path` | `./lancedb/` | Vector database location |
375
- | `CACHE_DIR` | `--cache-dir` | `./models/` | Model cache directory |
376
- | `MODEL_NAME` | `--model-name` | `Xenova/all-MiniLM-L6-v2` | Hugging Face embedding model |
377
- | `MAX_FILE_SIZE` | `--max-file-size` | `104857600` (100MB) | Maximum file size in bytes |
378
- | `CHUNK_MIN_LENGTH` | `--chunk-min-length` | `50` | Minimum chunk length in characters (1–10000) |
379
- | `STORE_IMAGES` | N/A | `false` | MCP server only: store supported PDF/DOCX images and return them with matched chunks. CLI uses `--images`. |
380
- | `RAG_DEVICE` | N/A | `cpu` | ONNX Runtime execution device |
381
- | `RAG_DTYPE` | N/A | `fp32` | Embedding dtype passed to the selected model |
382
-
383
- ### Document Roots (`BASE_DIR` and `BASE_DIRS`)
384
-
385
- mcp-local-rag only allows file operations inside configured roots. For multiple roots,
386
- `BASE_DIRS` must be a JSON array of non-empty paths:
387
-
388
- ```bash
389
- export BASE_DIRS='["/Users/me/Documents/work","/Users/me/Projects/specs"]'
390
- ```
391
-
392
- Root configuration is resolved in this order:
393
-
394
- 1. CLI `--base-dir <path>` flags (repeatable on `ingest`, `list`, and `sync`)
395
- 2. `BASE_DIRS`
396
- 3. `BASE_DIR`
397
- 4. Current directory
398
-
399
- Each source replaces the lower-priority source rather than merging with it. Invalid `BASE_DIRS`
400
- configuration fails instead of falling back to `BASE_DIR` or the current directory. `status`
401
- remains available in MCP so the client can report the configuration error.
402
-
403
- ```bash
404
- npx mcp-local-rag ingest --base-dir /Users/me/work --base-dir /Users/me/specs /Users/me/work/readme.md
405
- npx mcp-local-rag list --base-dir /Users/me/work --base-dir /Users/me/specs
406
- npx mcp-local-rag sync --base-dir /Users/me/work --base-dir /Users/me/specs
407
- BASE_DIRS='["/Users/me/work","/Users/me/specs"]' npx mcp-local-rag list
408
- ```
409
-
410
- ### Storage and Models
411
-
412
- `DB_PATH` and `CACHE_DIR` are relative to the process working directory by default. Set absolute
413
- paths when the MCP client may start the server from different project directories.
414
-
415
- Set `MODEL_NAME` or pass `--model-name` to choose a Hugging Face embedding model that fits the
416
- language and domain of your documents.
417
-
418
- mcp-local-rag generates embeddings with mean pooling and L2 normalization. When choosing a
419
- model, check whether these settings match its recommended inference setup, since the pooling
420
- method can affect retrieval quality.
421
-
422
- Changing `MODEL_NAME`, `RAG_DEVICE`, or `RAG_DTYPE` can make existing vectors incompatible.
423
- Use a new `DB_PATH` or delete the existing index and re-ingest after changing the embedding
424
- configuration.
425
-
426
- An example model for English documents is `Xenova/bge-small-en-v1.5`.
427
-
428
- ## Security and Operation
429
-
430
- - File access is restricted to `BASE_DIR`, `BASE_DIRS`, or CLI `--base-dir` roots.
431
- - Symlinks that resolve outside every configured root are rejected.
432
- - Document processing and search make no network requests after the required models are cached.
433
- - The server is designed for one local user and does not provide authentication or access control.
434
- - Do not run multiple CLI or MCP writers against the same `DB_PATH`. Read-only queries can run
435
- while a sync is active.
436
- - Back up an index by copying its `DB_PATH` directory while no writer is active.
437
-
438
- <details>
439
- <summary><strong>Troubleshooting</strong></summary>
440
-
441
- ### "No results found"
442
-
443
- Documents must be ingested first. Run `"List all ingested files"` to verify.
444
-
445
- ### Model download failed
446
-
447
- Check internet connection. If behind a proxy, configure network settings. The model can also be [downloaded manually](https://huggingface.co/Xenova/all-MiniLM-L6-v2).
448
-
449
- ### "File too large"
450
-
451
- Default limit is 100MB. Split large files or increase `MAX_FILE_SIZE`.
452
-
453
- ### Slow queries
454
-
455
- Check chunk count with `status`. Large documents with many chunks may slow queries. Consider splitting very large files.
456
-
457
- ### "Path outside BASE_DIR"
458
-
459
- Ensure file paths are within one of the configured roots (`BASE_DIR`, any `BASE_DIRS` entry, or any CLI `--base-dir`). Use absolute paths.
460
-
461
- ### "BASE_DIRS must be a JSON array..."
462
-
463
- `BASE_DIRS` accepts a JSON array of one or more non-empty path strings:
464
-
465
- - Valid: `BASE_DIRS='["/Users/me/work","/Users/me/specs"]'`
466
- - Invalid: `BASE_DIRS=/a:/b` (delimiter syntax not supported)
467
- - Invalid: `BASE_DIRS='[]'` (empty array)
468
-
469
- ### MCP client doesn't see tools
470
-
471
- 1. Verify config file syntax
472
- 2. Restart client completely (Cmd+Q on Mac for Cursor)
473
- 3. Test directly: `npx mcp-local-rag` should run without errors
474
-
475
- </details>
476
-
477
- ## Contributing
478
-
479
- Contributions welcome! See [CONTRIBUTING.md](CONTRIBUTING.md) for setup and guidelines.
480
-
481
- ## License
482
-
483
- MIT License. Free for personal and commercial use.
484
-
485
- ## Blog Posts
486
-
487
- - [Building a Local RAG for Agentic Coding](https://www.norsica.jp/blog/local-rag-agentic-coding): Technical deep-dive into the semantic chunking and hybrid search design.
488
-
489
- ## Acknowledgments
490
-
491
- Built with [Model Context Protocol](https://modelcontextprotocol.io/) by Anthropic, [LanceDB](https://lancedb.com/), and [Transformers.js](https://huggingface.co/docs/transformers.js).