nebula-memory-mcp 1.0.1 → 1.1.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
@@ -49,6 +49,23 @@
49
49
  | `stats` | 记忆库统计 | 无 |
50
50
  | `forget` | 按 id 删除 | `id`(必填) |
51
51
 
52
+ 四个工具都接受一个**可选** `namespace` 参数(见下节)。
53
+
54
+ ## 多库(namespace)
55
+
56
+ 想让同一个 MCP Server 管多套互不干扰的记忆(比如工作 / 个人分开)?在任意工具调用里加 `namespace` 即可:
57
+
58
+ ```json
59
+ {"text":"记在工作库","namespace":"work"}
60
+ ```
61
+
62
+ - **不填** `namespace` → 用默认单库(路径即 `NEBULA_DATA_DIR`,老用户无需改任何配置);
63
+ - **填了** → 用独立子库 `NEBULA_DATA_DIR/__ns__/<namespace>/`,各库完全隔离,召回也只在指定库内;
64
+ - `namespace` 只允许字母、数字、`_`、`-`,长度 ≤ 64(防目录穿越,非法值会报错);
65
+ - `recall` / `stats` / `forget` 同样支持 `namespace`,传了就只操作对应库。
66
+
67
+ 典型用法:给不同项目或不同 Agent 各起一个 namespace,一句话切换记忆库,无需启动多个 MCP 进程。
68
+
52
69
  ## 环境变量
53
70
 
54
71
  | 变量 | 说明 | 默认 |
package/mcp-server.mjs CHANGED
@@ -42,12 +42,38 @@ const DATA_DIR = path.resolve(
42
42
 
43
43
  const SERVER_INFO = {
44
44
  name: 'agent-memory',
45
- version: '1.0.0',
45
+ version: '1.1.0',
46
46
  }
47
47
 
48
48
  // MCP 协议版本(当前规范)
49
49
  const PROTOCOL_VERSION = '2024-11-05'
50
50
 
51
+ // ── 多库(namespace)支持 ────────────────────────────────────────────────
52
+ // 可选参数:不传 namespace 时走原来的 DATA_DIR 单库(完全向后兼容);
53
+ // 传了则用 DATA_DIR/__ns__/<name> 子目录,各库互不干扰。
54
+ const NS_ROOT = path.join(DATA_DIR, '__ns__')
55
+ const memCache = new Map() // namespace -> NebulaMemory 实例
56
+
57
+ function normalizeNamespace(ns) {
58
+ if (ns == null) return null
59
+ const s = String(ns).trim()
60
+ if (!s) return null
61
+ // 白名单:只允许字母数字、下划线、连字符;防止目录穿越。
62
+ if (!/^[A-Za-z0-9_-]{1,64}$/.test(s)) throw new Error(`invalid namespace: ${s}`)
63
+ return s
64
+ }
65
+
66
+ async function getMemory(ns) {
67
+ if (ns == null) return null // 调用方改用默认单库实例
68
+ if (!memCache.has(ns)) {
69
+ const dir = path.join(NS_ROOT, ns)
70
+ const m = new NebulaMemory({ dir, log: () => {} })
71
+ await m.init()
72
+ memCache.set(ns, m)
73
+ }
74
+ return memCache.get(ns)
75
+ }
76
+
51
77
  // ── stdio 行缓冲读写 ─────────────────────────────────────────────────────
52
78
  // MCP stdio transport:每行一个 JSON-RPC 消息(LF 分隔),无 Content-Length 头
53
79
  // (那是 LSP 的做法;MCP stdio 用换行分隔的 JSON)。
@@ -87,6 +113,7 @@ const TOOLS = [
87
113
  kind: { type: 'string', enum: ['episode', 'fact', 'note', 'profile'], description: '记忆类型,默认 episode' },
88
114
  importance: { type: 'number', description: '重要性 0~1,1.0 = 永久置顶', minimum: 0, maximum: 1 },
89
115
  tags: { type: 'array', items: { type: 'string' }, description: '标签' },
116
+ namespace: { type: 'string', description: '目标记忆库(可选)。不填=默认单库;填了写入独立子库,如 work/personal。仅字母数字、_、-,≤64字符。' },
90
117
  },
91
118
  required: ['text'],
92
119
  },
@@ -99,6 +126,7 @@ const TOOLS = [
99
126
  properties: {
100
127
  query: { type: 'string', description: '要检索的内容' },
101
128
  limit: { type: 'number', description: '最多返回条数,默认 6' },
129
+ namespace: { type: 'string', description: '目标记忆库(可选)。不填=默认单库;填了只在该子库内检索。' },
102
130
  },
103
131
  required: ['query'],
104
132
  },
@@ -106,21 +134,30 @@ const TOOLS = [
106
134
  {
107
135
  name: 'stats',
108
136
  description: '返回当前记忆库的统计信息(节点数、边数等)。',
109
- inputSchema: { type: 'object', properties: {} },
137
+ inputSchema: {
138
+ type: 'object',
139
+ properties: { namespace: { type: 'string', description: '目标记忆库(可选)。不填=默认单库。' } },
140
+ },
110
141
  },
111
142
  {
112
143
  name: 'forget',
113
144
  description: '按 id 删除一条记忆。',
114
145
  inputSchema: {
115
146
  type: 'object',
116
- properties: { id: { type: 'string', description: '记忆 id' } },
147
+ properties: {
148
+ id: { type: 'string', description: '记忆 id' },
149
+ namespace: { type: 'string', description: '目标记忆库(可选)。不填=默认单库。' },
150
+ },
117
151
  required: ['id'],
118
152
  },
119
153
  },
120
154
  ]
121
155
 
122
156
  // ── 工具执行 ─────────────────────────────────────────────────────────────
123
- async function callTool(name, args, mem) {
157
+ // defaultMem:默认单库实例;args.namespace 传了就切到对应子库。
158
+ async function callTool(name, args, defaultMem) {
159
+ const ns = normalizeNamespace(args.namespace)
160
+ const mem = ns == null ? defaultMem : await getMemory(ns)
124
161
  switch (name) {
125
162
  case 'remember': {
126
163
  const input = { text: String(args.text || '') }
@@ -156,8 +193,9 @@ async function callTool(name, args, mem) {
156
193
 
157
194
  // ── 主流程 ───────────────────────────────────────────────────────────────
158
195
  async function main() {
159
- const mem = new NebulaMemory({ dir: DATA_DIR, log: () => {} })
160
- await mem.init()
196
+ // 默认单库实例(不传 namespace 时用),保持与旧版完全一致的行为。
197
+ const defaultMem = new NebulaMemory({ dir: DATA_DIR, log: () => {} })
198
+ await defaultMem.init()
161
199
 
162
200
  readStdinLines(async (line) => {
163
201
  let msg
@@ -181,7 +219,7 @@ async function main() {
181
219
  break
182
220
  case 'tools/call': {
183
221
  const { name, arguments: args } = params || {}
184
- const result = await callTool(name, args || {}, mem)
222
+ const result = await callTool(name, args || {}, defaultMem)
185
223
  ok(id, result)
186
224
  break
187
225
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nebula-memory-mcp",
3
- "version": "1.0.1",
3
+ "version": "1.1.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",