@hop-top/kit 0.5.0-alpha.1 → 0.5.0-alpha.11
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 +275 -0
- package/LICENSE +1 -1
- package/LICENSE-cobra.txt +174 -0
- package/NOTICE +15 -0
- package/README.md +55 -259
- package/dist/aim-DZHwwiIc.d.ts +139 -0
- package/dist/aim.d.ts +1 -116
- package/dist/aim.js +13 -414
- package/dist/alias-D9_qFlyP.d.ts +174 -0
- package/dist/alias.d.ts +2 -73
- package/dist/alias.js +27 -207
- package/dist/api-DAexsu2H.d.ts +77 -0
- package/dist/api.d.ts +1 -62
- package/dist/api.js +10 -164
- package/dist/auth-2-jfgiHn.d.ts +34 -0
- package/dist/auth.d.ts +1 -26
- package/dist/auth.js +6 -40
- package/dist/chunk-3FIKGFKI.js +54 -0
- package/dist/chunk-4FMALWYB.js +669 -0
- package/dist/chunk-4GPCWBBA.js +143 -0
- package/dist/chunk-4UE5TCMQ.js +104 -0
- package/dist/chunk-65VPWMPV.js +1134 -0
- package/dist/chunk-6HFAEZ52.js +29 -0
- package/dist/chunk-6SVOHNIC.js +17 -0
- package/dist/chunk-BDNNPRX6.js +41 -0
- package/dist/chunk-BRNEKPWX.js +37 -0
- package/dist/chunk-BXJ2J27B.js +219 -0
- package/dist/chunk-CSJWB6D3.js +151 -0
- package/dist/chunk-CXBZTONY.js +65 -0
- package/dist/chunk-EE4M2UOF.js +818 -0
- package/dist/chunk-GFRAYYRD.js +817 -0
- package/dist/chunk-IVQHLI56.js +1890 -0
- package/dist/chunk-IZ6Q4SE3.js +178 -0
- package/dist/chunk-JKMQSAJP.js +174 -0
- package/dist/chunk-K7E34Q3A.js +1445 -0
- package/dist/chunk-KVG7YCTS.js +115 -0
- package/dist/chunk-L23O2FFE.js +51 -0
- package/dist/chunk-MFVUENZR.js +1549 -0
- package/dist/chunk-ML7D5DWY.js +263 -0
- package/dist/chunk-MQXYDYRD.js +968 -0
- package/dist/chunk-POR5L2DG.js +247 -0
- package/dist/chunk-QJVSKKRQ.js +369 -0
- package/dist/chunk-QQQYCWSC.js +240 -0
- package/dist/chunk-R5U7XKVJ.js +16 -0
- package/dist/chunk-RYKLVOVK.js +309 -0
- package/dist/chunk-SNVGDYX7.js +187 -0
- package/dist/chunk-T6OZMPPV.js +68 -0
- package/dist/chunk-UKZIAQA7.js +26 -0
- package/dist/chunk-VGOA5Y7S.js +251 -0
- package/dist/chunk-VWGP4NVJ.js +513 -0
- package/dist/chunk-WBVO43HU.js +355 -0
- package/dist/chunk-WUOAQVOA.js +86 -0
- package/dist/chunk-XJTRY453.js +868 -0
- package/dist/chunk-YOBAUCVN.js +583 -0
- package/dist/cli-RSRk1M36.d.ts +707 -0
- package/dist/cli.d.ts +7 -309
- package/dist/cli.js +125 -945
- package/dist/completion-csVun6SP.d.ts +112 -0
- package/dist/completion-values-By-mnjNt.d.ts +142 -0
- package/dist/completion-values.d.ts +1 -0
- package/dist/completion-values.js +29 -0
- package/dist/completion.d.ts +3 -0
- package/dist/completion.js +34 -0
- package/dist/compliance-AxFf-uMy.d.ts +60 -0
- package/dist/compliance.d.ts +1 -0
- package/dist/compliance.js +17 -0
- package/dist/config-CaK60ByQ.d.ts +241 -0
- package/dist/config.d.ts +1 -97
- package/dist/config.js +8 -78
- package/dist/errcorrect-BcX1Ft0K.d.ts +44 -0
- package/dist/errcorrect.d.ts +1 -37
- package/dist/errcorrect.js +8 -68
- package/dist/error-Do8Qji43.d.ts +231 -0
- package/dist/id/index.d.ts +2 -83
- package/dist/id/index.js +14 -65
- package/dist/index-BKJZClJx.d.ts +1486 -0
- package/dist/index-C3Zw4wDJ.d.ts +359 -0
- package/dist/index-CGE5yLBj.d.ts +328 -0
- package/dist/index-VLDcI5NF.d.ts +95 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +132 -0
- package/dist/llm-DYuR7_uX.d.ts +1069 -0
- package/dist/llm-anthropic-Cd_I2xXB.d.ts +67 -0
- package/dist/llm-anthropic.d.ts +2 -0
- package/dist/llm-anthropic.js +13 -0
- package/dist/llm-openai-DyFrucmV.d.ts +87 -0
- package/dist/llm-openai.d.ts +2 -0
- package/dist/llm-openai.js +19 -0
- package/dist/llm.d.ts +1 -188
- package/dist/llm.js +101 -356
- package/dist/log-CkaxMX3U.d.ts +115 -0
- package/dist/log.d.ts +2 -0
- package/dist/log.js +14 -0
- package/dist/mcp/index.d.ts +4 -0
- package/dist/mcp/index.js +185 -0
- package/dist/netpolicy-Brx5XZUB.d.ts +151 -0
- package/dist/netpolicy.d.ts +1 -0
- package/dist/netpolicy.js +25 -0
- package/dist/output-CjZWmL8s.d.ts +420 -0
- package/dist/output.d.ts +3 -213
- package/dist/output.js +135 -447
- package/dist/parity-CvMuYcVI.d.ts +61 -0
- package/dist/progress-CAzyxeWl.d.ts +38 -0
- package/dist/progress.d.ts +1 -30
- package/dist/progress.js +6 -55
- package/dist/provenance-SMFHm5gx.d.ts +30 -0
- package/dist/provenance.d.ts +1 -24
- package/dist/provenance.js +6 -31
- package/dist/routellm-B0nv59mH.d.ts +111 -0
- package/dist/routellm.d.ts +2 -64
- package/dist/routellm.js +13 -255
- package/dist/rpc-D31Lg0Kc.d.ts +32 -0
- package/dist/rpc.d.ts +2 -25
- package/dist/rpc.js +4 -98
- package/dist/safety-CUaYEjZc.d.ts +30 -0
- package/dist/safety.d.ts +1 -24
- package/dist/safety.js +6 -43
- package/dist/scope-BrhYB4ij.d.ts +141 -0
- package/dist/scope.d.ts +1 -121
- package/dist/scope.js +20 -377
- package/dist/serve-D8yWMrTc.d.ts +646 -0
- package/dist/serve.d.ts +3 -0
- package/dist/serve.js +75 -0
- package/dist/sqlstore.js +6 -39
- package/dist/stream-CrjeCp0M.d.ts +44 -0
- package/dist/stream.d.ts +1 -35
- package/dist/stream.js +8 -51
- package/dist/telemetry/index.d.ts +1 -315
- package/dist/telemetry/index.js +26 -599
- package/dist/toolspec-DcPwwoAZ.d.ts +368 -0
- package/dist/toolspec.d.ts +2 -0
- package/dist/toolspec.js +37 -0
- package/dist/tui/index.d.ts +6 -298
- package/dist/tui/index.js +17 -381
- package/dist/upgrade-C3RWGwRi.d.ts +125 -0
- package/dist/upgrade.d.ts +1 -95
- package/dist/upgrade.js +6 -114
- package/dist/xdg-DB7Shoib.d.ts +179 -0
- package/dist/xdg.d.ts +1 -169
- package/dist/xdg.js +14 -128
- package/package.json +192 -24
- package/src/README.md +15 -0
- package/src/alias.conformance.test.ts +250 -0
- package/src/alias.test.ts +206 -2
- package/src/alias.ts +196 -21
- package/src/cli.help-command.test.ts +105 -0
- package/src/cli.offline.test.ts +93 -0
- package/src/cli.parity.test.ts +192 -0
- package/src/cli.root.conformance.test.ts +166 -0
- package/src/cli.test.ts +95 -1
- package/src/cli.ts +234 -25
- package/src/completion-scripts.ts +16 -0
- package/src/completion-values.test.ts +76 -16
- package/src/completion-values.ts +170 -66
- package/src/completion.conformance.test.ts +298 -0
- package/src/completion.test.ts +268 -145
- package/src/completion.ts +772 -121
- package/src/compliance.test.ts +196 -2
- package/src/compliance.ts +440 -20
- package/src/config-merge.test.ts +425 -0
- package/src/config.test.ts +228 -1
- package/src/config.ts +429 -16
- package/src/delegation.test.ts +299 -0
- package/src/delegation.ts +807 -0
- package/src/gen/crud_pb.ts +7 -7
- package/src/id/README.md +38 -0
- package/src/id/contract.test.ts +1 -1
- package/src/id/index.test.ts +1 -1
- package/src/id/index.ts +3 -2
- package/src/index.test.ts +71 -0
- package/src/index.ts +46 -0
- package/src/llm-anthropic.conformance.test.ts +1135 -0
- package/src/llm-anthropic.test.ts +152 -0
- package/src/llm-anthropic.ts +940 -0
- package/src/llm-choice-hermetic.test.ts +335 -0
- package/src/llm-config.test.ts +232 -0
- package/src/llm-config.ts +106 -0
- package/src/llm-endpoint.ts +254 -0
- package/src/llm-error.ts +17 -0
- package/src/llm-errors.ts +152 -0
- package/src/llm-file.ts +126 -0
- package/src/llm-http.ts +317 -0
- package/src/llm-keys.ts +167 -0
- package/src/llm-media.conformance.test.ts +87 -0
- package/src/llm-media.test.ts +279 -0
- package/src/llm-media.ts +322 -0
- package/src/llm-openai.conformance.test.ts +407 -0
- package/src/llm-openai.test.ts +510 -0
- package/src/llm-openai.ts +708 -0
- package/src/llm-payload.ts +155 -0
- package/src/llm-quote.ts +46 -0
- package/src/llm-routellm.conformance.test.ts +44 -0
- package/src/llm-toolnames.conformance.test.ts +112 -0
- package/src/llm-toolnames.test.ts +132 -0
- package/src/llm-toolnames.ts +298 -0
- package/src/llm-uri.ts +99 -0
- package/src/llm.test.ts +43 -15
- package/src/llm.toolcall.test.ts +175 -0
- package/src/llm.ts +602 -190
- package/src/log.parity.test.ts +105 -0
- package/src/log.test.ts +270 -1
- package/src/log.ts +228 -49
- package/src/mcp/README.md +63 -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 +250 -0
- package/src/mcp/bridge.ts +286 -0
- package/src/mcp/conformance.test.ts +357 -0
- package/src/mcp/dispatch.ts +359 -0
- package/src/mcp/identity.test.ts +166 -0
- package/src/mcp/identity.ts +72 -0
- package/src/mcp/index.ts +176 -0
- package/src/mcp/legacy-notify.test.ts +59 -0
- package/src/mcp/legacy.ts +367 -0
- package/src/mcp/mcp.test.ts +707 -0
- package/src/mcp/modern-confirm.test.ts +520 -0
- package/src/mcp/modern-confirm.ts +484 -0
- package/src/mcp/modern.ts +788 -0
- package/src/mcp/stdio.test.ts +313 -0
- package/src/mcp/stdio.ts +254 -0
- package/src/mcp/tasks.ts +82 -0
- package/src/mcp/testtree.ts +250 -0
- package/src/mcp/toolname.test.ts +147 -0
- package/src/mcp/types.ts +689 -0
- package/src/netpolicy.test.ts +280 -0
- package/src/netpolicy.ts +256 -0
- package/src/output/README.md +46 -0
- package/src/output/builtins.ts +5 -5
- package/src/output/dispatch.ts +10 -2
- package/src/output/error.ts +459 -0
- package/src/output/formatter.ts +29 -5
- package/src/output/formatters/README.md +50 -0
- 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 +64 -3
- package/src/plan.ts +89 -0
- package/src/policy.ts +472 -0
- package/src/published-types.test.ts +133 -0
- package/src/routellm.test.ts +209 -28
- package/src/routellm.toolcall.test.ts +521 -0
- package/src/routellm.ts +213 -177
- package/src/router/README.md +44 -0
- package/src/router/server.test.ts +41 -0
- package/src/router/server.ts +11 -0
- package/src/safety.test.ts +19 -9
- package/src/scope-defaults.json +29 -29
- package/src/serve.test.ts +1257 -0
- package/src/serve.ts +1665 -0
- package/src/sideeffect.ts +105 -0
- package/src/telemetry/README.md +48 -0
- package/src/telemetry/client.test.ts +31 -0
- package/src/telemetry/client.ts +12 -6
- package/src/telemetry/offline.test.ts +64 -0
- package/src/toolspec-internal.ts +279 -0
- package/src/toolspec-manifest.test.ts +167 -0
- package/src/toolspec-manifest.ts +415 -0
- package/src/toolspec-spec-schemas.ts +12 -0
- package/src/toolspec-spec.test.ts +273 -0
- package/src/toolspec-spec.ts +458 -0
- package/src/toolspec.from-command.test.ts +350 -0
- package/src/toolspec.test.ts +2 -2
- package/src/toolspec.ts +222 -0
- package/src/triton/README.md +34 -0
- package/src/tui/README.md +42 -0
- package/src/tui/parity.test.ts +120 -31
- package/src/tui/parity.ts +58 -7
- package/src/upgrade.test.ts +291 -0
- package/src/upgrade.ts +116 -7
- package/src/wizard.test.ts +7 -7
- package/test/llm/conformance-helpers.ts +100 -0
- package/test/output/csv.test.ts +164 -0
- package/test/output/dispatch.test.ts +1 -2
- package/test/output/error.test.ts +277 -0
- package/test/output/smoke-capture.ts +2 -2
- package/test/output/smoke.test.ts +2 -2
- package/test/output/taxonomy-contract.test.ts +148 -0
- package/test/setup-env.ts +16 -0
- package/test/toolspec/fixture.ts +79 -0
- package/tsconfig.json +3 -1
- package/tsconfig.test.json +10 -0
- package/vitest.config.ts +9 -0
- package/src/tui/parity.json +0 -74
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Coverage for the commander bridge adapter: leaf enumeration, flag
|
|
3
|
+
* schema derivation, annotation classification, and the destructive
|
|
4
|
+
* ceiling on the invoke path.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Command, Option } from 'commander';
|
|
8
|
+
import { describe, expect, it } from 'vitest';
|
|
9
|
+
|
|
10
|
+
import { commanderBridge, type AnnotatedCommand } from './bridge.js';
|
|
11
|
+
import { createMcpHandler } from './dispatch.js';
|
|
12
|
+
import { SURFACE_MCP, type InvokeResult } from './types.js';
|
|
13
|
+
|
|
14
|
+
function annotate(cmd: Command, ann: Record<string, string>): Command {
|
|
15
|
+
(cmd as AnnotatedCommand).annotations = ann;
|
|
16
|
+
return cmd;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** root ├── widget {add,delete} ├── secret ├── ping */
|
|
20
|
+
function buildTree(): Command {
|
|
21
|
+
const root = new Command('root');
|
|
22
|
+
|
|
23
|
+
const widget = new Command('widget');
|
|
24
|
+
const add = new Command('add').description('Add a widget');
|
|
25
|
+
add.requiredOption('--name <n>', 'widget name');
|
|
26
|
+
add.option('--count <n>', 'widget count');
|
|
27
|
+
add.option('--force', 'force flag');
|
|
28
|
+
add.option('--tag <t...>', 'tag list');
|
|
29
|
+
const hidden = new Option('--hidden-flag <v>', 'should be hidden').hideHelp();
|
|
30
|
+
add.addOption(hidden);
|
|
31
|
+
annotate(add, { 'kit/side-effect': 'write' });
|
|
32
|
+
widget.addCommand(add);
|
|
33
|
+
|
|
34
|
+
const del = new Command('delete').description('Delete a widget');
|
|
35
|
+
annotate(del, { 'kit/side-effect': 'destructive' });
|
|
36
|
+
widget.addCommand(del);
|
|
37
|
+
root.addCommand(widget);
|
|
38
|
+
|
|
39
|
+
const secret = new Command('secret').description('Locked');
|
|
40
|
+
annotate(secret, { 'kit/auth-required': 'true' });
|
|
41
|
+
root.addCommand(secret);
|
|
42
|
+
|
|
43
|
+
const ping = new Command('ping').description('Ping the server');
|
|
44
|
+
annotate(ping, { 'kit/side-effect': 'read' });
|
|
45
|
+
root.addCommand(ping);
|
|
46
|
+
|
|
47
|
+
return root;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const run = (): InvokeResult => ({ stdout: 'ok\n', exitCode: 0 });
|
|
51
|
+
|
|
52
|
+
describe('commanderBridge', () => {
|
|
53
|
+
it('enumerates leaves as dotted paths, excluding groups and the root', () => {
|
|
54
|
+
const bridge = commanderBridge(buildTree(), { run });
|
|
55
|
+
expect(bridge.leaves().map((l) => l.path.join('.'))).toEqual([
|
|
56
|
+
'widget.add',
|
|
57
|
+
'widget.delete',
|
|
58
|
+
'secret',
|
|
59
|
+
'ping',
|
|
60
|
+
]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('derives a JSON Schema from options, excluding hidden ones', () => {
|
|
64
|
+
const bridge = commanderBridge(buildTree(), { run });
|
|
65
|
+
const add = bridge.leaves().find((l) => l.path.join('.') === 'widget.add');
|
|
66
|
+
expect(add?.properties).toEqual({
|
|
67
|
+
name: { type: 'string', description: 'widget name' },
|
|
68
|
+
count: { type: 'string', description: 'widget count' },
|
|
69
|
+
force: { type: 'boolean', description: 'force flag' },
|
|
70
|
+
tag: { type: 'array', description: 'tag list', items: { type: 'string' } },
|
|
71
|
+
});
|
|
72
|
+
expect(add?.required).toEqual(['name']);
|
|
73
|
+
expect(add?.properties).not.toHaveProperty('hidden-flag');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('types a negated flag as a boolean under its own long name', () => {
|
|
77
|
+
const root = new Command('root');
|
|
78
|
+
root.command('find').option('--no-color', 'plain output');
|
|
79
|
+
const bridge = commanderBridge(root, { run });
|
|
80
|
+
expect(bridge.leaves()[0]?.properties).toEqual({
|
|
81
|
+
'no-color': { type: 'boolean', description: 'plain output' },
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('keeps a flag and its negation as two booleans', () => {
|
|
86
|
+
const root = new Command('root');
|
|
87
|
+
root.command('find').option('--cache', 'use cache').option('--no-cache', 'skip cache');
|
|
88
|
+
const bridge = commanderBridge(root, { run });
|
|
89
|
+
expect(bridge.leaves()[0]?.properties).toEqual({
|
|
90
|
+
cache: { type: 'boolean', description: 'use cache' },
|
|
91
|
+
'no-cache': { type: 'boolean', description: 'skip cache' },
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('types a repeatable accumulator option as an array of strings', () => {
|
|
96
|
+
const root = new Command('root');
|
|
97
|
+
root
|
|
98
|
+
.command('find')
|
|
99
|
+
.option('-m, --match <p>', 'match pattern', (v: string, p: string[]) => p.concat([v]), []);
|
|
100
|
+
const bridge = commanderBridge(root, { run });
|
|
101
|
+
expect(bridge.leaves()[0]?.properties).toEqual({
|
|
102
|
+
match: {
|
|
103
|
+
type: 'array',
|
|
104
|
+
description: 'match pattern',
|
|
105
|
+
items: { type: 'string' },
|
|
106
|
+
default: [],
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('keeps a custom-parsed scalar option a string', () => {
|
|
112
|
+
const root = new Command('root');
|
|
113
|
+
root.command('find').option('--limit <n>', 'max rows', (v: string) => Number(v));
|
|
114
|
+
const bridge = commanderBridge(root, { run });
|
|
115
|
+
expect(bridge.leaves()[0]?.properties).toEqual({
|
|
116
|
+
limit: { type: 'string', description: 'max rows' },
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('publishes choices as enum and string defaults as default', () => {
|
|
121
|
+
const root = new Command('root');
|
|
122
|
+
root
|
|
123
|
+
.command('find')
|
|
124
|
+
.addOption(new Option('--color <c>', 'tint').choices(['red', 'blue']).default('red'))
|
|
125
|
+
.addOption(new Option('--tag <t...>', 'tags').choices(['a', 'b']))
|
|
126
|
+
.option('--sort <k>', 'sort key', 'name');
|
|
127
|
+
const bridge = commanderBridge(root, { run });
|
|
128
|
+
expect(bridge.leaves()[0]?.properties).toEqual({
|
|
129
|
+
color: { type: 'string', description: 'tint', enum: ['red', 'blue'], default: 'red' },
|
|
130
|
+
tag: { type: 'array', description: 'tags', items: { type: 'string', enum: ['a', 'b'] } },
|
|
131
|
+
sort: { type: 'string', description: 'sort key', default: 'name' },
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('omits a default whose type does not match the schema type', () => {
|
|
136
|
+
const root = new Command('root');
|
|
137
|
+
root
|
|
138
|
+
.command('find')
|
|
139
|
+
.option('--limit <n>', 'max rows', (v: string) => Number(v), 10)
|
|
140
|
+
.option('--fast', 'go fast', false);
|
|
141
|
+
const bridge = commanderBridge(root, { run });
|
|
142
|
+
expect(bridge.leaves()[0]?.properties).toEqual({
|
|
143
|
+
limit: { type: 'string', description: 'max rows' },
|
|
144
|
+
fast: { type: 'boolean', description: 'go fast', default: false },
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('classifies kit annotations into the SafetyClass', () => {
|
|
149
|
+
const bridge = commanderBridge(buildTree(), { run });
|
|
150
|
+
const byName = (n: string) =>
|
|
151
|
+
bridge.leaves().find((l) => l.path.join('.') === n);
|
|
152
|
+
expect(byName('widget.delete')?.class?.destructive).toBe(true);
|
|
153
|
+
expect(byName('secret')?.class?.authRequired).toBe(true);
|
|
154
|
+
expect(byName('ping')?.class?.destructive).toBeUndefined();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('blocks a destructive leaf on MCP under the default policy', async () => {
|
|
158
|
+
const bridge = commanderBridge(buildTree(), { run });
|
|
159
|
+
await expect(
|
|
160
|
+
bridge.invoke({
|
|
161
|
+
path: ['widget', 'delete'],
|
|
162
|
+
meta: { surface: SURFACE_MCP, requestedAt: new Date() },
|
|
163
|
+
}),
|
|
164
|
+
).rejects.toThrow(/destructive command blocked on this surface/);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('allows the same leaf once MCP is named in allowDestructiveOn', async () => {
|
|
168
|
+
const bridge = commanderBridge(buildTree(), {
|
|
169
|
+
run,
|
|
170
|
+
policy: { allowDestructiveOn: [SURFACE_MCP] },
|
|
171
|
+
});
|
|
172
|
+
await expect(
|
|
173
|
+
bridge.invoke({
|
|
174
|
+
path: ['widget', 'delete'],
|
|
175
|
+
meta: { surface: SURFACE_MCP, requestedAt: new Date() },
|
|
176
|
+
}),
|
|
177
|
+
).resolves.toEqual({ stdout: 'ok\n', exitCode: 0 });
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('rejects an unknown leaf path', async () => {
|
|
181
|
+
const bridge = commanderBridge(buildTree(), { run });
|
|
182
|
+
await expect(
|
|
183
|
+
bridge.invoke({
|
|
184
|
+
path: ['nope'],
|
|
185
|
+
meta: { surface: SURFACE_MCP, requestedAt: new Date() },
|
|
186
|
+
}),
|
|
187
|
+
).rejects.toThrow(/unknown command/);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('serves a real tools/list through the MCP handler', async () => {
|
|
191
|
+
const handler = createMcpHandler(commanderBridge(buildTree(), { run }));
|
|
192
|
+
const res = await handler({
|
|
193
|
+
method: 'POST',
|
|
194
|
+
headers: {},
|
|
195
|
+
body: '{"jsonrpc":"2.0","id":1,"method":"tools/list"}',
|
|
196
|
+
});
|
|
197
|
+
expect(res.status).toBe(200);
|
|
198
|
+
const body = JSON.parse(res.body) as {
|
|
199
|
+
result: { tools: Array<{ name: string }> };
|
|
200
|
+
};
|
|
201
|
+
expect(body.result.tools.map((t) => t.name)).toEqual([
|
|
202
|
+
'widget.add',
|
|
203
|
+
'widget.delete',
|
|
204
|
+
'secret',
|
|
205
|
+
'ping',
|
|
206
|
+
]);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('renders a destructive block as an isError result at HTTP 200', async () => {
|
|
210
|
+
const handler = createMcpHandler(commanderBridge(buildTree(), { run }));
|
|
211
|
+
const res = await handler({
|
|
212
|
+
method: 'POST',
|
|
213
|
+
headers: {},
|
|
214
|
+
body: '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"widget.delete"}}',
|
|
215
|
+
});
|
|
216
|
+
expect(res.status).toBe(200);
|
|
217
|
+
expect(res.body).toContain('"isError":true');
|
|
218
|
+
expect(res.body).toContain(
|
|
219
|
+
'destructive command blocked on this surface: widget delete on mcp',
|
|
220
|
+
);
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
describe('classify: destructive-name inference', () => {
|
|
225
|
+
const leafOf = (name: string, ann?: Record<string, string>) => {
|
|
226
|
+
const root = new Command('root');
|
|
227
|
+
const c = new Command(name).description(name);
|
|
228
|
+
if (ann) annotate(c, ann);
|
|
229
|
+
root.addCommand(c);
|
|
230
|
+
return commanderBridge(root, { run }).leaves()[0]!;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
it('treats an unannotated destructive-named leaf as destructive and blocks it', async () => {
|
|
234
|
+
const root = new Command('root');
|
|
235
|
+
root.addCommand(new Command('delete').description('d'));
|
|
236
|
+
const bridge = commanderBridge(root, { run });
|
|
237
|
+
expect(bridge.leaves()[0]!.class?.destructive).toBe(true);
|
|
238
|
+
await expect(
|
|
239
|
+
bridge.invoke({ path: ['delete'], args: [], flags: {}, meta: { surface: SURFACE_MCP } } as never),
|
|
240
|
+
).rejects.toThrow(/destructive command blocked on this surface/);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it('an explicit annotation wins, valid or malformed; empty infers; other names do not', () => {
|
|
244
|
+
expect(leafOf('delete', { 'kit/side-effect': 'read' }).class?.destructive).toBeUndefined();
|
|
245
|
+
expect(leafOf('delete', { 'kit/side-effect': 'bogus' }).class?.destructive).toBeUndefined();
|
|
246
|
+
expect(leafOf('delete', { 'kit/side-effect': '' }).class?.destructive).toBe(true);
|
|
247
|
+
expect(leafOf('delete-all').class?.destructive).toBeUndefined();
|
|
248
|
+
expect(leafOf('rm').class?.destructive).toBe(true);
|
|
249
|
+
});
|
|
250
|
+
});
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module mcp/bridge
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* A commander adapter for the MCP surface's `McpBridge` interface.
|
|
6
|
+
*
|
|
7
|
+
* The surface itself never imports commander — that is what keeps
|
|
8
|
+
* the handler framework-free and lets adopters on another command
|
|
9
|
+
* framework implement `McpBridge` directly. This adapter is the
|
|
10
|
+
* convenience path for the commander trees the TS SDK's own `cli`
|
|
11
|
+
* module builds.
|
|
12
|
+
*
|
|
13
|
+
* The safety gate lives here, on the invoke path, so BOTH eras
|
|
14
|
+
* inherit one destructive ceiling from one place: there is no way to
|
|
15
|
+
* reach a leaf through the modern handler that the legacy handler
|
|
16
|
+
* would have blocked.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type { Command, Option } from 'commander';
|
|
20
|
+
import { declaredArgs, takesNoValue } from '../toolspec-internal.js';
|
|
21
|
+
import { gateSideEffect, isDestructiveLike } from '../sideeffect.js';
|
|
22
|
+
import {
|
|
23
|
+
DestructiveBlockedError,
|
|
24
|
+
policyAllowed,
|
|
25
|
+
SurfaceNotEnabledError,
|
|
26
|
+
toolName,
|
|
27
|
+
UnknownCommandError,
|
|
28
|
+
defaultPolicy,
|
|
29
|
+
type FlagSchema,
|
|
30
|
+
type Invocation,
|
|
31
|
+
type InvokeResult,
|
|
32
|
+
type Leaf,
|
|
33
|
+
type McpBridge,
|
|
34
|
+
type Policy,
|
|
35
|
+
type SafetyClass,
|
|
36
|
+
type Surface,
|
|
37
|
+
} from './types.js';
|
|
38
|
+
|
|
39
|
+
/** Annotation keys read off a command, matching the kit/ vocabulary. */
|
|
40
|
+
const ANN_AUTH_REQUIRED = 'kit/auth-required';
|
|
41
|
+
const ANN_REQUIRES_CONFIRM = 'kit/requires-confirmation';
|
|
42
|
+
const ANN_PERMISSIONS = 'kit/permissions';
|
|
43
|
+
const ANN_EXIT_CODES = 'kit/exit-codes';
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A command carrying kit annotations. commander has no annotations
|
|
47
|
+
* concept, so they ride an optional property the CLI builder sets.
|
|
48
|
+
*/
|
|
49
|
+
export interface AnnotatedCommand extends Command {
|
|
50
|
+
annotations?: Record<string, string>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface CommanderBridgeOptions {
|
|
54
|
+
/** Policy gate. Defaults to defaultPolicy() (block remote destructive). */
|
|
55
|
+
policy?: Policy;
|
|
56
|
+
/**
|
|
57
|
+
* Runs one leaf and returns its result. Adopters supply this
|
|
58
|
+
* because kit does not dictate how a command's output is captured.
|
|
59
|
+
*/
|
|
60
|
+
run: (inv: Invocation, cmd: Command) => Promise<InvokeResult> | InvokeResult;
|
|
61
|
+
/** Per-leaf surface enablement override. */
|
|
62
|
+
enabled?: (leaf: Leaf) => Partial<Record<Surface, boolean>> | undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Builds an `McpBridge` over a commander tree. Leaves are the
|
|
67
|
+
* commands with no subcommands, enumerated depth-first in
|
|
68
|
+
* declaration order, with the root's own name excluded from the path
|
|
69
|
+
* (so `root widget add` is the tool `widget.add`).
|
|
70
|
+
*/
|
|
71
|
+
export function commanderBridge(
|
|
72
|
+
root: Command,
|
|
73
|
+
opts: CommanderBridgeOptions,
|
|
74
|
+
): McpBridge {
|
|
75
|
+
const policy = opts.policy ?? defaultPolicy();
|
|
76
|
+
|
|
77
|
+
const leaves = (): Leaf[] => {
|
|
78
|
+
const out: Leaf[] = [];
|
|
79
|
+
walkLeaves(root, [], (cmd, path) => {
|
|
80
|
+
const leaf: Leaf = {
|
|
81
|
+
path,
|
|
82
|
+
description: cmd.description() ?? '',
|
|
83
|
+
...collectFlags(cmd),
|
|
84
|
+
class: classify(cmd as AnnotatedCommand),
|
|
85
|
+
};
|
|
86
|
+
const args = declaredArgs(cmd);
|
|
87
|
+
if (args.length > 0) leaf.args = args;
|
|
88
|
+
const en = opts.enabled?.(leaf);
|
|
89
|
+
if (en) leaf.enabled = en;
|
|
90
|
+
out.push(leaf);
|
|
91
|
+
});
|
|
92
|
+
return out;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
leaves,
|
|
97
|
+
async invoke(inv: Invocation): Promise<InvokeResult> {
|
|
98
|
+
const target = leaves().find(
|
|
99
|
+
(l) =>
|
|
100
|
+
l.path.length === inv.path.length &&
|
|
101
|
+
l.path.every((seg, i) => seg === inv.path[i]),
|
|
102
|
+
);
|
|
103
|
+
if (target === undefined) {
|
|
104
|
+
throw new UnknownCommandError(
|
|
105
|
+
`unknown command: ${toolName(inv.path)}`,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
if (target.enabled?.[inv.meta.surface] === false) {
|
|
109
|
+
throw new SurfaceNotEnabledError(
|
|
110
|
+
`surface not enabled: ${inv.meta.surface}`,
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
// The destructive ceiling. Empty allowDestructiveOn means
|
|
114
|
+
// block-all, so a destructive leaf is unreachable on MCP unless
|
|
115
|
+
// the adopter explicitly opts that surface in.
|
|
116
|
+
if (!policyAllowed(policy, target.class ?? {}, inv.meta.surface)) {
|
|
117
|
+
throw new DestructiveBlockedError(
|
|
118
|
+
target.path.join(' '),
|
|
119
|
+
inv.meta.surface,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
const cmd = findCommand(root, target.path);
|
|
123
|
+
if (cmd === undefined) {
|
|
124
|
+
throw new UnknownCommandError(
|
|
125
|
+
`unknown command: ${toolName(inv.path)}`,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
return opts.run(inv, cmd);
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Reads a command's kit annotations into a SafetyClass. */
|
|
134
|
+
export function classify(cmd: AnnotatedCommand): SafetyClass {
|
|
135
|
+
const ann = cmd.annotations ?? {};
|
|
136
|
+
const cls: SafetyClass = {};
|
|
137
|
+
// A declared tier wins; an unannotated destructive-named leaf is the
|
|
138
|
+
// inferred destructive tier, as the CLI gate and Go's cmdreflect read it.
|
|
139
|
+
const tier = gateSideEffect(cmd.name(), ann);
|
|
140
|
+
if (tier !== undefined && isDestructiveLike(tier)) cls.destructive = true;
|
|
141
|
+
if (ann[ANN_AUTH_REQUIRED] === 'true') cls.authRequired = true;
|
|
142
|
+
if (ann[ANN_REQUIRES_CONFIRM] === 'true') cls.requiresConfirmation = true;
|
|
143
|
+
const perms = splitCSV(ann[ANN_PERMISSIONS]);
|
|
144
|
+
if (perms) cls.permissions = perms;
|
|
145
|
+
const codes = splitCSV(ann[ANN_EXIT_CODES]);
|
|
146
|
+
if (codes) cls.exitCodes = codes;
|
|
147
|
+
return cls;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Parses a comma-separated annotation value, dropping empty entries. */
|
|
151
|
+
function splitCSV(s: string | undefined): string[] | undefined {
|
|
152
|
+
if (!s) return undefined;
|
|
153
|
+
const out = s
|
|
154
|
+
.split(',')
|
|
155
|
+
.map((p) => p.trim())
|
|
156
|
+
.filter((p) => p !== '');
|
|
157
|
+
return out.length > 0 ? out : undefined;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Walks the tree depth-first, invoking `visit` for each leaf (a
|
|
162
|
+
* command with no subcommands). The root's own name is not part of
|
|
163
|
+
* any path.
|
|
164
|
+
*/
|
|
165
|
+
function walkLeaves(
|
|
166
|
+
cmd: Command,
|
|
167
|
+
path: string[],
|
|
168
|
+
visit: (cmd: Command, path: string[]) => void,
|
|
169
|
+
): void {
|
|
170
|
+
const subs = cmd.commands as Command[];
|
|
171
|
+
if (subs.length === 0) {
|
|
172
|
+
if (path.length > 0) visit(cmd, path);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
for (const sub of subs) {
|
|
176
|
+
walkLeaves(sub, [...path, sub.name()], visit);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Resolves a leaf path back to its commander Command. */
|
|
181
|
+
function findCommand(root: Command, path: string[]): Command | undefined {
|
|
182
|
+
let cur: Command = root;
|
|
183
|
+
for (const seg of path) {
|
|
184
|
+
const next = (cur.commands as Command[]).find((c) => c.name() === seg);
|
|
185
|
+
if (next === undefined) return undefined;
|
|
186
|
+
cur = next;
|
|
187
|
+
}
|
|
188
|
+
return cur;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Maps a command's options to JSON Schema properties plus the
|
|
193
|
+
* required-name list, filtering out hidden options. Long-flag names
|
|
194
|
+
* are used verbatim (`--dry-run` → `dry-run`), matching the Go
|
|
195
|
+
* surface's use of the pflag name rather than a camelCased accessor.
|
|
196
|
+
*/
|
|
197
|
+
export function collectFlags(cmd: Command): {
|
|
198
|
+
properties: Record<string, FlagSchema>;
|
|
199
|
+
required?: string[];
|
|
200
|
+
} {
|
|
201
|
+
const properties: Record<string, FlagSchema> = {};
|
|
202
|
+
const required: string[] = [];
|
|
203
|
+
const seen = new Set<string>();
|
|
204
|
+
|
|
205
|
+
for (const opt of cmd.options as Option[]) {
|
|
206
|
+
if (opt.hidden) continue;
|
|
207
|
+
const name = optionName(opt);
|
|
208
|
+
if (name === '' || seen.has(name)) continue;
|
|
209
|
+
seen.add(name);
|
|
210
|
+
properties[name] = flagProperty(opt);
|
|
211
|
+
if (opt.mandatory) required.push(name);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return required.length > 0 ? { properties, required } : { properties };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** The schema property name for an option: its long flag, undashed. */
|
|
218
|
+
function optionName(opt: Option): string {
|
|
219
|
+
const long = opt.long;
|
|
220
|
+
if (long) return long.replace(/^--/, '');
|
|
221
|
+
return (opt.short ?? '').replace(/^-/, '');
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Maps one option to a JSON Schema property object. */
|
|
225
|
+
function flagProperty(opt: Option): FlagSchema {
|
|
226
|
+
const type = optionJSONType(opt);
|
|
227
|
+
const prop: FlagSchema = {
|
|
228
|
+
type,
|
|
229
|
+
description: opt.description ?? '',
|
|
230
|
+
};
|
|
231
|
+
const choices = opt.argChoices;
|
|
232
|
+
if (type === 'array') {
|
|
233
|
+
prop.items = { type: 'string' };
|
|
234
|
+
if (choices && choices.length > 0) prop.items.enum = [...choices];
|
|
235
|
+
} else if (type === 'string' && choices && choices.length > 0) {
|
|
236
|
+
prop.enum = [...choices];
|
|
237
|
+
}
|
|
238
|
+
if (defaultMatches(type, opt.defaultValue)) {
|
|
239
|
+
prop.default = Array.isArray(opt.defaultValue)
|
|
240
|
+
? [...(opt.defaultValue as string[])]
|
|
241
|
+
: opt.defaultValue;
|
|
242
|
+
}
|
|
243
|
+
return prop;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Maps a commander option to a JSON Schema primitive.
|
|
248
|
+
*
|
|
249
|
+
* - variadic (`<v...>`) and accumulator options (a custom parser
|
|
250
|
+
* with an array default, commander's repeatable idiom) are arrays
|
|
251
|
+
* of strings;
|
|
252
|
+
* - value-less flags are booleans, negated ones (`--no-x`) included:
|
|
253
|
+
* commander's isBoolean() is false for those, but they take no
|
|
254
|
+
* value all the same;
|
|
255
|
+
* - everything else is a string.
|
|
256
|
+
*
|
|
257
|
+
* A negated flag keeps its long name (`no-x`), like every other
|
|
258
|
+
* property, so `{"no-x": true}` maps back to `--no-x` verbatim.
|
|
259
|
+
*/
|
|
260
|
+
function optionJSONType(opt: Option): string {
|
|
261
|
+
if (opt.variadic) return 'array';
|
|
262
|
+
if (opt.parseArg !== undefined && Array.isArray(opt.defaultValue)) {
|
|
263
|
+
return 'array';
|
|
264
|
+
}
|
|
265
|
+
if (takesNoValue(opt)) return 'boolean';
|
|
266
|
+
return 'string';
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Reports whether a commander default is publishable as the JSON
|
|
271
|
+
* Schema `default` of a property typed `type`. A default the schema
|
|
272
|
+
* type cannot hold (a parsed number on a string-typed option) is left
|
|
273
|
+
* out rather than published as a value the property would reject.
|
|
274
|
+
*/
|
|
275
|
+
function defaultMatches(type: string, value: unknown): boolean {
|
|
276
|
+
switch (type) {
|
|
277
|
+
case 'string':
|
|
278
|
+
return typeof value === 'string';
|
|
279
|
+
case 'boolean':
|
|
280
|
+
return typeof value === 'boolean';
|
|
281
|
+
case 'array':
|
|
282
|
+
return Array.isArray(value) && value.every((v) => typeof v === 'string');
|
|
283
|
+
default:
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
}
|