@kolivri/memoreal-mcp 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,376 +0,0 @@
1
- import { describe, it, before, after } from 'node:test';
2
- import assert from 'node:assert/strict';
3
- import fs from 'node:fs';
4
- import http from 'node:http';
5
- import os from 'node:os';
6
- import path from 'node:path';
7
- import readline from 'node:readline';
8
- import { spawn } from 'node:child_process';
9
- import { fileURLToPath } from 'node:url';
10
- import { MCP_TOOLS, MCP_RESOURCES, MCP_PROMPTS } from '../src/schemas.mjs';
11
-
12
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
13
- const SERVER_PATH = path.resolve(__dirname, '../src/server.mjs');
14
- const PKG_ROOT = path.resolve(__dirname, '..');
15
-
16
- function createStdioMcpClient(envOverrides = {}, executablePath = SERVER_PATH, useExecDirect = false, cwdOverride = PKG_ROOT) {
17
- const cmd = useExecDirect ? executablePath : process.execPath;
18
- const args = useExecDirect ? [] : [executablePath];
19
- const child = spawn(cmd, args, {
20
- cwd: cwdOverride,
21
- env: {
22
- ...process.env,
23
- ...envOverrides,
24
- },
25
- stdio: ['pipe', 'pipe', 'pipe'],
26
- });
27
-
28
- const rl = readline.createInterface({
29
- input: child.stdout,
30
- crlfDelay: Infinity,
31
- });
32
-
33
- const pending = new Map();
34
-
35
- rl.on('line', (line) => {
36
- const trimmed = line.trim();
37
- if (!trimmed) return;
38
- const msg = JSON.parse(trimmed);
39
- if (msg.id !== undefined && pending.has(msg.id)) {
40
- const { resolve } = pending.get(msg.id);
41
- pending.delete(msg.id);
42
- resolve(msg);
43
- }
44
- });
45
-
46
- let nextId = 1;
47
-
48
- function request(method, params = {}) {
49
- const id = nextId++;
50
- return new Promise((resolve, reject) => {
51
- const timer = setTimeout(() => {
52
- pending.delete(id);
53
- reject(new Error(`Timeout waiting for JSON-RPC response to ${method} (id=${id})`));
54
- }, 5000);
55
-
56
- pending.set(id, {
57
- resolve: (msg) => {
58
- clearTimeout(timer);
59
- resolve(msg);
60
- },
61
- });
62
-
63
- child.stdin.write(`${JSON.stringify({ jsonrpc: '2.0', id, method, params })}\n`);
64
- });
65
- }
66
-
67
- async function close() {
68
- rl.close();
69
- child.stdin.end();
70
- await new Promise((resolve) => {
71
- child.once('close', resolve);
72
- setTimeout(() => {
73
- child.kill();
74
- resolve();
75
- }, 500);
76
- });
77
- }
78
-
79
- return { request, close };
80
- }
81
-
82
- describe('@kolivri/memoreal-mcp stdio Cloud Connector suite', () => {
83
- let mockCloudServer;
84
- let mockCloudUrl;
85
- let receivedHeaders = [];
86
- let receivedBodies = [];
87
-
88
- before(async () => {
89
- mockCloudServer = http.createServer((req, res) => {
90
- let body = '';
91
- req.on('data', (chunk) => {
92
- body += chunk;
93
- });
94
- req.on('end', () => {
95
- receivedHeaders.push(req.headers);
96
- const parsed = JSON.parse(body);
97
- receivedBodies.push(parsed);
98
-
99
- res.writeHead(200, { 'content-type': 'application/json' });
100
- res.end(
101
- JSON.stringify({
102
- jsonrpc: '2.0',
103
- id: parsed.id,
104
- result: {
105
- content: [
106
- {
107
- type: 'text',
108
- text: JSON.stringify({
109
- ok: true,
110
- tool: parsed.params?.name,
111
- arguments: parsed.params?.arguments,
112
- }),
113
- },
114
- ],
115
- },
116
- })
117
- );
118
- });
119
- });
120
-
121
- await new Promise((resolve) => mockCloudServer.listen(0, '127.0.0.1', resolve));
122
- const addr = mockCloudServer.address();
123
- mockCloudUrl = `http://127.0.0.1:${addr.port}`;
124
- });
125
-
126
- after(async () => {
127
- if (mockCloudServer) {
128
- await new Promise((resolve) => mockCloudServer.close(resolve));
129
- }
130
- });
131
-
132
- it('initializes over JSON-RPC 2.0 and lists all 15 tools, 3 resources, and 3 prompts', async () => {
133
- const client = createStdioMcpClient({ MEMOREAL_API_KEY: '', MEMORIA_API_KEY: '' });
134
- try {
135
- const initRes = await client.request('initialize', {
136
- protocolVersion: '2024-11-05',
137
- clientInfo: { name: 'glama-inspector-test', version: '1.0.0' },
138
- capabilities: {},
139
- });
140
- assert.equal(initRes.jsonrpc, '2.0');
141
- assert.equal(initRes.result.serverInfo.name, '@kolivri/memoreal-mcp');
142
- assert.ok(initRes.result.instructions?.includes('Memoreal Cloud'), 'initialize returns server instructions');
143
-
144
- const toolsRes = await client.request('tools/list', {});
145
- assert.equal(MCP_TOOLS.length, 15, 'MCP_TOOLS defines exactly 15 tools');
146
- assert.equal(toolsRes.result.tools.length, 15);
147
- assert.equal(toolsRes.result.tools.length, MCP_TOOLS.length);
148
- assert.ok(
149
- toolsRes.result.tools.every((t) => t.annotations && typeof t.annotations.readOnlyHint === 'boolean'),
150
- 'all tools publish MCP quality annotations'
151
- );
152
- assert.ok(
153
- toolsRes.result.tools.every((t) => t.outputSchema && t.outputSchema.type === 'object' && /Use (when|at|to|after|before)/i.test(t.description)),
154
- 'all 15 tools publish outputSchema and explicit usage/disambiguation guidance for Glama TDQS 4.5+'
155
- );
156
- const feedbackTool = toolsRes.result.tools.find((t) => t.name === 'feedback');
157
- assert.equal(feedbackTool.annotations.idempotentHint, false, 'feedback is marked non-idempotent');
158
- const explainTool = toolsRes.result.tools.find((t) => t.name === 'explain_code');
159
- assert.deepEqual(explainTool.inputSchema.required, [], 'explain_code allows either symbol or target');
160
- const connectTool = toolsRes.result.tools.find((t) => t.name === 'connect_account');
161
- assert.ok(connectTool, 'connect_account tool is present');
162
- assert.equal(connectTool.title, 'Connect Memoreal Account');
163
- assert.deepEqual(connectTool.annotations, {
164
- readOnlyHint: false,
165
- destructiveHint: false,
166
- idempotentHint: false,
167
- openWorldHint: true,
168
- });
169
- assert.deepEqual(connectTool.inputSchema.required, [], 'connect_account inputSchema.required is empty');
170
- assert.equal(connectTool.outputSchema.type, 'object', 'connect_account outputSchema is object');
171
- assert.ok(
172
- /Use (when|at|to|after|before)/i.test(connectTool.description),
173
- 'connect_account description matches Glama TDQS disambiguation regex'
174
- );
175
- assert.ok(
176
- Array.isArray(connectTool.outputSchema.properties?.workspace?.anyOf),
177
- 'connect_account outputSchema.properties.workspace defines anyOf'
178
- );
179
- const workspaceTypes = connectTool.outputSchema.properties.workspace.anyOf.map((s) => s.type);
180
- assert.ok(workspaceTypes.includes('string'), 'workspace schema supports string');
181
- assert.ok(workspaceTypes.includes('object'), 'workspace schema supports object');
182
-
183
- const initBadProto = await client.request('initialize', {
184
- protocolVersion: { malformed: true },
185
- capabilities: {},
186
- });
187
- assert.equal(initBadProto.result.protocolVersion, '2024-11-05', 'non-string protocolVersion falls back to 2024-11-05');
188
-
189
- const initAck = await client.request('initialized', {});
190
- assert.deepEqual(initAck.result, {}, 'initialized with request id responds without hanging');
191
-
192
- const licenseText = fs.readFileSync(path.join(PKG_ROOT, 'LICENSE'), 'utf8');
193
- assert.match(licenseText, /IN NO EVENT SHALL THE\s+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE/, 'full MIT LICENSE text present');
194
- assert.match(licenseText, /Copyright \(c\) 2026 Kolivri$/m);
195
- assert.doesNotMatch(licenseText, /Kolivri Ltd/i);
196
-
197
- const glamaJson = JSON.parse(fs.readFileSync(path.join(PKG_ROOT, 'glama.json'), 'utf8'));
198
- assert.equal(glamaJson.$schema, 'https://glama.ai/mcp/schemas/server.json');
199
- assert.ok(Array.isArray(glamaJson.maintainers) && glamaJson.maintainers.includes('stuk88'));
200
-
201
- const resList = await client.request('resources/list', {});
202
- assert.equal(resList.result.resources.length, MCP_RESOURCES.length);
203
-
204
- const promptsList = await client.request('prompts/list', {});
205
- assert.equal(promptsList.result.prompts.length, MCP_PROMPTS.length);
206
-
207
- const pingRes = await client.request('ping', {});
208
- assert.deepEqual(pingRes.result, {});
209
- } finally {
210
- await client.close();
211
- }
212
- });
213
-
214
- it('requires MEMOREAL_API_KEY when calling any Memoreal Cloud tool or reading a resource', async () => {
215
- const client = createStdioMcpClient({ MEMOREAL_API_KEY: '', MEMORIA_API_KEY: '' });
216
- try {
217
- const callRes = await client.request('tools/call', {
218
- name: 'encode_memory',
219
- arguments: { content: 'Should fail without API key' },
220
- });
221
- assert.equal(callRes.result.isError, true);
222
- assert.match(callRes.result.content[0].text, /Missing Memoreal API key: set MEMOREAL_API_KEY/);
223
-
224
- const resRead = await client.request('resources/read', {
225
- uri: 'memory://stats',
226
- });
227
- assert.ok(resRead.error, 'resources/read without API key returns JSON-RPC error');
228
- assert.match(resRead.error.message, /Missing Memoreal API key: set MEMOREAL_API_KEY/);
229
- } finally {
230
- await client.close();
231
- }
232
- });
233
-
234
- it('forwards authenticated tool and resource calls over HTTP Bearer to MEMOREAL_API_URL/v1/mcp', async () => {
235
- const client = createStdioMcpClient({
236
- MEMOREAL_API_KEY: 'mem_dev_test_key_123',
237
- MEMOREAL_API_URL: mockCloudUrl,
238
- MEMOREAL_NAMESPACE: 'proj-ns',
239
- });
240
- try {
241
- const callRes = await client.request('tools/call', {
242
- name: 'encode_memory',
243
- arguments: { content: 'Cloud-stored memory fact', type: 'semantic' },
244
- });
245
- assert.equal(callRes.result.isError, false);
246
- assert.equal(callRes.result.structuredContent.ok, true);
247
- assert.equal(callRes.result.structuredContent.tool, 'encode_memory');
248
- assert.equal(callRes.result.structuredContent.arguments.namespace, 'proj-ns');
249
- assert.equal(receivedHeaders[0].authorization, 'Bearer mem_dev_test_key_123');
250
-
251
- const resRead = await client.request('resources/read', {
252
- uri: 'memory://stats',
253
- });
254
- assert.ok(Array.isArray(resRead.result.contents));
255
- assert.equal(resRead.result.contents[0].uri, 'memory://stats');
256
-
257
- const explainBySymbol = await client.request('tools/call', {
258
- name: 'explain_code',
259
- arguments: { target: '', symbol: 'createMemorealMcpService' },
260
- });
261
- assert.equal(explainBySymbol.result.isError, false);
262
- assert.equal(explainBySymbol.result.structuredContent.arguments.symbol, 'createMemorealMcpService');
263
- assert.equal(explainBySymbol.result.structuredContent.arguments.target, 'createMemorealMcpService');
264
- } finally {
265
- await client.close();
266
- }
267
- });
268
-
269
- it('resolves both memoreal-* and legacy memoria-* prompts', async () => {
270
- const client = createStdioMcpClient({
271
- MEMOREAL_API_KEY: 'test-key',
272
- MEMOREAL_API_URL: mockCloudUrl,
273
- });
274
- try {
275
- const p1 = await client.request('prompts/get', { name: 'memoreal-session-start', arguments: { namespace: 'ns-test' } });
276
- assert.ok(p1.result.description.includes('Memoreal Cloud'));
277
- assert.ok(p1.result.messages[0].content.text.includes('build_context'));
278
-
279
- const p1Legacy = await client.request('prompts/get', { name: 'memoria-session-start', arguments: { namespace: 'ns-test' } });
280
- assert.ok(p1Legacy.result.description.includes('Memoreal Cloud'));
281
- assert.ok(p1Legacy.result.messages[0].content.text.includes('build_context'));
282
-
283
- const p2 = await client.request('prompts/get', { name: 'memoreal-wrap-up', arguments: { namespace: 'ns-test' } });
284
- assert.ok(p2.result.description.includes('consolidate'));
285
-
286
- const p2Legacy = await client.request('prompts/get', { name: 'memoria-wrap-up', arguments: { namespace: 'ns-test' } });
287
- assert.ok(p2Legacy.result.description.includes('consolidate'));
288
-
289
- const p3 = await client.request('prompts/get', { name: 'memoreal-handoff', arguments: { from_wing: 'architect', to_wing: 'qa' } });
290
- assert.ok(p3.result.description.includes('Hand off context'));
291
-
292
- const p3Legacy = await client.request('prompts/get', { name: 'memoria-handoff', arguments: { from_wing: 'architect', to_wing: 'qa' } });
293
- assert.ok(p3Legacy.result.description.includes('Hand off context'));
294
- } finally {
295
- await client.close();
296
- }
297
- });
298
-
299
- it('supports legacy MEMORIA_API_KEY environment variable fallback', async () => {
300
- const client = createStdioMcpClient({
301
- MEMOREAL_API_KEY: '',
302
- MEMORIA_API_KEY: 'legacy_memoria_key_456',
303
- MEMORIA_API_URL: mockCloudUrl,
304
- MEMORIA_NAMESPACE: 'legacy-ns',
305
- });
306
- try {
307
- const callRes = await client.request('tools/call', {
308
- name: 'encode_memory',
309
- arguments: { content: 'Legacy env key test', type: 'semantic' },
310
- });
311
- assert.equal(callRes.result.isError, false);
312
- assert.equal(callRes.result.structuredContent.ok, true);
313
- assert.equal(callRes.result.structuredContent.arguments.namespace, 'legacy-ns');
314
- const authHeader = receivedHeaders[receivedHeaders.length - 1].authorization;
315
- assert.equal(authHeader, 'Bearer legacy_memoria_key_456');
316
- } finally {
317
- await client.close();
318
- }
319
- });
320
-
321
- it('starts stdio server when executed through an npm/npx bin symlink', async () => {
322
- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'memoreal-mcp-bin-'));
323
- const binPath = path.join(tmpDir, 'memoreal-mcp');
324
- fs.symlinkSync(SERVER_PATH, binPath);
325
- const pkg = JSON.parse(fs.readFileSync(path.join(PKG_ROOT, 'package.json'), 'utf8'));
326
- const client = createStdioMcpClient({ MEMOREAL_API_KEY: '', MEMORIA_API_KEY: '' }, binPath, true, tmpDir);
327
- try {
328
- const initRes = await client.request('initialize', {
329
- protocolVersion: '2024-11-05',
330
- clientInfo: { name: 'npx-symlink-test', version: '1.0.0' },
331
- capabilities: {},
332
- });
333
- assert.equal(initRes.result.serverInfo.name, '@kolivri/memoreal-mcp');
334
- assert.equal(initRes.result.serverInfo.version, pkg.version);
335
-
336
- const toolsRes = await client.request('tools/list', {});
337
- assert.equal(toolsRes.result.tools.length, MCP_TOOLS.length);
338
- } finally {
339
- await client.close();
340
- fs.rmSync(tmpDir, { recursive: true, force: true });
341
- }
342
- });
343
-
344
- it('exports createMemoriaMcpService as a functional alias for createMemorealMcpService', async () => {
345
- const { createMemorealMcpService, createMemoriaMcpService } = await import('../src/server.mjs');
346
- assert.equal(typeof createMemorealMcpService, 'function');
347
- assert.equal(createMemoriaMcpService, createMemorealMcpService);
348
-
349
- const s1 = createMemoriaMcpService({ apiKey: 'alias_key', apiUrl: mockCloudUrl });
350
- assert.equal(typeof s1.callTool, 'function');
351
- assert.equal(typeof s1.getPrompt, 'function');
352
- });
353
-
354
- it('starts stdio server when executed through legacy memoria-mcp bin symlink', async () => {
355
- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'memoria-mcp-legacy-bin-'));
356
- const binPath = path.join(tmpDir, 'memoria-mcp');
357
- fs.symlinkSync(SERVER_PATH, binPath);
358
- const pkg = JSON.parse(fs.readFileSync(path.join(PKG_ROOT, 'package.json'), 'utf8'));
359
- const client = createStdioMcpClient({ MEMOREAL_API_KEY: '', MEMORIA_API_KEY: '' }, binPath, true, tmpDir);
360
- try {
361
- const initRes = await client.request('initialize', {
362
- protocolVersion: '2024-11-05',
363
- clientInfo: { name: 'legacy-bin-test', version: '1.0.0' },
364
- capabilities: {},
365
- });
366
- assert.equal(initRes.result.serverInfo.name, '@kolivri/memoreal-mcp');
367
- assert.equal(initRes.result.serverInfo.version, pkg.version);
368
-
369
- const toolsRes = await client.request('tools/list', {});
370
- assert.equal(toolsRes.result.tools.length, MCP_TOOLS.length);
371
- } finally {
372
- await client.close();
373
- fs.rmSync(tmpDir, { recursive: true, force: true });
374
- }
375
- });
376
- });