ko-qmd 2.8.3-ko.2

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 (56) hide show
  1. package/CHANGELOG.md +1376 -0
  2. package/LICENSE +21 -0
  3. package/README.ko.md +86 -0
  4. package/README.md +1307 -0
  5. package/bin/qmd +192 -0
  6. package/dist/ast.d.ts +65 -0
  7. package/dist/ast.js +334 -0
  8. package/dist/bench/bench.d.ts +35 -0
  9. package/dist/bench/bench.js +327 -0
  10. package/dist/bench/score.d.ts +33 -0
  11. package/dist/bench/score.js +88 -0
  12. package/dist/bench/types.d.ts +80 -0
  13. package/dist/bench/types.js +8 -0
  14. package/dist/cli/build-info.json +4 -0
  15. package/dist/cli/embed-lock.d.ts +24 -0
  16. package/dist/cli/embed-lock.js +94 -0
  17. package/dist/cli/formatter.d.ts +120 -0
  18. package/dist/cli/formatter.js +355 -0
  19. package/dist/cli/mcp-pid.d.ts +25 -0
  20. package/dist/cli/mcp-pid.js +86 -0
  21. package/dist/cli/qmd.d.ts +43 -0
  22. package/dist/cli/qmd.js +4424 -0
  23. package/dist/cli/version.d.ts +42 -0
  24. package/dist/cli/version.js +80 -0
  25. package/dist/collections.d.ts +167 -0
  26. package/dist/collections.js +410 -0
  27. package/dist/db.d.ts +63 -0
  28. package/dist/db.js +130 -0
  29. package/dist/hangul.d.ts +54 -0
  30. package/dist/hangul.js +140 -0
  31. package/dist/index.d.ts +237 -0
  32. package/dist/index.js +244 -0
  33. package/dist/llm.d.ts +543 -0
  34. package/dist/llm.js +1747 -0
  35. package/dist/maintenance.d.ts +33 -0
  36. package/dist/maintenance.js +52 -0
  37. package/dist/mcp/origin-guard.d.ts +67 -0
  38. package/dist/mcp/origin-guard.js +137 -0
  39. package/dist/mcp/server.d.ts +116 -0
  40. package/dist/mcp/server.js +883 -0
  41. package/dist/paths.d.ts +1 -0
  42. package/dist/paths.js +4 -0
  43. package/dist/store.d.ts +1100 -0
  44. package/dist/store.js +4965 -0
  45. package/dist/trust.d.ts +152 -0
  46. package/dist/trust.js +249 -0
  47. package/package.json +114 -0
  48. package/scripts/build.mjs +72 -0
  49. package/scripts/check-package-grammars.mjs +29 -0
  50. package/scripts/package-smoke.mjs +65 -0
  51. package/scripts/test-all.mjs +38 -0
  52. package/skills/qmd/SKILL.md +316 -0
  53. package/skills/qmd/references/mcp-setup.md +119 -0
  54. package/skills/release/SKILL.md +141 -0
  55. package/skills/release/scripts/install-hooks.sh +38 -0
  56. package/skills/release/scripts/release-context.sh +129 -0
@@ -0,0 +1,883 @@
1
+ /**
2
+ * QMD MCP Server - Model Context Protocol server for QMD
3
+ *
4
+ * Exposes QMD search and document retrieval as MCP tools and resources.
5
+ * Documents are accessible via qmd:// URIs.
6
+ *
7
+ * Speaks MCP spec 2026-07-28 (stateless, no initialize handshake) and dual-speaks
8
+ * 2025-era clients via the official SDK entries (`serveStdio` / `createMcpHandler`).
9
+ */
10
+ import { createServer } from "node:http";
11
+ import { readFileSync } from "node:fs";
12
+ import { join, dirname } from "node:path";
13
+ import { fileURLToPath } from "url";
14
+ import { createMcpHandler, McpServer, ResourceTemplate } from "@modelcontextprotocol/server";
15
+ import { serveStdio } from "@modelcontextprotocol/server/stdio";
16
+ import { z } from "zod";
17
+ import { existsSync } from "fs";
18
+ import { createStore, extractSnippet, addLineNumbers, getDefaultDbPath, DEFAULT_MULTI_GET_MAX_BYTES, } from "../index.js";
19
+ import { getConfigPath } from "../collections.js";
20
+ import { enableProductionMode } from "../store.js";
21
+ import { checkRequestOrigin, resolveOriginGuard } from "./origin-guard.js";
22
+ // =============================================================================
23
+ // Helper functions
24
+ // =============================================================================
25
+ /**
26
+ * Encode a path for use in qmd:// URIs.
27
+ * Encodes special characters but preserves forward slashes for readability.
28
+ */
29
+ function encodeQmdPath(path) {
30
+ // Encode each path segment separately to preserve slashes
31
+ return path.split('/').map(segment => encodeURIComponent(segment)).join('/');
32
+ }
33
+ /**
34
+ * Format search results as human-readable text summary
35
+ */
36
+ function formatSearchSummary(results, query) {
37
+ if (results.length === 0) {
38
+ return `No results found for "${query}"`;
39
+ }
40
+ const lines = [`Found ${results.length} result${results.length === 1 ? '' : 's'} for "${query}":\n`];
41
+ for (const r of results) {
42
+ lines.push(`${r.docid} ${Math.round(r.score * 100)}% ${r.file} - ${r.title}`);
43
+ }
44
+ return lines.join('\n');
45
+ }
46
+ function getPackageVersion() {
47
+ try {
48
+ const pkgPath = join(dirname(fileURLToPath(import.meta.url)), "../../package.json");
49
+ const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
50
+ return pkg.version ?? "unknown";
51
+ }
52
+ catch {
53
+ return "unknown";
54
+ }
55
+ }
56
+ // =============================================================================
57
+ // MCP Server
58
+ // =============================================================================
59
+ /**
60
+ * Build dynamic server instructions from actual index state.
61
+ * Injected into the LLM's system prompt via MCP initialize (2025-era) and
62
+ * server/discover (2026-07-28) — gives the LLM immediate context about what's
63
+ * searchable without a tool call.
64
+ */
65
+ async function buildInstructions(store) {
66
+ const status = await store.getStatus();
67
+ const globalCtx = await store.getGlobalContext();
68
+ const lines = [];
69
+ // --- What is this? ---
70
+ lines.push(`QMD is your local search engine over ${status.totalDocuments} markdown documents.`);
71
+ if (globalCtx)
72
+ lines.push(`Context: ${globalCtx}`);
73
+ // --- What's searchable? ---
74
+ // Emit names only — the per-collection doc counts and descriptions can run to ~1.5 KB
75
+ // across a dozen collections, and the same info is available on demand via the `status` tool.
76
+ if (status.collections.length > 0) {
77
+ lines.push("");
78
+ const names = status.collections.map(c => c.name).join(", ");
79
+ lines.push(`Collections (scope with \`collections\` parameter): ${names}`);
80
+ lines.push("Call the `status` tool for collection descriptions, paths, and per-collection doc counts.");
81
+ }
82
+ // --- Capability gaps ---
83
+ if (!status.hasVectorIndex) {
84
+ lines.push("");
85
+ lines.push("Note: No vector embeddings yet. Run `qmd embed` to enable semantic search (vec/hyde).");
86
+ }
87
+ else if (status.needsEmbedding > 0) {
88
+ lines.push("");
89
+ lines.push(`Note: ${status.needsEmbedding} documents need embedding. Run \`qmd embed\` to update.`);
90
+ }
91
+ // --- Search tool ---
92
+ lines.push("");
93
+ lines.push("Search: Use `query` with sub-queries (lex/vec/hyde):");
94
+ lines.push(" - type:'lex' — BM25 keyword search (exact terms, fast)");
95
+ lines.push(" - type:'vec' — semantic vector search (meaning-based)");
96
+ lines.push(" - type:'hyde' — hypothetical document (write what the answer looks like)");
97
+ lines.push("");
98
+ lines.push(" Always provide `intent` on every search call to disambiguate and improve snippets.");
99
+ lines.push("");
100
+ lines.push("Examples:");
101
+ lines.push(" Quick keyword lookup: [{type:'lex', query:'error handling'}]");
102
+ lines.push(" Semantic search: [{type:'vec', query:'how to handle errors gracefully'}]");
103
+ lines.push(" Best results: [{type:'lex', query:'error'}, {type:'vec', query:'error handling best practices'}]");
104
+ lines.push(" With intent: searches=[{type:'lex', query:'performance'}], intent='web page load times'");
105
+ // --- Retrieval workflow ---
106
+ lines.push("");
107
+ lines.push("Retrieval:");
108
+ lines.push(" - `get` — single document by path or docid (#abc123). Supports a line-range suffix: `file.md:100` (from line 100) or `file.md:100:40` (40 lines from line 100).");
109
+ lines.push(" - `multi_get` — batch retrieve by glob (`journals/2025-05*.md`), comma-separated list, or docids (#abc123).");
110
+ // --- Non-obvious things that prevent mistakes ---
111
+ lines.push("");
112
+ lines.push("Tips:");
113
+ lines.push(" - File paths in results are relative to their collection.");
114
+ lines.push(" - Use `minScore: 0.5` to filter low-confidence results.");
115
+ lines.push(" - Results include a `context` field describing the content type.");
116
+ return lines.join("\n");
117
+ }
118
+ /**
119
+ * Create an MCP server with all QMD tools, resources, and prompts registered.
120
+ * Shared by both stdio and HTTP transports.
121
+ */
122
+ async function createMcpServer(store, inflight) {
123
+ // Wraps request handlers so a stdio EOF shutdown can wait for in-flight
124
+ // work to settle before disposing the store/llm underneath it.
125
+ const track = inflight?.track ?? ((fn) => fn);
126
+ const server = new McpServer({ name: "qmd", version: getPackageVersion() }, {
127
+ instructions: await buildInstructions(store),
128
+ // tools/list is static for the process lifetime; resources/read stays
129
+ // uncacheable because the index can change under us.
130
+ cacheHints: {
131
+ "tools/list": { ttlMs: 60_000, cacheScope: "private" },
132
+ "server/discover": { ttlMs: 60_000, cacheScope: "private" },
133
+ "resources/read": { ttlMs: 0, cacheScope: "private" },
134
+ },
135
+ });
136
+ // Pre-fetch default collection names for search tools
137
+ const defaultCollectionNames = await store.getDefaultCollectionNames();
138
+ // ---------------------------------------------------------------------------
139
+ // Resource: qmd://{path} - read-only access to documents by path
140
+ // Note: No list() - documents are discovered via search tools
141
+ // ---------------------------------------------------------------------------
142
+ server.registerResource("document", new ResourceTemplate("qmd://{+path}", { list: undefined }), {
143
+ title: "QMD Document",
144
+ description: "A markdown document from your QMD knowledge base. Use search tools to discover documents.",
145
+ mimeType: "text/markdown",
146
+ }, track(async (uri, { path }) => {
147
+ // Decode URL-encoded path (MCP clients send encoded URIs)
148
+ const pathStr = Array.isArray(path) ? path.join('/') : (path || '');
149
+ const decodedPath = decodeURIComponent(pathStr);
150
+ // Use SDK to find document — findDocument handles collection/path resolution
151
+ const result = await store.get(decodedPath, { includeBody: true });
152
+ if ("error" in result) {
153
+ const text = result.error === "excluded_by_ignore"
154
+ ? `Document excluded by ignore rule: ${decodedPath}\nCollection: ${result.collection}\nMatched path: ${result.path}\nIgnore rule: ${result.rule}`
155
+ : `Document not found: ${decodedPath}`;
156
+ return { contents: [{ uri: uri.href, text }] };
157
+ }
158
+ let text = addLineNumbers(result.body || ""); // Default to line numbers
159
+ if (result.context) {
160
+ text = `<!-- Context: ${result.context} -->\n\n` + text;
161
+ }
162
+ return {
163
+ contents: [{
164
+ uri: uri.href,
165
+ name: result.displayPath,
166
+ title: result.title || result.displayPath,
167
+ mimeType: "text/markdown",
168
+ text,
169
+ }],
170
+ };
171
+ }));
172
+ // ---------------------------------------------------------------------------
173
+ // Tool: query (Primary search tool)
174
+ // ---------------------------------------------------------------------------
175
+ const subSearchSchema = z.object({
176
+ type: z.enum(['lex', 'vec', 'hyde']).describe("lex = BM25 keywords (supports \"phrase\" and -negation); " +
177
+ "vec = semantic question; hyde = hypothetical answer passage"),
178
+ query: z.string().describe("The query text. For lex: use keywords, \"quoted phrases\", and -negation. " +
179
+ "For vec: natural language question. For hyde: 50-100 word answer passage."),
180
+ });
181
+ server.registerTool("query", {
182
+ title: "Query",
183
+ description: `Search the knowledge base using a query document — one or more typed sub-queries combined for best recall.
184
+
185
+ Each result includes a \`line\` field with the absolute 1-indexed line of the best match in the source markdown. To read more context around a hit, call \`get(file, fromLine = max(1, line - 20), maxLines = 80, lineNumbers = true)\`.
186
+
187
+ ## Query Types
188
+
189
+ **lex** — BM25 keyword search. Fast, exact, no LLM needed.
190
+ Full lex syntax:
191
+ - \`term\` — prefix match ("perf" matches "performance")
192
+ - \`"exact phrase"\` — phrase must appear verbatim
193
+ - \`-term\` or \`-"phrase"\` — exclude documents containing this
194
+
195
+ Good lex examples:
196
+ - \`"connection pool" timeout -redis\`
197
+ - \`"machine learning" -sports -athlete\`
198
+ - \`handleError async typescript\`
199
+
200
+ **vec** — Semantic vector search. Write a natural language question. Finds documents by meaning, not exact words.
201
+ - \`how does the rate limiter handle burst traffic?\`
202
+ - \`what is the tradeoff between consistency and availability?\`
203
+
204
+ **hyde** — Hypothetical document. Write 50-100 words that look like the answer. Often the most powerful for nuanced topics.
205
+ - \`The rate limiter uses a token bucket algorithm. When a client exceeds 100 req/min, subsequent requests return 429 until the window resets.\`
206
+
207
+ ## Strategy
208
+
209
+ Combine types for best results. First sub-query gets 2× weight — put your strongest signal first.
210
+
211
+ | Goal | Approach |
212
+ |------|----------|
213
+ | General search (recommended) | Pass \`query\` — auto-expanded into typed variants, fused, reranked |
214
+ | Know exact term/name | \`lex\` only |
215
+ | Concept search | \`vec\` only |
216
+ | Best recall | \`lex\` + \`vec\` |
217
+ | Complex/nuanced | \`lex\` + \`vec\` + \`hyde\` |
218
+ | Unknown vocabulary | Pass \`query\` with natural language so the server auto-expands it |
219
+
220
+ ## Examples
221
+
222
+ Simple lookup:
223
+ \`\`\`json
224
+ [{ "type": "lex", "query": "CAP theorem" }]
225
+ \`\`\`
226
+
227
+ Best recall on a technical topic:
228
+ \`\`\`json
229
+ [
230
+ { "type": "lex", "query": "\\"connection pool\\" timeout -redis" },
231
+ { "type": "vec", "query": "why do database connections time out under load" },
232
+ { "type": "hyde", "query": "Connection pool exhaustion occurs when all connections are in use and new requests must wait. This typically happens under high concurrency when queries run longer than expected." }
233
+ ]
234
+ \`\`\`
235
+
236
+ Intent-aware lex (C++ performance, not sports):
237
+ \`\`\`json
238
+ [
239
+ { "type": "lex", "query": "\\"C++ performance\\" optimization -sports -athlete" },
240
+ { "type": "vec", "query": "how to optimize C++ program performance" }
241
+ ]
242
+ \`\`\``,
243
+ annotations: { readOnlyHint: true, openWorldHint: false },
244
+ inputSchema: z.object({
245
+ query: z.string().optional().describe("Plain-text query, auto-expanded by the SDK into lex/vec/hyde variants, fused via " +
246
+ "RRF and reranked. Recommended default for most searches. Mutually exclusive with 'searches'."),
247
+ searches: z.array(subSearchSchema).max(10).optional().describe("Typed sub-queries to execute (lex/vec/hyde). First gets 2x weight. Use for precise " +
248
+ "control over retrieval strategy. Mutually exclusive with 'query'."),
249
+ limit: z.number().optional().default(10).describe("Max results (default: 10)"),
250
+ minScore: z.number().optional().default(0).describe("Min relevance 0-1 (default: 0)"),
251
+ candidateLimit: z.number().optional().describe("Maximum candidates to rerank (default: 40, lower = faster but may miss results)"),
252
+ collections: z.array(z.string()).optional().describe("Filter to collections (OR match)"),
253
+ intent: z.string().optional().describe("Background context to disambiguate the query. Example: query='performance', intent='web page load times and Core Web Vitals'. Does not search on its own."),
254
+ rerank: z.boolean().optional().default(true).describe("Rerank results using LLM (default: true). Set to false for faster results on CPU-only machines."),
255
+ }),
256
+ }, track(async ({ query, searches, limit, minScore, candidateLimit, collections, intent, rerank }) => {
257
+ // Require exactly one of `query` (plain text, auto-expanded) or `searches` (typed sub-queries).
258
+ if (!query && (!searches || searches.length === 0)) {
259
+ return {
260
+ content: [{ type: "text", text: "Error: provide either 'query' (plain text) or 'searches' (typed sub-queries)" }],
261
+ isError: true,
262
+ };
263
+ }
264
+ if (query && searches && searches.length > 0) {
265
+ return {
266
+ content: [{ type: "text", text: "Error: 'query' and 'searches' are mutually exclusive; provide only one" }],
267
+ isError: true,
268
+ };
269
+ }
270
+ // Use default collections if none specified
271
+ const effectiveCollections = collections ?? defaultCollectionNames;
272
+ // Plain `query` is auto-expanded by the SDK (expand → fuse → rerank);
273
+ // `searches` runs the caller's typed sub-queries directly.
274
+ const searchOptions = query
275
+ ? { query }
276
+ : { queries: (searches ?? []).map(s => ({ type: s.type, query: s.query })) };
277
+ const results = await store.search({
278
+ ...searchOptions,
279
+ collections: effectiveCollections.length > 0 ? effectiveCollections : undefined,
280
+ limit,
281
+ minScore,
282
+ candidateLimit,
283
+ rerank,
284
+ intent,
285
+ });
286
+ // Use the plain query, or the first lex/vec sub-query, for snippet extraction
287
+ const primaryQuery = query
288
+ || searches?.find(s => s.type === 'lex')?.query
289
+ || searches?.find(s => s.type === 'vec')?.query
290
+ || searches?.[0]?.query
291
+ || "";
292
+ const filtered = results.map(r => {
293
+ const { line, snippet } = extractSnippet(r.body, primaryQuery, 300, r.bestChunkPos, r.bestChunk.length, intent);
294
+ return {
295
+ docid: `#${r.docid}`,
296
+ file: r.displayPath,
297
+ title: r.title,
298
+ score: Math.round(r.score * 100) / 100,
299
+ context: r.context,
300
+ line,
301
+ snippet: addLineNumbers(snippet, line),
302
+ };
303
+ });
304
+ return {
305
+ content: [{ type: "text", text: formatSearchSummary(filtered, primaryQuery) }],
306
+ structuredContent: { results: filtered },
307
+ };
308
+ }));
309
+ // ---------------------------------------------------------------------------
310
+ // Tool: qmd_get (Retrieve document)
311
+ // ---------------------------------------------------------------------------
312
+ server.registerTool("get", {
313
+ title: "Get Document",
314
+ description: "Retrieve the full content of a document by its file path or docid. Use paths or docids (#abc123) from search results. Suggests similar files if not found.",
315
+ annotations: { readOnlyHint: true, openWorldHint: false },
316
+ inputSchema: z.object({
317
+ file: z.string().describe("File path or docid from search results. Supports a line-range suffix: 'pages/meeting.md:100' starts at line 100; 'pages/meeting.md:100:40' (or '#abc123:100:40') reads 40 lines from line 100."),
318
+ fromLine: z.number().optional().describe("Start from this line number (1-indexed)"),
319
+ maxLines: z.number().optional().describe("Maximum number of lines to return"),
320
+ lineNumbers: z.boolean().optional().default(true).describe("Add line numbers to output (format: 'N: content'). On by default; set false for raw content."),
321
+ }),
322
+ }, track(async ({ file, fromLine, maxLines, lineNumbers }) => {
323
+ // Support :line and :from:count suffixes in `file` (e.g. "foo.md:120" or
324
+ // "foo.md:120:40"). Explicit fromLine/maxLines args take precedence.
325
+ let parsedFromLine = fromLine;
326
+ let parsedMaxLines = maxLines;
327
+ let lookup = file;
328
+ const rangeMatch = lookup.match(/:(\d+):(\d+)$/);
329
+ if (rangeMatch) {
330
+ if (parsedFromLine === undefined)
331
+ parsedFromLine = parseInt(rangeMatch[1], 10);
332
+ if (parsedMaxLines === undefined)
333
+ parsedMaxLines = parseInt(rangeMatch[2], 10);
334
+ lookup = lookup.slice(0, -rangeMatch[0].length);
335
+ }
336
+ else {
337
+ const colonMatch = lookup.match(/:(\d+)$/);
338
+ if (colonMatch && colonMatch[1] && parsedFromLine === undefined) {
339
+ parsedFromLine = parseInt(colonMatch[1], 10);
340
+ lookup = lookup.slice(0, -colonMatch[0].length);
341
+ }
342
+ }
343
+ if (parsedFromLine !== undefined)
344
+ parsedFromLine = Math.max(1, parsedFromLine);
345
+ const result = await store.get(lookup, { includeBody: false });
346
+ if ("error" in result) {
347
+ let msg = result.error === "excluded_by_ignore"
348
+ ? `Document excluded by ignore rule: ${file}\nCollection: ${result.collection}\nMatched path: ${result.path}\nIgnore rule: ${result.rule}`
349
+ : `Document not found: ${file}`;
350
+ if (result.error === "not_found" && result.similarFiles.length > 0) {
351
+ msg += `\n\nDid you mean one of these?\n${result.similarFiles.map(s => ` - ${s}`).join('\n')}`;
352
+ }
353
+ return {
354
+ content: [{ type: "text", text: msg }],
355
+ isError: true,
356
+ };
357
+ }
358
+ const body = await store.getDocumentBody(result.filepath, { fromLine: parsedFromLine, maxLines: parsedMaxLines }) ?? "";
359
+ let text = body;
360
+ if (lineNumbers) {
361
+ const startLine = parsedFromLine || 1;
362
+ text = addLineNumbers(text, startLine);
363
+ }
364
+ if (result.context) {
365
+ text = `<!-- Context: ${result.context} -->\n\n` + text;
366
+ }
367
+ return {
368
+ content: [{
369
+ type: "resource",
370
+ resource: {
371
+ uri: `qmd://${encodeQmdPath(result.displayPath)}`,
372
+ name: result.displayPath,
373
+ title: result.title,
374
+ mimeType: "text/markdown",
375
+ text,
376
+ },
377
+ }],
378
+ };
379
+ }));
380
+ // ---------------------------------------------------------------------------
381
+ // Tool: qmd_multi_get (Retrieve multiple documents)
382
+ // ---------------------------------------------------------------------------
383
+ server.registerTool("multi_get", {
384
+ title: "Multi-Get Documents",
385
+ description: "Retrieve multiple documents by glob pattern (e.g., 'journals/2025-05*.md'), comma-separated list, or docids. Skips files larger than maxBytes.",
386
+ annotations: { readOnlyHint: true, openWorldHint: false },
387
+ inputSchema: z.object({
388
+ pattern: z.string().describe("Glob pattern, docid, or comma-separated list of file paths/docids"),
389
+ maxLines: z.number().optional().describe("Maximum lines per file"),
390
+ maxBytes: z.number().optional().default(DEFAULT_MULTI_GET_MAX_BYTES).describe("Skip files larger than this (default: 65536 = 64KB)"),
391
+ lineNumbers: z.boolean().optional().default(true).describe("Add line numbers to output (format: 'N: content'). On by default; set false for raw content."),
392
+ }),
393
+ }, track(async ({ pattern, maxLines, maxBytes, lineNumbers }) => {
394
+ const { docs, errors } = await store.multiGet(pattern, { includeBody: true, maxBytes: maxBytes || DEFAULT_MULTI_GET_MAX_BYTES });
395
+ if (docs.length === 0 && errors.length === 0) {
396
+ return {
397
+ content: [{ type: "text", text: `No files matched pattern: ${pattern}` }],
398
+ isError: true,
399
+ };
400
+ }
401
+ const content = [];
402
+ if (errors.length > 0) {
403
+ content.push({ type: "text", text: `Errors:\n${errors.join('\n')}` });
404
+ }
405
+ for (const result of docs) {
406
+ if (result.skipped) {
407
+ content.push({
408
+ type: "text",
409
+ text: `[SKIPPED: ${result.doc.displayPath} - ${result.skipReason}. Use 'qmd_get' with file="${result.doc.displayPath}" to retrieve.]`,
410
+ });
411
+ continue;
412
+ }
413
+ let text = result.doc.body || "";
414
+ if (maxLines !== undefined) {
415
+ const lines = text.split("\n");
416
+ text = lines.slice(0, maxLines).join("\n");
417
+ if (lines.length > maxLines) {
418
+ text += `\n\n[... truncated ${lines.length - maxLines} more lines]`;
419
+ }
420
+ }
421
+ if (lineNumbers) {
422
+ text = addLineNumbers(text);
423
+ }
424
+ if (result.doc.context) {
425
+ text = `<!-- Context: ${result.doc.context} -->\n\n` + text;
426
+ }
427
+ content.push({
428
+ type: "resource",
429
+ resource: {
430
+ uri: `qmd://${encodeQmdPath(result.doc.displayPath)}`,
431
+ name: result.doc.displayPath,
432
+ title: result.doc.title,
433
+ mimeType: "text/markdown",
434
+ text,
435
+ },
436
+ });
437
+ }
438
+ return { content };
439
+ }));
440
+ // ---------------------------------------------------------------------------
441
+ // Tool: qmd_status (Index status)
442
+ // ---------------------------------------------------------------------------
443
+ server.registerTool("status", {
444
+ title: "Index Status",
445
+ description: "Show the status of the QMD index: collections, document counts, and health information.",
446
+ annotations: { readOnlyHint: true, openWorldHint: false },
447
+ inputSchema: z.object({}),
448
+ }, track(async () => {
449
+ const status = await store.getStatus();
450
+ const summary = [
451
+ `QMD Index Status:`,
452
+ ` Total documents: ${status.totalDocuments}`,
453
+ ` Needs embedding: ${status.needsEmbedding}`,
454
+ ` Vector index: ${status.hasVectorIndex ? 'yes' : 'no'}`,
455
+ ` Collections: ${status.collections.length}`,
456
+ ];
457
+ for (const col of status.collections) {
458
+ summary.push(` - ${col.name}: ${col.path} (${col.documents} docs)`);
459
+ }
460
+ return {
461
+ content: [{ type: "text", text: summary.join('\n') }],
462
+ structuredContent: status,
463
+ };
464
+ }));
465
+ return server;
466
+ }
467
+ export function createInflightGate() {
468
+ // `active` is a running-handler counter, not a closed admission barrier.
469
+ // The barrier comes from the caller's ordering: registerStdioEofShutdown
470
+ // runs closeServer() (which stops the transport from dispatching new
471
+ // requests) BEFORE waitForIdle(), so by the time we wait, the only handlers
472
+ // that can still be running are ones already dispatched — there is no source
473
+ // of late admissions to guard against under the stdio transport.
474
+ let active = 0;
475
+ const waiters = [];
476
+ return {
477
+ track(fn) {
478
+ const wrapped = async (...args) => {
479
+ active += 1;
480
+ try {
481
+ return await fn(...args);
482
+ }
483
+ finally {
484
+ active -= 1;
485
+ if (active === 0) {
486
+ while (waiters.length > 0)
487
+ waiters.shift()();
488
+ }
489
+ }
490
+ };
491
+ return wrapped;
492
+ },
493
+ waitForIdle(timeoutMs) {
494
+ if (active === 0)
495
+ return Promise.resolve(true);
496
+ return new Promise((resolve) => {
497
+ const onIdle = () => {
498
+ clearTimeout(timer);
499
+ resolve(true);
500
+ };
501
+ const timer = setTimeout(() => {
502
+ const i = waiters.indexOf(onIdle);
503
+ if (i >= 0)
504
+ waiters.splice(i, 1);
505
+ resolve(false);
506
+ }, timeoutMs);
507
+ timer.unref?.();
508
+ waiters.push(onIdle);
509
+ });
510
+ },
511
+ };
512
+ }
513
+ /**
514
+ * Shut the stdio MCP server down when stdin reaches EOF (#751).
515
+ *
516
+ * The SDK's StdioServerTransport subscribes to stdin "data"/"error" only and
517
+ * never notices "end"/"close". When the parent MCP client dies, nothing tears
518
+ * the process down: the warm llama.cpp model's native handles keep the event
519
+ * loop alive, so the server reparents to PID 1, leaks RAM, and keeps the
520
+ * SQLite index open. stdin EOF means the client is gone, so this treats it as
521
+ * a disconnect: no new requests are accepted and nobody is left to read a
522
+ * response — but handlers that are already running get a bounded window to
523
+ * settle (waitForIdle) before their llm/store dependencies are torn down.
524
+ *
525
+ * Teardown order matters. Close the transport first so no further requests
526
+ * are dispatched, wait for in-flight handlers, then close the store last —
527
+ * which disposes the store's own llama.cpp instance and then the database, so
528
+ * the dispose path cannot hit an already-closed DB. (disposeLlm is an optional
529
+ * extra step for callers that own a separate instance; the MCP store does
530
+ * not.) Failures are logged best-effort (the parent's death may have closed
531
+ * stderr too) and do not stop the remaining steps. The function sets process.exitCode
532
+ * instead of calling process.exit() so `beforeExit` still fires and
533
+ * node-llama-cpp's auto-dispose runs before libc's static destructors —
534
+ * process.exit() during native-addon unload has caused exit-time crashes
535
+ * before (#59, #129; same rationale as finishSuccessfulCliCommand in the CLI).
536
+ *
537
+ * Returns the idempotent shutdown function: every invocation (manual, "end",
538
+ * "close", or already-ended stdin) shares one promise, and the promise never
539
+ * rejects.
540
+ */
541
+ export function registerStdioEofShutdown(options) {
542
+ const stdin = options.stdin ?? process.stdin;
543
+ const stderr = options.stderr ?? process.stderr;
544
+ const setExitCode = options.setExitCode ?? ((code) => { process.exitCode = code; });
545
+ const getExitCode = options.getExitCode ?? (() => (typeof process.exitCode === "number" ? process.exitCode : undefined));
546
+ let shutdownPromise = null;
547
+ // If the parent died, its stderr pipe may be gone: writes can throw
548
+ // synchronously or emit an async stream error. Logging must never take the
549
+ // teardown down with it.
550
+ stderr.on?.("error", () => { });
551
+ const safeWrite = (chunk) => {
552
+ try {
553
+ stderr.write(chunk);
554
+ }
555
+ catch {
556
+ // stderr went away with the parent
557
+ }
558
+ };
559
+ const performShutdown = async () => {
560
+ try {
561
+ stdin.off("end", onStdinEof);
562
+ stdin.off("close", onStdinEof);
563
+ }
564
+ catch {
565
+ // an exotic stdin may throw on off(); shutdown continues regardless
566
+ }
567
+ // Same stderr breadcrumb style as the HTTP transport's SIGTERM/SIGINT
568
+ // handlers; also gives tests an observable signal that the EOF path ran.
569
+ safeWrite("Shutting down (stdin closed)...\n");
570
+ let failed = false;
571
+ const step = async (name, run) => {
572
+ try {
573
+ await run();
574
+ }
575
+ catch (error) {
576
+ failed = true;
577
+ safeWrite(`QMD Warning: ${name} failed during stdio shutdown (${error instanceof Error ? error.message : String(error)}); continuing shutdown.\n`);
578
+ }
579
+ };
580
+ await step("server.close()", options.closeServer);
581
+ if (options.waitForIdle) {
582
+ await step("in-flight drain", async () => {
583
+ const idle = await options.waitForIdle(options.idleTimeoutMs ?? 5000);
584
+ if (!idle) {
585
+ safeWrite("QMD Warning: in-flight request did not settle before the shutdown deadline; continuing shutdown.\n");
586
+ }
587
+ });
588
+ }
589
+ if (options.disposeLlm) {
590
+ await step("llama disposal", options.disposeLlm);
591
+ }
592
+ await step("store.close()", options.closeStore);
593
+ try {
594
+ const prior = getExitCode();
595
+ if (failed) {
596
+ setExitCode(1);
597
+ }
598
+ else if (prior === undefined || prior === 0) {
599
+ setExitCode(0);
600
+ }
601
+ // else: keep an earlier nonzero status instead of masking it
602
+ }
603
+ catch {
604
+ // injected setExitCode/getExitCode must not break the shutdown promise
605
+ }
606
+ };
607
+ const shutdown = () => (shutdownPromise ??= performShutdown());
608
+ const onStdinEof = () => { void shutdown().catch(() => { }); };
609
+ stdin.once("end", onStdinEof);
610
+ stdin.once("close", onStdinEof);
611
+ // The parent can die between spawn and listener registration; check the
612
+ // stream flags after subscribing so an already-ended stdin still shuts down.
613
+ if (stdin.readableEnded || stdin.destroyed) {
614
+ onStdinEof();
615
+ }
616
+ return shutdown;
617
+ }
618
+ export async function startMcpServer(options = {}) {
619
+ // Opt into production mode when the MCP server is actually started, not
620
+ // when this module is merely imported for its exports. Importing the module
621
+ // at the top level flipped the global production flag and broke test
622
+ // isolation for downstream suites that expect the default (development)
623
+ // database path behaviour.
624
+ enableProductionMode();
625
+ const configPath = getConfigPath();
626
+ const store = await createStore({
627
+ dbPath: options.dbPath ?? getDefaultDbPath(),
628
+ ...(existsSync(configPath) ? { configPath } : {}),
629
+ });
630
+ const inflight = createInflightGate();
631
+ // serveStdio dual-speaks 2026-07-28 and 2025-era clients on one connection
632
+ // (opening exchange pins the era). A hand-wired StdioServerTransport would
633
+ // stay 2025-only even on SDK 2.x.
634
+ const handle = serveStdio(() => createMcpServer(store, inflight));
635
+ // Follow the parent's lifecycle: when stdin reaches EOF the client is gone
636
+ // and the server must exit instead of orphaning to PID 1 (#751). No
637
+ // disposeLlm here — store.close() disposes this store's own LlamaCpp
638
+ // instance, so passing the global disposeDefaultLlamaCpp would only risk
639
+ // tearing down an unrelated instance in an embedded process.
640
+ registerStdioEofShutdown({
641
+ closeServer: () => handle.close(),
642
+ waitForIdle: (timeoutMs) => inflight.waitForIdle(timeoutMs),
643
+ closeStore: () => store.close(),
644
+ });
645
+ }
646
+ /**
647
+ * Start MCP server over Streamable HTTP (JSON responses by default).
648
+ * Binds to `options.host` (default "localhost", overridable via the QMD_HOST
649
+ * env var) — set "0.0.0.0" to accept connections from other hosts, e.g. a
650
+ * container liveness probe. Returns a handle for shutdown and port discovery.
651
+ *
652
+ * HTTP is sessionless (MCP 2026-07-28): there is no `Mcp-Session-Id`, no
653
+ * initialize handshake, and no idle-session TTL. 2025-era clients are still
654
+ * served per-request via the SDK's stateless legacy fallback (initialize
655
+ * works as a standalone call; subsequent 2025 methods need a modern envelope
656
+ * or a stdio connection). The previous session reaper (#816) is gone because
657
+ * there are no sessions to reap.
658
+ */
659
+ export async function startMcpHttpServer(port, options = {}) {
660
+ // See startMcpServer() for the rationale — flip production mode here so the
661
+ // HTTP transport resolves the real database path, without leaking state into
662
+ // callers that only import this module for its exports (e.g. tests).
663
+ enableProductionMode();
664
+ const configPath = getConfigPath();
665
+ const store = await createStore({
666
+ dbPath: options.dbPath ?? getDefaultDbPath(),
667
+ ...(existsSync(configPath) ? { configPath } : {}),
668
+ });
669
+ // Pre-fetch default collection names for REST endpoint
670
+ const defaultCollectionNames = await store.getDefaultCollectionNames();
671
+ // Official 2026-07-28 HTTP entry: one factory, per-request instance, JSON
672
+ // responses (matches the previous enableJsonResponse: true). Dual-speaks
673
+ // 2025-era traffic statelessly by default (`legacy: "stateless"`).
674
+ const mcpHandler = createMcpHandler(() => createMcpServer(store), { responseMode: "json" });
675
+ const startTime = Date.now();
676
+ const quiet = options?.quiet ?? false;
677
+ /** Format timestamp for request logging */
678
+ function ts() {
679
+ return new Date().toISOString().slice(11, 23); // HH:mm:ss.SSS
680
+ }
681
+ /** Extract a human-readable label from a JSON-RPC body */
682
+ function describeRequest(body) {
683
+ const method = typeof body.method === "string" ? body.method : "unknown";
684
+ if (method === "tools/call") {
685
+ const tool = body.params?.name ?? "?";
686
+ const args = body.params?.arguments;
687
+ // Show query string if present, truncated
688
+ if (args?.query) {
689
+ const q = String(args.query).slice(0, 80);
690
+ return `tools/call ${tool} "${q}"`;
691
+ }
692
+ if (args?.file)
693
+ return `tools/call ${tool} ${args.file}`;
694
+ if (args?.path)
695
+ return `tools/call ${tool} ${args.path}`;
696
+ if (args?.pattern)
697
+ return `tools/call ${tool} ${args.pattern}`;
698
+ return `tools/call ${tool}`;
699
+ }
700
+ return method;
701
+ }
702
+ function log(msg) {
703
+ if (!quiet)
704
+ console.error(msg);
705
+ }
706
+ function nodeHeadersToWeb(nodeReq) {
707
+ const headers = new Headers();
708
+ for (const [k, v] of Object.entries(nodeReq.headers)) {
709
+ if (typeof v === "string")
710
+ headers.set(k, v);
711
+ else if (Array.isArray(v)) {
712
+ for (const item of v)
713
+ headers.append(k, item);
714
+ }
715
+ }
716
+ return headers;
717
+ }
718
+ // Helper to collect request body
719
+ async function collectBody(req) {
720
+ const chunks = [];
721
+ for await (const chunk of req)
722
+ chunks.push(chunk);
723
+ return Buffer.concat(chunks).toString();
724
+ }
725
+ const host = options.host ?? process.env.QMD_HOST ?? "localhost";
726
+ const originGuard = resolveOriginGuard({
727
+ host,
728
+ ...(options.allowedOrigins ? { allowedOrigins: options.allowedOrigins } : {}),
729
+ ...(options.allowedHosts ? { allowedHosts: options.allowedHosts } : {}),
730
+ });
731
+ const httpServer = createServer(async (nodeReq, nodeRes) => {
732
+ const reqStart = Date.now();
733
+ const pathname = (nodeReq.url || "/").split("?")[0];
734
+ try {
735
+ // DNS-rebinding screen, ahead of routing so REST /query /search are
736
+ // covered too — they bypass the MCP transport entirely (#881).
737
+ const origin = nodeReq.headers.origin;
738
+ const hostHeader = nodeReq.headers.host;
739
+ const verdict = checkRequestOrigin({
740
+ origin: typeof origin === "string" ? origin : undefined,
741
+ host: typeof hostHeader === "string" ? hostHeader : undefined,
742
+ }, originGuard);
743
+ if (!verdict.ok) {
744
+ nodeRes.writeHead(403, { "Content-Type": "application/json" });
745
+ nodeRes.end(JSON.stringify({
746
+ jsonrpc: "2.0",
747
+ error: { code: -32003, message: `Forbidden: ${verdict.reason}` },
748
+ id: null,
749
+ }));
750
+ log(`${ts()} ${nodeReq.method} ${pathname} 403 — ${verdict.reason}`);
751
+ return;
752
+ }
753
+ if (pathname === "/health" && nodeReq.method === "GET") {
754
+ const body = JSON.stringify({ status: "ok", uptime: Math.floor((Date.now() - startTime) / 1000) });
755
+ nodeRes.writeHead(200, { "Content-Type": "application/json" });
756
+ nodeRes.end(body);
757
+ log(`${ts()} GET /health (${Date.now() - reqStart}ms)`);
758
+ return;
759
+ }
760
+ // REST endpoint: POST /search — structured search without MCP protocol
761
+ // REST endpoint: POST /query (alias: /search) — structured search without MCP protocol
762
+ if ((pathname === "/query" || pathname === "/search") && nodeReq.method === "POST") {
763
+ const rawBody = await collectBody(nodeReq);
764
+ const params = JSON.parse(rawBody);
765
+ // Validate required fields
766
+ if (!params.searches || !Array.isArray(params.searches)) {
767
+ nodeRes.writeHead(400, { "Content-Type": "application/json" });
768
+ nodeRes.end(JSON.stringify({ error: "Missing required field: searches (array)" }));
769
+ return;
770
+ }
771
+ // Map to internal format
772
+ const searches = params.searches;
773
+ const queries = searches.map((s) => ({
774
+ type: s.type,
775
+ query: String(s.query || ""),
776
+ }));
777
+ // Use default collections if none specified
778
+ const effectiveCollections = Array.isArray(params.collections) ? params.collections.map(String) : defaultCollectionNames;
779
+ const results = await store.search({
780
+ queries,
781
+ collections: effectiveCollections.length > 0 ? effectiveCollections : undefined,
782
+ limit: typeof params.limit === "number" ? params.limit : 10,
783
+ minScore: typeof params.minScore === "number" ? params.minScore : 0,
784
+ candidateLimit: typeof params.candidateLimit === "number" ? params.candidateLimit : undefined,
785
+ intent: typeof params.intent === "string" ? params.intent : undefined,
786
+ rerank: typeof params.rerank === "boolean" ? params.rerank : undefined,
787
+ });
788
+ // Use first lex or vec query for snippet extraction
789
+ const primaryQuery = searches.find((s) => s.type === 'lex')?.query
790
+ || searches.find((s) => s.type === 'vec')?.query
791
+ || searches[0]?.query || "";
792
+ const formatted = results.map(r => {
793
+ const { line, snippet } = extractSnippet(r.body, String(primaryQuery), 300, r.bestChunkPos, r.bestChunk.length, typeof params.intent === "string" ? params.intent : undefined);
794
+ return {
795
+ docid: `#${r.docid}`,
796
+ file: `qmd://${encodeQmdPath(r.displayPath)}`,
797
+ title: r.title,
798
+ score: Math.round(r.score * 100) / 100,
799
+ context: r.context,
800
+ line,
801
+ snippet: addLineNumbers(snippet, line),
802
+ };
803
+ });
804
+ nodeRes.writeHead(200, { "Content-Type": "application/json" });
805
+ nodeRes.end(JSON.stringify({ results: formatted }));
806
+ log(`${ts()} POST /query ${params.searches.length} queries (${Date.now() - reqStart}ms)`);
807
+ return;
808
+ }
809
+ if (pathname === "/mcp") {
810
+ const rawBody = nodeReq.method !== "GET" && nodeReq.method !== "HEAD"
811
+ ? await collectBody(nodeReq)
812
+ : undefined;
813
+ let parsedBody;
814
+ if (rawBody) {
815
+ try {
816
+ parsedBody = JSON.parse(rawBody);
817
+ }
818
+ catch {
819
+ parsedBody = undefined;
820
+ }
821
+ }
822
+ const label = parsedBody && typeof parsedBody === "object" && parsedBody !== null
823
+ ? describeRequest(parsedBody)
824
+ : (nodeReq.method || "GET");
825
+ const hostHeader = typeof nodeReq.headers.host === "string" ? nodeReq.headers.host : `localhost:${port}`;
826
+ const url = `http://${hostHeader}${pathname}`;
827
+ const request = new Request(url, {
828
+ method: nodeReq.method || "GET",
829
+ headers: nodeHeadersToWeb(nodeReq),
830
+ ...(rawBody !== undefined ? { body: rawBody } : {}),
831
+ });
832
+ const response = await mcpHandler.fetch(request, parsedBody !== undefined ? { parsedBody } : undefined);
833
+ nodeRes.writeHead(response.status, Object.fromEntries(response.headers));
834
+ nodeRes.end(Buffer.from(await response.arrayBuffer()));
835
+ log(`${ts()} ${nodeReq.method} /mcp ${label} (${Date.now() - reqStart}ms)`);
836
+ return;
837
+ }
838
+ nodeRes.writeHead(404);
839
+ nodeRes.end("Not Found");
840
+ }
841
+ catch (err) {
842
+ console.error("HTTP handler error:", err);
843
+ nodeRes.writeHead(500);
844
+ nodeRes.end("Internal Server Error");
845
+ }
846
+ });
847
+ await new Promise((resolve, reject) => {
848
+ httpServer.on("error", reject);
849
+ httpServer.listen(port, host, () => resolve());
850
+ });
851
+ const actualPort = httpServer.address().port;
852
+ let stopping = false;
853
+ const stop = async () => {
854
+ if (stopping)
855
+ return;
856
+ stopping = true;
857
+ await mcpHandler.close();
858
+ httpServer.close();
859
+ await store.close();
860
+ };
861
+ process.on("SIGTERM", async () => {
862
+ console.error("Shutting down (SIGTERM)...");
863
+ await stop();
864
+ process.exit(0);
865
+ });
866
+ process.on("SIGINT", async () => {
867
+ console.error("Shutting down (SIGINT)...");
868
+ await stop();
869
+ process.exit(0);
870
+ });
871
+ log(`QMD MCP server listening on http://${host}:${actualPort}/mcp`);
872
+ if (originGuard.disabled) {
873
+ log("Warning: QMD_ALLOWED_ORIGINS=* — DNS-rebinding protection is off. Only do this behind your own authenticating proxy.");
874
+ }
875
+ else if (!originGuard.enforceHost) {
876
+ log(`Warning: bound to ${host} with no QMD_ALLOWED_HOSTS — Host validation is off and the index is readable by anyone who can reach this port.`);
877
+ }
878
+ return { httpServer, port: actualPort, stop };
879
+ }
880
+ // Run if this is the main module
881
+ if (fileURLToPath(import.meta.url) === process.argv[1] || process.argv[1]?.endsWith("/server.ts") || process.argv[1]?.endsWith("/server.js")) {
882
+ startMcpServer().catch(console.error);
883
+ }