@hop-top/kit 0.5.0-alpha.8 → 0.5.0-alpha.9
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 +22 -0
- package/dist/{chunk-263IZ6Z6.js → chunk-B4ULGCCN.js} +43 -154
- package/dist/{chunk-JTMPNXVQ.js → chunk-P6GRX4RV.js} +486 -83
- package/dist/chunk-UZ65A27P.js +147 -0
- package/dist/{index-B-pqGHhU.d.ts → index-CaOXdl38.d.ts} +337 -24
- package/dist/index.d.ts +2 -2
- package/dist/index.js +4 -3
- package/dist/mcp/index.d.ts +2 -1
- package/dist/mcp/index.js +31 -2
- package/dist/{toolspec-UrWnoJ3Y.d.ts → toolspec-96rkLfsh.d.ts} +7 -2
- package/dist/toolspec.d.ts +1 -1
- package/dist/toolspec.js +3 -2
- package/package.json +1 -1
- package/src/mcp/README.md +6 -0
- package/src/mcp/args-separator.test.ts +164 -0
- package/src/mcp/args.test.ts +223 -0
- package/src/mcp/args.ts +160 -0
- package/src/mcp/audit.test.ts +520 -0
- package/src/mcp/audit.ts +70 -0
- package/src/mcp/bridge.test.ts +72 -0
- package/src/mcp/bridge.ts +50 -5
- package/src/mcp/conformance.test.ts +1 -1
- package/src/mcp/dispatch.ts +48 -2
- package/src/mcp/index.ts +33 -1
- package/src/mcp/legacy-notify.test.ts +59 -0
- package/src/mcp/legacy.ts +55 -15
- package/src/mcp/modern-confirm.ts +42 -10
- package/src/mcp/modern.ts +99 -35
- package/src/mcp/stdio.test.ts +313 -0
- package/src/mcp/stdio.ts +254 -0
- package/src/mcp/toolname.test.ts +147 -0
- package/src/mcp/types.ts +125 -18
- package/src/toolspec-internal.ts +24 -0
- package/src/toolspec-manifest.test.ts +25 -0
- package/src/toolspec-manifest.ts +5 -1
- package/src/toolspec.from-command.test.ts +51 -0
- package/src/toolspec.ts +29 -15
package/src/mcp/args.ts
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module mcp/args
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* Positional arguments over MCP, ported from Go's
|
|
6
|
+
* go/transport/cmdsurface/surface_mcp_args.go.
|
|
7
|
+
*
|
|
8
|
+
* An MCP tool takes one JSON object of named arguments; a leaf takes
|
|
9
|
+
* flags by name and positional arguments by position. A leaf that
|
|
10
|
+
* declares its positionals (`Leaf.args`) publishes them as ONE extra
|
|
11
|
+
* property, "args": an array of strings in declared order — the key
|
|
12
|
+
* and shape every kit surface uses for positionals. One property per
|
|
13
|
+
* argument would share a namespace with the leaf's flags, so a flag
|
|
14
|
+
* added later could silently take an argument's name; the array
|
|
15
|
+
* reserves exactly one name, and a trailing variadic argument needs
|
|
16
|
+
* no extra syntax.
|
|
17
|
+
*
|
|
18
|
+
* The property is required, with minItems set to the number of
|
|
19
|
+
* required arguments, when any argument is; optional arguments only
|
|
20
|
+
* lengthen what the array may hold. No maxItems is published, as in
|
|
21
|
+
* Go: the command's own parser refuses a count it does not accept.
|
|
22
|
+
*
|
|
23
|
+
* A leaf whose "args" name is taken by a flag publishes no such
|
|
24
|
+
* property and says so in its description: the flag was part of the
|
|
25
|
+
* tool's schema first, and taking the name would change what an
|
|
26
|
+
* existing caller's "args" means.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import type { FlagSchema, Leaf, LeafArg } from './types.js';
|
|
30
|
+
|
|
31
|
+
/** The inputSchema property carrying a leaf's declared positionals. */
|
|
32
|
+
export const MCP_ARGS_PROPERTY = 'args';
|
|
33
|
+
|
|
34
|
+
/** Description note for a leaf whose positionals MCP cannot pass. */
|
|
35
|
+
const NOTE_ARGS_FLAG_TAKEN =
|
|
36
|
+
'Takes positional arguments that cannot be passed over MCP: its --args flag holds the "args" property.';
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Reports whether leaf's inputSchema carries the "args" property: it
|
|
40
|
+
* declares positional arguments and no flag already holds the name.
|
|
41
|
+
*/
|
|
42
|
+
export function mcpArgsPublished(leaf: Leaf): boolean {
|
|
43
|
+
if (!leaf.args || leaf.args.length === 0) return false;
|
|
44
|
+
return !Object.prototype.hasOwnProperty.call(
|
|
45
|
+
leaf.properties ?? {},
|
|
46
|
+
MCP_ARGS_PROPERTY,
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The description leaf's MCP tool publishes: its own description,
|
|
52
|
+
* followed by a note when it takes positionals the tool cannot pass.
|
|
53
|
+
*/
|
|
54
|
+
export function mcpToolDescription(leaf: Leaf): string {
|
|
55
|
+
const desc = leaf.description;
|
|
56
|
+
if (!leaf.args || leaf.args.length === 0 || mcpArgsPublished(leaf)) {
|
|
57
|
+
return desc;
|
|
58
|
+
}
|
|
59
|
+
return desc === '' ? NOTE_ARGS_FLAG_TAKEN : `${desc} ${NOTE_ARGS_FLAG_TAKEN}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* leaf's MCP tool inputSchema: one property per flag, the leaf's
|
|
64
|
+
* required flags listed as required, plus the "args" property when
|
|
65
|
+
* {@link mcpArgsPublished}.
|
|
66
|
+
*/
|
|
67
|
+
export function mcpInputSchema(leaf: Leaf): Record<string, unknown> {
|
|
68
|
+
const properties: Record<string, FlagSchema> = { ...(leaf.properties ?? {}) };
|
|
69
|
+
const required = [...(leaf.required ?? [])];
|
|
70
|
+
if (mcpArgsPublished(leaf)) {
|
|
71
|
+
const declared = leaf.args ?? [];
|
|
72
|
+
const prop: FlagSchema = {
|
|
73
|
+
type: 'array',
|
|
74
|
+
items: { type: 'string' },
|
|
75
|
+
description: argsDescription(declared),
|
|
76
|
+
};
|
|
77
|
+
const n = requiredCount(declared);
|
|
78
|
+
if (n > 0) {
|
|
79
|
+
prop.minItems = n;
|
|
80
|
+
required.push(MCP_ARGS_PROPERTY);
|
|
81
|
+
}
|
|
82
|
+
properties[MCP_ARGS_PROPERTY] = prop;
|
|
83
|
+
}
|
|
84
|
+
const schema: Record<string, unknown> = { type: 'object', properties };
|
|
85
|
+
if (required.length > 0) schema.required = required;
|
|
86
|
+
return schema;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The outcome of {@link mcpSplitArguments}. */
|
|
90
|
+
export type McpSplitResult =
|
|
91
|
+
| { flags?: Record<string, unknown>; args?: string[]; error?: undefined }
|
|
92
|
+
| { error: string };
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Splits a tools/call arguments object into an invocation's flags
|
|
96
|
+
* and positional args. When leaf publishes "args" that key is taken
|
|
97
|
+
* out of the flags: it must hold an array of strings, at least as
|
|
98
|
+
* many as the leaf declares required, and becomes `args` in order.
|
|
99
|
+
* Every other key is a flag, forwarded as-is. A leaf that publishes
|
|
100
|
+
* no "args" property gets every key as a flag.
|
|
101
|
+
*
|
|
102
|
+
* `error` names the problem for the caller to correct; the handlers
|
|
103
|
+
* return it as an isError tool result, before the confirmation gate.
|
|
104
|
+
*/
|
|
105
|
+
export function mcpSplitArguments(
|
|
106
|
+
leaf: Leaf,
|
|
107
|
+
args: Record<string, unknown> | undefined,
|
|
108
|
+
): McpSplitResult {
|
|
109
|
+
if (!mcpArgsPublished(leaf)) return { flags: args };
|
|
110
|
+
const declared = leaf.args ?? [];
|
|
111
|
+
const src = args ?? {};
|
|
112
|
+
if (!Object.prototype.hasOwnProperty.call(src, MCP_ARGS_PROPERTY)) {
|
|
113
|
+
return requireArgs(declared, []) ?? { flags: args };
|
|
114
|
+
}
|
|
115
|
+
const flags: Record<string, unknown> = {};
|
|
116
|
+
for (const [k, v] of Object.entries(src)) {
|
|
117
|
+
if (k !== MCP_ARGS_PROPERTY) flags[k] = v;
|
|
118
|
+
}
|
|
119
|
+
const raw = src[MCP_ARGS_PROPERTY];
|
|
120
|
+
if (raw !== null && !Array.isArray(raw)) {
|
|
121
|
+
return { error: '"args" must be an array of strings' };
|
|
122
|
+
}
|
|
123
|
+
const list = (raw ?? []) as unknown[];
|
|
124
|
+
if (!list.every((v) => typeof v === 'string')) {
|
|
125
|
+
return { error: '"args" must be an array of strings' };
|
|
126
|
+
}
|
|
127
|
+
const positional = list as string[];
|
|
128
|
+
const missing = requireArgs(declared, positional);
|
|
129
|
+
if (missing) return missing;
|
|
130
|
+
return {
|
|
131
|
+
flags: Object.keys(flags).length > 0 ? flags : undefined,
|
|
132
|
+
args: positional,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Names the first declared-required argument not supplied, with the
|
|
138
|
+
* wording every kit surface uses.
|
|
139
|
+
*/
|
|
140
|
+
function requireArgs(
|
|
141
|
+
declared: LeafArg[],
|
|
142
|
+
supplied: string[],
|
|
143
|
+
): { error: string } | undefined {
|
|
144
|
+
if (supplied.length < requiredCount(declared)) {
|
|
145
|
+
return {
|
|
146
|
+
error: `missing required argument: ${declared[supplied.length]?.name ?? ''}`,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function requiredCount(declared: LeafArg[]): number {
|
|
153
|
+
return declared.filter((a) => a.required).length;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Declared names in order, optional ones marked with "?". */
|
|
157
|
+
function argsDescription(declared: LeafArg[]): string {
|
|
158
|
+
const names = declared.map((a) => (a.required ? a.name : `${a.name}?`));
|
|
159
|
+
return `Positional arguments in order: ${names.join(', ')}`;
|
|
160
|
+
}
|
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The mount-level audit hook: one record per refusal the surface
|
|
3
|
+
* decides before adopter code runs, mirroring what Go's bridge sinks
|
|
4
|
+
* receive from the MCP surface. Pins, per event and per era, the
|
|
5
|
+
* record's fields, that the wire response is byte-identical with and
|
|
6
|
+
* without the hook, that a throwing hook changes nothing, and that
|
|
7
|
+
* neither a successful call nor an expired-but-authentic state is
|
|
8
|
+
* recorded.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { PassThrough } from 'node:stream';
|
|
12
|
+
import { describe, expect, it } from 'vitest';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
MCP_AUDIT_AUTH_REFUSED,
|
|
16
|
+
MCP_AUDIT_CONFIRMATION_DECLINED,
|
|
17
|
+
MCP_AUDIT_CONFIRMATION_REQUIRED,
|
|
18
|
+
MCP_AUDIT_CONFIRMATION_STATE_REJECTED,
|
|
19
|
+
MCP_AUDIT_DESTRUCTIVE_BLOCKED,
|
|
20
|
+
MCP_CONFIRM_REJECTION_STATE,
|
|
21
|
+
} from './audit.js';
|
|
22
|
+
import {
|
|
23
|
+
createMcpHandler,
|
|
24
|
+
mcpMountConfig,
|
|
25
|
+
type McpHttpRequest,
|
|
26
|
+
type McpHttpResponse,
|
|
27
|
+
} from './dispatch.js';
|
|
28
|
+
import {
|
|
29
|
+
ElicitationConfirmGate,
|
|
30
|
+
mcpConfirmArgsDigest,
|
|
31
|
+
mintMcpConfirmState,
|
|
32
|
+
} from './modern-confirm.js';
|
|
33
|
+
import { serveStdio } from './stdio.js';
|
|
34
|
+
import { legacyLockBridge, modernLockBridge, mrtrLockBridge } from './testtree.js';
|
|
35
|
+
import {
|
|
36
|
+
MCP_SPEC_2024_11_05,
|
|
37
|
+
MCP_SPEC_2026_07_28,
|
|
38
|
+
type McpAuditRecord,
|
|
39
|
+
type McpBridge,
|
|
40
|
+
type McpMountOptions,
|
|
41
|
+
} from './types.js';
|
|
42
|
+
|
|
43
|
+
const KEY = 'audit-unit-suite-shared-secret-32';
|
|
44
|
+
|
|
45
|
+
/** Collects every record the hook receives. */
|
|
46
|
+
function recorder(): {
|
|
47
|
+
records: McpAuditRecord[];
|
|
48
|
+
audit: (r: McpAuditRecord) => void;
|
|
49
|
+
} {
|
|
50
|
+
const records: McpAuditRecord[] = [];
|
|
51
|
+
return { records, audit: (r) => records.push(r) };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A legacy-era tools/call. */
|
|
55
|
+
function legacyCall(
|
|
56
|
+
name: string,
|
|
57
|
+
headers: Record<string, string> = {},
|
|
58
|
+
): McpHttpRequest {
|
|
59
|
+
return {
|
|
60
|
+
method: 'POST',
|
|
61
|
+
headers: { 'Content-Type': 'application/json', ...headers },
|
|
62
|
+
body: JSON.stringify({
|
|
63
|
+
jsonrpc: '2.0',
|
|
64
|
+
id: 1,
|
|
65
|
+
method: 'tools/call',
|
|
66
|
+
params: { name },
|
|
67
|
+
}),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The reserved _meta bag, with the client's elicitation declaration. */
|
|
72
|
+
function meta(elicitation?: unknown): Record<string, unknown> {
|
|
73
|
+
const caps: Record<string, unknown> = {};
|
|
74
|
+
if (elicitation !== undefined) caps.elicitation = elicitation;
|
|
75
|
+
return {
|
|
76
|
+
'io.modelcontextprotocol/clientCapabilities': caps,
|
|
77
|
+
'io.modelcontextprotocol/protocolVersion': MCP_SPEC_2026_07_28,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface ModernCallOpts {
|
|
82
|
+
id?: number;
|
|
83
|
+
tool: string;
|
|
84
|
+
args?: Record<string, unknown>;
|
|
85
|
+
meta?: Record<string, unknown>;
|
|
86
|
+
state?: string;
|
|
87
|
+
action?: string;
|
|
88
|
+
headers?: Record<string, string>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A modern-era tools/call, with optional MRTR retry members. */
|
|
92
|
+
function modernCall(o: ModernCallOpts): McpHttpRequest {
|
|
93
|
+
const params: Record<string, unknown> = {
|
|
94
|
+
name: o.tool,
|
|
95
|
+
_meta: o.meta ?? meta({}),
|
|
96
|
+
};
|
|
97
|
+
if (o.args !== undefined) params.arguments = o.args;
|
|
98
|
+
if (o.state !== undefined) params.requestState = o.state;
|
|
99
|
+
if (o.action !== undefined) {
|
|
100
|
+
params.inputResponses = { confirm: { action: o.action } };
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
method: 'POST',
|
|
104
|
+
headers: {
|
|
105
|
+
'Content-Type': 'application/json',
|
|
106
|
+
'MCP-Protocol-Version': MCP_SPEC_2026_07_28,
|
|
107
|
+
'Mcp-Method': 'tools/call',
|
|
108
|
+
'Mcp-Name': o.tool,
|
|
109
|
+
...(o.headers ?? {}),
|
|
110
|
+
},
|
|
111
|
+
body: JSON.stringify({
|
|
112
|
+
jsonrpc: '2.0',
|
|
113
|
+
id: o.id ?? 1,
|
|
114
|
+
method: 'tools/call',
|
|
115
|
+
params,
|
|
116
|
+
}),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Serves `reqs` in order against two fresh mounts, one silent and one
|
|
122
|
+
* audited, and asserts every response is byte-identical between them.
|
|
123
|
+
* Returns the audited mount's last response and its records.
|
|
124
|
+
*/
|
|
125
|
+
async function sameWire(
|
|
126
|
+
make: () => McpBridge,
|
|
127
|
+
opts: McpMountOptions,
|
|
128
|
+
reqs: McpHttpRequest[],
|
|
129
|
+
): Promise<{ res: McpHttpResponse; records: McpAuditRecord[] }> {
|
|
130
|
+
const silent = createMcpHandler(make(), opts);
|
|
131
|
+
const { records, audit } = recorder();
|
|
132
|
+
const audited = createMcpHandler(make(), { ...opts, audit });
|
|
133
|
+
let last: McpHttpResponse | undefined;
|
|
134
|
+
for (const req of reqs) {
|
|
135
|
+
const a = await silent(req);
|
|
136
|
+
const b = await audited(req);
|
|
137
|
+
expect(b).toEqual(a);
|
|
138
|
+
last = b;
|
|
139
|
+
}
|
|
140
|
+
if (last === undefined) throw new Error('no request served');
|
|
141
|
+
return { res: last, records };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** The one record expected for a refusal, checked field by field. */
|
|
145
|
+
function expectRecord(
|
|
146
|
+
r: McpAuditRecord | undefined,
|
|
147
|
+
want: { path: string[]; specVersion: string; error: string; extra?: Record<string, string> },
|
|
148
|
+
): void {
|
|
149
|
+
expect(r).toBeDefined();
|
|
150
|
+
if (r === undefined) return;
|
|
151
|
+
expect(r.path).toEqual(want.path);
|
|
152
|
+
expect(r.surface).toBe('mcp');
|
|
153
|
+
expect(r.requestedAt).toBeInstanceOf(Date);
|
|
154
|
+
expect(r.specVersion).toBe(want.specVersion);
|
|
155
|
+
expect(r.error).toBe(want.error);
|
|
156
|
+
expect(r.extra).toEqual({
|
|
157
|
+
mcp_spec_version: want.specVersion,
|
|
158
|
+
...(want.extra ?? {}),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
describe('audit hook — 2024-11-05 era', () => {
|
|
163
|
+
it('auth refused: one record, wire unchanged', async () => {
|
|
164
|
+
const { res, records } = await sameWire(legacyLockBridge, {}, [
|
|
165
|
+
legacyCall('secret', { Authorization: 'Bearer presence-only' }),
|
|
166
|
+
]);
|
|
167
|
+
expect(res.status).toBe(401);
|
|
168
|
+
expect(records).toHaveLength(1);
|
|
169
|
+
expectRecord(records[0], {
|
|
170
|
+
path: ['secret'],
|
|
171
|
+
specVersion: MCP_SPEC_2024_11_05,
|
|
172
|
+
error: MCP_AUDIT_AUTH_REFUSED,
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('confirmation required: one record, wire unchanged', async () => {
|
|
177
|
+
const { res, records } = await sameWire(legacyLockBridge, {}, [
|
|
178
|
+
legacyCall('deploy'),
|
|
179
|
+
]);
|
|
180
|
+
expect(res.status).toBe(428);
|
|
181
|
+
expect(records).toHaveLength(1);
|
|
182
|
+
expectRecord(records[0], {
|
|
183
|
+
path: ['deploy'],
|
|
184
|
+
specVersion: MCP_SPEC_2024_11_05,
|
|
185
|
+
error: MCP_AUDIT_CONFIRMATION_REQUIRED,
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('destructive ceiling: one record, wire unchanged', async () => {
|
|
190
|
+
const { res, records } = await sameWire(legacyLockBridge, {}, [
|
|
191
|
+
legacyCall('widget.delete'),
|
|
192
|
+
]);
|
|
193
|
+
expect(res.status).toBe(200);
|
|
194
|
+
expect(res.body).toContain('destructive command blocked');
|
|
195
|
+
expect(records).toHaveLength(1);
|
|
196
|
+
expectRecord(records[0], {
|
|
197
|
+
path: ['widget', 'delete'],
|
|
198
|
+
specVersion: MCP_SPEC_2024_11_05,
|
|
199
|
+
error: MCP_AUDIT_DESTRUCTIVE_BLOCKED,
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('a successful call and a satisfied gate record nothing', async () => {
|
|
204
|
+
const { records } = await sameWire(legacyLockBridge, {}, [
|
|
205
|
+
legacyCall('ping'),
|
|
206
|
+
legacyCall('deploy', { 'X-Confirm-Token': 'yes' }),
|
|
207
|
+
]);
|
|
208
|
+
expect(records).toEqual([]);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('a throwing hook leaves the response unchanged', async () => {
|
|
212
|
+
const silent = await createMcpHandler(legacyLockBridge())(legacyCall('secret'));
|
|
213
|
+
const loud = await createMcpHandler(legacyLockBridge(), {
|
|
214
|
+
audit: () => {
|
|
215
|
+
throw new Error('sink down');
|
|
216
|
+
},
|
|
217
|
+
})(legacyCall('secret'));
|
|
218
|
+
expect(loud).toEqual(silent);
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
describe('audit hook — 2026-07-28 era', () => {
|
|
223
|
+
it('auth refused: one record, wire unchanged', async () => {
|
|
224
|
+
const { res, records } = await sameWire(modernLockBridge, {}, [
|
|
225
|
+
modernCall({ tool: 'secret', headers: { Authorization: 'Bearer presence-only' } }),
|
|
226
|
+
]);
|
|
227
|
+
expect(res.status).toBe(401);
|
|
228
|
+
expect(records).toHaveLength(1);
|
|
229
|
+
expectRecord(records[0], {
|
|
230
|
+
path: ['secret'],
|
|
231
|
+
specVersion: MCP_SPEC_2026_07_28,
|
|
232
|
+
error: MCP_AUDIT_AUTH_REFUSED,
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('confirmation required (header gate, key-less mount): one record', async () => {
|
|
237
|
+
const { res, records } = await sameWire(modernLockBridge, {}, [
|
|
238
|
+
modernCall({ tool: 'deploy' }),
|
|
239
|
+
]);
|
|
240
|
+
expect(res.status).toBe(428);
|
|
241
|
+
expect(records).toHaveLength(1);
|
|
242
|
+
expectRecord(records[0], {
|
|
243
|
+
path: ['deploy'],
|
|
244
|
+
specVersion: MCP_SPEC_2026_07_28,
|
|
245
|
+
error: MCP_AUDIT_CONFIRMATION_REQUIRED,
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it('confirmation required (keyed mount, client without elicitation): one record', async () => {
|
|
250
|
+
const { res, records } = await sameWire(
|
|
251
|
+
() => mrtrLockBridge().bridge,
|
|
252
|
+
{ confirmationKey: KEY },
|
|
253
|
+
[modernCall({ tool: 'purge', meta: meta() })],
|
|
254
|
+
);
|
|
255
|
+
expect(res.status).toBe(428);
|
|
256
|
+
expect(records).toHaveLength(1);
|
|
257
|
+
expectRecord(records[0], {
|
|
258
|
+
path: ['purge'],
|
|
259
|
+
specVersion: MCP_SPEC_2026_07_28,
|
|
260
|
+
error: MCP_AUDIT_CONFIRMATION_REQUIRED,
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
for (const action of ['decline', 'cancel']) {
|
|
265
|
+
it(`confirmation declined (${action}): one record, wire unchanged`, async () => {
|
|
266
|
+
const silent = createMcpHandler(mrtrLockBridge().bridge, { confirmationKey: KEY });
|
|
267
|
+
const { records, audit } = recorder();
|
|
268
|
+
const audited = createMcpHandler(mrtrLockBridge().bridge, {
|
|
269
|
+
confirmationKey: KEY,
|
|
270
|
+
audit,
|
|
271
|
+
});
|
|
272
|
+
// The prompt itself is not a refusal: nothing is recorded.
|
|
273
|
+
const r1 = await audited(modernCall({ id: 1, tool: 'purge' }));
|
|
274
|
+
const state = (JSON.parse(r1.body) as { result: { requestState: string } }).result
|
|
275
|
+
.requestState;
|
|
276
|
+
expect(records).toEqual([]);
|
|
277
|
+
|
|
278
|
+
const retry = modernCall({ id: 2, tool: 'purge', state, action });
|
|
279
|
+
const a = await silent(retry);
|
|
280
|
+
const b = await audited(retry);
|
|
281
|
+
expect(b).toEqual(a);
|
|
282
|
+
expect(b.body).toContain('confirmation declined');
|
|
283
|
+
expect(records).toHaveLength(1);
|
|
284
|
+
expectRecord(records[0], {
|
|
285
|
+
path: ['purge'],
|
|
286
|
+
specVersion: MCP_SPEC_2026_07_28,
|
|
287
|
+
error: MCP_AUDIT_CONFIRMATION_DECLINED,
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
it('state rejected: one record carrying mcp_confirm_rejection, wire shape unchanged', async () => {
|
|
293
|
+
const { records, audit } = recorder();
|
|
294
|
+
const handler = createMcpHandler(mrtrLockBridge().bridge, {
|
|
295
|
+
confirmationKey: KEY,
|
|
296
|
+
audit,
|
|
297
|
+
});
|
|
298
|
+
const r1 = await handler(modernCall({ id: 1, tool: 'purge' }));
|
|
299
|
+
const state = (JSON.parse(r1.body) as { result: { requestState: string } }).result
|
|
300
|
+
.requestState;
|
|
301
|
+
const forged = state.slice(0, -1) + (state.endsWith('A') ? 'B' : 'A');
|
|
302
|
+
|
|
303
|
+
const r2 = await handler(modernCall({ id: 2, tool: 'purge', state: forged, action: 'accept' }));
|
|
304
|
+
const result = (JSON.parse(r2.body) as { result: Record<string, unknown> }).result;
|
|
305
|
+
// Re-prompted with fresh state, exactly as without the hook.
|
|
306
|
+
expect(result.resultType).toBe('input_required');
|
|
307
|
+
expect(result.requestState).not.toBe(forged);
|
|
308
|
+
expect(records).toHaveLength(1);
|
|
309
|
+
expectRecord(records[0], {
|
|
310
|
+
path: ['purge'],
|
|
311
|
+
specVersion: MCP_SPEC_2026_07_28,
|
|
312
|
+
error: MCP_AUDIT_CONFIRMATION_STATE_REJECTED,
|
|
313
|
+
extra: { mcp_confirm_rejection: MCP_CONFIRM_REJECTION_STATE },
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it('an authentic but expired state re-prompts without a record', async () => {
|
|
318
|
+
const { records, audit } = recorder();
|
|
319
|
+
const handler = createMcpHandler(mrtrLockBridge().bridge, {
|
|
320
|
+
confirmationKey: KEY,
|
|
321
|
+
audit,
|
|
322
|
+
});
|
|
323
|
+
// Minted under the mount's key for this exact binding: tool
|
|
324
|
+
// "purge", no arguments, no Authorization header. An hour stale.
|
|
325
|
+
const stale = mintMcpConfirmState(
|
|
326
|
+
new TextEncoder().encode(KEY),
|
|
327
|
+
{ tool: 'purge', argsDigest: mcpConfirmArgsDigest({}), principal: '' },
|
|
328
|
+
Math.floor(Date.now() / 1000) - 3600,
|
|
329
|
+
);
|
|
330
|
+
const res = await handler(modernCall({ tool: 'purge', state: stale, action: 'accept' }));
|
|
331
|
+
const result = (JSON.parse(res.body) as { result: Record<string, unknown> }).result;
|
|
332
|
+
expect(result.resultType).toBe('input_required');
|
|
333
|
+
expect(records).toEqual([]);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it('destructive ceiling: one record, wire unchanged', async () => {
|
|
337
|
+
const { res, records } = await sameWire(modernLockBridge, {}, [
|
|
338
|
+
modernCall({ tool: 'widget.delete' }),
|
|
339
|
+
]);
|
|
340
|
+
expect(res.status).toBe(200);
|
|
341
|
+
expect(res.body).toContain('destructive command blocked');
|
|
342
|
+
expect(records).toHaveLength(1);
|
|
343
|
+
expectRecord(records[0], {
|
|
344
|
+
path: ['widget', 'delete'],
|
|
345
|
+
specVersion: MCP_SPEC_2026_07_28,
|
|
346
|
+
error: MCP_AUDIT_DESTRUCTIVE_BLOCKED,
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it('destructive ceiling behind an accepted confirmation is still recorded', async () => {
|
|
351
|
+
const { records, audit } = recorder();
|
|
352
|
+
const handler = createMcpHandler(mrtrLockBridge().bridge, {
|
|
353
|
+
confirmationKey: KEY,
|
|
354
|
+
audit,
|
|
355
|
+
});
|
|
356
|
+
const r1 = await handler(modernCall({ id: 1, tool: 'vault.burn' }));
|
|
357
|
+
const state = (JSON.parse(r1.body) as { result: { requestState: string } }).result
|
|
358
|
+
.requestState;
|
|
359
|
+
const r2 = await handler(modernCall({ id: 2, tool: 'vault.burn', state, action: 'accept' }));
|
|
360
|
+
expect(r2.body).toContain('destructive command blocked');
|
|
361
|
+
expect(records).toHaveLength(1);
|
|
362
|
+
expectRecord(records[0], {
|
|
363
|
+
path: ['vault', 'burn'],
|
|
364
|
+
specVersion: MCP_SPEC_2026_07_28,
|
|
365
|
+
error: MCP_AUDIT_DESTRUCTIVE_BLOCKED,
|
|
366
|
+
});
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it('a successful call, a satisfied gate and an accepted prompt record nothing', async () => {
|
|
370
|
+
const { records, audit } = recorder();
|
|
371
|
+
const handler = createMcpHandler(mrtrLockBridge().bridge, {
|
|
372
|
+
confirmationKey: KEY,
|
|
373
|
+
audit,
|
|
374
|
+
});
|
|
375
|
+
const r1 = await handler(modernCall({ id: 1, tool: 'purge' }));
|
|
376
|
+
const state = (JSON.parse(r1.body) as { result: { requestState: string } }).result
|
|
377
|
+
.requestState;
|
|
378
|
+
const r2 = await handler(modernCall({ id: 2, tool: 'purge', state, action: 'accept' }));
|
|
379
|
+
expect(r2.body).toContain('"isError":false');
|
|
380
|
+
const r3 = await handler(
|
|
381
|
+
modernCall({ id: 3, tool: 'purge', meta: meta(), headers: { 'X-Confirm-Token': 'yes' } }),
|
|
382
|
+
);
|
|
383
|
+
expect(r3.body).toContain('"isError":false');
|
|
384
|
+
expect(records).toEqual([]);
|
|
385
|
+
|
|
386
|
+
const { records: plain, audit: auditPlain } = recorder();
|
|
387
|
+
await createMcpHandler(modernLockBridge(), { audit: auditPlain })(modernCall({ tool: 'ping' }));
|
|
388
|
+
expect(plain).toEqual([]);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it('a throwing hook leaves the response unchanged', async () => {
|
|
392
|
+
const req = modernCall({ tool: 'secret' });
|
|
393
|
+
const silent = await createMcpHandler(modernLockBridge())(req);
|
|
394
|
+
const loud = await createMcpHandler(modernLockBridge(), {
|
|
395
|
+
audit: () => {
|
|
396
|
+
throw new Error('sink down');
|
|
397
|
+
},
|
|
398
|
+
})(req);
|
|
399
|
+
expect(loud).toEqual(silent);
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
describe('audit hook — ElicitationConfirmGate keeps its in-memory rejections', () => {
|
|
404
|
+
const leaf = {
|
|
405
|
+
path: ['purge'],
|
|
406
|
+
description: 'Purge a target',
|
|
407
|
+
properties: {},
|
|
408
|
+
class: { requiresConfirmation: true },
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
it('a rejected state reaches both the rejections list and the hook', () => {
|
|
412
|
+
const { records, audit } = recorder();
|
|
413
|
+
const gate = new ElicitationConfirmGate(new TextEncoder().encode(KEY), undefined, audit);
|
|
414
|
+
const decision = gate.evaluate(
|
|
415
|
+
{ method: 'POST', headers: {}, body: '' },
|
|
416
|
+
leaf,
|
|
417
|
+
{ name: 'purge', _meta: meta({}), requestState: 'v1.99999999999.AAAA' },
|
|
418
|
+
);
|
|
419
|
+
expect(decision?.result.resultType).toBe('input_required');
|
|
420
|
+
expect(gate.rejections).toEqual([
|
|
421
|
+
{ path: 'purge', mcpConfirmRejection: MCP_CONFIRM_REJECTION_STATE },
|
|
422
|
+
]);
|
|
423
|
+
expect(records).toHaveLength(1);
|
|
424
|
+
expectRecord(records[0], {
|
|
425
|
+
path: ['purge'],
|
|
426
|
+
specVersion: MCP_SPEC_2026_07_28,
|
|
427
|
+
error: MCP_AUDIT_CONFIRMATION_STATE_REJECTED,
|
|
428
|
+
extra: { mcp_confirm_rejection: MCP_CONFIRM_REJECTION_STATE },
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
it('a throwing hook still records the rejection in memory', () => {
|
|
433
|
+
const gate = new ElicitationConfirmGate(new TextEncoder().encode(KEY), undefined, () => {
|
|
434
|
+
throw new Error('sink down');
|
|
435
|
+
});
|
|
436
|
+
const decision = gate.evaluate(
|
|
437
|
+
{ method: 'POST', headers: {}, body: '' },
|
|
438
|
+
leaf,
|
|
439
|
+
{ name: 'purge', _meta: meta({}), requestState: 'v1.99999999999.AAAA' },
|
|
440
|
+
);
|
|
441
|
+
expect(decision?.result.resultType).toBe('input_required');
|
|
442
|
+
expect(gate.rejections).toHaveLength(1);
|
|
443
|
+
});
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
describe('audit hook — plumbing', () => {
|
|
447
|
+
it('the resolved mount config carries the hook', () => {
|
|
448
|
+
const { audit } = recorder();
|
|
449
|
+
expect(mcpMountConfig({ audit }).audit).toBe(audit);
|
|
450
|
+
expect(mcpMountConfig({}).audit).toBeUndefined();
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
it('serveStdio records a rejected state for a bridge with mount options', async () => {
|
|
454
|
+
const { records, audit } = recorder();
|
|
455
|
+
const input = new PassThrough();
|
|
456
|
+
const output = new PassThrough();
|
|
457
|
+
let out = '';
|
|
458
|
+
output.on('data', (c: Buffer) => (out += c.toString('utf8')));
|
|
459
|
+
const lines = () => out.split('\n').filter((l) => l !== '');
|
|
460
|
+
const server = serveStdio(mrtrLockBridge().bridge, {
|
|
461
|
+
input,
|
|
462
|
+
output,
|
|
463
|
+
log: new PassThrough(),
|
|
464
|
+
mount: { confirmationKey: KEY, audit },
|
|
465
|
+
});
|
|
466
|
+
const call = (id: number, extra: Record<string, unknown>) =>
|
|
467
|
+
JSON.stringify({
|
|
468
|
+
jsonrpc: '2.0',
|
|
469
|
+
id,
|
|
470
|
+
method: 'tools/call',
|
|
471
|
+
params: { name: 'purge', _meta: meta({}), ...extra },
|
|
472
|
+
});
|
|
473
|
+
const reply = async (n: number) => {
|
|
474
|
+
while (lines().length < n) await new Promise((r) => setTimeout(r, 1));
|
|
475
|
+
return JSON.parse(lines()[n - 1] ?? '{}') as { result: Record<string, unknown> };
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
input.write(`${call(1, {})}\n`);
|
|
479
|
+
const r1 = await reply(1);
|
|
480
|
+
const state = String(r1.result.requestState);
|
|
481
|
+
const forged = state.slice(0, -1) + (state.endsWith('A') ? 'B' : 'A');
|
|
482
|
+
expect(records).toEqual([]);
|
|
483
|
+
|
|
484
|
+
input.write(
|
|
485
|
+
`${call(2, { requestState: forged, inputResponses: { confirm: { action: 'accept' } } })}\n`,
|
|
486
|
+
);
|
|
487
|
+
const r2 = await reply(2);
|
|
488
|
+
expect(r2.result.resultType).toBe('input_required');
|
|
489
|
+
input.end();
|
|
490
|
+
await server.done;
|
|
491
|
+
|
|
492
|
+
expect(records).toHaveLength(1);
|
|
493
|
+
expectRecord(records[0], {
|
|
494
|
+
path: ['purge'],
|
|
495
|
+
specVersion: MCP_SPEC_2026_07_28,
|
|
496
|
+
error: MCP_AUDIT_CONFIRMATION_STATE_REJECTED,
|
|
497
|
+
extra: { mcp_confirm_rejection: MCP_CONFIRM_REJECTION_STATE },
|
|
498
|
+
});
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
it('serveStdio reuses a createMcpHandler handler hook included', async () => {
|
|
502
|
+
const { records, audit } = recorder();
|
|
503
|
+
const handler = createMcpHandler(legacyLockBridge(), { audit });
|
|
504
|
+
const input = new PassThrough();
|
|
505
|
+
const output = new PassThrough();
|
|
506
|
+
let out = '';
|
|
507
|
+
output.on('data', (c: Buffer) => (out += c.toString('utf8')));
|
|
508
|
+
const server = serveStdio(handler, { input, output, log: new PassThrough() });
|
|
509
|
+
input.write('{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"secret"}}\n');
|
|
510
|
+
input.end();
|
|
511
|
+
await server.done;
|
|
512
|
+
expect(out).toContain('authentication required');
|
|
513
|
+
expect(records).toHaveLength(1);
|
|
514
|
+
expectRecord(records[0], {
|
|
515
|
+
path: ['secret'],
|
|
516
|
+
specVersion: MCP_SPEC_2024_11_05,
|
|
517
|
+
error: MCP_AUDIT_AUTH_REFUSED,
|
|
518
|
+
});
|
|
519
|
+
});
|
|
520
|
+
});
|