-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmcp.test.js
More file actions
562 lines (513 loc) · 19.9 KB
/
mcp.test.js
File metadata and controls
562 lines (513 loc) · 19.9 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
/**
* MCP (Model Context Protocol) server tests.
*
* Covers:
* - handshake (initialize / tools/list)
* - CRUD tools (list, read, write, create, delete, head)
* - skill discovery (list_skills, get_skill, get_pod_skill)
* - docs (list_docs, read_docs)
* - WAC enforcement (anonymous denied write, owner allowed)
*/
import { describe, it, before, after } from 'node:test';
import assert from 'node:assert';
import {
startTestServer,
stopTestServer,
request,
createTestPod,
getBaseUrl,
getPodToken
} from './helpers.js';
import { emitChange } from '../src/notifications/events.js';
let token;
async function rpc(body, opts = {}) {
const headers = { 'Content-Type': 'application/json' };
if (opts.token) headers.Authorization = `Bearer ${opts.token}`;
const res = await request('/mcp', {
method: 'POST',
headers,
body: JSON.stringify(body)
});
if (res.status === 204) return { status: 204, body: null };
const data = await res.json();
return { status: res.status, body: data };
}
describe('MCP server (--mcp enabled)', () => {
before(async () => {
await startTestServer({ mcp: true });
await createTestPod('mcptest');
token = getPodToken('mcptest');
});
after(async () => {
await stopTestServer();
});
it('responds to initialize with protocol version', async () => {
const { status, body } = await rpc({
jsonrpc: '2.0', id: 1, method: 'initialize',
params: { protocolVersion: '2025-03-26', capabilities: {}, clientInfo: { name: 'test', version: '0' } }
});
assert.strictEqual(status, 200);
assert.strictEqual(body.jsonrpc, '2.0');
assert.ok(body.result?.protocolVersion);
assert.strictEqual(body.result.serverInfo.name, 'jss-mcp');
});
it('lists tools', async () => {
const { body } = await rpc({ jsonrpc: '2.0', id: 2, method: 'tools/list' });
const names = body.result.tools.map(t => t.name);
for (const expected of [
'list_resources', 'read_resource', 'write_resource',
'create_resource', 'delete_resource', 'head_resource',
'list_skills', 'get_skill', 'get_pod_skill',
'list_docs', 'read_docs', 'pod_info'
]) {
assert.ok(names.includes(expected), `missing tool: ${expected}`);
}
});
it('write_resource denied without auth', async () => {
const { body } = await rpc({
jsonrpc: '2.0', id: 3, method: 'tools/call',
params: { name: 'write_resource', arguments: { path: '/mcptest/public/anon.txt', content: 'nope' } }
});
assert.ok(body.result?.isError, 'expected isError for anonymous write');
assert.match(body.result.content[0].text, /denied/i);
});
it('write_resource works with owner token', async () => {
const { body } = await rpc({
jsonrpc: '2.0', id: 4, method: 'tools/call',
params: {
name: 'write_resource',
arguments: { path: '/mcptest/public/hello.txt', content: 'hi', contentType: 'text/plain' }
}
}, { token });
assert.strictEqual(body.result.isError, false, body.result.content?.[0]?.text);
assert.match(body.result.content[0].text, /wrote/);
});
it('read_resource returns the written content', async () => {
const { body } = await rpc({
jsonrpc: '2.0', id: 5, method: 'tools/call',
params: { name: 'read_resource', arguments: { path: '/mcptest/public/hello.txt' } }
}, { token });
assert.strictEqual(body.result.isError, false);
const payload = JSON.parse(body.result.content[0].text);
assert.strictEqual(payload.body, 'hi');
});
it('list_resources lists the container', async () => {
const { body } = await rpc({
jsonrpc: '2.0', id: 6, method: 'tools/call',
params: { name: 'list_resources', arguments: { path: '/mcptest/public/' } }
}, { token });
assert.strictEqual(body.result.isError, false);
const payload = JSON.parse(body.result.content[0].text);
assert.ok(payload.items.some(i => i.name === 'hello.txt'));
});
it('create_resource auto-mints filename', async () => {
const { body } = await rpc({
jsonrpc: '2.0', id: 7, method: 'tools/call',
params: {
name: 'create_resource',
arguments: { container: '/mcptest/public/', slug: 'minted', content: 'x' }
}
}, { token });
assert.strictEqual(body.result.isError, false);
assert.match(body.result.content[0].text, /\/mcptest\/public\/minted/);
});
it('delete_resource removes the file', async () => {
const { body } = await rpc({
jsonrpc: '2.0', id: 8, method: 'tools/call',
params: { name: 'delete_resource', arguments: { path: '/mcptest/public/hello.txt' } }
}, { token });
assert.strictEqual(body.result.isError, false);
assert.match(body.result.content[0].text, /deleted/);
});
it('head_resource returns 404 on missing path', async () => {
const { body } = await rpc({
jsonrpc: '2.0', id: 9, method: 'tools/call',
params: { name: 'head_resource', arguments: { path: '/mcptest/public/does-not-exist' } }
}, { token });
assert.ok(body.result.isError);
});
it('list_skills returns the index shape', async () => {
const { body } = await rpc({
jsonrpc: '2.0', id: 10, method: 'tools/call',
params: { name: 'list_skills', arguments: {} }
}, { token });
assert.strictEqual(body.result.isError, false);
const payload = JSON.parse(body.result.content[0].text);
assert.strictEqual(payload['@type'], 'skill:SkillIndex');
assert.ok(Array.isArray(payload['skill:items']));
});
it('list_skills discovers per-app SKILL.md', async () => {
// Seed a per-app skill
await rpc({
jsonrpc: '2.0', id: 1010, method: 'tools/call',
params: {
name: 'write_resource',
arguments: {
path: '/mcptest/public/apps/demo/index.html',
content: '<h1>demo</h1>',
contentType: 'text/html'
}
}
}, { token });
await rpc({
jsonrpc: '2.0', id: 1011, method: 'tools/call',
params: {
name: 'write_resource',
arguments: {
path: '/mcptest/public/apps/demo/SKILL.md',
content: '# demo app skill',
contentType: 'text/markdown'
}
}
}, { token });
// Now list against the pod root — but list_skills walks /public/apps/
// and /private/bots/ at the pod root, not inside a named pod. For this
// test, we just verify the per-app discovery walks containers correctly
// by listing /mcptest/public/apps/ directly via list_resources and
// confirming "demo" comes back as a container (isContainer=true).
const { body } = await rpc({
jsonrpc: '2.0', id: 1012, method: 'tools/call',
params: { name: 'list_resources', arguments: { path: '/mcptest/public/apps/' } }
}, { token });
assert.strictEqual(body.result.isError, false);
const payload = JSON.parse(body.result.content[0].text);
const demo = payload.items.find(i => i.name === 'demo');
assert.ok(demo, 'demo container should be listed');
assert.strictEqual(demo.isContainer, true, 'isContainer must be true for directories');
});
it('list_docs returns the JSS-builtin doc set', async () => {
const { body } = await rpc({
jsonrpc: '2.0', id: 11, method: 'tools/call',
params: { name: 'list_docs', arguments: {} }
});
assert.strictEqual(body.result.isError, false);
const payload = JSON.parse(body.result.content[0].text);
assert.strictEqual(payload.source, 'jss-builtin');
assert.ok(payload.docs.some(d => d.name.endsWith('.md')));
});
it('read_docs fetches a known doc', async () => {
const { body } = await rpc({
jsonrpc: '2.0', id: 12, method: 'tools/call',
params: { name: 'read_docs', arguments: { name: 'git-support.md' } }
});
assert.strictEqual(body.result.isError, false);
const payload = JSON.parse(body.result.content[0].text);
assert.match(payload.body, /git/i);
});
it('read_docs rejects path traversal', async () => {
const { body } = await rpc({
jsonrpc: '2.0', id: 13, method: 'tools/call',
params: { name: 'read_docs', arguments: { name: '../package.json' } }
});
assert.ok(body.result.isError);
});
it('pod_info returns identity info', async () => {
const { body } = await rpc({
jsonrpc: '2.0', id: 14, method: 'tools/call',
params: { name: 'pod_info', arguments: {} }
}, { token });
assert.strictEqual(body.result.isError, false);
const payload = JSON.parse(body.result.content[0].text);
assert.strictEqual(payload.server, 'jss');
assert.ok(payload.identity);
});
it('rejects unknown method', async () => {
const { body } = await rpc({ jsonrpc: '2.0', id: 99, method: 'doesnt/exist' });
assert.strictEqual(body.error?.code, -32601);
});
// --- read_acl / write_acl (#496) ---
it('read_acl returns existing authorizations for /mcptest/public/', async () => {
const { body } = await rpc({
jsonrpc: '2.0', id: 200, method: 'tools/call',
params: { name: 'read_acl', arguments: { path: '/mcptest/public/' } }
}, { token });
assert.strictEqual(body.result.isError, false, body.result.content?.[0]?.text);
const payload = JSON.parse(body.result.content[0].text);
assert.strictEqual(payload.exists, true);
assert.ok(Array.isArray(payload.authorizations));
assert.ok(payload.authorizations.length > 0, 'should have at least owner auth');
// Owner auth should include Read, Write, Control
const ownerAuth = payload.authorizations.find(a => a.modes.includes('Control'));
assert.ok(ownerAuth, 'owner auth with Control should exist');
});
it('write_acl + read_acl round-trip', async () => {
const auths = [
{
agents: ['/mcptest/profile/card.jsonld#me'],
modes: ['Read', 'Write', 'Control'],
isDefault: true
},
{
agentClasses: ['acl:AuthenticatedAgent'],
modes: ['Read', 'Append'],
isDefault: true
},
{
agentClasses: ['foaf:Agent'],
modes: ['Read'],
isDefault: true
}
];
const wr = await rpc({
jsonrpc: '2.0', id: 201, method: 'tools/call',
params: { name: 'write_acl', arguments: { path: '/mcptest/public/', authorizations: auths } }
}, { token });
assert.strictEqual(wr.body.result.isError, false, wr.body.result.content?.[0]?.text);
const rd = await rpc({
jsonrpc: '2.0', id: 202, method: 'tools/call',
params: { name: 'read_acl', arguments: { path: '/mcptest/public/' } }
}, { token });
const payload = JSON.parse(rd.body.result.content[0].text);
assert.strictEqual(payload.authorizations.length, 3);
const classes = payload.authorizations.flatMap(a => a.agentClasses || []);
assert.ok(classes.includes('acl:AuthenticatedAgent'));
assert.ok(classes.includes('foaf:Agent'));
});
it('write_acl refuses to lock caller out (safety)', async () => {
// Try to write an ACL that grants Control only to a foreign WebID
// — would lock the caller (mcptest owner) out. Safety should refuse.
const { body } = await rpc({
jsonrpc: '2.0', id: 204, method: 'tools/call',
params: { name: 'write_acl', arguments: {
path: '/mcptest/public/',
authorizations: [
{
agents: ['https://stranger.example/profile#me'],
modes: ['Read', 'Write', 'Control'],
isDefault: true
}
]
} }
}, { token });
assert.ok(body.result.isError);
assert.match(body.result.content[0].text, /lock the caller out|would not grant Control/i);
});
it('write_acl denied without Control', async () => {
const { body } = await rpc({
jsonrpc: '2.0', id: 203, method: 'tools/call',
params: { name: 'write_acl', arguments: {
path: '/mcptest/public/',
authorizations: [{ agentClasses: ['foaf:Agent'], modes: ['Read'] }]
} }
}); // no token
assert.ok(body.result.isError);
assert.match(body.result.content[0].text, /control/i);
});
// --- call_remote_pod (#495) ---
it('call_remote_pod denied without federation gate access', async () => {
const { body } = await rpc({
jsonrpc: '2.0', id: 210, method: 'tools/call',
params: {
name: 'call_remote_pod',
arguments: {
pod_url: 'http://example.invalid',
tool: 'pod_info',
arguments: {}
}
}
}); // no token
assert.ok(body.result.isError);
// Anonymous → "local WebID identity" error; authenticated-but-not-gated → "federation gate"
assert.match(body.result.content[0].text, /federation/i);
});
it('call_remote_pod hits its own /mcp when gated open', async () => {
// Federation gate lives at <agent-pod>/private/federation/. For the
// mcptest pod owner that's /mcptest/private/federation/. Create the
// container, then grant AuthenticatedAgent Write there.
await rpc({
jsonrpc: '2.0', id: 220, method: 'tools/call',
params: {
name: 'create_resource',
arguments: { container: '/mcptest/private/', slug: 'federation', isContainer: true }
}
}, { token });
await rpc({
jsonrpc: '2.0', id: 221, method: 'tools/call',
params: {
name: 'write_acl',
arguments: {
path: '/mcptest/private/federation/',
authorizations: [
{
agents: ['/mcptest/profile/card.jsonld#me'],
modes: ['Read', 'Write', 'Control'],
isDefault: true
}
]
}
}
}, { token });
// Now call our own MCP back at /mcp invoking pod_info
const base = getBaseUrl();
const { body } = await rpc({
jsonrpc: '2.0', id: 222, method: 'tools/call',
params: {
name: 'call_remote_pod',
arguments: {
pod_url: base,
tool: 'pod_info',
arguments: {}
}
}
}, { token });
assert.strictEqual(body.result.isError, false, body.result.content?.[0]?.text);
const payload = JSON.parse(body.result.content[0].text);
assert.strictEqual(payload.depth, 1);
assert.ok(payload.remote_result);
});
it('call_remote_pod enforces depth cap', async () => {
// Force an inbound MCP-Federation-Depth header so the next hop trips MAX
const res = await request('/mcp', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'MCP-Federation-Depth': '3' // already at max → next hop would be 4
},
body: JSON.stringify({
jsonrpc: '2.0', id: 230, method: 'tools/call',
params: {
name: 'call_remote_pod',
arguments: {
pod_url: getBaseUrl(),
tool: 'pod_info',
arguments: {}
}
}
})
});
const data = await res.json();
assert.ok(data.result.isError);
assert.match(data.result.content[0].text, /depth exceeded/);
});
// --- subscribe (#494) ---
it('subscribe streams SSE events on resourceEvents change', async () => {
// Open the SSE stream
const res = await fetch(`${getBaseUrl()}/mcp`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
jsonrpc: '2.0', id: 300, method: 'tools/call',
params: { name: 'subscribe', arguments: { path: '/mcptest/public/' } }
})
});
assert.strictEqual(res.status, 200);
assert.match(res.headers.get('content-type') || '', /text\/event-stream/);
// Read the initial "subscribed" event then trigger a change
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
let events = [];
async function readUntil(predicate, timeoutMs = 3000) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const { value, done } = await Promise.race([
reader.read(),
new Promise(r => setTimeout(() => r({ value: null, done: false }), 200))
]);
if (done) break;
if (value) {
buffer += decoder.decode(value, { stream: true });
// Parse complete SSE events (terminated by \n\n)
let idx;
while ((idx = buffer.indexOf('\n\n')) !== -1) {
const chunk = buffer.slice(0, idx);
buffer = buffer.slice(idx + 2);
const dataLine = chunk.split('\n').find(l => l.startsWith('data: '));
if (dataLine) {
events.push(JSON.parse(dataLine.slice(6)));
}
}
if (predicate(events)) return;
}
}
}
await readUntil(e => e.length >= 1); // wait for "subscribed" event
assert.ok(events.length >= 1);
assert.strictEqual(events[0].method, 'notifications/tool_event');
assert.strictEqual(events[0].params.event.type, 'subscribed');
// Trigger a change inside scope
emitChange(`${getBaseUrl()}/mcptest/public/triggered.txt`);
await readUntil(e => e.some(ev => ev.params?.event?.type === 'resource_changed'), 3000);
const change = events.find(ev => ev.params?.event?.type === 'resource_changed');
assert.ok(change, `expected resource_changed event; got ${JSON.stringify(events)}`);
assert.strictEqual(change.params.event.path, '/mcptest/public/triggered.txt');
// Clean up: cancel stream
await reader.cancel();
});
it('subscribe filters by scope', async () => {
const res = await fetch(`${getBaseUrl()}/mcp`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
jsonrpc: '2.0', id: 301, method: 'tools/call',
params: { name: 'subscribe', arguments: { path: '/mcptest/public/' } }
})
});
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
let events = [];
async function read(ms = 800) {
const deadline = Date.now() + ms;
while (Date.now() < deadline) {
const { value, done } = await Promise.race([
reader.read(),
new Promise(r => setTimeout(() => r({ value: null, done: false }), 100))
]);
if (done) break;
if (value) {
buffer += decoder.decode(value, { stream: true });
let idx;
while ((idx = buffer.indexOf('\n\n')) !== -1) {
const chunk = buffer.slice(0, idx);
buffer = buffer.slice(idx + 2);
const dataLine = chunk.split('\n').find(l => l.startsWith('data: '));
if (dataLine) events.push(JSON.parse(dataLine.slice(6)));
}
}
}
}
await read(500); // pick up "subscribed"
// Out-of-scope change — should NOT trigger an event
emitChange(`${getBaseUrl()}/mcptest/private/notes/x.txt`);
await read(500);
const changes = events.filter(e => e.params?.event?.type === 'resource_changed');
assert.strictEqual(changes.length, 0, 'out-of-scope change should not emit');
await reader.cancel();
});
it('rejects unknown tool', async () => {
const { body } = await rpc({
jsonrpc: '2.0', id: 100, method: 'tools/call',
params: { name: 'fictional_tool', arguments: {} }
});
assert.ok(body.result?.isError);
assert.match(body.result.content[0].text, /unknown tool/);
});
});
describe('MCP server disabled (no flag)', () => {
before(async () => {
await startTestServer({});
});
after(async () => {
await stopTestServer();
});
it('blocks /mcp when flag is off', async () => {
// Without --mcp, the route isn't registered. The global auth hook fires
// first on the missing route and rejects (401) since /mcp isn't on the
// skip list when mcpEnabled is false. Either 401 or 404 is correct
// "MCP is not available here" behavior; both block tool dispatch.
const res = await request('/mcp', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'initialize' })
});
assert.ok(res.status === 404 || res.status === 401, `expected 404 or 401, got ${res.status}`);
});
});