@hop-top/kit 0.5.0-alpha.0 → 0.5.0-alpha.10
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 +262 -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-2JV3A7OP.js +1894 -0
- package/dist/chunk-3FIKGFKI.js +54 -0
- package/dist/chunk-4UE5TCMQ.js +104 -0
- package/dist/chunk-6HFAEZ52.js +29 -0
- package/dist/chunk-6SVOHNIC.js +17 -0
- package/dist/chunk-72XZWQX7.js +504 -0
- package/dist/chunk-ABQSE7AI.js +513 -0
- package/dist/chunk-BDNNPRX6.js +41 -0
- package/dist/chunk-BLS2OBIQ.js +263 -0
- package/dist/chunk-BRNEKPWX.js +37 -0
- package/dist/chunk-CSJWB6D3.js +151 -0
- package/dist/chunk-GGWJRWWA.js +309 -0
- package/dist/chunk-GHPJJV5L.js +613 -0
- package/dist/chunk-IXVQ62NZ.js +334 -0
- package/dist/chunk-JKMQSAJP.js +174 -0
- package/dist/chunk-JZ22ZUFA.js +1131 -0
- package/dist/chunk-K7VCKJ27.js +814 -0
- package/dist/chunk-KVG7YCTS.js +115 -0
- package/dist/chunk-L23O2FFE.js +51 -0
- package/dist/chunk-MQXYDYRD.js +968 -0
- package/dist/chunk-MUIQVU6E.js +251 -0
- package/dist/chunk-NLMS7HGP.js +178 -0
- package/dist/chunk-POR5L2DG.js +247 -0
- package/dist/chunk-PSFYJGDG.js +1445 -0
- package/dist/chunk-QJVSKKRQ.js +369 -0
- package/dist/chunk-QPUCXQXT.js +67 -0
- package/dist/chunk-QQQYCWSC.js +240 -0
- package/dist/chunk-R5U7XKVJ.js +16 -0
- package/dist/chunk-SNVGDYX7.js +187 -0
- package/dist/chunk-T46YPHKN.js +669 -0
- package/dist/chunk-UKZIAQA7.js +26 -0
- package/dist/chunk-WBVO43HU.js +355 -0
- package/dist/chunk-WUOAQVOA.js +86 -0
- package/dist/chunk-WWUKGJB5.js +90 -0
- package/dist/chunk-XJTRY453.js +868 -0
- package/dist/chunk-YGYQUFGY.js +219 -0
- package/dist/chunk-YOBAUCVN.js +583 -0
- package/dist/cli-C5B5tnpA.d.ts +400 -0
- package/dist/cli.d.ts +7 -309
- package/dist/cli.js +70 -885
- 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-DCe1boBz.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 +184 -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 +137 -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 +18 -322
- package/dist/upgrade-p_zh3pvV.d.ts +102 -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 +236 -46
- 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.test.ts +95 -1
- package/src/cli.ts +195 -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 +297 -0
- package/src/completion.test.ts +256 -145
- package/src/completion.ts +767 -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/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 +222 -0
- package/src/mcp/bridge.ts +291 -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/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/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 +410 -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 +142 -0
- package/src/tui/parity.ts +59 -13
- 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
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module mcp/modern-confirm
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* MRTR (multi round-trip requests) confirmation for the modern
|
|
6
|
+
* (2026-07-28) `tools/call` path: the elicitation-based strategy that
|
|
7
|
+
* fills the confirmation-gate slot when a mount is given key material
|
|
8
|
+
* via `confirmationKey`.
|
|
9
|
+
*
|
|
10
|
+
* Flow (spec: basic/patterns/mrtr + client/elicitation): the first
|
|
11
|
+
* call on a `kit/requires-confirmation` leaf returns `resultType`
|
|
12
|
+
* "input_required" carrying a single `elicitation/create` form request
|
|
13
|
+
* under the reserved `inputRequests` key "confirm" plus an
|
|
14
|
+
* integrity-protected `requestState`. The client gathers the user's
|
|
15
|
+
* decision and retries the original call (new JSON-RPC id) with
|
|
16
|
+
* `params.inputResponses` and the echoed `params.requestState`; the
|
|
17
|
+
* gate verifies the state and lets the invocation proceed only when the
|
|
18
|
+
* confirm response's action is "accept". Decline/cancel refuse the call
|
|
19
|
+
* with an isError complete result.
|
|
20
|
+
*
|
|
21
|
+
* Statelessness: everything a retry needs lives inside `requestState`.
|
|
22
|
+
* There is no server-side pending-request storage of any kind, so any
|
|
23
|
+
* instance holding the same key verifies state minted by any other —
|
|
24
|
+
* which is exactly why the key must be adopter-supplied and shared, and
|
|
25
|
+
* why there is deliberately no generated-at-mount default. The
|
|
26
|
+
* corollary (spec: replay warning) is that an accepted state remains
|
|
27
|
+
* honorable for identical (leaf, arguments, principal) until its TTL
|
|
28
|
+
* lapses; single-use redemption would require the server-side state
|
|
29
|
+
* this surface deliberately does not keep, so the short TTL bounds the
|
|
30
|
+
* window instead.
|
|
31
|
+
*
|
|
32
|
+
* Two verification failures are deliberately distinct: an
|
|
33
|
+
* expired-but-authentic state is a routine re-prompt; a state failing
|
|
34
|
+
* HMAC verification is never honored — the rejection is recorded as a
|
|
35
|
+
* security-relevant audit event first, and only then is a fresh prompt
|
|
36
|
+
* (with newly minted state) issued. Tampering can therefore cost a
|
|
37
|
+
* request, but is never silently treated as a re-prompt.
|
|
38
|
+
*
|
|
39
|
+
* MRTR confirmation is an alternative way to SATISFY confirmation,
|
|
40
|
+
* never a way to bypass the destructive ceiling: the policy gate runs
|
|
41
|
+
* inside `bridge.invoke` after this gate, exactly as on every other
|
|
42
|
+
* path.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
import { createHash, createHmac, timingSafeEqual } from 'node:crypto';
|
|
46
|
+
|
|
47
|
+
import {
|
|
48
|
+
canonicalJSONStringify,
|
|
49
|
+
errorResultBlock,
|
|
50
|
+
MCP_SPEC_2026_07_28,
|
|
51
|
+
META_KEY_CLIENT_CAPABILITIES,
|
|
52
|
+
RESULT_TYPE_INPUT_REQUIRED,
|
|
53
|
+
toolName,
|
|
54
|
+
type Leaf,
|
|
55
|
+
type McpAuditHook,
|
|
56
|
+
} from './types.js';
|
|
57
|
+
import { headerValue } from './legacy.js';
|
|
58
|
+
import type { McpHttpRequest } from './dispatch.js';
|
|
59
|
+
import {
|
|
60
|
+
auditRefusal,
|
|
61
|
+
MCP_AUDIT_CONFIRMATION_DECLINED,
|
|
62
|
+
MCP_AUDIT_CONFIRMATION_REQUIRED,
|
|
63
|
+
MCP_AUDIT_CONFIRMATION_STATE_REJECTED,
|
|
64
|
+
MCP_CONFIRM_REJECTION_STATE,
|
|
65
|
+
} from './audit.js';
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The single reserved `inputRequests` key the confirmation flow uses;
|
|
69
|
+
* the retry's answer is read from the same key in
|
|
70
|
+
* `params.inputResponses`.
|
|
71
|
+
*/
|
|
72
|
+
export const MCP_CONFIRM_KEY = 'confirm';
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Lifetime of a minted `requestState`, in seconds. Long enough for a
|
|
76
|
+
* human to read and answer the prompt; short enough to bound the replay
|
|
77
|
+
* window the stateless design cannot otherwise close (see the module
|
|
78
|
+
* comment).
|
|
79
|
+
*/
|
|
80
|
+
export const MCP_CONFIRM_STATE_TTL_SECONDS = 5 * 60;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Tags the `requestState` wire format so a future format change
|
|
84
|
+
* invalidates (rather than misparses) old state.
|
|
85
|
+
*/
|
|
86
|
+
export const MCP_CONFIRM_STATE_VERSION = 'v1';
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Domain separation for the MAC, so this tag can never be confused with
|
|
90
|
+
* any other HMAC minted under a shared key.
|
|
91
|
+
*/
|
|
92
|
+
const MAC_DOMAIN = `cmdsurface-mcp-confirm-${MCP_CONFIRM_STATE_VERSION}`;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The request context a `requestState` is bound to. The HMAC covers
|
|
96
|
+
* every field plus the expiry, so state presented for a different leaf,
|
|
97
|
+
* different arguments, or by a different principal fails verification
|
|
98
|
+
* outright (spec: reject state presented on a request that does not
|
|
99
|
+
* match / by a different principal).
|
|
100
|
+
*/
|
|
101
|
+
export interface McpConfirmBinding {
|
|
102
|
+
/** The leaf path key, space-joined, e.g. "widget purge". */
|
|
103
|
+
tool: string;
|
|
104
|
+
/** Hex SHA-256 of the canonically serialized `params.arguments`. */
|
|
105
|
+
argsDigest: string;
|
|
106
|
+
/**
|
|
107
|
+
* Hex SHA-256 of the `Authorization` header value, or "" when the
|
|
108
|
+
* request carried none. Hashed so the MAC input never embeds
|
|
109
|
+
* credential material.
|
|
110
|
+
*/
|
|
111
|
+
principal: string;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The outcome of verifying a presented `requestState`. */
|
|
115
|
+
export type McpConfirmStateStatus = 'valid' | 'expired' | 'invalid';
|
|
116
|
+
|
|
117
|
+
/** One recorded rejection of a state that failed integrity verification. */
|
|
118
|
+
export interface McpConfirmRejection {
|
|
119
|
+
path: string;
|
|
120
|
+
mcpConfirmRejection: string;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** A gate decision: the result body to write plus its HTTP status. */
|
|
124
|
+
export type McpConfirmDecision = { result: Record<string, unknown>; status: number };
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The MRTR confirmation gate for mounts configured with a
|
|
128
|
+
* `confirmationKey`.
|
|
129
|
+
*
|
|
130
|
+
* Clients that did not declare form-mode elicitation keep the
|
|
131
|
+
* `X-Confirm-Token` header gate: the spec forbids sending
|
|
132
|
+
* `inputRequests` for a capability the client never declared, and the
|
|
133
|
+
* capability stays optional precisely because this fallback exists — so
|
|
134
|
+
* a missing elicitation capability is never -32021 here.
|
|
135
|
+
*/
|
|
136
|
+
export class ElicitationConfirmGate {
|
|
137
|
+
private readonly key: Buffer;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* In-memory records of rejected states, kept beside the mount's
|
|
141
|
+
* `audit` hook for callers holding the gate directly.
|
|
142
|
+
*/
|
|
143
|
+
readonly rejections: McpConfirmRejection[] = [];
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* `toolNameSeparator` names the tool in the confirmation prompt, as
|
|
147
|
+
* the mount's `toolNameSeparator` option does in tools/list. `audit`
|
|
148
|
+
* is the mount's refusal hook: it receives the gate's three refusals
|
|
149
|
+
* (confirmation required, declined, state rejected).
|
|
150
|
+
*/
|
|
151
|
+
constructor(
|
|
152
|
+
key: Uint8Array,
|
|
153
|
+
private readonly toolNameSeparator?: string,
|
|
154
|
+
private readonly audit?: McpAuditHook,
|
|
155
|
+
) {
|
|
156
|
+
if (key.length === 0) {
|
|
157
|
+
throw new Error('cmdsurface: confirmationKey: empty key');
|
|
158
|
+
}
|
|
159
|
+
this.key = Buffer.from(key);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Runs the gate for one `tools/call`. Returns `undefined` to proceed
|
|
164
|
+
* with the invocation, or the refusal / interim result plus its HTTP
|
|
165
|
+
* status. The caller stamps the modern envelope onto whatever is
|
|
166
|
+
* returned.
|
|
167
|
+
*/
|
|
168
|
+
evaluate(
|
|
169
|
+
req: McpHttpRequest,
|
|
170
|
+
leaf: Leaf,
|
|
171
|
+
params: unknown,
|
|
172
|
+
): McpConfirmDecision | undefined {
|
|
173
|
+
if (!leaf.class?.requiresConfirmation) return undefined;
|
|
174
|
+
if (!clientSupportsFormElicitation(params)) {
|
|
175
|
+
const refused = headerConfirmationGate(req);
|
|
176
|
+
if (refused !== undefined) {
|
|
177
|
+
this.emit(leaf, MCP_AUDIT_CONFIRMATION_REQUIRED);
|
|
178
|
+
}
|
|
179
|
+
return refused;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const binding: McpConfirmBinding = {
|
|
183
|
+
tool: leaf.path.join(' '),
|
|
184
|
+
argsDigest: mcpConfirmArgsDigest(params),
|
|
185
|
+
principal: mcpConfirmPrincipal(req),
|
|
186
|
+
};
|
|
187
|
+
const retry = parseMcpConfirmRetry(params);
|
|
188
|
+
|
|
189
|
+
if (retry.state === '') {
|
|
190
|
+
// A first call — or a retry that dropped the state it was
|
|
191
|
+
// required to echo, which is indistinguishable from one and
|
|
192
|
+
// equally unverifiable. Prompt (again).
|
|
193
|
+
return { result: this.inputRequired(leaf, binding), status: 200 };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const status = verifyMcpConfirmState(
|
|
197
|
+
this.key,
|
|
198
|
+
retry.state,
|
|
199
|
+
binding,
|
|
200
|
+
nowSeconds(),
|
|
201
|
+
);
|
|
202
|
+
if (status === 'invalid') {
|
|
203
|
+
// Tampered, malformed, or presented for a different request /
|
|
204
|
+
// principal than it was minted for: never honored, audited, then
|
|
205
|
+
// re-prompted with fresh state (see the module comment).
|
|
206
|
+
this.auditRejection(leaf);
|
|
207
|
+
return { result: this.inputRequired(leaf, binding), status: 200 };
|
|
208
|
+
}
|
|
209
|
+
if (status === 'expired') {
|
|
210
|
+
// Authentic but past its TTL: a routine re-prompt (spec:
|
|
211
|
+
// re-request missing information rather than error), no audit.
|
|
212
|
+
return { result: this.inputRequired(leaf, binding), status: 200 };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (retry.action === 'accept') return undefined;
|
|
216
|
+
if (retry.action === 'decline' || retry.action === 'cancel') {
|
|
217
|
+
this.emit(leaf, MCP_AUDIT_CONFIRMATION_DECLINED);
|
|
218
|
+
return { result: errorResultBlock('confirmation declined'), status: 200 };
|
|
219
|
+
}
|
|
220
|
+
// The confirm answer is missing or unusable (absent inputResponses,
|
|
221
|
+
// non-object entry, unknown action): the requested information was
|
|
222
|
+
// not provided, so re-request it rather than erroring (spec SHOULD).
|
|
223
|
+
return { result: this.inputRequired(leaf, binding), status: 200 };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Builds the `input_required` result envelope for one confirmation
|
|
228
|
+
* prompt: the reserved "confirm" `elicitation/create` form request
|
|
229
|
+
* plus freshly minted `requestState` (satisfying the spec MUST of
|
|
230
|
+
* carrying at least one of `inputRequests` / `requestState` — this
|
|
231
|
+
* flow always carries both). The caller stamps the envelope (_meta
|
|
232
|
+
* serverInfo; `resultType` stays "input_required") and writes it at
|
|
233
|
+
* HTTP 200. Interim `input_required` results are never cacheable: no
|
|
234
|
+
* `ttlMs` / `cacheScope` members, ever.
|
|
235
|
+
*/
|
|
236
|
+
inputRequired(
|
|
237
|
+
leaf: Leaf,
|
|
238
|
+
binding: McpConfirmBinding,
|
|
239
|
+
): Record<string, unknown> {
|
|
240
|
+
const expiry = nowSeconds() + MCP_CONFIRM_STATE_TTL_SECONDS;
|
|
241
|
+
return {
|
|
242
|
+
resultType: RESULT_TYPE_INPUT_REQUIRED,
|
|
243
|
+
inputRequests: {
|
|
244
|
+
[MCP_CONFIRM_KEY]: {
|
|
245
|
+
method: 'elicitation/create',
|
|
246
|
+
params: {
|
|
247
|
+
mode: 'form',
|
|
248
|
+
message: `Approve execution of ${JSON.stringify(toolName(leaf.path, this.toolNameSeparator))}?`,
|
|
249
|
+
// No form fields: the approval rides the elicit action
|
|
250
|
+
// (accept / decline / cancel), so the requested schema is
|
|
251
|
+
// the empty object.
|
|
252
|
+
requestedSchema: { type: 'object', properties: {} },
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
},
|
|
256
|
+
requestState: mintMcpConfirmState(this.key, binding, expiry),
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Records a failed `requestState` verification as a
|
|
262
|
+
* security-relevant audit event: in memory on `rejections`, and on
|
|
263
|
+
* the mount's audit hook with the `mcp_confirm_rejection` extra Go's
|
|
264
|
+
* surfaces attach. Best-effort by the same contract: recording never
|
|
265
|
+
* affects the response.
|
|
266
|
+
*/
|
|
267
|
+
private auditRejection(leaf: Leaf): void {
|
|
268
|
+
this.rejections.push({
|
|
269
|
+
path: leaf.path.join(' '),
|
|
270
|
+
mcpConfirmRejection: MCP_CONFIRM_REJECTION_STATE,
|
|
271
|
+
});
|
|
272
|
+
this.emit(leaf, MCP_AUDIT_CONFIRMATION_STATE_REJECTED, {
|
|
273
|
+
mcp_confirm_rejection: MCP_CONFIRM_REJECTION_STATE,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** Emits one 2026-07-28 refusal record to the mount's audit hook. */
|
|
278
|
+
private emit(leaf: Leaf, error: string, extra?: Record<string, string>): void {
|
|
279
|
+
auditRefusal(this.audit, leaf.path, MCP_SPEC_2026_07_28, error, extra);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* The default gate: a `requiresConfirmation` leaf needs the
|
|
285
|
+
* `X-Confirm-Token` header, exactly as on the legacy path. This is the
|
|
286
|
+
* fallback for clients without form elicitation and the whole gate for
|
|
287
|
+
* mounts without a confirmation key.
|
|
288
|
+
*/
|
|
289
|
+
export function headerConfirmationGate(
|
|
290
|
+
req: McpHttpRequest,
|
|
291
|
+
): McpConfirmDecision | undefined {
|
|
292
|
+
if (headerValue(req, 'x-confirm-token')) return undefined;
|
|
293
|
+
return { result: errorResultBlock('confirmation required'), status: 428 };
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Reports whether the request's
|
|
298
|
+
* `io.modelcontextprotocol/clientCapabilities` declares form-mode
|
|
299
|
+
* elicitation. Per spec, an empty `elicitation` object (`{}`) declares
|
|
300
|
+
* form-only support; a non-empty object must name "form" among its
|
|
301
|
+
* modes (a url-only client cannot receive this flow's form request).
|
|
302
|
+
* Anything that is not a conforming object declaration — key absent,
|
|
303
|
+
* value null or non-object — counts as undeclared, failing toward the
|
|
304
|
+
* header fallback rather than toward sending a request the client never
|
|
305
|
+
* said it could handle.
|
|
306
|
+
*/
|
|
307
|
+
export function clientSupportsFormElicitation(params: unknown): boolean {
|
|
308
|
+
const meta = plainObject(plainObject(params)?._meta);
|
|
309
|
+
if (meta === undefined) return false;
|
|
310
|
+
const caps = plainObject(meta[META_KEY_CLIENT_CAPABILITIES]);
|
|
311
|
+
if (caps === undefined) return false;
|
|
312
|
+
const modes = plainObject(caps.elicitation);
|
|
313
|
+
if (modes === undefined) return false;
|
|
314
|
+
if (Object.keys(modes).length === 0) return true;
|
|
315
|
+
return 'form' in modes;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* The tolerant read of the MRTR retry members of a `tools/call`
|
|
320
|
+
* request. Members that are absent or of the wrong JSON type simply
|
|
321
|
+
* stay empty — the gate treats a missing state as "prompt" and a
|
|
322
|
+
* missing/unusable action as "re-prompt", so malformed retries converge
|
|
323
|
+
* on a fresh `input_required` rather than a decode error.
|
|
324
|
+
*/
|
|
325
|
+
export function parseMcpConfirmRetry(params: unknown): {
|
|
326
|
+
state: string;
|
|
327
|
+
action: string;
|
|
328
|
+
} {
|
|
329
|
+
const obj = plainObject(params);
|
|
330
|
+
if (obj === undefined) return { state: '', action: '' };
|
|
331
|
+
|
|
332
|
+
const state = typeof obj.requestState === 'string' ? obj.requestState : '';
|
|
333
|
+
|
|
334
|
+
let action = '';
|
|
335
|
+
const entry = plainObject(plainObject(obj.inputResponses)?.[MCP_CONFIRM_KEY]);
|
|
336
|
+
if (entry !== undefined && typeof entry.action === 'string') {
|
|
337
|
+
action = entry.action;
|
|
338
|
+
}
|
|
339
|
+
return { state, action };
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Derives the principal component of the state binding: hex SHA-256 of
|
|
344
|
+
* the `Authorization` value, "" when the header is absent.
|
|
345
|
+
* Presence-only bearer checking is all this surface does for auth
|
|
346
|
+
* (`class.authRequired`), so the raw header value is the closest stable
|
|
347
|
+
* principal identifier available; hashing keeps credential material out
|
|
348
|
+
* of the MAC input.
|
|
349
|
+
*/
|
|
350
|
+
export function mcpConfirmPrincipal(req: McpHttpRequest): string {
|
|
351
|
+
const auth = headerValue(req, 'authorization');
|
|
352
|
+
if (!auth) return '';
|
|
353
|
+
return createHash('sha256').update(auth, 'utf8').digest('hex');
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Returns the hex SHA-256 of the canonically serialized
|
|
358
|
+
* `params.arguments`. Canonical form sorts object keys at every nesting
|
|
359
|
+
* level, so equal argument sets digest identically regardless of the
|
|
360
|
+
* client's key order; absent arguments canonicalize to "null". Only the
|
|
361
|
+
* arguments participate: `_meta`, `inputResponses`, and `requestState`
|
|
362
|
+
* all legitimately differ between the first call and its retry, and the
|
|
363
|
+
* tool name is bound separately via the leaf path key.
|
|
364
|
+
*/
|
|
365
|
+
export function mcpConfirmArgsDigest(params: unknown): string {
|
|
366
|
+
// Go decodes params.arguments into map[string]any, so a non-object
|
|
367
|
+
// value (array, string, number) decodes as absent and canonicalizes
|
|
368
|
+
// to "null" — matched here rather than digesting the raw value.
|
|
369
|
+
const args = plainObject(plainObject(params)?.arguments) ?? null;
|
|
370
|
+
const canonical = canonicalJSONStringify(args);
|
|
371
|
+
return createHash('sha256').update(canonical, 'utf8').digest('hex');
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Computes the HMAC-SHA-256 tag binding a state to its expiry and
|
|
376
|
+
* request context. Each component is written length-prefixed so the
|
|
377
|
+
* concatenation is unambiguous whatever the component contents (no
|
|
378
|
+
* delimiter-injection ambiguity), with a leading domain-separation
|
|
379
|
+
* constant so the tag can never be confused with any other HMAC minted
|
|
380
|
+
* under a shared key.
|
|
381
|
+
*/
|
|
382
|
+
function mcpConfirmStateMac(
|
|
383
|
+
key: Buffer,
|
|
384
|
+
binding: McpConfirmBinding,
|
|
385
|
+
expiry: number,
|
|
386
|
+
): Buffer {
|
|
387
|
+
const mac = createHmac('sha256', key);
|
|
388
|
+
for (const part of [
|
|
389
|
+
MAC_DOMAIN,
|
|
390
|
+
String(expiry),
|
|
391
|
+
binding.tool,
|
|
392
|
+
binding.argsDigest,
|
|
393
|
+
binding.principal,
|
|
394
|
+
]) {
|
|
395
|
+
// Length-prefixed by BYTE length, matching Go's len(part) over a
|
|
396
|
+
// string's bytes rather than JS's UTF-16 code-unit count.
|
|
397
|
+
mac.update(`${Buffer.byteLength(part, 'utf8')}:${part}`, 'utf8');
|
|
398
|
+
}
|
|
399
|
+
return mac.digest();
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Renders an opaque-to-clients `requestState`:
|
|
404
|
+
* `v1.<expiry-unix>.<base64url(mac)>`. Only the version and expiry
|
|
405
|
+
* travel in the clear; the full binding is reconstructed from the retry
|
|
406
|
+
* request itself at verification time, which is what keeps the state
|
|
407
|
+
* small and the flow stateless.
|
|
408
|
+
*/
|
|
409
|
+
export function mintMcpConfirmState(
|
|
410
|
+
key: Uint8Array,
|
|
411
|
+
binding: McpConfirmBinding,
|
|
412
|
+
expiry: number,
|
|
413
|
+
): string {
|
|
414
|
+
const mac = mcpConfirmStateMac(Buffer.from(key), binding, expiry);
|
|
415
|
+
return `${MCP_CONFIRM_STATE_VERSION}.${expiry}.${mac.toString('base64url')}`;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Checks a presented state against the current request's binding.
|
|
420
|
+
* Authenticity is decided BEFORE expiry, so "expired" is only ever
|
|
421
|
+
* reported for a state that verifiably came from this key and this
|
|
422
|
+
* exact binding — a tampered expiry fails the MAC and lands in
|
|
423
|
+
* "invalid", never in "expired". Any structural defect (wrong part
|
|
424
|
+
* count, unknown version, non-decimal expiry, undecodable tag) is a
|
|
425
|
+
* verification failure too: a state that cannot be verified is never
|
|
426
|
+
* honored.
|
|
427
|
+
*/
|
|
428
|
+
export function verifyMcpConfirmState(
|
|
429
|
+
key: Uint8Array,
|
|
430
|
+
state: string,
|
|
431
|
+
binding: McpConfirmBinding,
|
|
432
|
+
now: number,
|
|
433
|
+
): McpConfirmStateStatus {
|
|
434
|
+
const parts = state.split('.');
|
|
435
|
+
if (parts.length !== 3 || parts[0] !== MCP_CONFIRM_STATE_VERSION) {
|
|
436
|
+
return 'invalid';
|
|
437
|
+
}
|
|
438
|
+
// Go's strconv.ParseInt is strict decimal: reject anything that is
|
|
439
|
+
// not an optionally-signed run of digits, which JS's Number() would
|
|
440
|
+
// otherwise accept as hex, exponent, or whitespace-padded.
|
|
441
|
+
if (!/^-?\d+$/.test(parts[1])) return 'invalid';
|
|
442
|
+
const expiry = Number(parts[1]);
|
|
443
|
+
if (!Number.isSafeInteger(expiry)) return 'invalid';
|
|
444
|
+
|
|
445
|
+
const tag = decodeBase64Url(parts[2]);
|
|
446
|
+
if (tag === undefined) return 'invalid';
|
|
447
|
+
|
|
448
|
+
const want = mcpConfirmStateMac(Buffer.from(key), binding, expiry);
|
|
449
|
+
if (tag.length !== want.length || !timingSafeEqual(tag, want)) {
|
|
450
|
+
return 'invalid';
|
|
451
|
+
}
|
|
452
|
+
if (expiry < now) return 'expired';
|
|
453
|
+
return 'valid';
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Strict, canonical base64url decode. Node's `Buffer.from(s,
|
|
458
|
+
* 'base64url')` is lenient — it silently drops non-alphabet bytes —
|
|
459
|
+
* whereas Go's `base64.RawURLEncoding.DecodeString` errors, so the
|
|
460
|
+
* round-trip check rejects anything Go would refuse.
|
|
461
|
+
*/
|
|
462
|
+
function decodeBase64Url(s: string): Buffer | undefined {
|
|
463
|
+
if (!/^[A-Za-z0-9_-]*$/.test(s)) return undefined;
|
|
464
|
+
const buf = Buffer.from(s, 'base64url');
|
|
465
|
+
if (buf.toString('base64url') !== s) return undefined;
|
|
466
|
+
return buf;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/** Whole seconds since the Unix epoch. */
|
|
470
|
+
function nowSeconds(): number {
|
|
471
|
+
return Math.floor(Date.now() / 1000);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Narrows a value to a plain JSON object, the shape every tolerant read
|
|
476
|
+
* above expects. Arrays and null are not objects for this purpose:
|
|
477
|
+
* treating them as absent is what makes malformed input converge on a
|
|
478
|
+
* fresh prompt rather than on a decode error.
|
|
479
|
+
*/
|
|
480
|
+
function plainObject(v: unknown): Record<string, unknown> | undefined {
|
|
481
|
+
if (v === null || v === undefined || typeof v !== 'object') return undefined;
|
|
482
|
+
if (Array.isArray(v)) return undefined;
|
|
483
|
+
return v as Record<string, unknown>;
|
|
484
|
+
}
|