@hop-top/kit 0.5.0-alpha.1 → 0.5.0-alpha.3
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 +107 -0
- package/README.md +333 -9
- package/dist/aim-CTp-kJW2.d.ts +139 -0
- package/dist/aim.d.ts +1 -116
- package/dist/alias-dhhr08Ei.d.ts +88 -0
- package/dist/alias.d.ts +2 -73
- package/dist/api-miiso81x.d.ts +77 -0
- package/dist/api.d.ts +1 -62
- package/dist/auth-2-jfgiHn.d.ts +34 -0
- package/dist/auth.d.ts +1 -26
- package/dist/cli-DLygZLi9.d.ts +450 -0
- package/dist/cli.d.ts +6 -309
- package/dist/cli.js +192 -63
- package/dist/config-BUY7_W2n.d.ts +103 -0
- package/dist/config.d.ts +1 -97
- package/dist/errcorrect-BcX1Ft0K.d.ts +44 -0
- package/dist/errcorrect.d.ts +1 -37
- package/dist/error-DISJKeDu.d.ts +144 -0
- package/dist/id/index.d.ts +2 -83
- package/dist/index-Bc5c7Ty5.d.ts +328 -0
- package/dist/index-C3Zw4wDJ.d.ts +359 -0
- package/dist/index-MLrRDvEG.d.ts +1073 -0
- package/dist/index-QSffbLnj.d.ts +94 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +6794 -0
- package/dist/llm-B5XUWH8T.d.ts +231 -0
- package/dist/llm.d.ts +1 -188
- package/dist/mcp/index.d.ts +2 -0
- package/dist/mcp/index.js +1453 -0
- package/dist/netpolicy-asvCfwi8.d.ts +138 -0
- package/dist/netpolicy.d.ts +1 -0
- package/dist/netpolicy.js +118 -0
- package/dist/output-4aXs91GY.d.ts +396 -0
- package/dist/output.d.ts +3 -213
- package/dist/output.js +567 -43
- package/dist/progress-CAzyxeWl.d.ts +38 -0
- package/dist/progress.d.ts +1 -30
- package/dist/provenance-SMFHm5gx.d.ts +30 -0
- package/dist/provenance.d.ts +1 -24
- package/dist/routellm-PGSt2D1y.d.ts +75 -0
- package/dist/routellm.d.ts +2 -64
- package/dist/rpc-D5xLEf8V.d.ts +32 -0
- package/dist/rpc.d.ts +2 -25
- package/dist/safety-CUaYEjZc.d.ts +30 -0
- package/dist/safety.d.ts +1 -24
- package/dist/scope-BrhYB4ij.d.ts +141 -0
- package/dist/scope.d.ts +1 -121
- package/dist/serve-DE-jscpb.d.ts +646 -0
- package/dist/serve.d.ts +3 -0
- package/dist/serve.js +1019 -0
- package/dist/stream-CrjeCp0M.d.ts +44 -0
- package/dist/stream.d.ts +1 -35
- package/dist/telemetry/index.d.ts +1 -315
- package/dist/telemetry/index.js +12 -1
- package/dist/tui/index.d.ts +5 -298
- package/dist/tui/index.js +3 -10
- package/dist/upgrade-p_zh3pvV.d.ts +102 -0
- package/dist/upgrade.d.ts +1 -95
- package/dist/xdg-DB7Shoib.d.ts +179 -0
- package/dist/xdg.d.ts +1 -169
- package/package.json +26 -3
- package/src/cli.offline.test.ts +93 -0
- package/src/cli.parity.test.ts +192 -0
- package/src/cli.ts +112 -12
- package/src/index.test.ts +58 -0
- package/src/index.ts +37 -0
- package/src/log.parity.test.ts +105 -0
- package/src/log.ts +64 -13
- package/src/mcp/bridge.test.ts +150 -0
- package/src/mcp/bridge.ts +246 -0
- package/src/mcp/conformance.test.ts +336 -0
- package/src/mcp/dispatch.ts +313 -0
- package/src/mcp/index.ts +138 -0
- package/src/mcp/legacy.ts +314 -0
- package/src/mcp/mcp.test.ts +697 -0
- package/src/mcp/modern-confirm.test.ts +520 -0
- package/src/mcp/modern-confirm.ts +452 -0
- package/src/mcp/modern.ts +723 -0
- package/src/mcp/tasks.ts +82 -0
- package/src/mcp/testtree.ts +250 -0
- package/src/mcp/types.ts +504 -0
- package/src/netpolicy.test.ts +202 -0
- package/src/netpolicy.ts +231 -0
- package/src/output/dispatch.ts +10 -2
- package/src/output/error.ts +316 -0
- package/src/output/formatter.ts +29 -5
- package/src/output/formatters/csv.ts +48 -12
- package/src/output/formatters/json.ts +4 -25
- package/src/output/formatters/project.ts +34 -0
- package/src/output/formatters/table.ts +8 -6
- package/src/output/formatters/text.ts +7 -3
- package/src/output/formatters/yaml.ts +5 -22
- package/src/output/projection.test.ts +298 -0
- package/src/output/projection.ts +33 -59
- package/src/output/template.ts +11 -21
- package/src/output.surface.test.ts +41 -0
- package/src/output.ts +41 -3
- package/src/serve.test.ts +1257 -0
- package/src/serve.ts +1665 -0
- package/src/telemetry/client.test.ts +31 -0
- package/src/telemetry/client.ts +7 -1
- package/src/telemetry/offline.test.ts +64 -0
- package/src/tui/parity.test.ts +120 -31
- package/src/tui/parity.ts +58 -7
- package/test/output/csv.test.ts +164 -0
- package/test/output/error.test.ts +246 -0
- package/src/tui/parity.json +0 -74
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit coverage for behavior the 17 shared wire fixtures do not
|
|
3
|
+
* reach: the era-detection edge cases in ADR 0042's worked table,
|
|
4
|
+
* the modern validation chain V1-V9, mount-option refusals, cache
|
|
5
|
+
* hints, the tasks-extension slot, and the safety gate itself.
|
|
6
|
+
*
|
|
7
|
+
* The fixtures remain the parity contract; these tests pin the rest
|
|
8
|
+
* of the normative surface so a regression outside fixture coverage
|
|
9
|
+
* still fails.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { describe, expect, it } from 'vitest';
|
|
13
|
+
|
|
14
|
+
import { createMcpHandler, detectMcpEra, parseJsonRpcRequest } from './dispatch.js';
|
|
15
|
+
import {
|
|
16
|
+
decodeMcpSentinel,
|
|
17
|
+
parseModernMeta,
|
|
18
|
+
rawToolCallName,
|
|
19
|
+
validModernRequestId,
|
|
20
|
+
} from './modern.js';
|
|
21
|
+
import { isTaskMethod, TASKS_SUPPORTED, TASK_METHODS } from './tasks.js';
|
|
22
|
+
import {
|
|
23
|
+
canonicalJSONStringify,
|
|
24
|
+
defaultPolicy,
|
|
25
|
+
encodeEnvelope,
|
|
26
|
+
policyAllowed,
|
|
27
|
+
RawJSON,
|
|
28
|
+
resolveMcpConfig,
|
|
29
|
+
SURFACE_CLI,
|
|
30
|
+
SURFACE_LIB,
|
|
31
|
+
SURFACE_MCP,
|
|
32
|
+
type McpBridge,
|
|
33
|
+
} from './types.js';
|
|
34
|
+
import { legacyLockBridge, modernLockBridge } from './testtree.js';
|
|
35
|
+
|
|
36
|
+
const MODERN_META = {
|
|
37
|
+
'io.modelcontextprotocol/clientCapabilities': {},
|
|
38
|
+
'io.modelcontextprotocol/protocolVersion': '2026-07-28',
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const MODERN_HEADERS = (method: string, name?: string) => ({
|
|
42
|
+
'MCP-Protocol-Version': '2026-07-28',
|
|
43
|
+
'Mcp-Method': method,
|
|
44
|
+
...(name !== undefined ? { 'Mcp-Name': name } : {}),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
function post(body: unknown, headers: Record<string, string | string[]> = {}) {
|
|
48
|
+
return {
|
|
49
|
+
method: 'POST',
|
|
50
|
+
headers,
|
|
51
|
+
body: typeof body === 'string' ? body : JSON.stringify(body),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function parsed(body: unknown) {
|
|
56
|
+
return parseJsonRpcRequest(
|
|
57
|
+
typeof body === 'string' ? body : JSON.stringify(body),
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
describe('era detection (ADR 0042 D1-D4, markers M1-M4)', () => {
|
|
62
|
+
it('D2: initialize is legacy even with every modern marker present', () => {
|
|
63
|
+
const req = post(
|
|
64
|
+
{ jsonrpc: '2.0', id: 1, method: 'initialize', params: { _meta: MODERN_META } },
|
|
65
|
+
MODERN_HEADERS('initialize', 'x'),
|
|
66
|
+
);
|
|
67
|
+
expect(detectMcpEra(req, parsed(req.body))).toBe('legacy');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('M4: server/discover is a marker on its own, with no headers', () => {
|
|
71
|
+
const req = post({ jsonrpc: '2.0', id: 1, method: 'server/discover' });
|
|
72
|
+
expect(detectMcpEra(req, parsed(req.body))).toBe('modern');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('M1: a bare Mcp-Method header routes modern', () => {
|
|
76
|
+
const req = post({ jsonrpc: '2.0', id: 1, method: 'tools/list' }, {
|
|
77
|
+
'Mcp-Method': 'tools/list',
|
|
78
|
+
});
|
|
79
|
+
expect(detectMcpEra(req, parsed(req.body))).toBe('modern');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('M2: a bare Mcp-Name header routes modern', () => {
|
|
83
|
+
const req = post({ jsonrpc: '2.0', id: 1, method: 'tools/call' }, {
|
|
84
|
+
'Mcp-Name': 'ping',
|
|
85
|
+
});
|
|
86
|
+
expect(detectMcpEra(req, parsed(req.body))).toBe('modern');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('M3: the reserved protocolVersion key routes modern by key presence', () => {
|
|
90
|
+
const req = post({
|
|
91
|
+
jsonrpc: '2.0',
|
|
92
|
+
id: 1,
|
|
93
|
+
method: 'tools/list',
|
|
94
|
+
// Value deliberately nonsense: detection tests key presence only.
|
|
95
|
+
params: { _meta: { 'io.modelcontextprotocol/protocolVersion': 12345 } },
|
|
96
|
+
});
|
|
97
|
+
expect(detectMcpEra(req, parsed(req.body))).toBe('modern');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('non-marker: bare params._meta (progressToken) stays legacy', () => {
|
|
101
|
+
const req = post({
|
|
102
|
+
jsonrpc: '2.0',
|
|
103
|
+
id: 1,
|
|
104
|
+
method: 'tools/list',
|
|
105
|
+
params: { _meta: { progressToken: 'p1', traceparent: 'x' } },
|
|
106
|
+
});
|
|
107
|
+
expect(detectMcpEra(req, parsed(req.body))).toBe('legacy');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// M3 must be tested on a method D2 does NOT short-circuit. On
|
|
111
|
+
// `initialize`, D2 returns legacy before M3 is ever consulted, so an
|
|
112
|
+
// implementation that wrongly treats bare `_meta` presence as a
|
|
113
|
+
// marker still looks correct there. These cases put the same payload
|
|
114
|
+
// on non-initialize methods, where M3 is the only rule that could
|
|
115
|
+
// route them modern — so they fail if, and only if, M3 is wrong.
|
|
116
|
+
it('M3 is keyed on the reserved key, not on _meta presence (D2 cannot mask it)', () => {
|
|
117
|
+
for (const method of ['tools/list', 'tools/call', 'nope']) {
|
|
118
|
+
// Bare _meta, no reserved key: must stay legacy.
|
|
119
|
+
const bare = post({
|
|
120
|
+
jsonrpc: '2.0',
|
|
121
|
+
id: 1,
|
|
122
|
+
method,
|
|
123
|
+
params: { _meta: { progressToken: 'p1' } },
|
|
124
|
+
});
|
|
125
|
+
expect(detectMcpEra(bare, parsed(bare.body)), `bare _meta on ${method}`).toBe(
|
|
126
|
+
'legacy',
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
// Same shape, reserved key added: must flip to modern. Pairing
|
|
130
|
+
// the two pins that the key is what decides, not the container.
|
|
131
|
+
const reserved = post({
|
|
132
|
+
jsonrpc: '2.0',
|
|
133
|
+
id: 1,
|
|
134
|
+
method,
|
|
135
|
+
params: {
|
|
136
|
+
_meta: {
|
|
137
|
+
progressToken: 'p1',
|
|
138
|
+
'io.modelcontextprotocol/protocolVersion': '2026-07-28',
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
expect(
|
|
143
|
+
detectMcpEra(reserved, parsed(reserved.body)),
|
|
144
|
+
`reserved key on ${method}`,
|
|
145
|
+
).toBe('modern');
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('an empty _meta object is not a marker', () => {
|
|
150
|
+
const req = post({
|
|
151
|
+
jsonrpc: '2.0',
|
|
152
|
+
id: 1,
|
|
153
|
+
method: 'tools/list',
|
|
154
|
+
params: { _meta: {} },
|
|
155
|
+
});
|
|
156
|
+
expect(detectMcpEra(req, parsed(req.body))).toBe('legacy');
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('non-marker: MCP-Protocol-Version header stays legacy at any value', () => {
|
|
160
|
+
for (const v of ['2024-11-05', '2025-06-18', '2026-07-28']) {
|
|
161
|
+
const req = post({ jsonrpc: '2.0', id: 1, method: 'tools/list' }, {
|
|
162
|
+
'MCP-Protocol-Version': v,
|
|
163
|
+
});
|
|
164
|
+
expect(detectMcpEra(req, parsed(req.body)), v).toBe('legacy');
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('D4: an unknown bare method is legacy, answered -32601 at HTTP 200', async () => {
|
|
169
|
+
const handler = createMcpHandler(legacyLockBridge());
|
|
170
|
+
const res = await handler(post({ jsonrpc: '2.0', id: 9, method: 'nope' }));
|
|
171
|
+
expect(res.status).toBe(200);
|
|
172
|
+
expect(res.body).toBe(
|
|
173
|
+
'{"jsonrpc":"2.0","id":9,"error":{"code":-32601,"message":"method not found: nope"}}\n',
|
|
174
|
+
);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
describe('modern validation chain (V1-V9)', () => {
|
|
179
|
+
const handler = () => createMcpHandler(modernLockBridge());
|
|
180
|
+
|
|
181
|
+
it('V2: a notification (no id) with markers gets HTTP 202 and no body', async () => {
|
|
182
|
+
const res = await handler()(
|
|
183
|
+
post(
|
|
184
|
+
{ jsonrpc: '2.0', method: 'tools/list', params: { _meta: MODERN_META } },
|
|
185
|
+
MODERN_HEADERS('tools/list'),
|
|
186
|
+
),
|
|
187
|
+
);
|
|
188
|
+
expect(res.status).toBe(202);
|
|
189
|
+
expect(res.body).toBe('');
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('V2: an explicit null id with markers is -32600 at 400', async () => {
|
|
193
|
+
const res = await handler()(
|
|
194
|
+
post(
|
|
195
|
+
{ jsonrpc: '2.0', id: null, method: 'tools/list', params: { _meta: MODERN_META } },
|
|
196
|
+
MODERN_HEADERS('tools/list'),
|
|
197
|
+
),
|
|
198
|
+
);
|
|
199
|
+
expect(res.status).toBe(400);
|
|
200
|
+
expect(res.body).toContain('"code":-32600');
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('V3: M3-only, no headers, is -32602 at 400 (clientCapabilities missing)', async () => {
|
|
204
|
+
const res = await handler()(
|
|
205
|
+
post({
|
|
206
|
+
jsonrpc: '2.0',
|
|
207
|
+
id: 1,
|
|
208
|
+
method: 'tools/call',
|
|
209
|
+
params: {
|
|
210
|
+
name: 'ping',
|
|
211
|
+
_meta: { 'io.modelcontextprotocol/protocolVersion': '2026-07-28' },
|
|
212
|
+
},
|
|
213
|
+
}),
|
|
214
|
+
);
|
|
215
|
+
expect(res.status).toBe(400);
|
|
216
|
+
expect(res.body).toContain('"code":-32602');
|
|
217
|
+
expect(res.body).toContain('io.modelcontextprotocol/clientCapabilities');
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('V4: complete _meta but no MCP-Protocol-Version header is -32020 at 400', async () => {
|
|
221
|
+
const res = await handler()(
|
|
222
|
+
post(
|
|
223
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'ping', _meta: MODERN_META } },
|
|
224
|
+
{ 'Mcp-Method': 'tools/call', 'Mcp-Name': 'ping' },
|
|
225
|
+
),
|
|
226
|
+
);
|
|
227
|
+
expect(res.status).toBe(400);
|
|
228
|
+
expect(res.body).toContain('"code":-32020');
|
|
229
|
+
expect(res.body).toContain('missing MCP-Protocol-Version header');
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('V6: a Mcp-Method header disagreeing with the body is -32020 at 400', async () => {
|
|
233
|
+
const res = await handler()(
|
|
234
|
+
post(
|
|
235
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/list', params: { _meta: MODERN_META } },
|
|
236
|
+
MODERN_HEADERS('tools/call'),
|
|
237
|
+
),
|
|
238
|
+
);
|
|
239
|
+
expect(res.status).toBe(400);
|
|
240
|
+
expect(res.body).toContain('"code":-32020');
|
|
241
|
+
expect(res.body).toContain('does not match body method');
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('V7: tools/call without a Mcp-Name header is -32020 at 400', async () => {
|
|
245
|
+
const res = await handler()(
|
|
246
|
+
post(
|
|
247
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'ping', _meta: MODERN_META } },
|
|
248
|
+
MODERN_HEADERS('tools/call'),
|
|
249
|
+
),
|
|
250
|
+
);
|
|
251
|
+
expect(res.status).toBe(400);
|
|
252
|
+
expect(res.body).toContain('missing Mcp-Name header');
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it('V7: an empty base64 sentinel decodes to "" and is rejected', async () => {
|
|
256
|
+
const res = await handler()(
|
|
257
|
+
post(
|
|
258
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'ping', _meta: MODERN_META } },
|
|
259
|
+
MODERN_HEADERS('tools/call', '=?base64??='),
|
|
260
|
+
),
|
|
261
|
+
);
|
|
262
|
+
expect(res.status).toBe(400);
|
|
263
|
+
expect(res.body).toContain('decodes to an empty value');
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it('V7: a base64-sentinel Mcp-Name matching the body is accepted', async () => {
|
|
267
|
+
const encoded = `=?base64?${Buffer.from('ping').toString('base64')}?=`;
|
|
268
|
+
const res = await handler()(
|
|
269
|
+
post(
|
|
270
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'ping', _meta: MODERN_META } },
|
|
271
|
+
MODERN_HEADERS('tools/call', encoded),
|
|
272
|
+
),
|
|
273
|
+
);
|
|
274
|
+
expect(res.status).toBe(200);
|
|
275
|
+
expect(res.body).toContain('"text":"pong\\n"');
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it('V8: an unknown method with a valid modern envelope is -32601 at 404', async () => {
|
|
279
|
+
const res = await handler()(
|
|
280
|
+
post(
|
|
281
|
+
{ jsonrpc: '2.0', id: 1, method: 'nope', params: { _meta: MODERN_META } },
|
|
282
|
+
MODERN_HEADERS('nope'),
|
|
283
|
+
),
|
|
284
|
+
);
|
|
285
|
+
expect(res.status).toBe(404);
|
|
286
|
+
expect(res.body).toContain('"code":-32601');
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it('duplicate headers: byte-identical values tolerated, differing rejected', async () => {
|
|
290
|
+
const same = await handler()(
|
|
291
|
+
post(
|
|
292
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/list', params: { _meta: MODERN_META } },
|
|
293
|
+
{ 'MCP-Protocol-Version': '2026-07-28', 'Mcp-Method': ['tools/list', 'tools/list'] },
|
|
294
|
+
),
|
|
295
|
+
);
|
|
296
|
+
expect(same.status).toBe(200);
|
|
297
|
+
|
|
298
|
+
const differing = await handler()(
|
|
299
|
+
post(
|
|
300
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/list', params: { _meta: MODERN_META } },
|
|
301
|
+
{ 'MCP-Protocol-Version': '2026-07-28', 'Mcp-Method': ['tools/list', 'tools/call'] },
|
|
302
|
+
),
|
|
303
|
+
);
|
|
304
|
+
expect(differing.status).toBe(400);
|
|
305
|
+
expect(differing.body).toContain('conflicting duplicate values');
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it('auth-required leaves are gated on the Authorization header', async () => {
|
|
309
|
+
const blocked = await handler()(
|
|
310
|
+
post(
|
|
311
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'secret', _meta: MODERN_META } },
|
|
312
|
+
MODERN_HEADERS('tools/call', 'secret'),
|
|
313
|
+
),
|
|
314
|
+
);
|
|
315
|
+
expect(blocked.status).toBe(401);
|
|
316
|
+
expect(blocked.body).toContain('authentication required');
|
|
317
|
+
// The refusal is still a complete modern result envelope.
|
|
318
|
+
expect(blocked.body).toContain('"resultType":"complete"');
|
|
319
|
+
|
|
320
|
+
const allowed = await handler()(
|
|
321
|
+
post(
|
|
322
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'secret', _meta: MODERN_META } },
|
|
323
|
+
{ ...MODERN_HEADERS('tools/call', 'secret'), Authorization: 'Bearer t' },
|
|
324
|
+
),
|
|
325
|
+
);
|
|
326
|
+
expect(allowed.status).toBe(200);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it('requires-confirmation leaves are gated on X-Confirm-Token (the default MRTR slot)', async () => {
|
|
330
|
+
const blocked = await handler()(
|
|
331
|
+
post(
|
|
332
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'deploy', _meta: MODERN_META } },
|
|
333
|
+
MODERN_HEADERS('tools/call', 'deploy'),
|
|
334
|
+
),
|
|
335
|
+
);
|
|
336
|
+
expect(blocked.status).toBe(428);
|
|
337
|
+
expect(blocked.body).toContain('confirmation required');
|
|
338
|
+
expect(blocked.body).toContain('"resultType":"complete"');
|
|
339
|
+
|
|
340
|
+
const allowed = await handler()(
|
|
341
|
+
post(
|
|
342
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'deploy', _meta: MODERN_META } },
|
|
343
|
+
{ ...MODERN_HEADERS('tools/call', 'deploy'), 'X-Confirm-Token': 'ok' },
|
|
344
|
+
),
|
|
345
|
+
);
|
|
346
|
+
expect(allowed.status).toBe(200);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it('records the spec version and client identity in the audit bag', async () => {
|
|
350
|
+
const seen: Array<Record<string, string> | undefined> = [];
|
|
351
|
+
const bridge: McpBridge = {
|
|
352
|
+
leaves: () => modernLockBridge().leaves(),
|
|
353
|
+
invoke: (inv) => {
|
|
354
|
+
seen.push(inv.meta.extra);
|
|
355
|
+
return { stdout: 'pong\n', exitCode: 0 };
|
|
356
|
+
},
|
|
357
|
+
};
|
|
358
|
+
await createMcpHandler(bridge)(
|
|
359
|
+
post(
|
|
360
|
+
{
|
|
361
|
+
jsonrpc: '2.0',
|
|
362
|
+
id: 1,
|
|
363
|
+
method: 'tools/call',
|
|
364
|
+
params: {
|
|
365
|
+
name: 'ping',
|
|
366
|
+
_meta: {
|
|
367
|
+
...MODERN_META,
|
|
368
|
+
'io.modelcontextprotocol/clientInfo': { name: 'probe', version: '9.9' },
|
|
369
|
+
},
|
|
370
|
+
},
|
|
371
|
+
},
|
|
372
|
+
MODERN_HEADERS('tools/call', 'ping'),
|
|
373
|
+
),
|
|
374
|
+
);
|
|
375
|
+
expect(seen[0]).toEqual({
|
|
376
|
+
mcp_spec_version: '2026-07-28',
|
|
377
|
+
mcp_client_name: 'probe',
|
|
378
|
+
mcp_client_version: '9.9',
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
describe('cacheable list results', () => {
|
|
384
|
+
it('server/discover and tools/list carry ttlMs + cacheScope; tools/call does not', async () => {
|
|
385
|
+
const handler = createMcpHandler(modernLockBridge(), {
|
|
386
|
+
cacheHints: { ttlMs: 30_000, cacheScope: 'public' },
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
const discover = await handler(
|
|
390
|
+
post(
|
|
391
|
+
{ jsonrpc: '2.0', id: 1, method: 'server/discover', params: { _meta: MODERN_META } },
|
|
392
|
+
MODERN_HEADERS('server/discover'),
|
|
393
|
+
),
|
|
394
|
+
);
|
|
395
|
+
expect(discover.body).toContain('"ttlMs":30000');
|
|
396
|
+
expect(discover.body).toContain('"cacheScope":"public"');
|
|
397
|
+
|
|
398
|
+
const list = await handler(
|
|
399
|
+
post(
|
|
400
|
+
{ jsonrpc: '2.0', id: 2, method: 'tools/list', params: { _meta: MODERN_META } },
|
|
401
|
+
MODERN_HEADERS('tools/list'),
|
|
402
|
+
),
|
|
403
|
+
);
|
|
404
|
+
expect(list.body).toContain('"ttlMs":30000');
|
|
405
|
+
expect(list.body).toContain('"cacheScope":"public"');
|
|
406
|
+
|
|
407
|
+
const call = await handler(
|
|
408
|
+
post(
|
|
409
|
+
{ jsonrpc: '2.0', id: 3, method: 'tools/call', params: { name: 'ping', _meta: MODERN_META } },
|
|
410
|
+
MODERN_HEADERS('tools/call', 'ping'),
|
|
411
|
+
),
|
|
412
|
+
);
|
|
413
|
+
expect(call.body).not.toContain('ttlMs');
|
|
414
|
+
expect(call.body).not.toContain('cacheScope');
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
it('defaults to ttlMs 0 / cacheScope private', () => {
|
|
418
|
+
const cfg = resolveMcpConfig();
|
|
419
|
+
expect(cfg.cacheTtlMs).toBe(0);
|
|
420
|
+
expect(cfg.cacheScope).toBe('private');
|
|
421
|
+
});
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
describe('tasks extension (unsupported)', () => {
|
|
425
|
+
it('is not supported and is not advertised', async () => {
|
|
426
|
+
expect(TASKS_SUPPORTED).toBe(false);
|
|
427
|
+
const handler = createMcpHandler(modernLockBridge());
|
|
428
|
+
const res = await handler(
|
|
429
|
+
post(
|
|
430
|
+
{ jsonrpc: '2.0', id: 1, method: 'server/discover', params: { _meta: MODERN_META } },
|
|
431
|
+
MODERN_HEADERS('server/discover'),
|
|
432
|
+
),
|
|
433
|
+
);
|
|
434
|
+
// capabilities.extensions is omitted entirely, not emitted empty.
|
|
435
|
+
expect(res.body).not.toContain('extensions');
|
|
436
|
+
expect(res.body).toContain('"capabilities":{"tools":{}}');
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it('answers every tasks/* method with -32601 at 404, like any unknown method', async () => {
|
|
440
|
+
const handler = createMcpHandler(modernLockBridge());
|
|
441
|
+
for (const method of TASK_METHODS) {
|
|
442
|
+
expect(isTaskMethod(method)).toBe(true);
|
|
443
|
+
const res = await handler(
|
|
444
|
+
post(
|
|
445
|
+
{ jsonrpc: '2.0', id: 1, method, params: { _meta: MODERN_META } },
|
|
446
|
+
MODERN_HEADERS(method),
|
|
447
|
+
),
|
|
448
|
+
);
|
|
449
|
+
expect(res.status, method).toBe(404);
|
|
450
|
+
expect(res.body, method).toBe(
|
|
451
|
+
`{"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"method not found: ${method}"}}\n`,
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
describe('mount options', () => {
|
|
458
|
+
it('refuses an explicitly empty spec-version set', () => {
|
|
459
|
+
expect(() => resolveMcpConfig({ specVersions: [] })).toThrow(
|
|
460
|
+
/at least one spec version/,
|
|
461
|
+
);
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
it('refuses an unrecognized spec version', () => {
|
|
465
|
+
expect(() =>
|
|
466
|
+
resolveMcpConfig({ specVersions: ['1999-01-01' as never] }),
|
|
467
|
+
).toThrow(/unrecognized version/);
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it('refuses a negative ttl and an unknown cache scope', () => {
|
|
471
|
+
expect(() => resolveMcpConfig({ cacheHints: { ttlMs: -1 } })).toThrow(
|
|
472
|
+
/negative ttl/,
|
|
473
|
+
);
|
|
474
|
+
expect(() =>
|
|
475
|
+
resolveMcpConfig({ cacheHints: { cacheScope: 'shared' as never } }),
|
|
476
|
+
).toThrow(/unknown cache scope/);
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
it('refuses an empty confirmation key', () => {
|
|
480
|
+
expect(() => resolveMcpConfig({ confirmationKey: '' })).toThrow(
|
|
481
|
+
/empty key/,
|
|
482
|
+
);
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
it('defaults to both eras, /mcp, and an empty origin allowlist', () => {
|
|
486
|
+
const cfg = resolveMcpConfig();
|
|
487
|
+
expect(cfg.legacyEnabled).toBe(true);
|
|
488
|
+
expect(cfg.modernEnabled).toBe(true);
|
|
489
|
+
expect(cfg.path).toBe('/mcp');
|
|
490
|
+
expect(cfg.originAllowlist).toEqual([]);
|
|
491
|
+
});
|
|
492
|
+
|
|
493
|
+
it('legacy-only mounts ignore modern markers entirely', async () => {
|
|
494
|
+
const handler = createMcpHandler(legacyLockBridge(), {
|
|
495
|
+
specVersions: ['2024-11-05'],
|
|
496
|
+
});
|
|
497
|
+
const res = await handler(
|
|
498
|
+
post(
|
|
499
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/list', params: { _meta: MODERN_META } },
|
|
500
|
+
MODERN_HEADERS('tools/list'),
|
|
501
|
+
),
|
|
502
|
+
);
|
|
503
|
+
// Served by the legacy handler: no resultType, no cache hints.
|
|
504
|
+
expect(res.status).toBe(200);
|
|
505
|
+
expect(res.body).not.toContain('resultType');
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
it('modern-only mounts reject a bare initialize rather than demoting it', async () => {
|
|
509
|
+
const handler = createMcpHandler(modernLockBridge(), {
|
|
510
|
+
specVersions: ['2026-07-28'],
|
|
511
|
+
});
|
|
512
|
+
const res = await handler(post({ jsonrpc: '2.0', id: 1, method: 'initialize' }));
|
|
513
|
+
expect(res.status).toBe(400);
|
|
514
|
+
// A modern-only server names its supported versions in any error
|
|
515
|
+
// it returns to initialize: legacy clients have no fall-forward.
|
|
516
|
+
expect(res.body).toContain('supported protocol versions: 2026-07-28');
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
it('enforces the Origin allowlist only when configured', async () => {
|
|
520
|
+
const open = createMcpHandler(modernLockBridge());
|
|
521
|
+
const openRes = await open(
|
|
522
|
+
post(
|
|
523
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/list', params: { _meta: MODERN_META } },
|
|
524
|
+
{ ...MODERN_HEADERS('tools/list'), Origin: 'https://evil.example' },
|
|
525
|
+
),
|
|
526
|
+
);
|
|
527
|
+
expect(openRes.status).toBe(200);
|
|
528
|
+
|
|
529
|
+
const gated = createMcpHandler(modernLockBridge(), {
|
|
530
|
+
originAllowlist: ['https://good.example'],
|
|
531
|
+
});
|
|
532
|
+
const blocked = await gated(
|
|
533
|
+
post(
|
|
534
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/list', params: { _meta: MODERN_META } },
|
|
535
|
+
{ ...MODERN_HEADERS('tools/list'), Origin: 'https://evil.example' },
|
|
536
|
+
),
|
|
537
|
+
);
|
|
538
|
+
expect(blocked.status).toBe(403);
|
|
539
|
+
|
|
540
|
+
const allowed = await gated(
|
|
541
|
+
post(
|
|
542
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/list', params: { _meta: MODERN_META } },
|
|
543
|
+
{ ...MODERN_HEADERS('tools/list'), Origin: 'https://good.example' },
|
|
544
|
+
),
|
|
545
|
+
);
|
|
546
|
+
expect(allowed.status).toBe(200);
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
it('answers GET and DELETE with 405 when the modern era is enabled', async () => {
|
|
550
|
+
const handler = createMcpHandler(modernLockBridge());
|
|
551
|
+
for (const method of ['GET', 'DELETE']) {
|
|
552
|
+
const res = await handler({ method, headers: {}, body: '' });
|
|
553
|
+
expect(res.status, method).toBe(405);
|
|
554
|
+
}
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
it('honours a custom serverInfo on both eras', async () => {
|
|
558
|
+
const opts = { serverInfo: { name: 'probe', version: '1.2.3' } };
|
|
559
|
+
const legacy = await createMcpHandler(legacyLockBridge(), opts)(
|
|
560
|
+
post({ jsonrpc: '2.0', id: 1, method: 'initialize' }),
|
|
561
|
+
);
|
|
562
|
+
expect(legacy.body).toContain('"name":"probe","version":"1.2.3"');
|
|
563
|
+
|
|
564
|
+
const modern = await createMcpHandler(modernLockBridge(), opts)(
|
|
565
|
+
post(
|
|
566
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/list', params: { _meta: MODERN_META } },
|
|
567
|
+
MODERN_HEADERS('tools/list'),
|
|
568
|
+
),
|
|
569
|
+
);
|
|
570
|
+
expect(modern.body).toContain('"name":"probe","version":"1.2.3"');
|
|
571
|
+
});
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
describe('safety gate (Policy.Allowed)', () => {
|
|
575
|
+
const destructive = { destructive: true };
|
|
576
|
+
|
|
577
|
+
it('always allows the local-runtime surfaces', () => {
|
|
578
|
+
const p = defaultPolicy();
|
|
579
|
+
expect(policyAllowed(p, destructive, SURFACE_CLI)).toBe(true);
|
|
580
|
+
expect(policyAllowed(p, destructive, SURFACE_LIB)).toBe(true);
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
it('allows non-destructive commands on every surface', () => {
|
|
584
|
+
expect(policyAllowed(defaultPolicy(), {}, SURFACE_MCP)).toBe(true);
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
it('blocks destructive commands on remote surfaces by default', () => {
|
|
588
|
+
expect(policyAllowed(defaultPolicy(), destructive, SURFACE_MCP)).toBe(false);
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
it('allows destructive commands only on explicitly named surfaces', () => {
|
|
592
|
+
const p = { allowDestructiveOn: [SURFACE_MCP] };
|
|
593
|
+
expect(policyAllowed(p, destructive, SURFACE_MCP)).toBe(true);
|
|
594
|
+
expect(policyAllowed(p, destructive, 'rest')).toBe(false);
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
it('opting MCP in makes a destructive leaf reachable on both eras', async () => {
|
|
598
|
+
const opts = { policy: { allowDestructiveOn: [SURFACE_MCP] } };
|
|
599
|
+
// The lock bridges apply the DEFAULT policy internally, so this
|
|
600
|
+
// asserts the option plumbing on a bridge that honours it.
|
|
601
|
+
const bridge: McpBridge = {
|
|
602
|
+
leaves: () => modernLockBridge().leaves(),
|
|
603
|
+
invoke: () => ({ stdout: 'deleted\n', exitCode: 0 }),
|
|
604
|
+
};
|
|
605
|
+
const res = await createMcpHandler(bridge, opts)(
|
|
606
|
+
post(
|
|
607
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'widget.delete', _meta: MODERN_META } },
|
|
608
|
+
MODERN_HEADERS('tools/call', 'widget.delete'),
|
|
609
|
+
),
|
|
610
|
+
);
|
|
611
|
+
expect(res.status).toBe(200);
|
|
612
|
+
expect(res.body).toContain('"isError":false');
|
|
613
|
+
});
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
describe('wire encoding', () => {
|
|
617
|
+
it('sorts object keys lexicographically, as Go does for maps', () => {
|
|
618
|
+
expect(canonicalJSONStringify({ b: 1, a: 2, c: 3 })).toBe(
|
|
619
|
+
'{"a":2,"b":1,"c":3}',
|
|
620
|
+
);
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
it('emits envelope members in Go struct order with a trailing newline', () => {
|
|
624
|
+
expect(
|
|
625
|
+
encodeEnvelope({ id: new RawJSON('1'), result: { z: 1, a: 2 } }),
|
|
626
|
+
).toBe('{"jsonrpc":"2.0","id":1,"result":{"a":2,"z":1}}\n');
|
|
627
|
+
});
|
|
628
|
+
|
|
629
|
+
it('round-trips an explicit null id and omits an absent one', () => {
|
|
630
|
+
expect(encodeEnvelope({ id: new RawJSON('null'), result: {} })).toBe(
|
|
631
|
+
'{"jsonrpc":"2.0","id":null,"result":{}}\n',
|
|
632
|
+
);
|
|
633
|
+
expect(encodeEnvelope({ result: {} })).toBe(
|
|
634
|
+
'{"jsonrpc":"2.0","result":{}}\n',
|
|
635
|
+
);
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
it('places error data after message', () => {
|
|
639
|
+
expect(
|
|
640
|
+
encodeEnvelope({
|
|
641
|
+
id: new RawJSON('5'),
|
|
642
|
+
error: { code: -32022, message: 'boom', data: { b: 1, a: 2 } },
|
|
643
|
+
}),
|
|
644
|
+
).toBe(
|
|
645
|
+
'{"jsonrpc":"2.0","id":5,"error":{"code":-32022,"message":"boom","data":{"a":2,"b":1}}}\n',
|
|
646
|
+
);
|
|
647
|
+
});
|
|
648
|
+
});
|
|
649
|
+
|
|
650
|
+
describe('helpers', () => {
|
|
651
|
+
it('validModernRequestId accepts strings and integers only', () => {
|
|
652
|
+
expect(validModernRequestId(new RawJSON('"a"'))).toBe(true);
|
|
653
|
+
expect(validModernRequestId(new RawJSON('7'))).toBe(true);
|
|
654
|
+
expect(validModernRequestId(new RawJSON('null'))).toBe(false);
|
|
655
|
+
expect(validModernRequestId(new RawJSON('true'))).toBe(false);
|
|
656
|
+
expect(validModernRequestId(new RawJSON('1.5'))).toBe(false);
|
|
657
|
+
expect(validModernRequestId(new RawJSON('{}'))).toBe(false);
|
|
658
|
+
expect(validModernRequestId(new RawJSON('[]'))).toBe(false);
|
|
659
|
+
});
|
|
660
|
+
|
|
661
|
+
it('decodeMcpSentinel passes plain values and fails closed on bad base64', () => {
|
|
662
|
+
expect(decodeMcpSentinel('ping')).toBe('ping');
|
|
663
|
+
expect(decodeMcpSentinel(`=?base64?${Buffer.from('a.b').toString('base64')}?=`)).toBe('a.b');
|
|
664
|
+
expect(decodeMcpSentinel('=?base64?!!!?=')).toBeUndefined();
|
|
665
|
+
});
|
|
666
|
+
|
|
667
|
+
it('rawToolCallName distinguishes absent from non-string', () => {
|
|
668
|
+
expect(rawToolCallName({ name: 'x' })).toEqual({
|
|
669
|
+
name: 'x',
|
|
670
|
+
present: true,
|
|
671
|
+
isString: true,
|
|
672
|
+
});
|
|
673
|
+
expect(rawToolCallName({})).toEqual({
|
|
674
|
+
name: '',
|
|
675
|
+
present: false,
|
|
676
|
+
isString: false,
|
|
677
|
+
});
|
|
678
|
+
expect(rawToolCallName({ name: 12 })).toEqual({
|
|
679
|
+
name: '',
|
|
680
|
+
present: true,
|
|
681
|
+
isString: false,
|
|
682
|
+
});
|
|
683
|
+
});
|
|
684
|
+
|
|
685
|
+
it('parseModernMeta requires both reserved keys', () => {
|
|
686
|
+
const ok = parseModernMeta({ _meta: MODERN_META });
|
|
687
|
+
expect('meta' in ok && ok.meta.version).toBe('2026-07-28');
|
|
688
|
+
|
|
689
|
+
const noCaps = parseModernMeta({
|
|
690
|
+
_meta: { 'io.modelcontextprotocol/protocolVersion': '2026-07-28' },
|
|
691
|
+
});
|
|
692
|
+
expect('error' in noCaps).toBe(true);
|
|
693
|
+
|
|
694
|
+
const noParams = parseModernMeta(undefined);
|
|
695
|
+
expect('error' in noParams).toBe(true);
|
|
696
|
+
});
|
|
697
|
+
});
|