@datafrog-io/n2n-memory 1.2.1 → 1.2.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.
- package/CHANGELOG.md +26 -1
- package/README.md +114 -9
- package/build/core/memory-manager.js +66 -51
- package/build/core/memory-manager.js.map +1 -1
- package/build/core/memory-service.js +36 -38
- package/build/core/memory-service.js.map +1 -1
- package/build/handlers/mcp-handlers.js +6 -5
- package/build/handlers/mcp-handlers.js.map +1 -1
- package/build/index.js +23 -7
- package/build/index.js.map +1 -1
- package/build/tools/definitions.js +17 -12
- package/build/tools/definitions.js.map +1 -1
- package/build/tools/schemas.js +1 -1
- package/build/tools/schemas.js.map +1 -1
- package/build/utils/logging.js +27 -0
- package/build/utils/logging.js.map +1 -0
- package/build/utils/path-utils.js +21 -9
- package/build/utils/path-utils.js.map +1 -1
- package/docs/API_REFERENCE.md +283 -0
- package/docs/API_REFERENCE_zh.md +283 -0
- package/docs/CHANGELOG_zh.md +108 -0
- package/docs/DESIGN.md +72 -0
- package/docs/DESIGN_zh.md +72 -0
- package/docs/DEVELOPMENT.md +98 -0
- package/docs/DEVELOPMENT_zh.md +98 -0
- package/docs/README_zh.md +195 -0
- package/llms.txt +83 -0
- package/package.json +55 -13
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
# n2n-memory API Reference
|
|
2
|
+
|
|
3
|
+
[中文版](./API_REFERENCE_zh.md)
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
N2N-Memory provides project-local knowledge graph tools over MCP. Every tool requires `projectPath`, the absolute path of the project root or workspace top-level directory. The server does not recursively search parent directories; this is intentional to prevent cross-project memory pollution.
|
|
8
|
+
|
|
9
|
+
Project recognition requires a strong marker such as `.git`, `.mcp`, `package.json`, `tsconfig.json`, or a language-specific build file. README-only directories are treated as weak roots and rejected.
|
|
10
|
+
|
|
11
|
+
## SEO and discoverability notes
|
|
12
|
+
|
|
13
|
+
- Recommended labels: **project-local MCP memory**, **AI coding memory graph**, **local-first AI context storage**.
|
|
14
|
+
- Primary user intent: repository context recovery, deterministic project memory, and safe AI coding assistant handoff.
|
|
15
|
+
- This API is designed for local-first agents and does not depend on cloud infrastructure.
|
|
16
|
+
|
|
17
|
+
## Shared Parameters
|
|
18
|
+
|
|
19
|
+
All tools include:
|
|
20
|
+
|
|
21
|
+
- `projectPath` (`string`, required): Absolute project root or workspace top-level path.
|
|
22
|
+
- `confirmNewProjectRoot` (`string`, optional): Required only when initializing a project that does not yet have `.mcp`. Use the `detectedRoot` value returned by the handshake response.
|
|
23
|
+
|
|
24
|
+
If the server recognizes a project root but `.mcp` is missing, it returns an `AWAITING_CONFIRMATION` error response. Call the same tool again with `confirmNewProjectRoot` set to the detected root.
|
|
25
|
+
|
|
26
|
+
## Data Storage
|
|
27
|
+
|
|
28
|
+
- Cold graph: `.mcp/memory.json`
|
|
29
|
+
- Hot context: `.mcp/context.json`
|
|
30
|
+
- Atomic writes: JSON is written to a temporary file and then moved into place.
|
|
31
|
+
- Stable diffs: entities, observations, and relations are sorted before graph writes.
|
|
32
|
+
- Data integrity: existing but unreadable JSON files raise an error instead of being treated as empty memory.
|
|
33
|
+
- Path containment: exported files must stay inside the project root.
|
|
34
|
+
- Logging: full local paths are hidden from server logs by default. Set `N2N_LOG_LEVEL=debug` to include complete paths while troubleshooting.
|
|
35
|
+
|
|
36
|
+
## Data Model
|
|
37
|
+
|
|
38
|
+
### Entity
|
|
39
|
+
```json
|
|
40
|
+
{
|
|
41
|
+
"name": "MemoryService",
|
|
42
|
+
"entityType": "CLASS",
|
|
43
|
+
"observations": ["Coordinates graph and context reads"]
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Relation
|
|
48
|
+
```json
|
|
49
|
+
{
|
|
50
|
+
"from": "MemoryService",
|
|
51
|
+
"to": "MemoryManager",
|
|
52
|
+
"relationType": "USES"
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Relations must reference existing entities. Orphan relations are rejected.
|
|
57
|
+
|
|
58
|
+
### Project Context
|
|
59
|
+
```json
|
|
60
|
+
{
|
|
61
|
+
"activeTask": "Refactor storage layer",
|
|
62
|
+
"status": "IN_PROGRESS",
|
|
63
|
+
"reason": "Optional status reason",
|
|
64
|
+
"nextSteps": ["Run tests"],
|
|
65
|
+
"lastCommit": "abc123 or message",
|
|
66
|
+
"updatedAt": "2026-06-14T00:00:00.000Z"
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`updatedAt` is managed by the server when context is written.
|
|
71
|
+
|
|
72
|
+
## Resources & Templates
|
|
73
|
+
|
|
74
|
+
### Static Resource
|
|
75
|
+
- **URI**: `mcp://memory/graph`
|
|
76
|
+
- **Note**: Requires the client to manage project path context or use the template below.
|
|
77
|
+
|
|
78
|
+
### Resource Template
|
|
79
|
+
- **Template**: `mcp://memory/graph?path={path}`
|
|
80
|
+
- **Example**: `mcp://memory/graph?path=/home/deploy/projects/n2n-memory`
|
|
81
|
+
- **Behavior**: Returns the complete graph and active context for established projects. New projects must be initialized through tools because resources cannot perform the confirmation handshake.
|
|
82
|
+
|
|
83
|
+
## Tool Responses & Metadata
|
|
84
|
+
|
|
85
|
+
Most mutating tools return structured JSON:
|
|
86
|
+
|
|
87
|
+
```json
|
|
88
|
+
{
|
|
89
|
+
"status": "success",
|
|
90
|
+
"message": "Human-readable description of the operation.",
|
|
91
|
+
"_protocol": {
|
|
92
|
+
"action": "MEMORY_UPDATED",
|
|
93
|
+
"reminder": "Remember to call n2n_update_context before 'git commit'."
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
The `_protocol` field guides AI assistants during development cycles:
|
|
99
|
+
|
|
100
|
+
- `action`: Logical state change, such as `MEMORY_LOADED`, `MEMORY_UPDATED`, or `CONTEXT_SYNCED`.
|
|
101
|
+
- `reminder`: Synchronization hint for AI assistants.
|
|
102
|
+
- `policy` / `tip`: Context-specific behavior guidance when present.
|
|
103
|
+
|
|
104
|
+
## Common usage patterns
|
|
105
|
+
|
|
106
|
+
1. Start each session by calling `n2n_read_graph` with `summaryMode: true` to inspect the project index.
|
|
107
|
+
2. Add durable facts with `n2n_add_entities` and `n2n_add_observations`.
|
|
108
|
+
3. Link facts with `n2n_create_relations`.
|
|
109
|
+
4. Track daily work in `n2n_update_context` before major refactors.
|
|
110
|
+
5. Before commit, call `n2n_read_graph` again so the context reminder is refreshed.
|
|
111
|
+
|
|
112
|
+
Example `n2n_update_context` payload:
|
|
113
|
+
|
|
114
|
+
```json
|
|
115
|
+
{
|
|
116
|
+
"projectPath": "/absolute/path/to/repo",
|
|
117
|
+
"activeTask": "Refactor MCP error handling",
|
|
118
|
+
"status": "IN_PROGRESS",
|
|
119
|
+
"nextSteps": ["Run tests", "Update docs"],
|
|
120
|
+
"reason": "Refactor without changing public API"
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Tools
|
|
125
|
+
|
|
126
|
+
### `n2n_add_entities`
|
|
127
|
+
|
|
128
|
+
Create new entities or merge observations into existing entities with the same name.
|
|
129
|
+
|
|
130
|
+
Additional input:
|
|
131
|
+
|
|
132
|
+
- `entities` (`Entity[]`, required)
|
|
133
|
+
|
|
134
|
+
Notes:
|
|
135
|
+
|
|
136
|
+
- Existing entity type is preserved.
|
|
137
|
+
- Observations are deduplicated with lightweight similarity checks.
|
|
138
|
+
|
|
139
|
+
### `n2n_add_observations`
|
|
140
|
+
|
|
141
|
+
Append observations to existing entities.
|
|
142
|
+
|
|
143
|
+
Additional input:
|
|
144
|
+
|
|
145
|
+
- `observations` (`Array<{ entityName: string; contents: string[] }>`, required)
|
|
146
|
+
|
|
147
|
+
Notes:
|
|
148
|
+
|
|
149
|
+
- Observations for missing entities are skipped.
|
|
150
|
+
- The response reports how many observation fragments were actually added after deduplication.
|
|
151
|
+
|
|
152
|
+
### `n2n_create_relations`
|
|
153
|
+
|
|
154
|
+
Create directed relations between existing entities.
|
|
155
|
+
|
|
156
|
+
Additional input:
|
|
157
|
+
|
|
158
|
+
- `relations` (`Relation[]`, required)
|
|
159
|
+
|
|
160
|
+
Notes:
|
|
161
|
+
|
|
162
|
+
- Duplicate relations are ignored.
|
|
163
|
+
- Relations pointing to missing entities are rejected.
|
|
164
|
+
|
|
165
|
+
### `n2n_read_graph`
|
|
166
|
+
|
|
167
|
+
Read project graph and active context together.
|
|
168
|
+
|
|
169
|
+
Additional input:
|
|
170
|
+
|
|
171
|
+
- `summaryMode` (`boolean`, optional): Return entity names and types without observations.
|
|
172
|
+
- `limit` (`number`, optional): Maximum entities to return.
|
|
173
|
+
- `offset` (`number`, optional): Number of entities to skip.
|
|
174
|
+
|
|
175
|
+
Output includes:
|
|
176
|
+
|
|
177
|
+
- `graph`
|
|
178
|
+
- `context`
|
|
179
|
+
- `totalEntityCount`
|
|
180
|
+
- `isTruncated`
|
|
181
|
+
- `_protocol`
|
|
182
|
+
|
|
183
|
+
When paginated, relations are included only when both endpoints are present in the current page.
|
|
184
|
+
|
|
185
|
+
### `n2n_get_graph_summary`
|
|
186
|
+
|
|
187
|
+
Return a lightweight entity index plus total relation count.
|
|
188
|
+
|
|
189
|
+
Additional input:
|
|
190
|
+
|
|
191
|
+
- `limit` (`number`, optional)
|
|
192
|
+
- `offset` (`number`, optional)
|
|
193
|
+
|
|
194
|
+
Output includes:
|
|
195
|
+
|
|
196
|
+
- `entities`: `{ name, type }[]`
|
|
197
|
+
- `relationCount`
|
|
198
|
+
- `totalEntityCount`
|
|
199
|
+
- `isTruncated`
|
|
200
|
+
|
|
201
|
+
### `n2n_update_context`
|
|
202
|
+
|
|
203
|
+
Update hot project context.
|
|
204
|
+
|
|
205
|
+
Additional input:
|
|
206
|
+
|
|
207
|
+
- `activeTask` (`string`, optional)
|
|
208
|
+
- `status` (`"IN_PROGRESS" | "COMPLETED" | "BLOCKED" | "PLANNING"`, optional)
|
|
209
|
+
- `reason` (`string`, optional)
|
|
210
|
+
- `nextSteps` (`string[]`, optional)
|
|
211
|
+
- `lastCommit` (`string`, optional)
|
|
212
|
+
|
|
213
|
+
Notes:
|
|
214
|
+
|
|
215
|
+
- Updates are merged with the current context.
|
|
216
|
+
- `updatedAt` is set automatically.
|
|
217
|
+
|
|
218
|
+
### `n2n_search`
|
|
219
|
+
|
|
220
|
+
Search entities by name, type, and observations. Fuzzy matching is enabled by default.
|
|
221
|
+
|
|
222
|
+
Additional input:
|
|
223
|
+
|
|
224
|
+
- `query` (`string`, required)
|
|
225
|
+
- `limit` (`number`, optional)
|
|
226
|
+
- `offset` (`number`, optional)
|
|
227
|
+
- `fuzzy` (`boolean`, optional, default `true`)
|
|
228
|
+
- `minScore` (`number`, optional, default `0.3`)
|
|
229
|
+
|
|
230
|
+
Output includes matching entities, related relations, `totalResults`, and `isTruncated`.
|
|
231
|
+
|
|
232
|
+
### `n2n_delete_entities`
|
|
233
|
+
|
|
234
|
+
Delete entities and all relations attached to them.
|
|
235
|
+
|
|
236
|
+
Additional input:
|
|
237
|
+
|
|
238
|
+
- `entityNames` (`string[]`, required)
|
|
239
|
+
|
|
240
|
+
Output reports the number of deleted entities.
|
|
241
|
+
|
|
242
|
+
### `n2n_delete_observations`
|
|
243
|
+
|
|
244
|
+
Delete exact observation strings from entities.
|
|
245
|
+
|
|
246
|
+
Additional input:
|
|
247
|
+
|
|
248
|
+
- `deletions` (`Array<{ entityName: string; observations: string[] }>`, required)
|
|
249
|
+
|
|
250
|
+
Output reports the number of deleted observations.
|
|
251
|
+
|
|
252
|
+
### `n2n_delete_relations`
|
|
253
|
+
|
|
254
|
+
Delete exact relation triples.
|
|
255
|
+
|
|
256
|
+
Additional input:
|
|
257
|
+
|
|
258
|
+
- `relations` (`Relation[]`, required)
|
|
259
|
+
|
|
260
|
+
Output reports the number of deleted relations.
|
|
261
|
+
|
|
262
|
+
### `n2n_open_nodes`
|
|
263
|
+
|
|
264
|
+
Retrieve specific entities by name.
|
|
265
|
+
|
|
266
|
+
Additional input:
|
|
267
|
+
|
|
268
|
+
- `names` (`string[]`, required)
|
|
269
|
+
|
|
270
|
+
Output includes matching entities and relations where both endpoints are among the returned entities.
|
|
271
|
+
|
|
272
|
+
### `n2n_export_markdown`
|
|
273
|
+
|
|
274
|
+
Export the graph to Markdown for review or documentation.
|
|
275
|
+
|
|
276
|
+
Additional input:
|
|
277
|
+
|
|
278
|
+
- `outputPath` (`string`, optional): Relative output path inside the project root. Defaults to `KNOWLEDGE_GRAPH.md`.
|
|
279
|
+
|
|
280
|
+
Notes:
|
|
281
|
+
|
|
282
|
+
- Absolute paths are rejected.
|
|
283
|
+
- Relative paths that escape the project root are rejected.
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
# n2n-memory API 参考手册
|
|
2
|
+
|
|
3
|
+
[English](./API_REFERENCE.md)
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
N2N-Memory 通过 MCP 提供项目本地知识图谱工具。所有工具都要求传入 `projectPath`,即项目根目录或工作区顶级目录的绝对路径。服务不会递归向上搜索父目录,这是为了避免跨项目记忆污染。
|
|
8
|
+
|
|
9
|
+
项目识别要求存在 `.git`、`.mcp`、`package.json`、`tsconfig.json` 或语言构建文件等强标记。只有 README 的目录会被视为弱根目录并被拒绝。
|
|
10
|
+
|
|
11
|
+
## SEO 与可发现性说明
|
|
12
|
+
|
|
13
|
+
- 推荐检索词:**项目级 MCP memory**、**AI 编码记忆图谱**、**本地优先 AI 上下文存储**。
|
|
14
|
+
- 主要使用意图:仓库上下文恢复、确定性的项目记忆、与 AI 编码助手的安全交接。
|
|
15
|
+
- 本 API 采用本地优先设计,不依赖云端服务。
|
|
16
|
+
|
|
17
|
+
## 共享参数
|
|
18
|
+
|
|
19
|
+
所有工具都包含:
|
|
20
|
+
|
|
21
|
+
- `projectPath`(`string`,必填):项目根目录或工作区顶级目录的绝对路径。
|
|
22
|
+
- `confirmNewProjectRoot`(`string`,可选):仅在初始化尚未创建 `.mcp` 的项目时使用。取值应为握手响应中的 `detectedRoot`。
|
|
23
|
+
|
|
24
|
+
如果服务识别到项目根目录,但 `.mcp` 尚不存在,会返回 `AWAITING_CONFIRMATION` 错误响应。此时使用同一个工具再次调用,并将 `confirmNewProjectRoot` 设置为返回的 `detectedRoot`。
|
|
25
|
+
|
|
26
|
+
## 数据存储
|
|
27
|
+
|
|
28
|
+
- 冷知识图谱:`.mcp/memory.json`
|
|
29
|
+
- 热任务上下文:`.mcp/context.json`
|
|
30
|
+
- 原子写入:JSON 先写入临时文件,再移动到目标位置。
|
|
31
|
+
- 稳定 diff:写入图谱前会对实体、观测事实、关系执行排序。
|
|
32
|
+
- 数据完整性:已存在但无法读取的 JSON 文件会抛错,不会被当成空记忆。
|
|
33
|
+
- 路径边界:导出文件必须保留在项目根目录内部。
|
|
34
|
+
- 日志:服务日志默认隐藏完整本地路径。排查问题时可设置 `N2N_LOG_LEVEL=debug` 输出完整路径。
|
|
35
|
+
|
|
36
|
+
## 数据模型
|
|
37
|
+
|
|
38
|
+
### Entity
|
|
39
|
+
```json
|
|
40
|
+
{
|
|
41
|
+
"name": "MemoryService",
|
|
42
|
+
"entityType": "CLASS",
|
|
43
|
+
"observations": ["Coordinates graph and context reads"]
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Relation
|
|
48
|
+
```json
|
|
49
|
+
{
|
|
50
|
+
"from": "MemoryService",
|
|
51
|
+
"to": "MemoryManager",
|
|
52
|
+
"relationType": "USES"
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
关系必须指向已存在实体。指向不存在实体的孤儿关系会被拒绝。
|
|
57
|
+
|
|
58
|
+
### Project Context
|
|
59
|
+
```json
|
|
60
|
+
{
|
|
61
|
+
"activeTask": "Refactor storage layer",
|
|
62
|
+
"status": "IN_PROGRESS",
|
|
63
|
+
"reason": "Optional status reason",
|
|
64
|
+
"nextSteps": ["Run tests"],
|
|
65
|
+
"lastCommit": "abc123 or message",
|
|
66
|
+
"updatedAt": "2026-06-14T00:00:00.000Z"
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`updatedAt` 由服务在写入上下文时自动维护。
|
|
71
|
+
|
|
72
|
+
## 资源与模板
|
|
73
|
+
|
|
74
|
+
### 静态资源
|
|
75
|
+
- **URI**: `mcp://memory/graph`
|
|
76
|
+
- **注意**: 要求客户端手动管理项目路径上下文,或使用下方的资源模板。
|
|
77
|
+
|
|
78
|
+
### 资源模板
|
|
79
|
+
- **模板**: `mcp://memory/graph?path={path}`
|
|
80
|
+
- **示例**: `mcp://memory/graph?path=/home/deploy/projects/n2n-memory`
|
|
81
|
+
- **行为**: 对已初始化项目返回完整图谱与活跃上下文。新项目必须通过工具完成确认握手,因为资源读取无法携带初始化确认参数。
|
|
82
|
+
|
|
83
|
+
## 工具响应与元数据
|
|
84
|
+
|
|
85
|
+
多数写入工具返回结构化 JSON:
|
|
86
|
+
|
|
87
|
+
```json
|
|
88
|
+
{
|
|
89
|
+
"status": "success",
|
|
90
|
+
"message": "操作的人类可读描述。",
|
|
91
|
+
"_protocol": {
|
|
92
|
+
"action": "MEMORY_UPDATED",
|
|
93
|
+
"reminder": "Remember to call n2n_update_context before 'git commit'."
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
`_protocol` 字段用于在开发周期内引导 AI 助手:
|
|
99
|
+
|
|
100
|
+
- `action`:逻辑状态变更,例如 `MEMORY_LOADED`、`MEMORY_UPDATED` 或 `CONTEXT_SYNCED`。
|
|
101
|
+
- `reminder`:给 AI 助手的同步提醒。
|
|
102
|
+
- `policy` / `tip`:存在时表示特定上下文下的行为提示。
|
|
103
|
+
|
|
104
|
+
## 常见使用流程
|
|
105
|
+
|
|
106
|
+
1. 会话开始时先调用 `n2n_read_graph`,可用 `summaryMode: true` 获取项目实体索引。
|
|
107
|
+
2. 用 `n2n_add_entities`、`n2n_add_observations` 写入长期事实。
|
|
108
|
+
3. 用 `n2n_create_relations` 建立关系。
|
|
109
|
+
4. 在关键阶段用 `n2n_update_context` 记录任务进展。
|
|
110
|
+
5. 提交前再次调用 `n2n_read_graph`,让上下文同步提醒保持最新。
|
|
111
|
+
|
|
112
|
+
`n2n_update_context` 示例:
|
|
113
|
+
|
|
114
|
+
```json
|
|
115
|
+
{
|
|
116
|
+
"projectPath": "/absolute/path/to/repo",
|
|
117
|
+
"activeTask": "重构 MCP 错误处理",
|
|
118
|
+
"status": "IN_PROGRESS",
|
|
119
|
+
"nextSteps": ["运行测试", "更新文档"],
|
|
120
|
+
"reason": "在不改动外部行为的前提下优化上下文"
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## 工具
|
|
125
|
+
|
|
126
|
+
### `n2n_add_entities`
|
|
127
|
+
|
|
128
|
+
创建新实体,或将观测事实合并到同名实体。
|
|
129
|
+
|
|
130
|
+
额外输入:
|
|
131
|
+
|
|
132
|
+
- `entities`(`Entity[]`,必填)
|
|
133
|
+
|
|
134
|
+
说明:
|
|
135
|
+
|
|
136
|
+
- 已存在实体的 `entityType` 会保留。
|
|
137
|
+
- 观测事实会通过轻量相似度规则去重。
|
|
138
|
+
|
|
139
|
+
### `n2n_add_observations`
|
|
140
|
+
|
|
141
|
+
向已存在实体追加观测事实。
|
|
142
|
+
|
|
143
|
+
额外输入:
|
|
144
|
+
|
|
145
|
+
- `observations`(`Array<{ entityName: string; contents: string[] }>`,必填)
|
|
146
|
+
|
|
147
|
+
说明:
|
|
148
|
+
|
|
149
|
+
- 指向不存在实体的观测事实会被跳过。
|
|
150
|
+
- 响应会报告去重后实际新增的观测事实数量。
|
|
151
|
+
|
|
152
|
+
### `n2n_create_relations`
|
|
153
|
+
|
|
154
|
+
在已存在实体之间创建有向关系。
|
|
155
|
+
|
|
156
|
+
额外输入:
|
|
157
|
+
|
|
158
|
+
- `relations`(`Relation[]`,必填)
|
|
159
|
+
|
|
160
|
+
说明:
|
|
161
|
+
|
|
162
|
+
- 重复关系会被忽略。
|
|
163
|
+
- 指向不存在实体的关系会被拒绝。
|
|
164
|
+
|
|
165
|
+
### `n2n_read_graph`
|
|
166
|
+
|
|
167
|
+
同时读取项目知识图谱和活跃上下文。
|
|
168
|
+
|
|
169
|
+
额外输入:
|
|
170
|
+
|
|
171
|
+
- `summaryMode`(`boolean`,可选):只返回实体名称和类型,不返回观测事实。
|
|
172
|
+
- `limit`(`number`,可选):最多返回多少个实体。
|
|
173
|
+
- `offset`(`number`,可选):跳过多少个实体。
|
|
174
|
+
|
|
175
|
+
输出包含:
|
|
176
|
+
|
|
177
|
+
- `graph`
|
|
178
|
+
- `context`
|
|
179
|
+
- `totalEntityCount`
|
|
180
|
+
- `isTruncated`
|
|
181
|
+
- `_protocol`
|
|
182
|
+
|
|
183
|
+
分页读取时,只有当关系两端实体都在当前页中,该关系才会返回。
|
|
184
|
+
|
|
185
|
+
### `n2n_get_graph_summary`
|
|
186
|
+
|
|
187
|
+
返回轻量实体索引和关系总数。
|
|
188
|
+
|
|
189
|
+
额外输入:
|
|
190
|
+
|
|
191
|
+
- `limit`(`number`,可选)
|
|
192
|
+
- `offset`(`number`,可选)
|
|
193
|
+
|
|
194
|
+
输出包含:
|
|
195
|
+
|
|
196
|
+
- `entities`:`{ name, type }[]`
|
|
197
|
+
- `relationCount`
|
|
198
|
+
- `totalEntityCount`
|
|
199
|
+
- `isTruncated`
|
|
200
|
+
|
|
201
|
+
### `n2n_update_context`
|
|
202
|
+
|
|
203
|
+
更新热任务上下文。
|
|
204
|
+
|
|
205
|
+
额外输入:
|
|
206
|
+
|
|
207
|
+
- `activeTask`(`string`,可选)
|
|
208
|
+
- `status`(`"IN_PROGRESS" | "COMPLETED" | "BLOCKED" | "PLANNING"`,可选)
|
|
209
|
+
- `reason`(`string`,可选)
|
|
210
|
+
- `nextSteps`(`string[]`,可选)
|
|
211
|
+
- `lastCommit`(`string`,可选)
|
|
212
|
+
|
|
213
|
+
说明:
|
|
214
|
+
|
|
215
|
+
- 更新会与当前上下文合并。
|
|
216
|
+
- `updatedAt` 会自动写入。
|
|
217
|
+
|
|
218
|
+
### `n2n_search`
|
|
219
|
+
|
|
220
|
+
按实体名称、类型、观测事实搜索图谱。默认启用模糊匹配。
|
|
221
|
+
|
|
222
|
+
额外输入:
|
|
223
|
+
|
|
224
|
+
- `query`(`string`,必填)
|
|
225
|
+
- `limit`(`number`,可选)
|
|
226
|
+
- `offset`(`number`,可选)
|
|
227
|
+
- `fuzzy`(`boolean`,可选,默认 `true`)
|
|
228
|
+
- `minScore`(`number`,可选,默认 `0.3`)
|
|
229
|
+
|
|
230
|
+
输出包含匹配实体、相关关系、`totalResults` 与 `isTruncated`。
|
|
231
|
+
|
|
232
|
+
### `n2n_delete_entities`
|
|
233
|
+
|
|
234
|
+
删除实体及其关联关系。
|
|
235
|
+
|
|
236
|
+
额外输入:
|
|
237
|
+
|
|
238
|
+
- `entityNames`(`string[]`,必填)
|
|
239
|
+
|
|
240
|
+
输出会报告删除的实体数量。
|
|
241
|
+
|
|
242
|
+
### `n2n_delete_observations`
|
|
243
|
+
|
|
244
|
+
从实体中删除精确匹配的观测事实字符串。
|
|
245
|
+
|
|
246
|
+
额外输入:
|
|
247
|
+
|
|
248
|
+
- `deletions`(`Array<{ entityName: string; observations: string[] }>`,必填)
|
|
249
|
+
|
|
250
|
+
输出会报告删除的观测事实数量。
|
|
251
|
+
|
|
252
|
+
### `n2n_delete_relations`
|
|
253
|
+
|
|
254
|
+
删除精确匹配的关系三元组。
|
|
255
|
+
|
|
256
|
+
额外输入:
|
|
257
|
+
|
|
258
|
+
- `relations`(`Relation[]`,必填)
|
|
259
|
+
|
|
260
|
+
输出会报告删除的关系数量。
|
|
261
|
+
|
|
262
|
+
### `n2n_open_nodes`
|
|
263
|
+
|
|
264
|
+
按名称读取指定实体。
|
|
265
|
+
|
|
266
|
+
额外输入:
|
|
267
|
+
|
|
268
|
+
- `names`(`string[]`,必填)
|
|
269
|
+
|
|
270
|
+
输出包含匹配实体,以及两端都在返回实体集合内的关系。
|
|
271
|
+
|
|
272
|
+
### `n2n_export_markdown`
|
|
273
|
+
|
|
274
|
+
将图谱导出为 Markdown,便于审阅或生成文档。
|
|
275
|
+
|
|
276
|
+
额外输入:
|
|
277
|
+
|
|
278
|
+
- `outputPath`(`string`,可选):项目根目录内的相对输出路径,默认 `KNOWLEDGE_GRAPH.md`。
|
|
279
|
+
|
|
280
|
+
说明:
|
|
281
|
+
|
|
282
|
+
- 绝对路径会被拒绝。
|
|
283
|
+
- 试图逃逸项目根目录的相对路径会被拒绝。
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# 变更日志 (Changelog)
|
|
2
|
+
|
|
3
|
+
[English](../CHANGELOG.md)
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
本项目的所有重大变更都将记录在此文件中。
|
|
8
|
+
|
|
9
|
+
## [未发布]
|
|
10
|
+
|
|
11
|
+
### 修复 (Fixed)
|
|
12
|
+
- 普通读取现在会在底层 `.mcp` 文件变化时刷新已缓存的图谱/上下文 snapshot。
|
|
13
|
+
- 已存在但无法读取的 `memory.json` 或 `context.json` 现在会抛出数据完整性错误,不再被当成空状态。
|
|
14
|
+
- `n2n_create_relations` 现在会拒绝指向不存在实体的关系。
|
|
15
|
+
- `n2n_export_markdown` 现在会拒绝绝对输出路径,以及试图逃逸项目根目录的相对路径。
|
|
16
|
+
- `projectPath` 校验现在会在路径解析前拒绝相对路径。
|
|
17
|
+
- 生产依赖与开发依赖审计现在均可达到 0 漏洞。
|
|
18
|
+
|
|
19
|
+
### 变更 (Changed)
|
|
20
|
+
- 明确 `projectPath` 必须指向项目根目录或工作区顶级目录。
|
|
21
|
+
- 将 JSON 存储写入收敛到共享的原子写入路径,并在持久化前规范化图谱副本。
|
|
22
|
+
- 测试框架从 Mocha 切换到 Vitest,以保持开发依赖树干净。
|
|
23
|
+
- 工具发现现在会暴露由 Zod Schema 生成的具体 JSON 输入 Schema。
|
|
24
|
+
- 只有 README 的目录会被视为弱根目录,并在项目识别时被拒绝。
|
|
25
|
+
- 服务日志默认隐藏完整本地路径,除非设置 `N2N_LOG_LEVEL=debug`。
|
|
26
|
+
- 更新英文与中文文档,反映当前工具集、存储契约、路径边界和图谱完整性行为。
|
|
27
|
+
|
|
28
|
+
### 新增 (Added)
|
|
29
|
+
- 新增用于 PR 和 `main` 分支推送的 GitHub CI workflow。
|
|
30
|
+
- 发布 workflow 现在会在 `npm publish` 前运行完整检查。
|
|
31
|
+
- 新增 npm 包 `files` 白名单和更完整的 package metadata。
|
|
32
|
+
- 新增开源治理文件:贡献指南、安全策略、行为准则、issue 模板和 PR 模板。
|
|
33
|
+
|
|
34
|
+
## [1.2.1] - 2026-01-12
|
|
35
|
+
|
|
36
|
+
### 新增 (Added)
|
|
37
|
+
- **结构化 JSON 工具响应**:
|
|
38
|
+
- 将工具响应从纯文本升级为结构化 JSON。
|
|
39
|
+
- 新增 `status`、`message` 和 `_protocol` 元数据字段,提高机器可读性。
|
|
40
|
+
- **动态 N2N-SYNC 协议提醒**:
|
|
41
|
+
- 自动向 `nextSteps` 注入 `[N2N-SYNC]` 协议提醒。
|
|
42
|
+
- 在工具元数据中嵌入协议提示,引导 AI 助手形成更好的同步习惯。
|
|
43
|
+
- **基于相似度的观察去重**:
|
|
44
|
+
- 新增 `similarity.ts` 工具模块,包含 Jaccard 相似度、Levenshtein 编辑距离和包含检测算法。
|
|
45
|
+
- `addEntities()` 和 `addObservations()` 现在使用智能去重替代精确匹配的 `Set` 去重。
|
|
46
|
+
- 检测到重复时自动保留更详细(更长)的观察记录。
|
|
47
|
+
- 防止 "version 2.4.1" 和 "version 2.4.1 is the current release" 等冗余条目同时存在。
|
|
48
|
+
|
|
49
|
+
- **支持模糊搜索与相关度排序**:
|
|
50
|
+
- `n2n_search` 工具现默认支持模糊匹配。
|
|
51
|
+
- 新增可选参数:`fuzzy`(布尔值)和 `minScore`(0-1 阈值)。
|
|
52
|
+
- 搜索结果按相关度评分排序(最相关的在前)。
|
|
53
|
+
- 通过 Levenshtein 相似度处理拼写错误。
|
|
54
|
+
- 支持词级别的部分匹配和基于 Jaccard 指数的语义相似度。
|
|
55
|
+
|
|
56
|
+
- **新增单元测试**:
|
|
57
|
+
- 为所有相似度函数新增完整测试套件 (`similarity.test.ts`)。
|
|
58
|
+
- 共 99 个测试全部通过。
|
|
59
|
+
|
|
60
|
+
## [1.1.0] - 2024-12-19
|
|
61
|
+
|
|
62
|
+
### 新增 (Added)
|
|
63
|
+
- 双语 README 支持:分为 `README.md`(英文)和 `docs/README_zh.md`(中文)。
|
|
64
|
+
- 初始化 `CHANGELOG.md` 以追踪项目演进和技术发现。
|
|
65
|
+
- **全文档双语化**:包括 API 参考、设计方案和开发指南,均已实现中英双语关联。
|
|
66
|
+
- **基础设施强化**:
|
|
67
|
+
- 更新 `package.json` 中的 `engines` 字段,要求 Node.js >= 20 和 npm >= 10(与 MCP SDK 保持一致)。
|
|
68
|
+
- 在 MCP Server 启动时增加了环境检查逻辑,主动提醒版本不匹配风险。
|
|
69
|
+
- **依赖维护**:
|
|
70
|
+
- 将所有依赖项升级至最新的稳定版本(包括 `sinon`, `typescript`, `eslint` 等)。
|
|
71
|
+
- 验证当前依赖为 0 漏洞且全部为最新。
|
|
72
|
+
- **高可靠架构演进**:
|
|
73
|
+
- 实现了 **双缓存服务 (Dual-Buffer Service)**:
|
|
74
|
+
- **Snapshot Buffer**:内存级快照,提供瞬时读取响应。
|
|
75
|
+
- **Write Queue**:串行写入队列,并集成 `proper-lockfile` 解决多进程冲突。
|
|
76
|
+
- **原子化持久化**:采用临时文件交换(Atomic Rename)机制,确保 JSON 文件完整性。
|
|
77
|
+
- **AI 引导路径定位**:优化工具描述,通过 AI 助手主动报告项目路径实现“启动即加载”。
|
|
78
|
+
- **模块化重构与 SDK 现代化 (Clean Architecture & Modernization)**:
|
|
79
|
+
- 迁移至最新的 `@modelcontextprotocol/sdk` 高级 `McpServer` API。
|
|
80
|
+
- 彻底解决了 `Server` 类被弃用的警告 (TS6385)。
|
|
81
|
+
- 将庞大的 `index.ts` 按照功能拆分为清晰的目录结构:
|
|
82
|
+
- `/core`:核心领域逻辑(MemoryService, MemoryManager)。
|
|
83
|
+
- `/tools`:工具元数据定义与 Zod Schema 校验。
|
|
84
|
+
- `/handlers`:通过 `registerAll` 实现纯粹的请求分发。
|
|
85
|
+
- `/utils`:环境检查等系统工具。
|
|
86
|
+
- **冷热数据分离 (Project Context)**:
|
|
87
|
+
- 引入 `context.json` 专门存储高频变动的“热状态”(当前任务、状态、后续步骤)。
|
|
88
|
+
- 将架构级的“冷知识”隔离在 `memory.json` 中,减少数据污染。
|
|
89
|
+
- 为 `context.json` 配置了独立的物理锁 (`proper-lockfile`) 和内存互斥锁 (`Mutex`)。
|
|
90
|
+
- 新增工具 `n2n_update_context` 并集成了强制性的 Git 同步协议。
|
|
91
|
+
- `n2n_read_graph` 现在支持“一站式读取”,同时返回知识图谱与当前上下文。
|
|
92
|
+
- **N2N-SYNC 协议强制约束**:
|
|
93
|
+
- 在 MCP 工具元数据中硬编码了强制性的同步约束。
|
|
94
|
+
- 会话启动(`n2n_read_graph`)现在会自动绑定 AI 执行“Git 提交前更新记忆”的策略。
|
|
95
|
+
- 在所有写入工具中增加了 `[MANDATORY PROTOCOL]` 和 `[HARD CONSTRAINT]` 标签,有效防止重构过程中的上下文漂移。
|
|
96
|
+
- 优化了测试套件兼容性,修复了模块化后的路径引用问题。
|
|
97
|
+
|
|
98
|
+
### 修复 (Fixed)
|
|
99
|
+
- **NPM 发布兼容性**:识别并记录了 Trusted Publishing (OIDC) 的要求。
|
|
100
|
+
- **发现**:NPM CLI 10.x 版本在 OIDC 发布时可能存在问题。
|
|
101
|
+
- **建议**:使用 Node.js 20+ LTS 配合 npm 10+ 以确保发布稳定性。
|
|
102
|
+
|
|
103
|
+
## [1.0.0] - 2024-12-19 之前
|
|
104
|
+
|
|
105
|
+
### 新增 (Added)
|
|
106
|
+
- 项目本地记忆隔离的核心 MCP 功能。
|
|
107
|
+
- 认知碎片持久化于 `[项目根目录]/.mcp/memory.json`。
|
|
108
|
+
- 工具集:`n2n_add_entities`, `n2n_add_observations`, `n2n_create_relations`, `n2n_read_graph`, `n2n_search`。
|