nox-mem 3.7.0 → 3.8.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.
package/README.md CHANGED
@@ -249,7 +249,7 @@ nox-mem stats — chunk/entity/vector counts
249
249
  nox-mem kg-build — extract knowledge graph entities
250
250
  nox-mem reflect "question" — synthesis over memory + KG, with cited sources
251
251
  nox-mem-api — start HTTP API on $NOX_API_PORT (default 18802)
252
- nox-mem-mcp — start MCP server over stdio (21 tools, incl. nox_mem_answer, for agents)
252
+ nox-mem-mcp — start MCP server over stdio (22 tools, incl. nox_mem_answer and nox_mem_get, for agents)
253
253
  nox-mem doctor — health check (core + optional integrations)
254
254
  nox-mem --help — full command reference
255
255
  ```
@@ -0,0 +1,13 @@
1
+ /**
2
+ * On a stdio MCP server, stdout carries JSON-RPC and nothing else. Library code logs with
3
+ * console.log (the schema migrations print "[MIGRATE V5] ..." the first time a DB is opened), and
4
+ * one such line on stdout is a frame the client cannot parse. Measured 2026-11-07 on the 3.8.0
5
+ * tarball: a fresh install's first MCP session wrote that line before the first response.
6
+ *
7
+ * Imported FIRST by mcp-server.ts: ES modules evaluate their imports in order, so this runs before
8
+ * any module that could log. The protocol itself writes with process.stdout.write, untouched.
9
+ */
10
+ console.log = console.error;
11
+ console.info = console.error;
12
+ console.debug = console.error;
13
+ export {};
@@ -3,8 +3,9 @@
3
3
  * nox-mem MCP Server — exposes memory tools via Model Context Protocol (stdio)
4
4
  * Tools: search, stats, primer, ingest, answer, ... (see TOOLS below)
5
5
  */
6
- import { closeDb } from "./db.js";
7
- import { searchHybrid, formatResults } from "./search.js";
6
+ import "./lib/stdout-guard.js"; // must stay the first import: stdout is JSON-RPC only
7
+ import { getDb, closeDb } from "./db.js";
8
+ import { searchHybrid, formatResultsForAgent } from "./search.js";
8
9
  import { crossSearch, formatCrossResults, getCrossStats } from "./cross-search.js";
9
10
  import { getMetrics } from "./metrics.js";
10
11
  import { buildGraph, getGraphStats, formatEntityQuery, upsertDecision, getCurrentDecision, getDecisionHistory, listDecisions } from "./knowledge-graph.js";
@@ -35,6 +36,17 @@ const TOOLS = [
35
36
  required: ["query"],
36
37
  },
37
38
  },
39
+ {
40
+ name: "nox_mem_get",
41
+ description: "Return the full text of memory chunks by id (the 'chunk N' shown in nox_mem_search results). Use when a search hit was cut or you need the exact text.",
42
+ inputSchema: {
43
+ type: "object",
44
+ properties: {
45
+ id: { type: "number", description: "One chunk id" },
46
+ ids: { type: "array", items: { type: "number" }, description: "Several chunk ids (max 10)" },
47
+ },
48
+ },
49
+ },
38
50
  {
39
51
  name: "nox_mem_stats",
40
52
  description: "Get nox-mem database statistics: chunk counts by type, consolidation status, DB size",
@@ -209,6 +221,24 @@ const TOOLS = [
209
221
  inputSchema: noxMemAnswerTool.inputSchema,
210
222
  },
211
223
  ];
224
+ const MAX_GET_IDS = 10;
225
+ /** Read-only by design: no access counter, no telemetry, nothing a ranking signal could learn from. */
226
+ function getChunks(ids) {
227
+ const rows = getDb()
228
+ .prepare(`SELECT id, source_file, chunk_type, chunk_text, source_date FROM chunks
229
+ WHERE id IN (${ids.map(() => "?").join(",")})`)
230
+ .all(...ids);
231
+ const byId = new Map(rows.map((r) => [r.id, r]));
232
+ return ids
233
+ .map((id) => {
234
+ const r = byId.get(id);
235
+ if (!r)
236
+ return `chunk ${id}: not found`;
237
+ const date = r.source_date ? ` · ${r.source_date}` : "";
238
+ return `chunk ${r.id} · ${r.source_file} · ${r.chunk_type}${date}\n${r.chunk_text}`;
239
+ })
240
+ .join("\n\n---\n\n");
241
+ }
212
242
  function sendResponse(id, result) {
213
243
  const msg = JSON.stringify({ jsonrpc: "2.0", id, result });
214
244
  process.stdout.write(msg + "\n");
@@ -261,7 +291,16 @@ async function handleRequest(req) {
261
291
  changedSince: args.changed_since,
262
292
  });
263
293
  const results = await searchHybrid(query, limit, true, filter, { surface: "mcp", agent: process.env.NOX_AGENT_NAME });
264
- text = formatResults(results);
294
+ text = formatResultsForAgent(results);
295
+ break;
296
+ }
297
+ case "nox_mem_get": {
298
+ const raw = args.ids ?? (args.id != null ? [args.id] : []);
299
+ const ids = (Array.isArray(raw) ? raw : [raw]).map(Number);
300
+ if (ids.length === 0 || ids.length > MAX_GET_IDS || ids.some((n) => !Number.isInteger(n) || n <= 0)) {
301
+ throw new Error(`nox_mem_get takes "id" or "ids" (1 to ${MAX_GET_IDS} positive integers)`);
302
+ }
303
+ text = getChunks(ids);
265
304
  break;
266
305
  }
267
306
  case "nox_mem_stats":
package/dist/search.js CHANGED
@@ -676,6 +676,40 @@ export function formatResults(results) {
676
676
  })
677
677
  .join("\n\n");
678
678
  }
679
+ /**
680
+ * Formatter for agents (MCP). `formatResults` shows 200 characters per hit, which is a preview
681
+ * for a person at a terminal who can open the file. An agent usually cannot open it (the files
682
+ * live on the memory host, or its permissions forbid it), so it saw the opening of the right
683
+ * chunk and never the fact further down: measured 2026-11-06, 10 of 36 tasks answered when the
684
+ * agent was told to search, and 25 of 40 runs tried to read the file the preview came from.
685
+ *
686
+ * Each hit carries its chunk id and its whole text (chunks are capped at MAX_CHUNK_CHARS at
687
+ * ingest). A total budget keeps a 20-hit search bounded: a hit past the budget keeps at least
688
+ * MIN_PER_HIT characters and says how to get the rest (`nox_mem_get`), so a cut is never silent.
689
+ */
690
+ export const AGENT_OUTPUT_BUDGET_CHARS = 24_000;
691
+ const MIN_PER_HIT = 400;
692
+ export function formatResultsForAgent(results, budget = AGENT_OUTPUT_BUDGET_CHARS) {
693
+ if (results.length === 0)
694
+ return "No results found.";
695
+ let used = 0;
696
+ return results
697
+ .map((r, i) => {
698
+ const tag = r.match_type ? ` [${r.match_type}]` : "";
699
+ const idPart = r.id != null ? ` · chunk ${r.id}` : "";
700
+ const datePart = r.source_date ? ` · ${r.source_date}` : "";
701
+ const head = `#${i + 1} [${r.score}${tag}] ${r.source_file}${idPart}${datePart}`;
702
+ const room = Math.max(MIN_PER_HIT, budget - used);
703
+ let body = r.chunk_text;
704
+ if (body.length > room) {
705
+ const how = r.id != null ? `nox_mem_get with id ${r.id}` : "open the source file";
706
+ body = `${body.slice(0, room)}\n[cut at ${room} of ${r.chunk_text.length} characters: ${how} returns the rest]`;
707
+ }
708
+ used += body.length;
709
+ return `${head}\n${body}`;
710
+ })
711
+ .join("\n\n---\n\n");
712
+ }
679
713
  // ─── Test-only exports (named with _ prefix to signal "do not use externally") ─
680
714
  export const _internals = {
681
715
  recordAccess,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nox-mem",
3
- "version": "3.7.0",
3
+ "version": "3.8.0",
4
4
  "description": "Pain-weighted hybrid memory engine: FTS5 + semantic search + knowledge graph, self-hosted on any Linux VPS.",
5
5
  "type": "module",
6
6
  "license": "MIT",