@miniaxolotl/grove 0.2.4

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.
Files changed (46) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +21 -0
  3. package/README.md +113 -0
  4. package/dist/config.d.ts +27 -0
  5. package/dist/config.d.ts.map +1 -0
  6. package/dist/config.js +27 -0
  7. package/dist/config.js.map +1 -0
  8. package/dist/index.d.ts +2 -0
  9. package/dist/index.d.ts.map +1 -0
  10. package/dist/index.js +580 -0
  11. package/dist/index.js.map +1 -0
  12. package/dist/repositories/entity.repository.d.ts +44 -0
  13. package/dist/repositories/entity.repository.d.ts.map +1 -0
  14. package/dist/repositories/entity.repository.js +203 -0
  15. package/dist/repositories/entity.repository.js.map +1 -0
  16. package/dist/repositories/memory.repository.d.ts +78 -0
  17. package/dist/repositories/memory.repository.d.ts.map +1 -0
  18. package/dist/repositories/memory.repository.js +283 -0
  19. package/dist/repositories/memory.repository.js.map +1 -0
  20. package/dist/repositories/relation.repository.d.ts +38 -0
  21. package/dist/repositories/relation.repository.d.ts.map +1 -0
  22. package/dist/repositories/relation.repository.js +113 -0
  23. package/dist/repositories/relation.repository.js.map +1 -0
  24. package/dist/services/embedding.d.ts +3 -0
  25. package/dist/services/embedding.d.ts.map +1 -0
  26. package/dist/services/embedding.js +107 -0
  27. package/dist/services/embedding.js.map +1 -0
  28. package/dist/services/qdrant.d.ts +35 -0
  29. package/dist/services/qdrant.d.ts.map +1 -0
  30. package/dist/services/qdrant.js +100 -0
  31. package/dist/services/qdrant.js.map +1 -0
  32. package/dist/services/reranking.d.ts +6 -0
  33. package/dist/services/reranking.d.ts.map +1 -0
  34. package/dist/services/reranking.js +24 -0
  35. package/dist/services/reranking.js.map +1 -0
  36. package/eslint.config.js +1 -0
  37. package/package.json +56 -0
  38. package/src/config.ts +27 -0
  39. package/src/index.ts +663 -0
  40. package/src/repositories/entity.repository.ts +265 -0
  41. package/src/repositories/memory.repository.ts +392 -0
  42. package/src/repositories/relation.repository.ts +151 -0
  43. package/src/services/embedding.ts +142 -0
  44. package/src/services/qdrant.ts +184 -0
  45. package/src/services/reranking.ts +40 -0
  46. package/tsconfig.json +10 -0
package/src/index.ts ADDED
@@ -0,0 +1,663 @@
1
+ import { FastMCP } from "fastmcp";
2
+ import { z } from "zod";
3
+ import { createServer } from "node:http";
4
+ import { memoryRepository } from "./repositories/memory.repository.ts";
5
+ import { entityRepository } from "./repositories/entity.repository.ts";
6
+ import { relationRepository } from "./repositories/relation.repository.ts";
7
+ import { config, hasRemoteReranking } from "./config.ts";
8
+ import { rerankDocuments } from "./services/reranking.ts";
9
+
10
+ let collectionsReady = false;
11
+
12
+ const server = new FastMCP({
13
+ name: "grove",
14
+ version: "0.1.0",
15
+ });
16
+
17
+ // ── Memory Tools ──────────────────────────────────────────────────────────────
18
+
19
+ server.addTool({
20
+ name: "memory_save",
21
+ description: "Save a memory document to semantic storage",
22
+ parameters: z.object({
23
+ information: z.string(),
24
+ metadata: z
25
+ .object({
26
+ source: z.string().optional(),
27
+ project: z.string().optional(),
28
+ tags: z.array(z.string()).optional(),
29
+ })
30
+ .optional(),
31
+ }),
32
+ execute: async (args) => {
33
+ const result = await memoryRepository.save(args.information, {
34
+ ...args.metadata,
35
+ createdAt: new Date().toISOString(),
36
+ });
37
+ return `Saved memory: ${result.id}`;
38
+ },
39
+ });
40
+
41
+ server.addTool({
42
+ name: "memory_search",
43
+ description: "Search memories by semantic similarity with optional reranking",
44
+ parameters: z.object({
45
+ query: z.string(),
46
+ project: z.string().optional(),
47
+ tags: z.array(z.string()).optional(),
48
+ limit: z.number().optional().default(5),
49
+ rerank: z.boolean().optional().default(true),
50
+ rerankTopK: z.number().optional().default(20),
51
+ }),
52
+ execute: async (args) => {
53
+ const limit = args.limit ?? 5;
54
+ const rerankTopK = args.rerankTopK ?? 20;
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
+ }
78
+
79
+ return {
80
+ content: [
81
+ { type: "text" as const, text: JSON.stringify(results, null, 2) },
82
+ ],
83
+ };
84
+ },
85
+ });
86
+
87
+ server.addTool({
88
+ name: "memory_forget",
89
+ description: "Delete memories by IDs or filter",
90
+ parameters: z.object({
91
+ ids: z.array(z.string()).optional(),
92
+ project: z.string().optional(),
93
+ tags: z.array(z.string()).optional(),
94
+ }),
95
+ execute: async (args) => {
96
+ if (args.ids && args.ids.length > 0) {
97
+ await memoryRepository.delete(args.ids);
98
+ return `Deleted ${args.ids.length} memories`;
99
+ }
100
+ if (args.project || args.tags) {
101
+ await memoryRepository.deleteByFilter({
102
+ project: args.project,
103
+ tags: args.tags,
104
+ });
105
+ return "Deleted memories by filter";
106
+ }
107
+ return "No IDs or filter provided";
108
+ },
109
+ });
110
+
111
+ // ── Entity Tools ──────────────────────────────────────────────────────────────
112
+
113
+ server.addTool({
114
+ name: "entity_create",
115
+ description: "Create an entity",
116
+ parameters: z.object({
117
+ name: z.string(),
118
+ entityType: z.string(),
119
+ observations: z.array(z.string()).optional().default([]),
120
+ metadata: z.record(z.unknown()).optional(),
121
+ }),
122
+ execute: async (args) => {
123
+ const result = await entityRepository.create(
124
+ args.name,
125
+ args.entityType,
126
+ args.observations ?? [],
127
+ args.metadata,
128
+ );
129
+ return `Created entity: ${result.name} (${result.id})`;
130
+ },
131
+ });
132
+
133
+ server.addTool({
134
+ name: "entity_search",
135
+ description: "Find entities by name or type",
136
+ parameters: z.object({
137
+ query: z.string().optional(),
138
+ entityType: z.string().optional(),
139
+ limit: z.number().optional().default(10),
140
+ }),
141
+ execute: async (args) => {
142
+ const results = await entityRepository.search(
143
+ args.query,
144
+ args.entityType,
145
+ args.limit,
146
+ );
147
+ return {
148
+ content: [
149
+ { type: "text" as const, text: JSON.stringify(results, null, 2) },
150
+ ],
151
+ };
152
+ },
153
+ });
154
+
155
+ server.addTool({
156
+ name: "entity_add_observations",
157
+ description: "Add observations to an existing entity",
158
+ parameters: z.object({
159
+ name: z.string(),
160
+ observations: z.array(z.string()),
161
+ }),
162
+ execute: async (args) => {
163
+ const result = await entityRepository.addObservations(
164
+ args.name,
165
+ args.observations,
166
+ );
167
+ if (!result) return `Entity not found: ${args.name}`;
168
+ return `Added observations to: ${result.name}`;
169
+ },
170
+ });
171
+
172
+ server.addTool({
173
+ name: "entity_get",
174
+ description: "Get full entity details by name",
175
+ parameters: z.object({
176
+ name: z.string(),
177
+ }),
178
+ execute: async (args) => {
179
+ const result = await entityRepository.get(args.name);
180
+ if (!result) return `Entity not found: ${args.name}`;
181
+ return {
182
+ content: [
183
+ { type: "text" as const, text: JSON.stringify(result, null, 2) },
184
+ ],
185
+ };
186
+ },
187
+ });
188
+
189
+ // ── Relation Tools ────────────────────────────────────────────────────────────
190
+
191
+ server.addTool({
192
+ name: "relation_create",
193
+ description: "Create a relation between two entities",
194
+ parameters: z.object({
195
+ from: z.string(),
196
+ relationType: z.string(),
197
+ to: z.string(),
198
+ metadata: z.record(z.unknown()).optional(),
199
+ }),
200
+ execute: async (args) => {
201
+ const result = await relationRepository.create(
202
+ args.from,
203
+ args.relationType,
204
+ args.to,
205
+ args.metadata,
206
+ );
207
+ return `Created relation: ${args.from} --[${args.relationType}]--> ${args.to} (${result.id})`;
208
+ },
209
+ });
210
+
211
+ server.addTool({
212
+ name: "relation_search",
213
+ description: "Find relations by source, target, or type",
214
+ parameters: z.object({
215
+ from: z.string().optional(),
216
+ to: z.string().optional(),
217
+ relationType: z.string().optional(),
218
+ limit: z.number().optional().default(20),
219
+ }),
220
+ execute: async (args) => {
221
+ const results = await relationRepository.search(args, args.limit);
222
+ return {
223
+ content: [
224
+ { type: "text" as const, text: JSON.stringify(results, null, 2) },
225
+ ],
226
+ };
227
+ },
228
+ });
229
+
230
+ server.addTool({
231
+ name: "relation_delete",
232
+ description: "Delete relations",
233
+ parameters: z.object({
234
+ from: z.string().optional(),
235
+ to: z.string().optional(),
236
+ relationType: z.string().optional(),
237
+ }),
238
+ execute: async (args) => {
239
+ await relationRepository.delete(args);
240
+ return "Relations deleted";
241
+ },
242
+ });
243
+
244
+ // ── Batch Memory Tools ─────────────────────────────────────────────────────────
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
+ };
304
+ },
305
+ });
306
+
307
+ // ── Memory Update / Get ────────────────────────────────────────────────────────
308
+
309
+ server.addTool({
310
+ name: "memory_update",
311
+ description: "Update text or metadata of an existing memory by ID",
312
+ parameters: z.object({
313
+ id: z.string(),
314
+ text: z.string().optional(),
315
+ metadata: z
316
+ .object({
317
+ source: z.string().optional(),
318
+ project: z.string().optional(),
319
+ tags: z.array(z.string()).optional(),
320
+ })
321
+ .optional(),
322
+ }),
323
+ execute: async (args) => {
324
+ const result = await memoryRepository.update(args.id, {
325
+ text: args.text,
326
+ metadata: args.metadata,
327
+ });
328
+ if (!result) return `Memory not found: ${args.id}`;
329
+ return {
330
+ content: [
331
+ { type: "text" as const, text: JSON.stringify(result, null, 2) },
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
+ };
351
+ },
352
+ });
353
+
354
+ // ── Memory Scroll / Stats ─────────────────────────────────────────────────────
355
+
356
+ server.addTool({
357
+ name: "memory_scroll",
358
+ description: "Paginate through memories with cursor-based pagination",
359
+ parameters: z.object({
360
+ project: z.string().optional(),
361
+ tags: z.array(z.string()).optional(),
362
+ limit: z.number().optional().default(100),
363
+ offset: z.string().optional(),
364
+ }),
365
+ execute: async (args) => {
366
+ const { memories, offset } = await memoryRepository.scroll({
367
+ project: args.project,
368
+ tags: args.tags,
369
+ limit: args.limit,
370
+ offset: args.offset,
371
+ });
372
+ return {
373
+ content: [
374
+ {
375
+ type: "text" as const,
376
+ text: JSON.stringify({ memories, nextOffset: offset }, null, 2),
377
+ },
378
+ ],
379
+ };
380
+ },
381
+ });
382
+
383
+ server.addTool({
384
+ name: "memory_stats",
385
+ description: "Get statistics about the memories collection",
386
+ parameters: z.object({}),
387
+ execute: async () => {
388
+ const stats = await memoryRepository.stats();
389
+ return {
390
+ content: [
391
+ { type: "text" as const, text: JSON.stringify(stats, null, 2) },
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
+ };
437
+ },
438
+ });
439
+
440
+ // ── Entity / Relation Stats ───────────────────────────────────────────────────
441
+
442
+ server.addTool({
443
+ name: "entity_update",
444
+ description: "Update an existing entity by ID",
445
+ parameters: z.object({
446
+ id: z.string(),
447
+ name: z.string().optional(),
448
+ entityType: z.string().optional(),
449
+ metadata: z.record(z.unknown()).optional(),
450
+ }),
451
+ execute: async (args) => {
452
+ const result = await entityRepository.update(args.id, {
453
+ name: args.name,
454
+ entityType: args.entityType,
455
+ metadata: args.metadata,
456
+ });
457
+ if (!result) return `Entity not found: ${args.id}`;
458
+ return {
459
+ content: [
460
+ { type: "text" as const, text: JSON.stringify(result, null, 2) },
461
+ ],
462
+ };
463
+ },
464
+ });
465
+
466
+ server.addTool({
467
+ name: "entity_stats",
468
+ description: "Get statistics about the entities collection",
469
+ parameters: z.object({}),
470
+ execute: async () => {
471
+ const stats = await entityRepository.stats();
472
+ return {
473
+ content: [
474
+ { type: "text" as const, text: JSON.stringify(stats, null, 2) },
475
+ ],
476
+ };
477
+ },
478
+ });
479
+
480
+ server.addTool({
481
+ name: "relation_stats",
482
+ description: "Get statistics about the relations collection",
483
+ parameters: z.object({}),
484
+ execute: async () => {
485
+ const stats = await relationRepository.stats();
486
+ return {
487
+ content: [
488
+ { type: "text" as const, text: JSON.stringify(stats, null, 2) },
489
+ ],
490
+ };
491
+ },
492
+ });
493
+
494
+ // ── List Tools ────────────────────────────────────────────────────────────────
495
+
496
+ server.addTool({
497
+ name: "memory_note",
498
+ description: "Quick-save a note (lightweight memory with auto-tag 'note')",
499
+ parameters: z.object({
500
+ content: z.string(),
501
+ project: z.string().optional(),
502
+ tags: z.array(z.string()).optional(),
503
+ }),
504
+ execute: async (args) => {
505
+ const tags = [...(args.tags ?? []), "note"];
506
+ const result = await memoryRepository.save(args.content, {
507
+ project: args.project,
508
+ tags,
509
+ createdAt: new Date().toISOString(),
510
+ });
511
+ return `Saved note: ${result.id}`;
512
+ },
513
+ });
514
+
515
+ server.addTool({
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
+ };
548
+ },
549
+ });
550
+
551
+ server.addTool({
552
+ name: "entity_list",
553
+ description:
554
+ "List all entities, optionally filtered by type, with pagination",
555
+ parameters: z.object({
556
+ entityType: z.string().optional(),
557
+ limit: z.number().optional().default(100),
558
+ offset: z.string().optional(),
559
+ }),
560
+ execute: async (args) => {
561
+ const { entities, offset } = await entityRepository.list(
562
+ args.entityType,
563
+ args.limit,
564
+ args.offset,
565
+ );
566
+ return {
567
+ content: [
568
+ {
569
+ type: "text" as const,
570
+ text: JSON.stringify({ entities, nextOffset: offset }, null, 2),
571
+ },
572
+ ],
573
+ };
574
+ },
575
+ });
576
+
577
+ server.addTool({
578
+ name: "relation_list",
579
+ description:
580
+ "List all relations, optionally filtered by type, with pagination",
581
+ parameters: z.object({
582
+ relationType: z.string().optional(),
583
+ limit: z.number().optional().default(100),
584
+ offset: z.string().optional(),
585
+ }),
586
+ execute: async (args) => {
587
+ const { relations, offset } = await relationRepository.list(
588
+ args.relationType,
589
+ args.limit,
590
+ args.offset,
591
+ );
592
+ return {
593
+ content: [
594
+ {
595
+ type: "text" as const,
596
+ text: JSON.stringify({ relations, nextOffset: offset }, null, 2),
597
+ },
598
+ ],
599
+ };
600
+ },
601
+ });
602
+
603
+ // ── Initialize collections on startup ────────────────────────────────────────
604
+
605
+ async function init(retries = 10, delayMs = 3000) {
606
+ for (let attempt = 1; attempt <= retries; attempt++) {
607
+ try {
608
+ await memoryRepository.init();
609
+ await entityRepository.init();
610
+ await relationRepository.init();
611
+ collectionsReady = true;
612
+ console.error("Collections initialized");
613
+ return;
614
+ } catch (err) {
615
+ if (attempt === retries) {
616
+ console.error(`Failed to initialize collections after ${retries} attempts:`, err);
617
+ return;
618
+ }
619
+ console.error(`Collection init attempt ${attempt}/${retries} failed, retrying in ${delayMs / 1000}s...`);
620
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
621
+ }
622
+ }
623
+ }
624
+
625
+ init();
626
+
627
+ if (config.transport === "http") {
628
+ // Health check server (separate port)
629
+ const healthPort = config.port + 1;
630
+ createServer((req, res) => {
631
+ if (req.url === "/ready") {
632
+ if (collectionsReady) {
633
+ res.writeHead(200, { "Content-Type": "application/json" });
634
+ res.end(JSON.stringify({ status: "ready" }));
635
+ } else {
636
+ res.writeHead(503, { "Content-Type": "application/json" });
637
+ res.end(JSON.stringify({ status: "initializing" }));
638
+ }
639
+ return;
640
+ }
641
+ if (req.url === "/health") {
642
+ res.writeHead(200, { "Content-Type": "application/json" });
643
+ res.end(JSON.stringify({ status: "healthy", uptime: process.uptime() }));
644
+ return;
645
+ }
646
+ res.writeHead(404);
647
+ res.end();
648
+ }).listen(healthPort, () => {
649
+ console.error(
650
+ `[grove] Health check server on http://localhost:${healthPort}`,
651
+ );
652
+ });
653
+
654
+ server.start({
655
+ transportType: "httpStream",
656
+ httpStream: { endpoint: "/mcp", port: config.port },
657
+ });
658
+ console.error(
659
+ `[grove] HTTP server listening on http://localhost:${config.port}/mcp`,
660
+ );
661
+ } else {
662
+ server.start({ transportType: "stdio" });
663
+ }