-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-stdio.test.ts
More file actions
562 lines (487 loc) · 21.2 KB
/
mcp-stdio.test.ts
File metadata and controls
562 lines (487 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/**
* Tests for EngramMCPStdioServer
*
* Covers:
* - MCP protocol handshake (initialize, ping, tools/list)
* - All differential tools: engram_link, engram_related, engram_timeline,
* engram_namespaces, engram_forget
* - Delegation to base MCPToolsAdapter tools
* - Error handling (unknown tool, missing memory)
*/
import { describe, it, expect, beforeEach } from 'vitest';
import { MemoryManager } from '../src/memory-manager';
import { EngramMCPStdioServer } from '../src/mcp-stdio';
// ── Helpers ───────────────────────────────────────────────────────────────────
function makeServer() {
const manager = new MemoryManager();
manager.start();
return new EngramMCPStdioServer(manager);
}
async function rpc(
server: EngramMCPStdioServer,
method: string,
params: Record<string, unknown> = {},
id: number | string = 1,
) {
return server.handleRequest({ jsonrpc: '2.0', id, method, params }) as Promise<
Record<string, unknown>
>;
}
async function storeMemory(
server: EngramMCPStdioServer,
content: string,
options: Record<string, unknown> = {},
) {
const resp = await rpc(server, 'tools/call', {
name: 'engram_store',
arguments: { content, type: 'semantic', ...options },
});
const result = resp['result'] as Record<string, unknown>;
const text = (result['content'] as Array<{ text: string }>)[0].text;
const parsed = JSON.parse(text) as { id: string };
return parsed.id;
}
// ── Protocol ──────────────────────────────────────────────────────────────────
describe('EngramMCPStdioServer — protocol', () => {
it('handles initialize handshake', async () => {
const server = makeServer();
const resp = await rpc(server, 'initialize', {
protocolVersion: '2024-11-05',
capabilities: {},
clientInfo: { name: 'test', version: '1.0' },
});
expect(resp['result']).toBeDefined();
const result = resp['result'] as Record<string, unknown>;
expect(result['protocolVersion']).toBe('2024-11-05');
expect((result['serverInfo'] as Record<string, unknown>)['name']).toBe('engram');
});
it('responds to ping', async () => {
const server = makeServer();
const resp = await rpc(server, 'ping');
expect(resp['result']).toEqual({});
});
it('returns null for notifications/initialized', async () => {
const server = makeServer();
const resp = await server.handleRequest({
jsonrpc: '2.0',
method: 'notifications/initialized',
});
expect(resp).toBeNull();
});
it('returns error for unknown method', async () => {
const server = makeServer();
const resp = await rpc(server, 'unknown/method') as Record<string, unknown>;
expect(resp['error']).toBeDefined();
expect((resp['error'] as Record<string, number>)['code']).toBe(-32601);
});
it('returns error for invalid JSON-RPC version', async () => {
const server = makeServer();
const resp = await server.handleRequest({
jsonrpc: '1.0', id: 1, method: 'ping', params: {},
}) as Record<string, unknown>;
expect(resp['error']).toBeDefined();
});
it('returns error for non-object body', async () => {
const server = makeServer();
const resp = await server.handleRequest('bad input') as Record<string, unknown>;
expect(resp['error']).toBeDefined();
});
});
// ── tools/list ────────────────────────────────────────────────────────────────
describe('EngramMCPStdioServer — tools/list', () => {
it('lists all base tools', async () => {
const server = makeServer();
const resp = await rpc(server, 'tools/list');
const tools = ((resp['result'] as Record<string, unknown>)['tools'] as Array<{ name: string }>);
const names = tools.map((t) => t.name);
expect(names).toContain('engram_store');
expect(names).toContain('engram_recall');
expect(names).toContain('engram_get');
expect(names).toContain('engram_update');
expect(names).toContain('engram_sweep');
expect(names).toContain('engram_stats');
expect(names).toContain('engram_consolidate');
});
it('lists all differential tools', async () => {
const server = makeServer();
const resp = await rpc(server, 'tools/list');
const tools = ((resp['result'] as Record<string, unknown>)['tools'] as Array<{ name: string }>);
const names = tools.map((t) => t.name);
expect(names).toContain('engram_link');
expect(names).toContain('engram_related');
expect(names).toContain('engram_timeline');
expect(names).toContain('engram_namespaces');
expect(names).toContain('engram_forget');
});
it('total tool count is 25 (7 base + 5 differential + 3 behavior + 10 three-layer)', async () => {
const server = makeServer();
const resp = await rpc(server, 'tools/list');
const tools = (resp['result'] as Record<string, unknown>)['tools'] as unknown[];
expect(tools.length).toBe(25);
});
});
// ── Base tools (delegation) ───────────────────────────────────────────────────
describe('EngramMCPStdioServer — base tool delegation', () => {
it('engram_store stores a memory and returns id', async () => {
const server = makeServer();
const id = await storeMemory(server, 'TypeScript is great for large codebases');
expect(typeof id).toBe('string');
expect(id.length).toBeGreaterThan(0);
});
it('engram_recall finds stored memory by keyword', async () => {
const server = makeServer();
await storeMemory(server, 'The Ebbinghaus forgetting curve models memory decay');
const resp = await rpc(server, 'tools/call', {
name: 'engram_recall',
arguments: { keywords: ['Ebbinghaus', 'decay'], limit: 5 },
});
const result = resp['result'] as Record<string, unknown>;
const text = (result['content'] as Array<{ text: string }>)[0].text;
expect(text).toContain('Ebbinghaus');
});
it('engram_get retrieves memory by id', async () => {
const server = makeServer();
const id = await storeMemory(server, 'Specific memory for retrieval');
const resp = await rpc(server, 'tools/call', {
name: 'engram_get',
arguments: { id },
});
const result = resp['result'] as Record<string, unknown>;
const text = (result['content'] as Array<{ text: string }>)[0].text;
expect(text).toContain('Specific memory for retrieval');
});
it('engram_stats returns statistics', async () => {
const server = makeServer();
await storeMemory(server, 'test 1');
await storeMemory(server, 'test 2');
const resp = await rpc(server, 'tools/call', {
name: 'engram_stats',
arguments: {},
});
const result = resp['result'] as Record<string, unknown>;
const text = (result['content'] as Array<{ text: string }>)[0].text;
const stats = JSON.parse(text);
expect(stats.total).toBeGreaterThanOrEqual(2);
});
});
// ── engram_link ───────────────────────────────────────────────────────────────
describe('EngramMCPStdioServer — engram_link', () => {
it('links two existing memories', async () => {
const server = makeServer();
const id1 = await storeMemory(server, 'Caffeine improves focus');
const id2 = await storeMemory(server, 'Focus is key to productivity');
const resp = await rpc(server, 'tools/call', {
name: 'engram_link',
arguments: { fromId: id1, toId: id2, relation: 'supports' },
});
const result = resp['result'] as Record<string, unknown>;
expect((result as Record<string, boolean>)['isError']).toBeFalsy();
const text = (result['content'] as Array<{ text: string }>)[0].text;
const data = JSON.parse(text);
expect(data.link.relation).toBe('supports');
expect(data.from.id).toBe(id1);
expect(data.to.id).toBe(id2);
});
it('returns error if source memory not found', async () => {
const server = makeServer();
const id2 = await storeMemory(server, 'Some memory');
const resp = await rpc(server, 'tools/call', {
name: 'engram_link',
arguments: { fromId: 'nonexistent-id', toId: id2, relation: 'extends' },
});
const result = resp['result'] as Record<string, unknown>;
expect(result['isError']).toBe(true);
});
it('returns error if target memory not found', async () => {
const server = makeServer();
const id1 = await storeMemory(server, 'Some memory');
const resp = await rpc(server, 'tools/call', {
name: 'engram_link',
arguments: { fromId: id1, toId: 'nonexistent-id', relation: 'causes' },
});
const result = resp['result'] as Record<string, unknown>;
expect(result['isError']).toBe(true);
});
it('supports custom relation types', async () => {
const server = makeServer();
const id1 = await storeMemory(server, 'Memory A');
const id2 = await storeMemory(server, 'Memory B');
const resp = await rpc(server, 'tools/call', {
name: 'engram_link',
arguments: { fromId: id1, toId: id2, relation: 'my-custom-relation' },
});
const result = resp['result'] as Record<string, unknown>;
const data = JSON.parse((result['content'] as Array<{ text: string }>)[0].text);
expect(data.link.relation).toBe('my-custom-relation');
});
});
// ── engram_related ────────────────────────────────────────────────────────────
describe('EngramMCPStdioServer — engram_related', () => {
it('finds related memories after linking', async () => {
const server = makeServer();
const id1 = await storeMemory(server, 'Root memory');
const id2 = await storeMemory(server, 'Child memory A');
const id3 = await storeMemory(server, 'Child memory B');
await rpc(server, 'tools/call', {
name: 'engram_link',
arguments: { fromId: id1, toId: id2, relation: 'extends' },
});
await rpc(server, 'tools/call', {
name: 'engram_link',
arguments: { fromId: id1, toId: id3, relation: 'causes' },
});
const resp = await rpc(server, 'tools/call', {
name: 'engram_related',
arguments: { id: id1 },
});
const result = resp['result'] as Record<string, unknown>;
const data = JSON.parse((result['content'] as Array<{ text: string }>)[0].text);
expect(data.relatedCount).toBe(2);
});
it('returns empty related for unlinked memory', async () => {
const server = makeServer();
const id1 = await storeMemory(server, 'Isolated memory');
const resp = await rpc(server, 'tools/call', {
name: 'engram_related',
arguments: { id: id1 },
});
const result = resp['result'] as Record<string, unknown>;
const data = JSON.parse((result['content'] as Array<{ text: string }>)[0].text);
expect(data.relatedCount).toBe(0);
});
it('filters by direction=outgoing', async () => {
const server = makeServer();
const id1 = await storeMemory(server, 'Source');
const id2 = await storeMemory(server, 'Target');
await rpc(server, 'tools/call', {
name: 'engram_link',
arguments: { fromId: id1, toId: id2, relation: 'precedes' },
});
// id2 has incoming link from id1
const resp = await rpc(server, 'tools/call', {
name: 'engram_related',
arguments: { id: id2, direction: 'outgoing' },
});
const result = resp['result'] as Record<string, unknown>;
const data = JSON.parse((result['content'] as Array<{ text: string }>)[0].text);
expect(data.relatedCount).toBe(0);
const resp2 = await rpc(server, 'tools/call', {
name: 'engram_related',
arguments: { id: id2, direction: 'incoming' },
});
const result2 = resp2['result'] as Record<string, unknown>;
const data2 = JSON.parse((result2['content'] as Array<{ text: string }>)[0].text);
expect(data2.relatedCount).toBe(1);
});
it('returns error for nonexistent memory', async () => {
const server = makeServer();
const resp = await rpc(server, 'tools/call', {
name: 'engram_related',
arguments: { id: 'does-not-exist' },
});
const result = resp['result'] as Record<string, unknown>;
expect(result['isError']).toBe(true);
});
});
// ── engram_timeline ───────────────────────────────────────────────────────────
describe('EngramMCPStdioServer — engram_timeline', () => {
it('returns memories within time window', async () => {
const server = makeServer();
const before = Date.now() - 1;
await storeMemory(server, 'Memory in window A');
await storeMemory(server, 'Memory in window B');
const after = Date.now() + 1;
const resp = await rpc(server, 'tools/call', {
name: 'engram_timeline',
arguments: { after: before, before: after },
});
const result = resp['result'] as Record<string, unknown>;
const data = JSON.parse((result['content'] as Array<{ text: string }>)[0].text);
expect(data.total).toBeGreaterThanOrEqual(2);
});
it('respects limit parameter', async () => {
const server = makeServer();
const t = Date.now() - 1;
await storeMemory(server, 'M1');
await storeMemory(server, 'M2');
await storeMemory(server, 'M3');
const resp = await rpc(server, 'tools/call', {
name: 'engram_timeline',
arguments: { after: t, before: Date.now() + 1, limit: 2 },
});
const result = resp['result'] as Record<string, unknown>;
const data = JSON.parse((result['content'] as Array<{ text: string }>)[0].text);
expect(data.memories.length).toBeLessThanOrEqual(2);
});
it('filters by namespace', async () => {
const server = makeServer();
const t = Date.now() - 1;
await storeMemory(server, 'In NS1', { namespace: 'ns1' });
await storeMemory(server, 'In NS2', { namespace: 'ns2' });
const resp = await rpc(server, 'tools/call', {
name: 'engram_timeline',
arguments: { after: t, before: Date.now() + 1, namespace: 'ns1' },
});
const result = resp['result'] as Record<string, unknown>;
const data = JSON.parse((result['content'] as Array<{ text: string }>)[0].text);
expect(data.memories.every((m: { namespace: string }) => m.namespace === 'ns1')).toBe(true);
});
it('returns empty for past time window with no memories', async () => {
const server = makeServer();
const resp = await rpc(server, 'tools/call', {
name: 'engram_timeline',
arguments: { after: 0, before: 1 }, // epoch ms 0–1 = no real memories
});
const result = resp['result'] as Record<string, unknown>;
const data = JSON.parse((result['content'] as Array<{ text: string }>)[0].text);
expect(data.total).toBe(0);
});
});
// ── engram_namespaces ─────────────────────────────────────────────────────────
describe('EngramMCPStdioServer — engram_namespaces', () => {
it('returns default namespace after storing a memory', async () => {
const server = makeServer();
await storeMemory(server, 'Default namespace memory');
const resp = await rpc(server, 'tools/call', {
name: 'engram_namespaces',
arguments: {},
});
const result = resp['result'] as Record<string, unknown>;
const data = JSON.parse((result['content'] as Array<{ text: string }>)[0].text);
const names = data.namespaces.map((n: { name: string }) => n.name);
expect(names).toContain('default');
});
it('lists multiple namespaces', async () => {
const server = makeServer();
await storeMemory(server, 'In alpha', { namespace: 'alpha' });
await storeMemory(server, 'In beta', { namespace: 'beta' });
await storeMemory(server, 'In alpha 2', { namespace: 'alpha' });
const resp = await rpc(server, 'tools/call', {
name: 'engram_namespaces',
arguments: {},
});
const result = resp['result'] as Record<string, unknown>;
const data = JSON.parse((result['content'] as Array<{ text: string }>)[0].text);
const names = data.namespaces.map((n: { name: string }) => n.name);
expect(names).toContain('alpha');
expect(names).toContain('beta');
// alpha has 2 memories, should come first (sorted by count desc)
const alpha = data.namespaces.find((n: { name: string; count: number }) => n.name === 'alpha');
expect(alpha?.count).toBe(2);
});
});
// ── engram_forget ──────────────────────────────────────────────────────────────
describe('EngramMCPStdioServer — engram_forget', () => {
it('deletes an existing memory', async () => {
const server = makeServer();
const id = await storeMemory(server, 'Memory to be forgotten');
const resp = await rpc(server, 'tools/call', {
name: 'engram_forget',
arguments: { id, reason: 'test deletion' },
});
const result = resp['result'] as Record<string, unknown>;
expect(result['isError']).toBeFalsy();
const data = JSON.parse((result['content'] as Array<{ text: string }>)[0].text);
expect(data.deleted).toBe(id);
expect(data.reason).toBe('test deletion');
});
it('memory is gone after forget', async () => {
const server = makeServer();
const id = await storeMemory(server, 'Transient memory');
await rpc(server, 'tools/call', {
name: 'engram_forget',
arguments: { id },
});
const getResp = await rpc(server, 'tools/call', {
name: 'engram_get',
arguments: { id },
});
const result = getResp['result'] as Record<string, unknown>;
const text = (result['content'] as Array<{ text: string }>)[0].text;
expect(text).toContain('not found');
expect(result['isError']).toBe(true);
});
it('also removes associated links', async () => {
const server = makeServer();
const id1 = await storeMemory(server, 'Memory with links');
const id2 = await storeMemory(server, 'Linked memory');
await rpc(server, 'tools/call', {
name: 'engram_link',
arguments: { fromId: id1, toId: id2, relation: 'extends' },
});
await rpc(server, 'tools/call', {
name: 'engram_forget',
arguments: { id: id1 },
});
// Verify link is cleaned up — id2 should have 0 related
const resp = await rpc(server, 'tools/call', {
name: 'engram_related',
arguments: { id: id2 },
});
const result = resp['result'] as Record<string, unknown>;
const data = JSON.parse((result['content'] as Array<{ text: string }>)[0].text);
expect(data.relatedCount).toBe(0);
});
it('returns error when memory does not exist', async () => {
const server = makeServer();
const resp = await rpc(server, 'tools/call', {
name: 'engram_forget',
arguments: { id: 'ghost-memory-id' },
});
const result = resp['result'] as Record<string, unknown>;
expect(result['isError']).toBe(true);
});
});
// ── Combined workflow ─────────────────────────────────────────────────────────
describe('EngramMCPStdioServer — combined workflow', () => {
it('store → link → timeline → related → forget', async () => {
const server = makeServer();
const t0 = Date.now() - 1;
const idA = await storeMemory(server, 'Concept A: attention mechanisms', {
type: 'semantic',
importance: 'high',
tags: ['transformer', 'attention'],
});
const idB = await storeMemory(server, 'Concept B: self-attention extends attention', {
type: 'semantic',
});
// Link
await rpc(server, 'tools/call', {
name: 'engram_link',
arguments: { fromId: idA, toId: idB, relation: 'extends' },
});
// Timeline
const timelineResp = await rpc(server, 'tools/call', {
name: 'engram_timeline',
arguments: { after: t0, before: Date.now() + 1 },
});
const tlData = JSON.parse(
((timelineResp['result'] as Record<string, unknown>)['content'] as Array<{ text: string }>)[0].text,
);
expect(tlData.total).toBeGreaterThanOrEqual(2);
// Related
const relResp = await rpc(server, 'tools/call', {
name: 'engram_related',
arguments: { id: idA },
});
const relData = JSON.parse(
((relResp['result'] as Record<string, unknown>)['content'] as Array<{ text: string }>)[0].text,
);
expect(relData.relatedCount).toBe(1);
expect(relData.related[0].relation).toBe('extends');
// Forget A
await rpc(server, 'tools/call', {
name: 'engram_forget',
arguments: { id: idA },
});
// B should have no links now
const relResp2 = await rpc(server, 'tools/call', {
name: 'engram_related',
arguments: { id: idB },
});
const relData2 = JSON.parse(
((relResp2['result'] as Record<string, unknown>)['content'] as Array<{ text: string }>)[0].text,
);
expect(relData2.relatedCount).toBe(0);
});
});