nebula-memory-mcp 1.2.6 → 1.3.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
@@ -46,10 +46,12 @@
46
46
  |---|---|---|
47
47
  | `remember` | 写入记忆 | `text`(必填)、`kind`、`importance`(1.0=永久置顶)、`tags` |
48
48
  | `recall` | 召回相关记忆 | `query`(必填)、`limit` |
49
- | `stats` | 记忆库统计 | 无 |
49
+ | `stats` | 记忆库统计(回报当前落在哪个库、怎么解析来的) | 无 |
50
50
  | `forget` | 按 id 删除 | `id`(必填) |
51
+ | `compact` | 压缩记忆库文件(清理追加历史) | 无 |
52
+ | `list_namespaces` | **列出默认库 + 各 namespace 的节点/边数**,确认隔离是否生效 | 无 |
51
53
 
52
- 四个工具都接受一个**可选** `namespace` 参数(见下节)。
54
+ 前五个工具都接受一个**可选** `namespace` 参数(见下节);`list_namespaces` 用来总览。
53
55
 
54
56
  ## 多库(namespace)
55
57
 
@@ -62,15 +64,54 @@
62
64
  - **不填** `namespace` → 用默认单库(路径即 `NEBULA_DATA_DIR`,老用户无需改任何配置);
63
65
  - **填了** → 用独立子库 `NEBULA_DATA_DIR/__ns__/<namespace>/`,各库完全隔离,召回也只在指定库内;
64
66
  - `namespace` 只允许字母、数字、`_`、`-`,长度 ≤ 64(防目录穿越,非法值会报错);
65
- - `recall` / `stats` / `forget` 同样支持 `namespace`,传了就只操作对应库。
67
+ - `recall` / `stats` / `forget` / `compact` 同样支持 `namespace`,传了就只操作对应库。
66
68
 
67
69
  典型用法:给不同项目或不同 Agent 各起一个 namespace,一句话切换记忆库,无需启动多个 MCP 进程。
68
70
 
71
+ ### 不想每次都传 namespace?两个环境变量(1.3.0 起)
72
+
73
+ | 环境变量 | 效果 |
74
+ |---|---|
75
+ | `NEBULA_DEFAULT_NAMESPACE=work` | 调用**不传** `namespace` 时,固定落到 `work` 这个子库 |
76
+ | `NEBULA_AUTO_NAMESPACE=1` | 调用**不传** `namespace` 时,按**当前工作目录**派生:`cwd-<sha1(cwd) 前 8 位>` |
77
+
78
+ ```json
79
+ {
80
+ "mcpServers": {
81
+ "agent-memory": {
82
+ "command": "npx",
83
+ "args": ["-y", "nebula-memory-mcp@latest"],
84
+ "env": {
85
+ "NEBULA_DATA_DIR": "/path/to/memory",
86
+ "NEBULA_AUTO_NAMESPACE": "1"
87
+ }
88
+ }
89
+ }
90
+ }
91
+ ```
92
+
93
+ > `NEBULA_AUTO_NAMESPACE` 是**按项目自动分库**(Cursor 写 Java 项目、Cline 写 Python 项目会自动落在不同库)。
94
+ > 它**默认关闭**:不传 namespace 就走默认单库,这是向后兼容的硬承诺 —— 自动分库会让老用户「记忆突然不见了」。
95
+
96
+ **优先级**:显式 `namespace` 参数 > `NEBULA_DEFAULT_NAMESPACE` > `NEBULA_AUTO_NAMESPACE`(cwd 派生)> 默认单库。
97
+
98
+ **验证隔离是否生效**:调 `list_namespaces`,会返回默认库与每个子库的节点/边数,以及当前模式(`default` / `cwd-auto` / `env:<name>`):
99
+
100
+ ```json
101
+ {
102
+ "default": { "dir": "...", "nodes": 1234, "edges": 2100 },
103
+ "namespaces": [{ "name": "work", "nodes": 42, "edges": 17 }],
104
+ "mode": "cwd-auto"
105
+ }
106
+ ```
107
+
69
108
  ## 环境变量
70
109
 
71
110
  | 变量 | 说明 | 默认 |
72
111
  |---|---|---|
73
- | `NEBULA_DATA_DIR` | 记忆数据目录 | `~/.agent-memory` |
112
+
113
+ | `NEBULA_AUTO_NAMESPACE` | `1` 时按 cwd 自动派生 namespace(仅在不传参数且未设默认库时生效) | 空(关闭) |
114
+ | `NEBULA_DEFAULT_NAMESPACE` | 不传 namespace 时固定使用的子库名 | 空(走默认单库) || `NEBULA_DATA_DIR` | 记忆数据目录 | `~/.agent-memory` |
74
115
 
75
116
  想让多个 AI 客户端共享记忆?把它们的 `NEBULA_DATA_DIR` 填成**同一个目录**即可。
76
117
 
@@ -112,7 +153,7 @@ agent-memory(引擎真源,9 个模块) ──人工对齐──▶ 本
112
153
 
113
154
  | 上游 `agent-memory` | **本仓库** | `nebula-workbuddy-mcp` | 备注 |
114
155
  |---|---|---|---|
115
- | **2.2.1**(薄索引一致性修复) | **1.2.5** | 1.0.8(待同步) | 引擎与上游 2.2.1 对齐 |
156
+ | **2.3.0**(+ 外部写入监听开关) | **1.3.0** | 1.0.9(待同步) | 引擎与上游 2.3.0 对齐 |
116
157
 
117
158
  ## License
118
159
 
package/mcp-server.mjs CHANGED
@@ -25,6 +25,8 @@
25
25
  */
26
26
 
27
27
  import { createRequire } from 'node:module'
28
+ import { createHash } from 'node:crypto'
29
+ import fs from 'node:fs'
28
30
  import path from 'node:path'
29
31
  import os from 'node:os'
30
32
  import { fileURLToPath } from 'node:url'
@@ -43,7 +45,7 @@ const DATA_DIR = path.resolve(
43
45
  const SERVER_INFO = {
44
46
  name: 'agent-memory',
45
47
  // 与 package.json 的 version 保持一致(发版时必须一起改,见 README「引擎来历与同步」)
46
- version: '1.2.6',
48
+ version: '1.3.0',
47
49
  }
48
50
 
49
51
  // MCP 协议版本(当前规范)
@@ -68,6 +70,86 @@ function normalizeNamespace(ns) {
68
70
  return s
69
71
  }
70
72
 
73
+ // 命名空间解析优先级:显式参数 > NEBULA_DEFAULT_NAMESPACE > cwd 派生(opt-in)> 默认单库。
74
+ // 为什么 cwd 派生默认关:不传 namespace 走默认单库是**向后兼容**的硬承诺,
75
+ // 自动分库会让老用户「记忆突然不见了」。要按项目隔离就显式打开。
76
+ const AUTO_NS = /^(1|true|yes)$/i.test(String(process.env.NEBULA_AUTO_NAMESPACE || ''))
77
+ const DEFAULT_NS_ENV = String(process.env.NEBULA_DEFAULT_NAMESPACE || '').trim()
78
+
79
+ /** 按当前工作目录派生命名空间:cwd-<sha1(cwd) 前 8 位> */
80
+ function deriveCwdNamespace() {
81
+ return 'cwd-' + createHash('sha1').update(process.cwd()).digest('hex').slice(0, 8)
82
+ }
83
+
84
+ /**
85
+ * @returns {{ns: string|null, from: 'arg'|'env'|'cwd'|'default'}}
86
+ */
87
+ function resolveNamespace(explicit) {
88
+ const byArg = normalizeNamespace(explicit)
89
+ if (byArg) return { ns: byArg, from: 'arg' }
90
+ if (DEFAULT_NS_ENV) return { ns: normalizeNamespace(DEFAULT_NS_ENV), from: 'env' }
91
+ if (AUTO_NS) return { ns: deriveCwdNamespace(), from: 'cwd' }
92
+ return { ns: null, from: 'default' }
93
+ }
94
+
95
+ /** 数文件行数(只扫字节,不解析) */
96
+ function countLines(file) {
97
+ try {
98
+ const b = fs.readFileSync(file)
99
+ let n = 0
100
+ let i = -1
101
+ while ((i = b.indexOf(10, i + 1)) !== -1) n++
102
+ return n
103
+ } catch {
104
+ return 0
105
+ }
106
+ }
107
+
108
+ /** 文件字节数(不存在=0) */
109
+ function fileSize(file) {
110
+ try {
111
+ return fs.statSync(file).size
112
+ } catch {
113
+ return 0
114
+ }
115
+ }
116
+
117
+ /** 一个库的规模:薄索引位点对得上就用它(快),否则数行(可能正在被写) */
118
+ function libSize(dir) {
119
+ const nodesFile = path.join(dir, 'nodes.jsonl')
120
+ const edgeFile = path.join(dir, 'edges.jsonl')
121
+ try {
122
+ const p = JSON.parse(fs.readFileSync(path.join(dir, 'index.thin.json'), 'utf8'))
123
+ // 位点校验:薄索引可能是「刚 init 的空库」写的,之后又追加过 → 不能盲信
124
+ if (p.nodeBytes === fileSize(nodesFile) && p.edgeBytes === fileSize(edgeFile)) {
125
+ return { nodes: (p.nodes || []).length, edges: (p.edges || []).length }
126
+ }
127
+ } catch {
128
+ /* 没有薄索引:走数行 */
129
+ }
130
+ return { nodes: countLines(nodesFile), edges: countLines(edgeFile) }
131
+ }
132
+
133
+ /** 默认库 + 所有 namespace 的清单 */
134
+ function listNamespaces(defaultMem) {
135
+ const out = {
136
+ default: { dir: DATA_DIR, nodes: defaultMem ? defaultMem.nodes.size : 0, edges: defaultMem ? defaultMem.edgeMap.size : 0 },
137
+ namespaces: [],
138
+ mode: AUTO_NS ? 'cwd-auto' : DEFAULT_NS_ENV ? 'env:' + DEFAULT_NS_ENV : 'default',
139
+ }
140
+ try {
141
+ for (const ent of fs.readdirSync(NS_ROOT, { withFileTypes: true })) {
142
+ if (!ent.isDirectory()) continue
143
+ const dir = path.join(NS_ROOT, ent.name)
144
+ out.namespaces.push({ name: ent.name, dir, ...libSize(dir) })
145
+ }
146
+ } catch {
147
+ /* 还没有 __ns__ 目录:正常 */
148
+ }
149
+ out.namespaces.sort((a, b) => a.name.localeCompare(b.name))
150
+ return out
151
+ }
152
+
71
153
  async function getMemory(ns) {
72
154
  if (ns == null) return null // 调用方改用默认单库实例
73
155
  if (!memCache.has(ns)) {
@@ -164,14 +246,22 @@ const TOOLS = [
164
246
  properties: { namespace: { type: 'string', description: '目标记忆库(可选)。不填=默认单库。' } },
165
247
  },
166
248
  },
249
+ {
250
+ name: 'list_namespaces',
251
+ description: '列出所有记忆库:默认库 + 每个 namespace 的节点/边数,以及当前命名空间模式(default / cwd-auto / env:<name>)。用它可以确认「多项目隔离」是否真的生效。',
252
+ inputSchema: { type: 'object', properties: {} },
253
+ },
167
254
  ]
168
255
 
169
256
  // ── 工具执行 ─────────────────────────────────────────────────────────────
170
257
  // defaultMem:默认单库实例;args.namespace 传了就切到对应子库。
171
258
  async function callTool(name, args, defaultMem) {
172
- const ns = normalizeNamespace(args.namespace)
259
+ const { ns, from } = resolveNamespace(args.namespace)
173
260
  const mem = ns == null ? defaultMem : await getMemory(ns)
174
261
  switch (name) {
262
+ case 'list_namespaces': {
263
+ return { content: [{ type: 'text', text: JSON.stringify(listNamespaces(defaultMem), null, 2) }] }
264
+ }
175
265
  case 'remember': {
176
266
  const input = { text: String(args.text || '') }
177
267
  if (args.kind) input.kind = args.kind
@@ -193,7 +283,7 @@ async function callTool(name, args, defaultMem) {
193
283
  }
194
284
  case 'stats': {
195
285
  const s = await mem.stats()
196
- return { content: [{ type: 'text', text: JSON.stringify(s) }] }
286
+ return { content: [{ type: 'text', text: JSON.stringify({ ...s, namespace: ns || '(default)', namespaceFrom: from }) }] }
197
287
  }
198
288
  case 'forget': {
199
289
  const r = await mem.forget(String(args.id || ''))
@@ -236,6 +326,8 @@ async function main() {
236
326
  // 默认单库实例(不传 namespace 时用),保持与旧版完全一致的行为。
237
327
  const defaultMem = new NebulaMemory({ dir: DATA_DIR, log: () => {}, options: ENGINE_OPTS })
238
328
  await defaultMem.init()
329
+ const nsMode = AUTO_NS ? `cwd-auto(当前 cwd → ${deriveCwdNamespace()})` : DEFAULT_NS_ENV ? `env:${DEFAULT_NS_ENV}` : 'default(不传 namespace 走默认单库)'
330
+ console.error(`[agent-memory] dataDir=${DATA_DIR} namespaceMode=${nsMode}`)
239
331
 
240
332
  readStdinLines(async (line) => {
241
333
  let msg
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nebula-memory-mcp",
3
- "version": "1.2.6",
3
+ "version": "1.3.0",
4
4
  "mcpName": "io.github.yb-zhai/nebula-memory",
5
5
  "description": "Zero-dependency local Agent memory MCP Server (stdio JSON-RPC). Works offline, no API keys, no database. Exposes remember/recall/stats/forget to Claude Desktop, Cursor, ChatGPT and any MCP client.",
6
6
  "type": "module",