@lojban/semantic-search-mcp 1.0.11 → 1.0.12
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 +13 -53
- package/package.json +1 -1
- package/src/index.ts +86 -218
- package/src/scanner.ts +11 -21
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ Use it in **Cursor**, **Claude Code**, or any IDE that supports MCP to search th
|
|
|
12
12
|
|
|
13
13
|
## How it works
|
|
14
14
|
|
|
15
|
-
- **Indexing**:
|
|
15
|
+
- **Indexing**: On startup, if `SEMANTIC_SEARCH_INDEX_DIRS` is set (comma-separated paths), the server scans those directories in the background for `.txt`, `.md`, `.tsv`, `.csv`. Each non-empty line gets a vector embedding (via [Hugging Face Transformers.js](https://huggingface.co/docs/transformers.js), model `Xenova/all-MiniLM-L6-v2`) and is stored in a local SQLite database with [@dao-xyz/sqlite3-vec](https://www.npmjs.com/package/@dao-xyz/sqlite3-vec) (SQLite + sqlite-vec for Node and browser). Indexing runs asynchronously so the server stays responsive and uses bounded memory.
|
|
16
16
|
- **Search**: You send a natural-language query; the server embeds it and returns the closest lines by cosine similarity.
|
|
17
17
|
- **Storage**: Index is stored in your project's `.semantic-search/data/` (or set `SEMANTIC_SEARCH_DATA_DIR`). No cloud, no API keys.
|
|
18
18
|
|
|
@@ -44,22 +44,21 @@ The package is published as [**@lojban/semantic-search-mcp**](https://www.npmjs.
|
|
|
44
44
|
}
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
-
No `cwd` needed: the server stores its index in your **project directory** (`.semantic-search/data/`), so open your project in Cursor and the index is per-workspace. To use a fixed data directory instead, add `"env": { "SEMANTIC_SEARCH_DATA_DIR": "/path/to/data" }`.
|
|
47
|
+
No `cwd` needed: the server stores its index in your **project directory** (`.semantic-search/data/`), so open your project in Cursor and the index is per-workspace. To use a fixed data directory instead, add `"env": { "SEMANTIC_SEARCH_DATA_DIR": "/path/to/data" }`. To have the server index directories on startup, set `"env": { "SEMANTIC_SEARCH_INDEX_DIRS": "./dictionary,./glossary" }` (comma-separated paths).
|
|
48
48
|
|
|
49
|
-
2. **Restart Cursor** (or reload the window).
|
|
49
|
+
2. **Restart Cursor** (or reload the window). If `SEMANTIC_SEARCH_INDEX_DIRS` is set, indexing starts automatically in the background.
|
|
50
50
|
|
|
51
51
|
3. In chat or Composer, ask the AI to use the tools:
|
|
52
|
-
- **Index**: "Index the directory `./my-dictionary`" (or a list of paths). Optionally "clear existing index first."
|
|
53
52
|
- **Search**: "Search the index for …" or "Find entries similar to …"
|
|
54
|
-
- **Stats**: "How many lines/files are in the index?"
|
|
53
|
+
- **Stats**: "How many lines/files are in the index?" or "Is indexing still running?" — stats include progress and start time (locale-formatted) when indexing is in progress.
|
|
55
54
|
|
|
56
|
-
The AI will call `
|
|
55
|
+
The AI will call `search` and `get_index_stats` for you.
|
|
57
56
|
|
|
58
57
|
## Use in other AI IDEs (Claude Code, etc.)
|
|
59
58
|
|
|
60
59
|
Any environment that supports MCP over stdio can use this server. Run:
|
|
61
60
|
|
|
62
|
-
- **One-liner**: `npx -y @lojban/semantic-search-mcp` — dependencies are installed on first run; index is stored in the current working directory's `.semantic-search/data/`.
|
|
61
|
+
- **One-liner**: `npx -y @lojban/semantic-search-mcp` — dependencies are installed on first run; index is stored in the current working directory's `.semantic-search/data/`. Set env `SEMANTIC_SEARCH_INDEX_DIRS` (comma-separated paths) to index those directories on startup in the background. Tools: `search`, `get_index_stats`.
|
|
63
62
|
|
|
64
63
|
**From source**: Clone the repo, run `npm install` once, then use `"command": "npx", "args": ["tsx", "src/index.ts"], "cwd": "/path/to/semantic-search-mcp"` or `"command": "node", "args": ["/path/to/semantic-search-mcp/run.mjs"]` (no `cwd` needed with the latter). See [MCP_SETUP.md](MCP_SETUP.md) for details.
|
|
65
64
|
|
|
@@ -67,60 +66,21 @@ Any environment that supports MCP over stdio can use this server. Run:
|
|
|
67
66
|
|
|
68
67
|
| Tool | Description |
|
|
69
68
|
|------|-------------|
|
|
70
|
-
| `index_directories` | Scan one or more directories and index every line of supported text files. Pass `directories` (array of paths) or set env `SEMANTIC_SEARCH_INDEX_DIRS` (comma-separated). Optional: `clear_existing: true` to replace the index. |
|
|
71
69
|
| `search` | Semantic search: `query` (string), optional `limit` (default 10). Returns file path, line number, content, and similarity score. |
|
|
72
|
-
| `get_index_stats` | Returns total number of indexed files and lines. |
|
|
70
|
+
| `get_index_stats` | Returns total number of indexed files and lines. When indexing is running in the background, also returns progress: `indexing.started_at` (locale-formatted), `lines_indexed_so_far`, `files_indexed_so_far`, and `in_progress`. |
|
|
73
71
|
|
|
74
|
-
### Indexing
|
|
72
|
+
### Indexing on startup
|
|
75
73
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
**In Cursor (natural language):**
|
|
79
|
-
|
|
80
|
-
- "Index these directories: `./dictionary`, `./glossary`, and `./notes`."
|
|
81
|
-
- "Index `./data/lojban-eng` and `/home/me/other-corpus` with clear_existing true."
|
|
82
|
-
- "Clear the index and re-index only `./tsv` and `./exports`."
|
|
83
|
-
|
|
84
|
-
**Under the hood** the tool receives:
|
|
85
|
-
|
|
86
|
-
```json
|
|
87
|
-
{
|
|
88
|
-
"directories": ["./dictionary", "./glossary", "./notes"],
|
|
89
|
-
"clear_existing": false
|
|
90
|
-
}
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
To replace the entire index with new content from several places:
|
|
94
|
-
|
|
95
|
-
```json
|
|
96
|
-
{
|
|
97
|
-
"directories": ["/path/to/dict1", "/path/to/dict2", "/path/to/corpus"],
|
|
98
|
-
"clear_existing": true
|
|
99
|
-
}
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
Paths can be anywhere on disk (e.g. different drives or projects); the server reads and indexes all supported text/TSV/CSV files under each directory recursively.
|
|
103
|
-
|
|
104
|
-
### Memory and batch size
|
|
105
|
-
|
|
106
|
-
Indexing uses **adaptive batch size** based on free system RAM so the OS doesn’t freeze on low-memory machines. The server reads `os.freemem()`, keeps a reserve (default 400MB), and caps batch size between 32 and 512 lines. You can tune this with env vars:
|
|
107
|
-
|
|
108
|
-
- **`SEMANTIC_SEARCH_RESERVE_MB`** — MB of RAM to keep free (default `400`).
|
|
109
|
-
- **`SEMANTIC_SEARCH_MIN_BATCH`** — minimum lines per batch (default `32`).
|
|
110
|
-
- **`SEMANTIC_SEARCH_MAX_BATCH`** — maximum lines per batch (default `512`).
|
|
111
|
-
|
|
112
|
-
Example: `SEMANTIC_SEARCH_RESERVE_MB=800 SEMANTIC_SEARCH_MAX_BATCH=256` to leave more headroom and use smaller batches.
|
|
113
|
-
|
|
114
|
-
- **`SEMANTIC_SEARCH_GC`** — explicit GC after each batch is **on by default** when Node is run with `--expose-gc` (helps avoid OS freezes during long indexing). In MCP use e.g. `"args": ["--expose-gc", "-y", "@lojban/semantic-search-mcp"]`. Set to `0` or `false` to disable.
|
|
74
|
+
Set the environment variable **`SEMANTIC_SEARCH_INDEX_DIRS`** to a comma-separated list of directories to index. When the MCP server starts, it begins indexing those directories in the background (async). The index is cleared and rebuilt each time the server starts. Use absolute paths or paths relative to the server's working directory. The server reads and indexes all supported text/TSV/CSV files under each directory recursively. Indexing uses bounded memory and yields to the event loop so the OS stays responsive.
|
|
115
75
|
|
|
116
76
|
## Example: Lojban dictionary gaps
|
|
117
77
|
|
|
118
|
-
1. Put your dictionary TSV (e.g. `jbo-eng.tsv`) in a folder.
|
|
119
|
-
2.
|
|
120
|
-
3.
|
|
78
|
+
1. Put your dictionary TSV (e.g. `jbo-eng.tsv`) in a folder (e.g. `./dictionary`).
|
|
79
|
+
2. Set `SEMANTIC_SEARCH_INDEX_DIRS=./dictionary` in your MCP config (or in the environment). Restart the server; indexing runs in the background.
|
|
80
|
+
3. In Cursor: "Search for entries similar to 'to cause to become warm' and limit 20."
|
|
121
81
|
4. Or: "Search for 'emotional state of joy' and show me what we have; then suggest word combinations the dictionary might be missing."
|
|
122
82
|
|
|
123
|
-
The index is stored in `.semantic-search/data/vectors.db` (or your project root).
|
|
83
|
+
The index is stored in `.semantic-search/data/vectors.db` (or your project root). Restart the server to re-index when you add or change files.
|
|
124
84
|
|
|
125
85
|
## Development
|
|
126
86
|
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -5,10 +5,9 @@ import {
|
|
|
5
5
|
CallToolRequestSchema,
|
|
6
6
|
ListToolsRequestSchema,
|
|
7
7
|
} from '@modelcontextprotocol/sdk/types.js';
|
|
8
|
-
import os from 'node:os';
|
|
9
8
|
import path from 'path';
|
|
10
9
|
import { getEmbedding, getBatchEmbeddings } from './embeddings.js';
|
|
11
|
-
import { createVectorStorage, type SearchResult
|
|
10
|
+
import { createVectorStorage, type SearchResult } from './storage.js';
|
|
12
11
|
import { scanDirectories } from './scanner.js';
|
|
13
12
|
|
|
14
13
|
// Data dir: use env, or project cwd so each workspace has its own index when run via npx from Cursor
|
|
@@ -17,193 +16,80 @@ const dataDir =
|
|
|
17
16
|
path.join(process.cwd(), '.semantic-search', 'data');
|
|
18
17
|
const DB_PATH = path.join(dataDir, 'vectors.db');
|
|
19
18
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
directories: string[];
|
|
19
|
+
// Background indexing state (progress for get_index_stats)
|
|
20
|
+
const indexingState = {
|
|
21
|
+
inProgress: false,
|
|
22
|
+
startedAt: null as Date | null,
|
|
23
|
+
linesIndexed: 0,
|
|
24
|
+
filesIndexed: 0,
|
|
25
|
+
error: null as string | null,
|
|
28
26
|
};
|
|
29
27
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
startedAt: null,
|
|
33
|
-
finishedAt: null,
|
|
34
|
-
lastError: null,
|
|
35
|
-
indexedLines: 0,
|
|
36
|
-
indexedFiles: 0,
|
|
37
|
-
directories: [],
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
// Single "mutex": only one indexing job is allowed to run. Starting a new job aborts the previous one.
|
|
41
|
-
let currentIndexingAbortController: AbortController | null = null;
|
|
42
|
-
let currentJobId = 0;
|
|
43
|
-
|
|
44
|
-
// Adaptive batch size: reserve RAM so we don't freeze the OS (env overrides in bytes or MB)
|
|
45
|
-
const RESERVE_MB = Number(process.env.SEMANTIC_SEARCH_RESERVE_MB) || 400;
|
|
46
|
-
const RESERVE_BYTES = RESERVE_MB * 1024 * 1024;
|
|
47
|
-
const MIN_BATCH = Number(process.env.SEMANTIC_SEARCH_MIN_BATCH) || 32;
|
|
48
|
-
const MAX_BATCH = Number(process.env.SEMANTIC_SEARCH_MAX_BATCH) || 128;
|
|
49
|
-
// Explicit GC after each batch (when --expose-gc is available). Default on; set SEMANTIC_SEARCH_GC=0 or false to disable.
|
|
50
|
-
const ENABLE_GC = process.env.SEMANTIC_SEARCH_GC !== '0' && process.env.SEMANTIC_SEARCH_GC !== 'false';
|
|
28
|
+
// Batch size kept small to avoid high RAM usage during indexing
|
|
29
|
+
const INDEX_BATCH_SIZE = 256;
|
|
51
30
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Compute batch size from current free system RAM. Keeps reserve free to avoid freezing the OS.
|
|
57
|
-
*/
|
|
58
|
-
function getAdaptiveBatchSize(): number {
|
|
59
|
-
const free = os.freemem();
|
|
60
|
-
const available = free > RESERVE_BYTES ? free - RESERVE_BYTES : Math.floor(free / 2);
|
|
61
|
-
const batch = Math.floor(available / BYTES_PER_LINE_ESTIMATE);
|
|
62
|
-
const clamped = Math.max(MIN_BATCH, Math.min(MAX_BATCH, batch));
|
|
63
|
-
return clamped;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Request indexing of directories. If another indexing job is running, it is aborted first.
|
|
68
|
-
* Then a new job is started (clears index and rebuilds).
|
|
69
|
-
*/
|
|
70
|
-
function requestIndexing(storage: VectorStorage, directories: string[]): void {
|
|
71
|
-
if (!directories.length) {
|
|
72
|
-
console.error('No directories to index. Set SEMANTIC_SEARCH_INDEX_DIRS (comma-separated paths).');
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// Abort any in-progress indexing so it doesn't conflict or flush this job's work.
|
|
77
|
-
if (currentIndexingAbortController) {
|
|
78
|
-
currentIndexingAbortController.abort();
|
|
79
|
-
currentIndexingAbortController = null;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
currentJobId += 1;
|
|
83
|
-
const jobId = currentJobId;
|
|
84
|
-
currentIndexingAbortController = new AbortController();
|
|
85
|
-
const signal = currentIndexingAbortController.signal;
|
|
86
|
-
|
|
87
|
-
indexStatus.isIndexing = true;
|
|
88
|
-
indexStatus.startedAt = Date.now();
|
|
89
|
-
indexStatus.finishedAt = null;
|
|
90
|
-
indexStatus.lastError = null;
|
|
91
|
-
indexStatus.directories = directories;
|
|
92
|
-
indexStatus.indexedLines = 0;
|
|
93
|
-
indexStatus.indexedFiles = 0;
|
|
94
|
-
|
|
95
|
-
void startIndexing(storage, directories, signal, jobId);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
async function startIndexing(
|
|
99
|
-
storage: VectorStorage,
|
|
100
|
-
directories: string[],
|
|
101
|
-
signal: AbortSignal,
|
|
102
|
-
jobId: number
|
|
31
|
+
async function runBackgroundIndexing(
|
|
32
|
+
storage: Awaited<ReturnType<typeof createVectorStorage>>,
|
|
33
|
+
directories: string[]
|
|
103
34
|
): Promise<void> {
|
|
104
|
-
|
|
35
|
+
indexingState.inProgress = true;
|
|
36
|
+
indexingState.startedAt = new Date();
|
|
37
|
+
indexingState.linesIndexed = 0;
|
|
38
|
+
indexingState.filesIndexed = 0;
|
|
39
|
+
indexingState.error = null;
|
|
40
|
+
storage.clear();
|
|
105
41
|
|
|
106
42
|
try {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const processBatch = async (batchToProcess: any[]) => {
|
|
117
|
-
if (batchToProcess.length === 0) return;
|
|
118
|
-
const contents = batchToProcess.map((l) => l.content);
|
|
43
|
+
let currentBatch: Array<{ filePath: string; lineNumber: number; content: string }> = [];
|
|
44
|
+
let processingPromise: Promise<void> | null = null;
|
|
45
|
+
const seenFiles = new Set<string>();
|
|
46
|
+
|
|
47
|
+
const processBatch = async (
|
|
48
|
+
batch: Array<{ filePath: string; lineNumber: number; content: string }>
|
|
49
|
+
): Promise<void> => {
|
|
50
|
+
if (batch.length === 0) return;
|
|
51
|
+
const contents = batch.map((l) => l.content);
|
|
119
52
|
const embeddings = await getBatchEmbeddings(contents);
|
|
120
|
-
|
|
121
|
-
const batchData = batchToProcess.map((line, idx) => ({
|
|
53
|
+
const batchData = batch.map((line, idx) => ({
|
|
122
54
|
filePath: line.filePath,
|
|
123
55
|
lineNumber: line.lineNumber,
|
|
124
56
|
content: line.content,
|
|
125
57
|
embedding: embeddings[idx],
|
|
126
58
|
}));
|
|
127
|
-
|
|
128
59
|
await storage.upsertLinesBatch(batchData);
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
// Explicit GC when enabled (default) and Node run with --expose-gc
|
|
133
|
-
if (ENABLE_GC && typeof (globalThis as { gc?: () => void }).gc === 'function') {
|
|
134
|
-
(globalThis as { gc: () => void }).gc();
|
|
135
|
-
}
|
|
60
|
+
for (const l of batch) seenFiles.add(l.filePath);
|
|
61
|
+
indexingState.linesIndexed += batch.length;
|
|
62
|
+
indexingState.filesIndexed = seenFiles.size;
|
|
136
63
|
};
|
|
137
64
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
let batchSize = getAdaptiveBatchSize();
|
|
141
|
-
console.error(`Adaptive batch size: ${batchSize} (free RAM: ${Math.round(os.freemem() / 1024 / 1024)}MB, reserve: ${RESERVE_MB}MB)`);
|
|
65
|
+
const yieldToEventLoop = (): Promise<void> =>
|
|
66
|
+
new Promise((resolve) => setImmediate(resolve));
|
|
142
67
|
|
|
143
68
|
for await (const line of scanDirectories(directories)) {
|
|
144
|
-
if (signal.aborted) break;
|
|
145
|
-
|
|
146
69
|
currentBatch.push(line);
|
|
147
|
-
if (currentBatch.length >=
|
|
70
|
+
if (currentBatch.length >= INDEX_BATCH_SIZE) {
|
|
71
|
+
if (processingPromise) await processingPromise;
|
|
148
72
|
const batchToProcess = currentBatch;
|
|
149
73
|
currentBatch = [];
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
await processBatch(batchToProcess);
|
|
153
|
-
if (signal.aborted) break;
|
|
74
|
+
processingPromise = processBatch(batchToProcess);
|
|
75
|
+
await yieldToEventLoop();
|
|
154
76
|
}
|
|
155
77
|
}
|
|
156
78
|
|
|
157
|
-
if (
|
|
158
|
-
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (currentBatch.length > 0) {
|
|
163
|
-
await processBatch(currentBatch);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (!isCurrentJob()) return;
|
|
79
|
+
if (processingPromise) await processingPromise;
|
|
80
|
+
if (currentBatch.length > 0) await processBatch(currentBatch);
|
|
167
81
|
|
|
168
82
|
const stats = await storage.getStats();
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
indexStatus.finishedAt = Date.now();
|
|
172
|
-
|
|
173
|
-
console.error(
|
|
174
|
-
`Finished indexing ${stats.totalLines} lines from ${stats.totalFiles} files in background job.`
|
|
175
|
-
);
|
|
83
|
+
indexingState.linesIndexed = stats.totalLines;
|
|
84
|
+
indexingState.filesIndexed = stats.totalFiles;
|
|
176
85
|
} catch (err) {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
indexStatus.lastError = message;
|
|
180
|
-
indexStatus.finishedAt = Date.now();
|
|
181
|
-
}
|
|
182
|
-
console.error('Error during indexing job:', err);
|
|
86
|
+
indexingState.error = err instanceof Error ? err.message : String(err);
|
|
87
|
+
console.error('Background indexing error:', indexingState.error);
|
|
183
88
|
} finally {
|
|
184
|
-
|
|
185
|
-
indexStatus.isIndexing = false;
|
|
186
|
-
}
|
|
187
|
-
if (currentIndexingAbortController && currentJobId === jobId) {
|
|
188
|
-
currentIndexingAbortController = null;
|
|
189
|
-
}
|
|
89
|
+
indexingState.inProgress = false;
|
|
190
90
|
}
|
|
191
91
|
}
|
|
192
92
|
|
|
193
|
-
function ensureInitialIndexing(storage: VectorStorage): void {
|
|
194
|
-
const envDirs = process.env.SEMANTIC_SEARCH_INDEX_DIRS;
|
|
195
|
-
const directories = envDirs ? envDirs.split(',').map((d) => d.trim()).filter(Boolean) : [];
|
|
196
|
-
|
|
197
|
-
if (!directories.length) {
|
|
198
|
-
console.error(
|
|
199
|
-
'Semantic Search MCP: SEMANTIC_SEARCH_INDEX_DIRS is not set; automatic indexing on startup is disabled.'
|
|
200
|
-
);
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
requestIndexing(storage, directories);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
93
|
async function main() {
|
|
208
94
|
const storage = await createVectorStorage(DB_PATH);
|
|
209
95
|
|
|
@@ -222,19 +108,9 @@ async function main() {
|
|
|
222
108
|
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
223
109
|
return {
|
|
224
110
|
tools: [
|
|
225
|
-
{
|
|
226
|
-
name: 'index_directories',
|
|
227
|
-
description:
|
|
228
|
-
'Trigger background indexing of directories from SEMANTIC_SEARCH_INDEX_DIRS (comma-separated). Clears and rebuilds the index asynchronously.',
|
|
229
|
-
inputSchema: {
|
|
230
|
-
type: 'object',
|
|
231
|
-
properties: {},
|
|
232
|
-
},
|
|
233
|
-
},
|
|
234
111
|
{
|
|
235
112
|
name: 'search',
|
|
236
|
-
description:
|
|
237
|
-
'Search for lines semantically similar to the query. Returns the most relevant lines from indexed files.',
|
|
113
|
+
description: 'Search for lines semantically similar to the query. Returns the most relevant lines from indexed files.',
|
|
238
114
|
inputSchema: {
|
|
239
115
|
type: 'object',
|
|
240
116
|
properties: {
|
|
@@ -253,7 +129,7 @@ async function main() {
|
|
|
253
129
|
},
|
|
254
130
|
{
|
|
255
131
|
name: 'get_index_stats',
|
|
256
|
-
description: 'Get statistics
|
|
132
|
+
description: 'Get statistics about the current index (number of files and lines indexed). If indexing is running in the background, returns progress and start time (locale-formatted).',
|
|
257
133
|
inputSchema: {
|
|
258
134
|
type: 'object',
|
|
259
135
|
properties: {},
|
|
@@ -268,40 +144,6 @@ async function main() {
|
|
|
268
144
|
|
|
269
145
|
try {
|
|
270
146
|
switch (name) {
|
|
271
|
-
case 'index_directories': {
|
|
272
|
-
const envDirs = process.env.SEMANTIC_SEARCH_INDEX_DIRS;
|
|
273
|
-
const directories = envDirs ? envDirs.split(',').map((d) => d.trim()).filter(Boolean) : [];
|
|
274
|
-
if (!directories.length) {
|
|
275
|
-
throw new Error(
|
|
276
|
-
'No directories to index. Set SEMANTIC_SEARCH_INDEX_DIRS (comma-separated paths).'
|
|
277
|
-
);
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
// Abort any in-progress indexing and start a new job (clears and rebuilds).
|
|
281
|
-
requestIndexing(storage, directories);
|
|
282
|
-
|
|
283
|
-
const stats = await storage.getStats();
|
|
284
|
-
return {
|
|
285
|
-
content: [
|
|
286
|
-
{
|
|
287
|
-
type: 'text',
|
|
288
|
-
text: JSON.stringify({
|
|
289
|
-
success: true,
|
|
290
|
-
indexing: indexStatus.isIndexing,
|
|
291
|
-
indexed_lines: stats.totalLines,
|
|
292
|
-
indexed_files: stats.totalFiles,
|
|
293
|
-
started_at: indexStatus.startedAt,
|
|
294
|
-
finished_at: indexStatus.finishedAt,
|
|
295
|
-
last_error: indexStatus.lastError,
|
|
296
|
-
message: indexStatus.isIndexing
|
|
297
|
-
? `Indexing started in background. Currently ${stats.totalLines} lines from ${stats.totalFiles} files in index.`
|
|
298
|
-
: `Indexing completed. Indexed ${stats.totalLines} lines from ${stats.totalFiles} files.`,
|
|
299
|
-
}),
|
|
300
|
-
},
|
|
301
|
-
],
|
|
302
|
-
};
|
|
303
|
-
}
|
|
304
|
-
|
|
305
147
|
case 'search': {
|
|
306
148
|
const query = (args as { query: string; limit?: number }).query;
|
|
307
149
|
const limit = (args as { query: string; limit?: number }).limit ?? 10;
|
|
@@ -329,21 +171,41 @@ async function main() {
|
|
|
329
171
|
|
|
330
172
|
case 'get_index_stats': {
|
|
331
173
|
const stats = await storage.getStats();
|
|
174
|
+
const payload: {
|
|
175
|
+
total_files: number;
|
|
176
|
+
total_lines: number;
|
|
177
|
+
indexing?: {
|
|
178
|
+
in_progress: boolean;
|
|
179
|
+
started_at: string;
|
|
180
|
+
lines_indexed_so_far: number;
|
|
181
|
+
files_indexed_so_far: number;
|
|
182
|
+
error?: string;
|
|
183
|
+
};
|
|
184
|
+
} = {
|
|
185
|
+
total_files: stats.totalFiles,
|
|
186
|
+
total_lines: stats.totalLines,
|
|
187
|
+
};
|
|
188
|
+
if (indexingState.inProgress && indexingState.startedAt) {
|
|
189
|
+
payload.indexing = {
|
|
190
|
+
in_progress: true,
|
|
191
|
+
started_at: indexingState.startedAt.toLocaleString(),
|
|
192
|
+
lines_indexed_so_far: indexingState.linesIndexed,
|
|
193
|
+
files_indexed_so_far: indexingState.filesIndexed,
|
|
194
|
+
};
|
|
195
|
+
} else if (indexingState.error) {
|
|
196
|
+
payload.indexing = {
|
|
197
|
+
in_progress: false,
|
|
198
|
+
started_at: indexingState.startedAt?.toLocaleString() ?? '',
|
|
199
|
+
lines_indexed_so_far: indexingState.linesIndexed,
|
|
200
|
+
files_indexed_so_far: indexingState.filesIndexed,
|
|
201
|
+
error: indexingState.error,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
332
204
|
return {
|
|
333
205
|
content: [
|
|
334
206
|
{
|
|
335
207
|
type: 'text',
|
|
336
|
-
text: JSON.stringify(
|
|
337
|
-
total_files: stats.totalFiles,
|
|
338
|
-
total_lines: stats.totalLines,
|
|
339
|
-
is_indexing: indexStatus.isIndexing,
|
|
340
|
-
indexed_lines: indexStatus.indexedLines,
|
|
341
|
-
indexed_files: indexStatus.indexedFiles,
|
|
342
|
-
started_at: indexStatus.startedAt,
|
|
343
|
-
finished_at: indexStatus.finishedAt,
|
|
344
|
-
last_error: indexStatus.lastError,
|
|
345
|
-
directories: indexStatus.directories,
|
|
346
|
-
}),
|
|
208
|
+
text: JSON.stringify(payload),
|
|
347
209
|
},
|
|
348
210
|
],
|
|
349
211
|
};
|
|
@@ -365,8 +227,14 @@ async function main() {
|
|
|
365
227
|
await server.connect(transport);
|
|
366
228
|
console.error('Semantic Search MCP Server running on stdio');
|
|
367
229
|
|
|
368
|
-
|
|
369
|
-
|
|
230
|
+
const envDirs = process.env.SEMANTIC_SEARCH_INDEX_DIRS;
|
|
231
|
+
const directories = envDirs ? envDirs.split(',').map((d) => d.trim()).filter(Boolean) : [];
|
|
232
|
+
if (directories.length > 0) {
|
|
233
|
+
console.error(`Starting background indexing for ${directories.length} directories...`);
|
|
234
|
+
runBackgroundIndexing(storage, directories).catch((err) => {
|
|
235
|
+
console.error('Background indexing failed:', err);
|
|
236
|
+
});
|
|
237
|
+
}
|
|
370
238
|
}
|
|
371
239
|
|
|
372
240
|
main().catch(console.error);
|
package/src/scanner.ts
CHANGED
|
@@ -38,7 +38,6 @@ export async function* scanDirectory(dirPath: string): AsyncGenerator<FileLine>
|
|
|
38
38
|
for (const filePath of files) {
|
|
39
39
|
if (!isTextFile(filePath)) continue;
|
|
40
40
|
|
|
41
|
-
let fileStream: ReturnType<typeof createReadStream> | null = null;
|
|
42
41
|
try {
|
|
43
42
|
const stats = statSync(filePath);
|
|
44
43
|
if (stats.size > MAX_FILE_SIZE) {
|
|
@@ -46,35 +45,26 @@ export async function* scanDirectory(dirPath: string): AsyncGenerator<FileLine>
|
|
|
46
45
|
continue;
|
|
47
46
|
}
|
|
48
47
|
|
|
49
|
-
fileStream = createReadStream(filePath);
|
|
48
|
+
const fileStream = createReadStream(filePath);
|
|
50
49
|
const rl = readline.createInterface({
|
|
51
50
|
input: fileStream,
|
|
52
51
|
crlfDelay: Infinity,
|
|
53
52
|
});
|
|
54
53
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
};
|
|
66
|
-
}
|
|
54
|
+
let lineNumber = 0;
|
|
55
|
+
for await (const line of rl) {
|
|
56
|
+
lineNumber++;
|
|
57
|
+
const trimmed = line.trim();
|
|
58
|
+
if (trimmed.length >= MIN_LINE_LENGTH) {
|
|
59
|
+
yield {
|
|
60
|
+
filePath,
|
|
61
|
+
lineNumber,
|
|
62
|
+
content: trimmed,
|
|
63
|
+
};
|
|
67
64
|
}
|
|
68
|
-
} finally {
|
|
69
|
-
rl.close();
|
|
70
|
-
fileStream.destroy();
|
|
71
|
-
fileStream = null;
|
|
72
65
|
}
|
|
73
66
|
} catch (err) {
|
|
74
67
|
console.error(`Error reading file ${filePath}:`, err);
|
|
75
|
-
if (fileStream) {
|
|
76
|
-
fileStream.destroy();
|
|
77
|
-
}
|
|
78
68
|
}
|
|
79
69
|
}
|
|
80
70
|
}
|