@miniaxolotl/grove 0.2.4 → 0.2.6
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 +1 -1
- package/README.md +6 -6
- package/dist/config.d.ts +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +9 -1
- package/dist/config.js.map +1 -1
- package/dist/index.js +260 -292
- package/dist/index.js.map +1 -1
- package/dist/repositories/entity.repository.d.ts.map +1 -1
- package/dist/repositories/entity.repository.js +24 -41
- package/dist/repositories/entity.repository.js.map +1 -1
- package/dist/repositories/memory.repository.d.ts +16 -35
- package/dist/repositories/memory.repository.d.ts.map +1 -1
- package/dist/repositories/memory.repository.js +71 -132
- package/dist/repositories/memory.repository.js.map +1 -1
- package/dist/repositories/relation.repository.d.ts.map +1 -1
- package/dist/repositories/relation.repository.js +25 -43
- package/dist/repositories/relation.repository.js.map +1 -1
- package/dist/services/embedding.d.ts +1 -0
- package/dist/services/embedding.d.ts.map +1 -1
- package/dist/services/embedding.js +8 -1
- package/dist/services/embedding.js.map +1 -1
- package/dist/services/qdrant.d.ts +5 -2
- package/dist/services/qdrant.d.ts.map +1 -1
- package/dist/services/qdrant.js +27 -10
- package/dist/services/qdrant.js.map +1 -1
- package/dist/utils/filter.d.ts +10 -0
- package/dist/utils/filter.d.ts.map +1 -0
- package/dist/utils/filter.js +15 -0
- package/dist/utils/filter.js.map +1 -0
- package/eslint.config.js +1 -1
- package/package.json +14 -13
- package/src/config.ts +6 -1
- package/src/index.ts +270 -339
- package/src/repositories/entity.repository.ts +30 -42
- package/src/repositories/memory.repository.ts +105 -181
- package/src/repositories/relation.repository.ts +33 -41
- package/src/services/embedding.ts +9 -1
- package/src/services/qdrant.ts +30 -13
- package/src/utils/filter.ts +20 -0
- package/tsconfig.json +1 -1
package/src/index.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { entityRepository } from "./repositories/entity.repository.ts";
|
|
|
6
6
|
import { relationRepository } from "./repositories/relation.repository.ts";
|
|
7
7
|
import { config, hasRemoteReranking } from "./config.ts";
|
|
8
8
|
import { rerankDocuments } from "./services/reranking.ts";
|
|
9
|
+
import { warmup } from "./services/embedding.ts";
|
|
9
10
|
|
|
10
11
|
let collectionsReady = false;
|
|
11
12
|
|
|
@@ -14,6 +15,15 @@ const server = new FastMCP({
|
|
|
14
15
|
version: "0.1.0",
|
|
15
16
|
});
|
|
16
17
|
|
|
18
|
+
function text(content: string) {
|
|
19
|
+
return { content: [{ type: "text" as const, text: content }] };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function toolErr(err: unknown) {
|
|
23
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
24
|
+
return text(`Error: ${message}`);
|
|
25
|
+
}
|
|
26
|
+
|
|
17
27
|
// ── Memory Tools ──────────────────────────────────────────────────────────────
|
|
18
28
|
|
|
19
29
|
server.addTool({
|
|
@@ -26,15 +36,21 @@ server.addTool({
|
|
|
26
36
|
source: z.string().optional(),
|
|
27
37
|
project: z.string().optional(),
|
|
28
38
|
tags: z.array(z.string()).optional(),
|
|
39
|
+
importance: z.number().optional(),
|
|
40
|
+
sessionId: z.string().optional(),
|
|
29
41
|
})
|
|
30
42
|
.optional(),
|
|
31
43
|
}),
|
|
32
44
|
execute: async (args) => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
45
|
+
try {
|
|
46
|
+
const result = await memoryRepository.save(args.information, {
|
|
47
|
+
...args.metadata,
|
|
48
|
+
createdAt: new Date().toISOString(),
|
|
49
|
+
});
|
|
50
|
+
return text(`Saved memory: ${result.id}`);
|
|
51
|
+
} catch (err) {
|
|
52
|
+
return toolErr(err);
|
|
53
|
+
}
|
|
38
54
|
},
|
|
39
55
|
});
|
|
40
56
|
|
|
@@ -46,41 +62,34 @@ server.addTool({
|
|
|
46
62
|
project: z.string().optional(),
|
|
47
63
|
tags: z.array(z.string()).optional(),
|
|
48
64
|
limit: z.number().optional().default(5),
|
|
49
|
-
rerank: z.boolean().optional().default(
|
|
65
|
+
rerank: z.boolean().optional().default(hasRemoteReranking),
|
|
50
66
|
rerankTopK: z.number().optional().default(20),
|
|
51
67
|
}),
|
|
52
68
|
execute: async (args) => {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const results = await memoryRepository.search(args.query, {
|
|
57
|
-
project: args.project,
|
|
58
|
-
tags: args.tags,
|
|
59
|
-
limit: args.rerank && hasRemoteReranking ? rerankTopK : limit,
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
if (args.rerank && hasRemoteReranking && results.length > limit) {
|
|
63
|
-
const docs = results.map((r) => r.text);
|
|
64
|
-
const reranked = await rerankDocuments(args.query, docs, limit);
|
|
65
|
-
const rerankedResults = reranked
|
|
66
|
-
.map((r) => ({ ...results[r.index], score: r.relevanceScore }))
|
|
67
|
-
.filter((r) => r.text !== undefined);
|
|
68
|
-
|
|
69
|
-
return {
|
|
70
|
-
content: [
|
|
71
|
-
{
|
|
72
|
-
type: "text" as const,
|
|
73
|
-
text: JSON.stringify(rerankedResults, null, 2),
|
|
74
|
-
},
|
|
75
|
-
],
|
|
76
|
-
};
|
|
77
|
-
}
|
|
69
|
+
try {
|
|
70
|
+
const limit = args.limit ?? 5;
|
|
71
|
+
const rerankTopK = args.rerankTopK ?? 20;
|
|
78
72
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
73
|
+
const results = await memoryRepository.search(args.query, {
|
|
74
|
+
project: args.project,
|
|
75
|
+
tags: args.tags,
|
|
76
|
+
limit: args.rerank && hasRemoteReranking ? rerankTopK : limit,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
if (args.rerank && hasRemoteReranking && results.length > limit) {
|
|
80
|
+
const docs = results.map((r) => r.text);
|
|
81
|
+
const reranked = await rerankDocuments(args.query, docs, limit);
|
|
82
|
+
const rerankedResults = reranked
|
|
83
|
+
.map((r) => ({ ...results[r.index], score: r.relevanceScore }))
|
|
84
|
+
.filter((r) => r.text !== undefined);
|
|
85
|
+
|
|
86
|
+
return text(JSON.stringify(rerankedResults, null, 2));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return text(JSON.stringify(results, null, 2));
|
|
90
|
+
} catch (err) {
|
|
91
|
+
return toolErr(err);
|
|
92
|
+
}
|
|
84
93
|
},
|
|
85
94
|
});
|
|
86
95
|
|
|
@@ -93,18 +102,64 @@ server.addTool({
|
|
|
93
102
|
tags: z.array(z.string()).optional(),
|
|
94
103
|
}),
|
|
95
104
|
execute: async (args) => {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
105
|
+
try {
|
|
106
|
+
if (args.ids && args.ids.length > 0) {
|
|
107
|
+
await memoryRepository.delete(args.ids);
|
|
108
|
+
return text(`Deleted ${args.ids.length} memories`);
|
|
109
|
+
}
|
|
110
|
+
if (args.project || args.tags) {
|
|
111
|
+
await memoryRepository.deleteByFilter({
|
|
112
|
+
project: args.project,
|
|
113
|
+
tags: args.tags,
|
|
114
|
+
});
|
|
115
|
+
return text("Deleted memories by filter");
|
|
116
|
+
}
|
|
117
|
+
return text("No IDs or filter provided");
|
|
118
|
+
} catch (err) {
|
|
119
|
+
return toolErr(err);
|
|
99
120
|
}
|
|
100
|
-
|
|
101
|
-
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
server.addTool({
|
|
125
|
+
name: "memory_compact",
|
|
126
|
+
description: "Compact memories by session or project, removing low-importance ones (importance < 0.6)",
|
|
127
|
+
parameters: z.object({
|
|
128
|
+
project: z.string().optional(),
|
|
129
|
+
sessionId: z.string().optional(),
|
|
130
|
+
importanceThreshold: z.number().optional().default(0.6),
|
|
131
|
+
}),
|
|
132
|
+
execute: async (args) => {
|
|
133
|
+
try {
|
|
134
|
+
const result = await memoryRepository.compact({
|
|
102
135
|
project: args.project,
|
|
103
|
-
|
|
136
|
+
sessionId: args.sessionId,
|
|
137
|
+
importanceThreshold: args.importanceThreshold,
|
|
104
138
|
});
|
|
105
|
-
return
|
|
139
|
+
return text(`Compacted ${result.compacted} memories`);
|
|
140
|
+
} catch (err) {
|
|
141
|
+
return toolErr(err);
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
server.addTool({
|
|
147
|
+
name: "memory_prune",
|
|
148
|
+
description: "Delete memories below an importance threshold",
|
|
149
|
+
parameters: z.object({
|
|
150
|
+
threshold: z.number(),
|
|
151
|
+
project: z.string().optional(),
|
|
152
|
+
}),
|
|
153
|
+
execute: async (args) => {
|
|
154
|
+
try {
|
|
155
|
+
const result = await memoryRepository.prune({
|
|
156
|
+
threshold: args.threshold,
|
|
157
|
+
project: args.project,
|
|
158
|
+
});
|
|
159
|
+
return text(`Pruned ${result.pruned} memories`);
|
|
160
|
+
} catch (err) {
|
|
161
|
+
return toolErr(err);
|
|
106
162
|
}
|
|
107
|
-
return "No IDs or filter provided";
|
|
108
163
|
},
|
|
109
164
|
});
|
|
110
165
|
|
|
@@ -120,13 +175,17 @@ server.addTool({
|
|
|
120
175
|
metadata: z.record(z.unknown()).optional(),
|
|
121
176
|
}),
|
|
122
177
|
execute: async (args) => {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
178
|
+
try {
|
|
179
|
+
const result = await entityRepository.create(
|
|
180
|
+
args.name,
|
|
181
|
+
args.entityType,
|
|
182
|
+
args.observations ?? [],
|
|
183
|
+
args.metadata,
|
|
184
|
+
);
|
|
185
|
+
return text(`Created entity: ${result.name} (${result.id})`);
|
|
186
|
+
} catch (err) {
|
|
187
|
+
return toolErr(err);
|
|
188
|
+
}
|
|
130
189
|
},
|
|
131
190
|
});
|
|
132
191
|
|
|
@@ -139,16 +198,16 @@ server.addTool({
|
|
|
139
198
|
limit: z.number().optional().default(10),
|
|
140
199
|
}),
|
|
141
200
|
execute: async (args) => {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
201
|
+
try {
|
|
202
|
+
const results = await entityRepository.search(
|
|
203
|
+
args.query,
|
|
204
|
+
args.entityType,
|
|
205
|
+
args.limit,
|
|
206
|
+
);
|
|
207
|
+
return text(JSON.stringify(results, null, 2));
|
|
208
|
+
} catch (err) {
|
|
209
|
+
return toolErr(err);
|
|
210
|
+
}
|
|
152
211
|
},
|
|
153
212
|
});
|
|
154
213
|
|
|
@@ -160,12 +219,16 @@ server.addTool({
|
|
|
160
219
|
observations: z.array(z.string()),
|
|
161
220
|
}),
|
|
162
221
|
execute: async (args) => {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
222
|
+
try {
|
|
223
|
+
const result = await entityRepository.addObservations(
|
|
224
|
+
args.name,
|
|
225
|
+
args.observations,
|
|
226
|
+
);
|
|
227
|
+
if (!result) return text(`Entity not found: ${args.name}`);
|
|
228
|
+
return text(`Added observations to: ${result.name}`);
|
|
229
|
+
} catch (err) {
|
|
230
|
+
return toolErr(err);
|
|
231
|
+
}
|
|
169
232
|
},
|
|
170
233
|
});
|
|
171
234
|
|
|
@@ -176,13 +239,13 @@ server.addTool({
|
|
|
176
239
|
name: z.string(),
|
|
177
240
|
}),
|
|
178
241
|
execute: async (args) => {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
242
|
+
try {
|
|
243
|
+
const result = await entityRepository.get(args.name);
|
|
244
|
+
if (!result) return text(`Entity not found: ${args.name}`);
|
|
245
|
+
return text(JSON.stringify(result, null, 2));
|
|
246
|
+
} catch (err) {
|
|
247
|
+
return toolErr(err);
|
|
248
|
+
}
|
|
186
249
|
},
|
|
187
250
|
});
|
|
188
251
|
|
|
@@ -198,13 +261,19 @@ server.addTool({
|
|
|
198
261
|
metadata: z.record(z.unknown()).optional(),
|
|
199
262
|
}),
|
|
200
263
|
execute: async (args) => {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
264
|
+
try {
|
|
265
|
+
const result = await relationRepository.create(
|
|
266
|
+
args.from,
|
|
267
|
+
args.relationType,
|
|
268
|
+
args.to,
|
|
269
|
+
args.metadata,
|
|
270
|
+
);
|
|
271
|
+
return text(
|
|
272
|
+
`Created relation: ${args.from} --[${args.relationType}]--> ${args.to} (${result.id})`,
|
|
273
|
+
);
|
|
274
|
+
} catch (err) {
|
|
275
|
+
return toolErr(err);
|
|
276
|
+
}
|
|
208
277
|
},
|
|
209
278
|
});
|
|
210
279
|
|
|
@@ -218,12 +287,12 @@ server.addTool({
|
|
|
218
287
|
limit: z.number().optional().default(20),
|
|
219
288
|
}),
|
|
220
289
|
execute: async (args) => {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}
|
|
290
|
+
try {
|
|
291
|
+
const results = await relationRepository.search(args, args.limit);
|
|
292
|
+
return text(JSON.stringify(results, null, 2));
|
|
293
|
+
} catch (err) {
|
|
294
|
+
return toolErr(err);
|
|
295
|
+
}
|
|
227
296
|
},
|
|
228
297
|
});
|
|
229
298
|
|
|
@@ -236,75 +305,16 @@ server.addTool({
|
|
|
236
305
|
relationType: z.string().optional(),
|
|
237
306
|
}),
|
|
238
307
|
execute: async (args) => {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
})
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
server.addTool({
|
|
247
|
-
name: "memory_save_batch",
|
|
248
|
-
description: "Save multiple memories in a single call for efficiency",
|
|
249
|
-
parameters: z.object({
|
|
250
|
-
items: z.array(
|
|
251
|
-
z.object({
|
|
252
|
-
information: z.string(),
|
|
253
|
-
metadata: z
|
|
254
|
-
.object({
|
|
255
|
-
source: z.string().optional(),
|
|
256
|
-
project: z.string().optional(),
|
|
257
|
-
tags: z.array(z.string()).optional(),
|
|
258
|
-
})
|
|
259
|
-
.optional(),
|
|
260
|
-
}),
|
|
261
|
-
),
|
|
262
|
-
}),
|
|
263
|
-
execute: async (args) => {
|
|
264
|
-
const items = args.items.map((item) => ({
|
|
265
|
-
text: item.information,
|
|
266
|
-
metadata: item.metadata,
|
|
267
|
-
}));
|
|
268
|
-
const results = await memoryRepository.saveBatch(items);
|
|
269
|
-
return {
|
|
270
|
-
content: [
|
|
271
|
-
{
|
|
272
|
-
type: "text" as const,
|
|
273
|
-
text: JSON.stringify(
|
|
274
|
-
{ saved: results.length, ids: results.map((r) => r.id) },
|
|
275
|
-
null,
|
|
276
|
-
2,
|
|
277
|
-
),
|
|
278
|
-
},
|
|
279
|
-
],
|
|
280
|
-
};
|
|
281
|
-
},
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
server.addTool({
|
|
285
|
-
name: "memory_search_batch",
|
|
286
|
-
description: "Search multiple queries at once for efficiency",
|
|
287
|
-
parameters: z.object({
|
|
288
|
-
queries: z.array(
|
|
289
|
-
z.object({
|
|
290
|
-
query: z.string(),
|
|
291
|
-
project: z.string().optional(),
|
|
292
|
-
tags: z.array(z.string()).optional(),
|
|
293
|
-
limit: z.number().optional().default(5),
|
|
294
|
-
}),
|
|
295
|
-
),
|
|
296
|
-
}),
|
|
297
|
-
execute: async (args) => {
|
|
298
|
-
const results = await memoryRepository.searchBatch(args.queries);
|
|
299
|
-
return {
|
|
300
|
-
content: [
|
|
301
|
-
{ type: "text" as const, text: JSON.stringify(results, null, 2) },
|
|
302
|
-
],
|
|
303
|
-
};
|
|
308
|
+
try {
|
|
309
|
+
await relationRepository.delete(args);
|
|
310
|
+
return text("Relations deleted");
|
|
311
|
+
} catch (err) {
|
|
312
|
+
return toolErr(err);
|
|
313
|
+
}
|
|
304
314
|
},
|
|
305
315
|
});
|
|
306
316
|
|
|
307
|
-
// ── Memory Update
|
|
317
|
+
// ── Memory Update ─────────────────────────────────────────────────────────────
|
|
308
318
|
|
|
309
319
|
server.addTool({
|
|
310
320
|
name: "memory_update",
|
|
@@ -317,37 +327,22 @@ server.addTool({
|
|
|
317
327
|
source: z.string().optional(),
|
|
318
328
|
project: z.string().optional(),
|
|
319
329
|
tags: z.array(z.string()).optional(),
|
|
330
|
+
importance: z.number().optional(),
|
|
331
|
+
sessionId: z.string().optional(),
|
|
320
332
|
})
|
|
321
333
|
.optional(),
|
|
322
334
|
}),
|
|
323
335
|
execute: async (args) => {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
}
|
|
334
|
-
},
|
|
335
|
-
});
|
|
336
|
-
|
|
337
|
-
server.addTool({
|
|
338
|
-
name: "memory_get",
|
|
339
|
-
description: "Get a single memory by its ID",
|
|
340
|
-
parameters: z.object({
|
|
341
|
-
id: z.string(),
|
|
342
|
-
}),
|
|
343
|
-
execute: async (args) => {
|
|
344
|
-
const result = await memoryRepository.getById(args.id);
|
|
345
|
-
if (!result) return `Memory not found: ${args.id}`;
|
|
346
|
-
return {
|
|
347
|
-
content: [
|
|
348
|
-
{ type: "text" as const, text: JSON.stringify(result, null, 2) },
|
|
349
|
-
],
|
|
350
|
-
};
|
|
336
|
+
try {
|
|
337
|
+
const result = await memoryRepository.update(args.id, {
|
|
338
|
+
text: args.text,
|
|
339
|
+
metadata: args.metadata,
|
|
340
|
+
});
|
|
341
|
+
if (!result) return text(`Memory not found: ${args.id}`);
|
|
342
|
+
return text(JSON.stringify(result, null, 2));
|
|
343
|
+
} catch (err) {
|
|
344
|
+
return toolErr(err);
|
|
345
|
+
}
|
|
351
346
|
},
|
|
352
347
|
});
|
|
353
348
|
|
|
@@ -363,20 +358,17 @@ server.addTool({
|
|
|
363
358
|
offset: z.string().optional(),
|
|
364
359
|
}),
|
|
365
360
|
execute: async (args) => {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
},
|
|
378
|
-
],
|
|
379
|
-
};
|
|
361
|
+
try {
|
|
362
|
+
const { memories, offset } = await memoryRepository.scroll({
|
|
363
|
+
project: args.project,
|
|
364
|
+
tags: args.tags,
|
|
365
|
+
limit: args.limit,
|
|
366
|
+
offset: args.offset,
|
|
367
|
+
});
|
|
368
|
+
return text(JSON.stringify({ memories, nextOffset: offset }, null, 2));
|
|
369
|
+
} catch (err) {
|
|
370
|
+
return toolErr(err);
|
|
371
|
+
}
|
|
380
372
|
},
|
|
381
373
|
});
|
|
382
374
|
|
|
@@ -385,55 +377,12 @@ server.addTool({
|
|
|
385
377
|
description: "Get statistics about the memories collection",
|
|
386
378
|
parameters: z.object({}),
|
|
387
379
|
execute: async () => {
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
}
|
|
394
|
-
},
|
|
395
|
-
});
|
|
396
|
-
|
|
397
|
-
// ── Memory Import / Export ────────────────────────────────────────────────────
|
|
398
|
-
|
|
399
|
-
server.addTool({
|
|
400
|
-
name: "memory_export",
|
|
401
|
-
description: "Export all memories as JSON",
|
|
402
|
-
parameters: z.object({}),
|
|
403
|
-
execute: async () => {
|
|
404
|
-
const memories = await memoryRepository.export();
|
|
405
|
-
return {
|
|
406
|
-
content: [
|
|
407
|
-
{ type: "text" as const, text: JSON.stringify(memories, null, 2) },
|
|
408
|
-
],
|
|
409
|
-
};
|
|
410
|
-
},
|
|
411
|
-
});
|
|
412
|
-
|
|
413
|
-
server.addTool({
|
|
414
|
-
name: "memory_import",
|
|
415
|
-
description: "Import memories from JSON array",
|
|
416
|
-
parameters: z.object({
|
|
417
|
-
items: z.array(
|
|
418
|
-
z.object({
|
|
419
|
-
text: z.string(),
|
|
420
|
-
metadata: z
|
|
421
|
-
.object({
|
|
422
|
-
source: z.string().optional(),
|
|
423
|
-
project: z.string().optional(),
|
|
424
|
-
tags: z.array(z.string()).optional(),
|
|
425
|
-
})
|
|
426
|
-
.optional(),
|
|
427
|
-
}),
|
|
428
|
-
),
|
|
429
|
-
}),
|
|
430
|
-
execute: async (args) => {
|
|
431
|
-
const result = await memoryRepository.import(args.items);
|
|
432
|
-
return {
|
|
433
|
-
content: [
|
|
434
|
-
{ type: "text" as const, text: JSON.stringify(result, null, 2) },
|
|
435
|
-
],
|
|
436
|
-
};
|
|
380
|
+
try {
|
|
381
|
+
const stats = await memoryRepository.stats();
|
|
382
|
+
return text(JSON.stringify(stats, null, 2));
|
|
383
|
+
} catch (err) {
|
|
384
|
+
return toolErr(err);
|
|
385
|
+
}
|
|
437
386
|
},
|
|
438
387
|
});
|
|
439
388
|
|
|
@@ -449,17 +398,17 @@ server.addTool({
|
|
|
449
398
|
metadata: z.record(z.unknown()).optional(),
|
|
450
399
|
}),
|
|
451
400
|
execute: async (args) => {
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
}
|
|
401
|
+
try {
|
|
402
|
+
const result = await entityRepository.update(args.id, {
|
|
403
|
+
name: args.name,
|
|
404
|
+
entityType: args.entityType,
|
|
405
|
+
metadata: args.metadata,
|
|
406
|
+
});
|
|
407
|
+
if (!result) return text(`Entity not found: ${args.id}`);
|
|
408
|
+
return text(JSON.stringify(result, null, 2));
|
|
409
|
+
} catch (err) {
|
|
410
|
+
return toolErr(err);
|
|
411
|
+
}
|
|
463
412
|
},
|
|
464
413
|
});
|
|
465
414
|
|
|
@@ -468,12 +417,12 @@ server.addTool({
|
|
|
468
417
|
description: "Get statistics about the entities collection",
|
|
469
418
|
parameters: z.object({}),
|
|
470
419
|
execute: async () => {
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
}
|
|
420
|
+
try {
|
|
421
|
+
const stats = await entityRepository.stats();
|
|
422
|
+
return text(JSON.stringify(stats, null, 2));
|
|
423
|
+
} catch (err) {
|
|
424
|
+
return toolErr(err);
|
|
425
|
+
}
|
|
477
426
|
},
|
|
478
427
|
});
|
|
479
428
|
|
|
@@ -482,12 +431,12 @@ server.addTool({
|
|
|
482
431
|
description: "Get statistics about the relations collection",
|
|
483
432
|
parameters: z.object({}),
|
|
484
433
|
execute: async () => {
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
}
|
|
434
|
+
try {
|
|
435
|
+
const stats = await relationRepository.stats();
|
|
436
|
+
return text(JSON.stringify(stats, null, 2));
|
|
437
|
+
} catch (err) {
|
|
438
|
+
return toolErr(err);
|
|
439
|
+
}
|
|
491
440
|
},
|
|
492
441
|
});
|
|
493
442
|
|
|
@@ -502,49 +451,17 @@ server.addTool({
|
|
|
502
451
|
tags: z.array(z.string()).optional(),
|
|
503
452
|
}),
|
|
504
453
|
execute: async (args) => {
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
})
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
name: "memory_notes",
|
|
517
|
-
description: "Save multiple notes at once (batch lightweight memory saves)",
|
|
518
|
-
parameters: z.object({
|
|
519
|
-
notes: z.array(
|
|
520
|
-
z.object({
|
|
521
|
-
content: z.string(),
|
|
522
|
-
project: z.string().optional(),
|
|
523
|
-
tags: z.array(z.string()).optional(),
|
|
524
|
-
}),
|
|
525
|
-
),
|
|
526
|
-
}),
|
|
527
|
-
execute: async (args) => {
|
|
528
|
-
const items = args.notes.map((n) => ({
|
|
529
|
-
text: n.content,
|
|
530
|
-
metadata: {
|
|
531
|
-
project: n.project,
|
|
532
|
-
tags: [...(n.tags ?? []), "note"],
|
|
533
|
-
},
|
|
534
|
-
}));
|
|
535
|
-
const results = await memoryRepository.saveBatch(items);
|
|
536
|
-
return {
|
|
537
|
-
content: [
|
|
538
|
-
{
|
|
539
|
-
type: "text" as const,
|
|
540
|
-
text: JSON.stringify(
|
|
541
|
-
{ saved: results.length, ids: results.map((r) => r.id) },
|
|
542
|
-
null,
|
|
543
|
-
2,
|
|
544
|
-
),
|
|
545
|
-
},
|
|
546
|
-
],
|
|
547
|
-
};
|
|
454
|
+
try {
|
|
455
|
+
const tags = [...(args.tags ?? []), "note"];
|
|
456
|
+
const result = await memoryRepository.save(args.content, {
|
|
457
|
+
project: args.project,
|
|
458
|
+
tags,
|
|
459
|
+
createdAt: new Date().toISOString(),
|
|
460
|
+
});
|
|
461
|
+
return text(`Saved note: ${result.id}`);
|
|
462
|
+
} catch (err) {
|
|
463
|
+
return toolErr(err);
|
|
464
|
+
}
|
|
548
465
|
},
|
|
549
466
|
});
|
|
550
467
|
|
|
@@ -558,19 +475,16 @@ server.addTool({
|
|
|
558
475
|
offset: z.string().optional(),
|
|
559
476
|
}),
|
|
560
477
|
execute: async (args) => {
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
},
|
|
572
|
-
],
|
|
573
|
-
};
|
|
478
|
+
try {
|
|
479
|
+
const { entities, offset } = await entityRepository.list(
|
|
480
|
+
args.entityType,
|
|
481
|
+
args.limit,
|
|
482
|
+
args.offset,
|
|
483
|
+
);
|
|
484
|
+
return text(JSON.stringify({ entities, nextOffset: offset }, null, 2));
|
|
485
|
+
} catch (err) {
|
|
486
|
+
return toolErr(err);
|
|
487
|
+
}
|
|
574
488
|
},
|
|
575
489
|
});
|
|
576
490
|
|
|
@@ -584,19 +498,16 @@ server.addTool({
|
|
|
584
498
|
offset: z.string().optional(),
|
|
585
499
|
}),
|
|
586
500
|
execute: async (args) => {
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
},
|
|
598
|
-
],
|
|
599
|
-
};
|
|
501
|
+
try {
|
|
502
|
+
const { relations, offset } = await relationRepository.list(
|
|
503
|
+
args.relationType,
|
|
504
|
+
args.limit,
|
|
505
|
+
args.offset,
|
|
506
|
+
);
|
|
507
|
+
return text(JSON.stringify({ relations, nextOffset: offset }, null, 2));
|
|
508
|
+
} catch (err) {
|
|
509
|
+
return toolErr(err);
|
|
510
|
+
}
|
|
600
511
|
},
|
|
601
512
|
});
|
|
602
513
|
|
|
@@ -609,12 +520,17 @@ async function init(retries = 10, delayMs = 3000) {
|
|
|
609
520
|
await entityRepository.init();
|
|
610
521
|
await relationRepository.init();
|
|
611
522
|
collectionsReady = true;
|
|
612
|
-
console.
|
|
523
|
+
console.log("Collections initialized");
|
|
524
|
+
try {
|
|
525
|
+
await warmup();
|
|
526
|
+
} catch (err) {
|
|
527
|
+
console.warn("Embedding warmup failed (non-fatal):", err);
|
|
528
|
+
}
|
|
613
529
|
return;
|
|
614
530
|
} catch (err) {
|
|
615
531
|
if (attempt === retries) {
|
|
616
532
|
console.error(`Failed to initialize collections after ${retries} attempts:`, err);
|
|
617
|
-
|
|
533
|
+
process.exit(1);
|
|
618
534
|
}
|
|
619
535
|
console.error(`Collection init attempt ${attempt}/${retries} failed, retrying in ${delayMs / 1000}s...`);
|
|
620
536
|
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
@@ -646,7 +562,7 @@ if (config.transport === "http") {
|
|
|
646
562
|
res.writeHead(404);
|
|
647
563
|
res.end();
|
|
648
564
|
}).listen(healthPort, () => {
|
|
649
|
-
console.
|
|
565
|
+
console.log(
|
|
650
566
|
`[grove] Health check server on http://localhost:${healthPort}`,
|
|
651
567
|
);
|
|
652
568
|
});
|
|
@@ -655,9 +571,24 @@ if (config.transport === "http") {
|
|
|
655
571
|
transportType: "httpStream",
|
|
656
572
|
httpStream: { endpoint: "/mcp", port: config.port },
|
|
657
573
|
});
|
|
658
|
-
console.
|
|
574
|
+
console.log(
|
|
659
575
|
`[grove] HTTP server listening on http://localhost:${config.port}/mcp`,
|
|
660
576
|
);
|
|
661
577
|
} else {
|
|
662
578
|
server.start({ transportType: "stdio" });
|
|
663
579
|
}
|
|
580
|
+
|
|
581
|
+
function shutdown(signal: string) {
|
|
582
|
+
console.error(`[grove] Received ${signal}, shutting down...`);
|
|
583
|
+
server.stop().then(() => {
|
|
584
|
+
console.error("[grove] Server stopped");
|
|
585
|
+
process.exit(0);
|
|
586
|
+
});
|
|
587
|
+
setTimeout(() => {
|
|
588
|
+
console.error("[grove] Force exit after timeout");
|
|
589
|
+
process.exit(1);
|
|
590
|
+
}, 5000);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
process.on("SIGTERM", () => shutdown("SIGTERM"));
|
|
594
|
+
process.on("SIGINT", () => shutdown("SIGINT"));
|