nebula-memory-mcp 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +61 -2
- package/mcp-server.mjs +200 -0
- package/memory/dynamics.cjs +451 -0
- package/memory/extract.cjs +388 -0
- package/memory/fragments.cjs +168 -0
- package/memory/index.mjs +14 -0
- package/memory/recall.cjs +397 -0
- package/memory/store.cjs +2071 -0
- package/memory/tokenize.cjs +323 -0
- package/package.json +38 -4
- package/shared/constants.cjs +15 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 yb-zhai
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,62 @@
|
|
|
1
|
-
#
|
|
1
|
+
# nebula-memory-mcp
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**零依赖、纯本地的 Agent 记忆 MCP Server。** 不要数据库、不要 API Key、不联网、不花钱,一条命令接入 Claude / Cursor / ChatGPT 等任何 MCP 客户端。
|
|
4
|
+
|
|
5
|
+
这是 [agent-memory](https://github.com/yb-zhai/agent-memory) 的 MCP 入口包。核心引擎是纯 Node.js + JSONL 文件,检索用词频 + 倒排索引 + 图扩散(四臂召回),**不调embedding模型、不烧 token**。
|
|
6
|
+
|
|
7
|
+
## 特性
|
|
8
|
+
|
|
9
|
+
- 🔌 **零依赖**:纯 Node.js,无 npm 依赖,装完即用
|
|
10
|
+
- 🏠 **本地优先**:记忆存在你自己的机器,数据不出本机
|
|
11
|
+
- ⚡ **毫秒级召回**:实测平均 28.8ms
|
|
12
|
+
- 🧩 **通用 MCP**:Claude Desktop / Cursor / ChatGPT / Grok / Windsurf / Cline 都能接
|
|
13
|
+
|
|
14
|
+
## 快速接入
|
|
15
|
+
|
|
16
|
+
### Claude Desktop
|
|
17
|
+
|
|
18
|
+
在 `claude_desktop_config.json` 里加:
|
|
19
|
+
|
|
20
|
+
```json
|
|
21
|
+
{
|
|
22
|
+
"mcpServers": {
|
|
23
|
+
"nebula-memory": {
|
|
24
|
+
"command": "npx",
|
|
25
|
+
"args": ["-y", "nebula-memory-mcp"],
|
|
26
|
+
"env": {
|
|
27
|
+
"NEBULA_DATA_DIR": "/path/to/your/memory"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
重启 Claude Desktop 即可。
|
|
35
|
+
|
|
36
|
+
### Cursor / Windsurf / Cline
|
|
37
|
+
|
|
38
|
+
在各自的 MCP 设置里添加 server,填:
|
|
39
|
+
- command: `npx`
|
|
40
|
+
- args: `-y nebula-memory-mcp`
|
|
41
|
+
- env: `NEBULA_DATA_DIR`(可选,默认 `~/.agent-memory`)
|
|
42
|
+
|
|
43
|
+
## 提供的工具
|
|
44
|
+
|
|
45
|
+
| 工具 | 作用 | 参数 |
|
|
46
|
+
|---|---|---|
|
|
47
|
+
| `remember` | 写入记忆 | `text`(必填)、`kind`、`importance`(1.0=永久置顶)、`tags` |
|
|
48
|
+
| `recall` | 召回相关记忆 | `query`(必填)、`limit` |
|
|
49
|
+
| `stats` | 记忆库统计 | 无 |
|
|
50
|
+
| `forget` | 按 id 删除 | `id`(必填) |
|
|
51
|
+
|
|
52
|
+
## 环境变量
|
|
53
|
+
|
|
54
|
+
| 变量 | 说明 | 默认 |
|
|
55
|
+
|---|---|---|
|
|
56
|
+
| `NEBULA_DATA_DIR` | 记忆数据目录 | `~/.agent-memory` |
|
|
57
|
+
|
|
58
|
+
想让多个 AI 客户端共享记忆?把它们的 `NEBULA_DATA_DIR` 填成**同一个目录**即可。
|
|
59
|
+
|
|
60
|
+
## License
|
|
61
|
+
|
|
62
|
+
MIT © 2026 yb-zhai
|
package/mcp-server.mjs
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* agent-memory MCP Server — 纯本地版(零依赖)。
|
|
4
|
+
*
|
|
5
|
+
* 一个 stdio 上的 Model Context Protocol (MCP) 服务,把 NebulaMemory 记忆引擎
|
|
6
|
+
* 暴露给 Claude Desktop / Cursor / Windsurf 等任何 MCP 客户端。
|
|
7
|
+
*
|
|
8
|
+
* 设计原则:
|
|
9
|
+
* - 零依赖:手写 stdio + JSON-RPC 2.0,不引 @modelcontextprotocol/sdk;
|
|
10
|
+
* - 纯本地:只接本地四臂引擎(NebulaMemory),不碰 Hindsight / Postgres / LLM;
|
|
11
|
+
* - 与模型无关:任何 MCP 客户端接入后,都能读写这套记忆。
|
|
12
|
+
*
|
|
13
|
+
* 当前只出纯本地版;Hindsight 语义增强版后续跟进。
|
|
14
|
+
*
|
|
15
|
+
* 用法(Claude Desktop 的 claude_desktop_config.json):
|
|
16
|
+
* {
|
|
17
|
+
* "mcpServers": {
|
|
18
|
+
* "agent-memory": {
|
|
19
|
+
* "command": "node",
|
|
20
|
+
* "args": ["C:\\path\\to\\agent-memory\\mcp-server.mjs"],
|
|
21
|
+
* "env": { "NEBULA_DATA_DIR": "C:\\path\\to\\memory" }
|
|
22
|
+
* }
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { createRequire } from 'node:module'
|
|
28
|
+
import path from 'node:path'
|
|
29
|
+
import os from 'node:os'
|
|
30
|
+
import { fileURLToPath } from 'node:url'
|
|
31
|
+
|
|
32
|
+
const require = createRequire(import.meta.url)
|
|
33
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
34
|
+
const { NebulaMemory } = require(path.join(__dirname, 'memory', 'store.cjs'))
|
|
35
|
+
|
|
36
|
+
// ── 配置 ────────────────────────────────────────────────────────────────
|
|
37
|
+
const DATA_DIR = path.resolve(
|
|
38
|
+
process.env.NEBULA_DATA_DIR ||
|
|
39
|
+
process.env.MEMORY_NEBULA_DATA_DIR ||
|
|
40
|
+
path.join(os.homedir(), '.agent-memory')
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
const SERVER_INFO = {
|
|
44
|
+
name: 'agent-memory',
|
|
45
|
+
version: '1.0.0',
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// MCP 协议版本(当前规范)
|
|
49
|
+
const PROTOCOL_VERSION = '2024-11-05'
|
|
50
|
+
|
|
51
|
+
// ── stdio 行缓冲读写 ─────────────────────────────────────────────────────
|
|
52
|
+
// MCP stdio transport:每行一个 JSON-RPC 消息(LF 分隔),无 Content-Length 头
|
|
53
|
+
// (那是 LSP 的做法;MCP stdio 用换行分隔的 JSON)。
|
|
54
|
+
|
|
55
|
+
function readStdinLines(onLine) {
|
|
56
|
+
let buf = ''
|
|
57
|
+
process.stdin.setEncoding('utf8')
|
|
58
|
+
process.stdin.on('data', (chunk) => {
|
|
59
|
+
buf += chunk
|
|
60
|
+
let idx
|
|
61
|
+
while ((idx = buf.indexOf('\n')) >= 0) {
|
|
62
|
+
const line = buf.slice(0, idx).replace(/\r$/, '')
|
|
63
|
+
buf = buf.slice(idx + 1)
|
|
64
|
+
if (line.trim()) onLine(line)
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
process.stdin.on('end', () => { if (buf.trim()) onLine(buf) })
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function send(msg) {
|
|
71
|
+
process.stdout.write(JSON.stringify(msg) + '\n')
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// ── JSON-RPC 响应助手 ────────────────────────────────────────────────────
|
|
75
|
+
function ok(id, result) { send({ jsonrpc: '2.0', id, result }) }
|
|
76
|
+
function err(id, code, message) { send({ jsonrpc: '2.0', id, error: { code, message } }) }
|
|
77
|
+
|
|
78
|
+
// ── MCP 工具定义 ─────────────────────────────────────────────────────────
|
|
79
|
+
const TOOLS = [
|
|
80
|
+
{
|
|
81
|
+
name: 'remember',
|
|
82
|
+
description: '写入一条记忆。可选 importance(0~1,1.0 表示置顶/永久事实,永不受时间衰减,用于存 API 密钥、用户称呼偏好等关键信息)、kind(episode/fact/note/profile)、tags。',
|
|
83
|
+
inputSchema: {
|
|
84
|
+
type: 'object',
|
|
85
|
+
properties: {
|
|
86
|
+
text: { type: 'string', description: '要记住的内容' },
|
|
87
|
+
kind: { type: 'string', enum: ['episode', 'fact', 'note', 'profile'], description: '记忆类型,默认 episode' },
|
|
88
|
+
importance: { type: 'number', description: '重要性 0~1,1.0 = 永久置顶', minimum: 0, maximum: 1 },
|
|
89
|
+
tags: { type: 'array', items: { type: 'string' }, description: '标签' },
|
|
90
|
+
},
|
|
91
|
+
required: ['text'],
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
name: 'recall',
|
|
96
|
+
description: '召回与 query 相关的记忆。本地四臂检索,返回按相关度排序的记忆列表。',
|
|
97
|
+
inputSchema: {
|
|
98
|
+
type: 'object',
|
|
99
|
+
properties: {
|
|
100
|
+
query: { type: 'string', description: '要检索的内容' },
|
|
101
|
+
limit: { type: 'number', description: '最多返回条数,默认 6' },
|
|
102
|
+
},
|
|
103
|
+
required: ['query'],
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
name: 'stats',
|
|
108
|
+
description: '返回当前记忆库的统计信息(节点数、边数等)。',
|
|
109
|
+
inputSchema: { type: 'object', properties: {} },
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
name: 'forget',
|
|
113
|
+
description: '按 id 删除一条记忆。',
|
|
114
|
+
inputSchema: {
|
|
115
|
+
type: 'object',
|
|
116
|
+
properties: { id: { type: 'string', description: '记忆 id' } },
|
|
117
|
+
required: ['id'],
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
]
|
|
121
|
+
|
|
122
|
+
// ── 工具执行 ─────────────────────────────────────────────────────────────
|
|
123
|
+
async function callTool(name, args, mem) {
|
|
124
|
+
switch (name) {
|
|
125
|
+
case 'remember': {
|
|
126
|
+
const input = { text: String(args.text || '') }
|
|
127
|
+
if (args.kind) input.kind = args.kind
|
|
128
|
+
if (args.importance != null) input.importance = Number(args.importance)
|
|
129
|
+
if (Array.isArray(args.tags)) input.tags = args.tags.map(String)
|
|
130
|
+
const r = await mem.remember(input)
|
|
131
|
+
return {
|
|
132
|
+
content: [{ type: 'text', text: JSON.stringify({ id: r.id, deduped: r.deduped }) }],
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
case 'recall': {
|
|
136
|
+
const query = String(args.query || '')
|
|
137
|
+
const limit = Math.max(1, Math.min(50, Number(args.limit) || 6))
|
|
138
|
+
const hits = await mem.recall(query, { limit })
|
|
139
|
+
const text = hits.length
|
|
140
|
+
? hits.map((h, i) => `${i + 1}. [${h.kind}] ${h.text} (score ${h.score})`).join('\n')
|
|
141
|
+
: '(无匹配记忆)'
|
|
142
|
+
return { content: [{ type: 'text', text }] }
|
|
143
|
+
}
|
|
144
|
+
case 'stats': {
|
|
145
|
+
const s = await mem.stats()
|
|
146
|
+
return { content: [{ type: 'text', text: JSON.stringify(s) }] }
|
|
147
|
+
}
|
|
148
|
+
case 'forget': {
|
|
149
|
+
const r = await mem.forget(String(args.id || ''))
|
|
150
|
+
return { content: [{ type: 'text', text: JSON.stringify(r) }] }
|
|
151
|
+
}
|
|
152
|
+
default:
|
|
153
|
+
throw new Error(`unknown tool: ${name}`)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// ── 主流程 ───────────────────────────────────────────────────────────────
|
|
158
|
+
async function main() {
|
|
159
|
+
const mem = new NebulaMemory({ dir: DATA_DIR, log: () => {} })
|
|
160
|
+
await mem.init()
|
|
161
|
+
|
|
162
|
+
readStdinLines(async (line) => {
|
|
163
|
+
let msg
|
|
164
|
+
try { msg = JSON.parse(line) } catch { return } // 忽略无法解析的行
|
|
165
|
+
const { id, method, params } = msg
|
|
166
|
+
|
|
167
|
+
// 通知(无 id)→ 不回复
|
|
168
|
+
if (id == null) return
|
|
169
|
+
|
|
170
|
+
try {
|
|
171
|
+
switch (method) {
|
|
172
|
+
case 'initialize':
|
|
173
|
+
ok(id, {
|
|
174
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
175
|
+
capabilities: { tools: {} },
|
|
176
|
+
serverInfo: SERVER_INFO,
|
|
177
|
+
})
|
|
178
|
+
break
|
|
179
|
+
case 'tools/list':
|
|
180
|
+
ok(id, { tools: TOOLS })
|
|
181
|
+
break
|
|
182
|
+
case 'tools/call': {
|
|
183
|
+
const { name, arguments: args } = params || {}
|
|
184
|
+
const result = await callTool(name, args || {}, mem)
|
|
185
|
+
ok(id, result)
|
|
186
|
+
break
|
|
187
|
+
}
|
|
188
|
+
case 'ping':
|
|
189
|
+
ok(id, {})
|
|
190
|
+
break
|
|
191
|
+
default:
|
|
192
|
+
err(id, -32601, `method not found: ${method}`)
|
|
193
|
+
}
|
|
194
|
+
} catch (e) {
|
|
195
|
+
err(id, -32603, e.message || String(e))
|
|
196
|
+
}
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
main().catch((e) => { console.error(e); process.exit(1) })
|