@lushly-dev/afd-server 2.2.1 → 2.4.0
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/README.md +468 -6
- package/dist/approval-crypto.d.ts +26 -4
- package/dist/approval-crypto.d.ts.map +1 -1
- package/dist/approval-crypto.js +90 -7
- package/dist/approval-crypto.js.map +1 -1
- package/dist/approval-relay.d.ts +5 -5
- package/dist/approval-relay.d.ts.map +1 -1
- package/dist/approval.d.ts +6 -1
- package/dist/approval.d.ts.map +1 -1
- package/dist/approval.js +43 -32
- package/dist/approval.js.map +1 -1
- package/dist/bootstrap/afd-context.d.ts +2 -27
- package/dist/bootstrap/afd-context.d.ts.map +1 -1
- package/dist/bootstrap/afd-context.js +6 -36
- package/dist/bootstrap/afd-context.js.map +1 -1
- package/dist/bootstrap/afd-help.d.ts +24 -2
- package/dist/bootstrap/afd-help.d.ts.map +1 -1
- package/dist/bootstrap/afd-help.js +17 -2
- package/dist/bootstrap/afd-help.js.map +1 -1
- package/dist/bootstrap/described-command.d.ts +1 -0
- package/dist/bootstrap/described-command.d.ts.map +1 -1
- package/dist/bootstrap/described-command.js.map +1 -1
- package/dist/bootstrap/registry.d.ts +13 -0
- package/dist/bootstrap/registry.d.ts.map +1 -1
- package/dist/bootstrap/registry.js +30 -1
- package/dist/bootstrap/registry.js.map +1 -1
- package/dist/command-allow.d.ts +31 -0
- package/dist/command-allow.d.ts.map +1 -0
- package/dist/command-allow.js +55 -0
- package/dist/command-allow.js.map +1 -0
- package/dist/command-names.d.ts +17 -0
- package/dist/command-names.d.ts.map +1 -1
- package/dist/command-names.js +55 -0
- package/dist/command-names.js.map +1 -1
- package/dist/command-routing.d.ts +10 -3
- package/dist/command-routing.d.ts.map +1 -1
- package/dist/command-routing.js +18 -7
- package/dist/command-routing.js.map +1 -1
- package/dist/confirmation-gate.d.ts +32 -0
- package/dist/confirmation-gate.d.ts.map +1 -0
- package/dist/confirmation-gate.js +140 -0
- package/dist/confirmation-gate.js.map +1 -0
- package/dist/confirmations.d.ts +15 -23
- package/dist/confirmations.d.ts.map +1 -1
- package/dist/confirmations.js +8 -127
- package/dist/confirmations.js.map +1 -1
- package/dist/context-fields-describe.d.ts +18 -0
- package/dist/context-fields-describe.d.ts.map +1 -0
- package/dist/context-fields-describe.js +17 -0
- package/dist/context-fields-describe.js.map +1 -0
- package/dist/context-fields.d.ts +50 -0
- package/dist/context-fields.d.ts.map +1 -0
- package/dist/context-fields.js +146 -0
- package/dist/context-fields.js.map +1 -0
- package/dist/context-scope.d.ts +29 -2
- package/dist/context-scope.d.ts.map +1 -1
- package/dist/context-scope.js +37 -1
- package/dist/context-scope.js.map +1 -1
- package/dist/credentials.d.ts +17 -0
- package/dist/credentials.d.ts.map +1 -0
- package/dist/credentials.js +16 -0
- package/dist/credentials.js.map +1 -0
- package/dist/define-core.d.ts +213 -0
- package/dist/define-core.d.ts.map +1 -0
- package/dist/define-core.js +161 -0
- package/dist/define-core.js.map +1 -0
- package/dist/define.d.ts +13 -8
- package/dist/define.d.ts.map +1 -1
- package/dist/define.js +13 -8
- package/dist/define.js.map +1 -1
- package/dist/direct-registry.d.ts +154 -7
- package/dist/direct-registry.d.ts.map +1 -1
- package/dist/direct-registry.js +111 -13
- package/dist/direct-registry.js.map +1 -1
- package/dist/direct-tool-surface.d.ts +66 -0
- package/dist/direct-tool-surface.d.ts.map +1 -0
- package/dist/direct-tool-surface.js +162 -0
- package/dist/direct-tool-surface.js.map +1 -0
- package/dist/direct.d.ts +68 -0
- package/dist/direct.d.ts.map +1 -0
- package/dist/direct.js +66 -0
- package/dist/direct.js.map +1 -0
- package/dist/execution.d.ts +11 -1
- package/dist/execution.d.ts.map +1 -1
- package/dist/execution.js +18 -4
- package/dist/execution.js.map +1 -1
- package/dist/frontmatter.d.ts +16 -0
- package/dist/frontmatter.d.ts.map +1 -0
- package/dist/frontmatter.js +111 -0
- package/dist/frontmatter.js.map +1 -0
- package/dist/http-handler.d.ts +11 -2
- package/dist/http-handler.d.ts.map +1 -1
- package/dist/http-handler.js +46 -15
- package/dist/http-handler.js.map +1 -1
- package/dist/http-security.d.ts.map +1 -1
- package/dist/http-security.js.map +1 -1
- package/dist/http-sessions.d.ts +12 -4
- package/dist/http-sessions.d.ts.map +1 -1
- package/dist/http-sessions.js +4 -4
- package/dist/http-sessions.js.map +1 -1
- package/dist/http-sse.js.map +1 -1
- package/dist/index.d.ts +16 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -4
- package/dist/index.js.map +1 -1
- package/dist/issue-message.d.ts +20 -0
- package/dist/issue-message.d.ts.map +1 -0
- package/dist/issue-message.js +55 -0
- package/dist/issue-message.js.map +1 -0
- package/dist/journal-replay.d.ts +7 -0
- package/dist/journal-replay.d.ts.map +1 -1
- package/dist/journal-replay.js +18 -1
- package/dist/journal-replay.js.map +1 -1
- package/dist/journal.d.ts +9 -1
- package/dist/journal.d.ts.map +1 -1
- package/dist/journal.js +44 -1
- package/dist/journal.js.map +1 -1
- package/dist/json-rpc.d.ts +2 -2
- package/dist/json-rpc.d.ts.map +1 -1
- package/dist/json-rpc.js.map +1 -1
- package/dist/json-schema.d.ts +30 -0
- package/dist/json-schema.d.ts.map +1 -0
- package/dist/json-schema.js +25 -0
- package/dist/json-schema.js.map +1 -0
- package/dist/lazy-tools.d.ts +3 -0
- package/dist/lazy-tools.d.ts.map +1 -1
- package/dist/lazy-tools.js +2 -0
- package/dist/lazy-tools.js.map +1 -1
- package/dist/mcp-auth-bearer.d.ts +23 -0
- package/dist/mcp-auth-bearer.d.ts.map +1 -0
- package/dist/mcp-auth-bearer.js +50 -0
- package/dist/mcp-auth-bearer.js.map +1 -0
- package/dist/mcp-auth-jwt.d.ts +178 -0
- package/dist/mcp-auth-jwt.d.ts.map +1 -0
- package/dist/mcp-auth-jwt.js +365 -0
- package/dist/mcp-auth-jwt.js.map +1 -0
- package/dist/mcp-auth-resource.d.ts +74 -0
- package/dist/mcp-auth-resource.d.ts.map +1 -0
- package/dist/mcp-auth-resource.js +196 -0
- package/dist/mcp-auth-resource.js.map +1 -0
- package/dist/mcp-auth-rules.d.ts +135 -0
- package/dist/mcp-auth-rules.d.ts.map +1 -0
- package/dist/mcp-auth-rules.js +306 -0
- package/dist/mcp-auth-rules.js.map +1 -0
- package/dist/mcp-auth.d.ts +81 -0
- package/dist/mcp-auth.d.ts.map +1 -0
- package/dist/mcp-auth.js +256 -0
- package/dist/mcp-auth.js.map +1 -0
- package/dist/mcp-modern.d.ts +29 -5
- package/dist/mcp-modern.d.ts.map +1 -1
- package/dist/mcp-modern.js +99 -19
- package/dist/mcp-modern.js.map +1 -1
- package/dist/meta-argument.d.ts +42 -0
- package/dist/meta-argument.d.ts.map +1 -0
- package/dist/meta-argument.js +102 -0
- package/dist/meta-argument.js.map +1 -0
- package/dist/meta-keys.d.ts +29 -0
- package/dist/meta-keys.d.ts.map +1 -0
- package/dist/meta-keys.js +90 -0
- package/dist/meta-keys.js.map +1 -0
- package/dist/meta-tools.d.ts +8 -1
- package/dist/meta-tools.d.ts.map +1 -1
- package/dist/meta-tools.js +17 -7
- package/dist/meta-tools.js.map +1 -1
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +0 -1
- package/dist/middleware.js.map +1 -1
- package/dist/patterns.d.ts +46 -0
- package/dist/patterns.d.ts.map +1 -0
- package/dist/patterns.js +38 -0
- package/dist/patterns.js.map +1 -0
- package/dist/principal-binding.d.ts +42 -0
- package/dist/principal-binding.d.ts.map +1 -0
- package/dist/principal-binding.js +70 -0
- package/dist/principal-binding.js.map +1 -0
- package/dist/request-context.d.ts +7 -1
- package/dist/request-context.d.ts.map +1 -1
- package/dist/request-context.js +14 -3
- package/dist/request-context.js.map +1 -1
- package/dist/resources.d.ts +122 -0
- package/dist/resources.d.ts.map +1 -0
- package/dist/resources.js +256 -0
- package/dist/resources.js.map +1 -0
- package/dist/schema.d.ts +7 -171
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +10 -135
- package/dist/schema.js.map +1 -1
- package/dist/server-types.d.ts +94 -7
- package/dist/server-types.d.ts.map +1 -1
- package/dist/server-types.js +2 -0
- package/dist/server-types.js.map +1 -1
- package/dist/server.d.ts +2 -2
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +45 -31
- package/dist/server.js.map +1 -1
- package/dist/skills.d.ts +95 -0
- package/dist/skills.d.ts.map +1 -0
- package/dist/skills.js +340 -0
- package/dist/skills.js.map +1 -0
- package/dist/strict-json.d.ts +17 -0
- package/dist/strict-json.d.ts.map +1 -0
- package/dist/strict-json.js +93 -0
- package/dist/strict-json.js.map +1 -0
- package/dist/tool-router.d.ts +13 -1
- package/dist/tool-router.d.ts.map +1 -1
- package/dist/tool-router.js +44 -10
- package/dist/tool-router.js.map +1 -1
- package/dist/tools.d.ts +10 -5
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +48 -22
- package/dist/tools.js.map +1 -1
- package/dist/validation.d.ts +6 -45
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +25 -58
- package/dist/validation.js.map +1 -1
- package/dist/verified-credentials.d.ts +34 -0
- package/dist/verified-credentials.d.ts.map +1 -0
- package/dist/verified-credentials.js +51 -0
- package/dist/verified-credentials.js.map +1 -0
- package/package.json +18 -6
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview The confirmation gate (`spec/command-metadata.md`, "Confirmations"): the check
|
|
3
|
+
* that decides whether a call to confirm-gated commands may run, its binding digest, and the
|
|
4
|
+
* check of the HTTP routes.
|
|
5
|
+
*
|
|
6
|
+
* Split from `confirmations.ts` because the binding digest uses `node:crypto`: only the MCP server
|
|
7
|
+
* (`server.ts`) and its HTTP handler import this module, so the tool router and the tool list stay
|
|
8
|
+
* free of Node.js built-ins for `createDirectToolSurface`. In-process calls are never gated.
|
|
9
|
+
*/
|
|
10
|
+
import { createHash } from 'node:crypto';
|
|
11
|
+
import { ErrorCodes } from '@lushly-dev/afd-core';
|
|
12
|
+
import { confirmationChannel, } from './confirmations.js';
|
|
13
|
+
/** JSON with object keys sorted, so equal inputs give equal digests. */
|
|
14
|
+
function canonicalJson(value) {
|
|
15
|
+
if (Array.isArray(value))
|
|
16
|
+
return `[${value.map(canonicalJson).join(',')}]`;
|
|
17
|
+
if (value && typeof value === 'object') {
|
|
18
|
+
const entries = Object.entries(value)
|
|
19
|
+
.filter(([, item]) => item !== undefined)
|
|
20
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
21
|
+
return `{${entries.map(([key, item]) => `${JSON.stringify(key)}:${canonicalJson(item)}`).join(',')}}`;
|
|
22
|
+
}
|
|
23
|
+
return JSON.stringify(value ?? null);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The binding of a confirmation: a digest of the gated commands and their input. The lane that
|
|
27
|
+
* mints the `requestState` binds the call's principal to the whole state (`mcp-modern.ts`), so
|
|
28
|
+
* this digest never needs to name the caller.
|
|
29
|
+
*/
|
|
30
|
+
export function confirmationBinding(steps) {
|
|
31
|
+
return createHash('sha256')
|
|
32
|
+
.update(canonicalJson(steps.map((step) => [step.command, step.input ?? {}])))
|
|
33
|
+
.digest('base64url');
|
|
34
|
+
}
|
|
35
|
+
function prompted(steps, commands) {
|
|
36
|
+
return steps.map((step) => ({
|
|
37
|
+
...step,
|
|
38
|
+
prompt: commands.get(step.command)?.confirmPrompt ?? '',
|
|
39
|
+
}));
|
|
40
|
+
}
|
|
41
|
+
function elicitationMessage(steps) {
|
|
42
|
+
const [first] = steps;
|
|
43
|
+
if (steps.length === 1 && first)
|
|
44
|
+
return first.prompt;
|
|
45
|
+
return [
|
|
46
|
+
'This request runs commands that need confirmation:',
|
|
47
|
+
...steps.map((step) => `- ${step.command}: ${step.prompt}`),
|
|
48
|
+
].join('\n');
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Create the gate for `commands` (the remotely callable ones). With mode `off` every call
|
|
52
|
+
* proceeds.
|
|
53
|
+
*/
|
|
54
|
+
export function createConfirmationGate(mode, commands) {
|
|
55
|
+
if (mode !== 'off' && mode !== 'enforce' && mode !== 'require-human') {
|
|
56
|
+
throw new Error("confirmations must be 'off', 'enforce' or 'require-human'");
|
|
57
|
+
}
|
|
58
|
+
const byName = new Map(commands.map((command) => [command.name, command]));
|
|
59
|
+
const gated = (steps) => mode === 'off' ? [] : steps.filter((step) => !!byName.get(step.command)?.confirmPrompt);
|
|
60
|
+
return {
|
|
61
|
+
mode,
|
|
62
|
+
gated,
|
|
63
|
+
async check(steps, acknowledged, context, howToAcknowledge) {
|
|
64
|
+
const gatedSteps = gated(steps);
|
|
65
|
+
if (gatedSteps.length === 0)
|
|
66
|
+
return { kind: 'proceed' };
|
|
67
|
+
const names = [...new Set(gatedSteps.map((step) => step.command))];
|
|
68
|
+
const binding = confirmationBinding(gatedSteps);
|
|
69
|
+
const channel = confirmationChannel(context);
|
|
70
|
+
const response = channel ? await channel.response(binding) : 'missing';
|
|
71
|
+
if (response === 'accept')
|
|
72
|
+
return { kind: 'proceed' };
|
|
73
|
+
if (response === 'decline') {
|
|
74
|
+
return {
|
|
75
|
+
kind: 'error',
|
|
76
|
+
error: {
|
|
77
|
+
code: ErrorCodes.CONFIRMATION_DECLINED,
|
|
78
|
+
message: `The user did not confirm ${names.map((name) => `'${name}'`).join(', ')}`,
|
|
79
|
+
suggestion: 'Do not run this again without asking the user first; the user chose not to proceed.',
|
|
80
|
+
retryable: false,
|
|
81
|
+
details: { commands: names },
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
if (mode === 'enforce') {
|
|
86
|
+
const covered = channel?.acknowledged === true ||
|
|
87
|
+
acknowledged === 'all' ||
|
|
88
|
+
(acknowledged instanceof Set && names.every((name) => acknowledged.has(name)));
|
|
89
|
+
if (covered)
|
|
90
|
+
return { kind: 'proceed' };
|
|
91
|
+
}
|
|
92
|
+
if (channel?.canElicit) {
|
|
93
|
+
const message = elicitationMessage(prompted(gatedSteps, byName));
|
|
94
|
+
return { kind: 'input-required', result: await channel.request(message, binding) };
|
|
95
|
+
}
|
|
96
|
+
const [firstStep] = prompted(gatedSteps, byName);
|
|
97
|
+
return {
|
|
98
|
+
kind: 'error',
|
|
99
|
+
error: {
|
|
100
|
+
code: ErrorCodes.CONFIRMATION_REQUIRED,
|
|
101
|
+
message: names.length === 1 && firstStep
|
|
102
|
+
? `'${firstStep.command}' needs confirmation: ${firstStep.prompt}`
|
|
103
|
+
: `${names.map((name) => `'${name}'`).join(', ')} need confirmation`,
|
|
104
|
+
suggestion: mode === 'enforce'
|
|
105
|
+
? `Make sure the user intends this, then ${howToAcknowledge}.`
|
|
106
|
+
: 'This server accepts only a confirmation from the user: use an MCP client that supports elicitation.',
|
|
107
|
+
retryable: false,
|
|
108
|
+
details: { commands: names },
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/** Header that acknowledges confirm-gated commands on the HTTP routes: their names, comma-separated. */
|
|
115
|
+
export const AFD_CONFIRM_HEADER = 'afd-confirm';
|
|
116
|
+
/** The command names an `Afd-Confirm` header acknowledges. */
|
|
117
|
+
function headerAcknowledgement(req) {
|
|
118
|
+
const raw = req.headers[AFD_CONFIRM_HEADER];
|
|
119
|
+
const value = Array.isArray(raw) ? raw.join(',') : raw;
|
|
120
|
+
if (!value)
|
|
121
|
+
return undefined;
|
|
122
|
+
return new Set(value
|
|
123
|
+
.split(',')
|
|
124
|
+
.map((name) => name.trim())
|
|
125
|
+
.filter(Boolean));
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The check of the HTTP routes (`/rpc`, `/batch`, `/stream`), which have no elicitation channel:
|
|
129
|
+
* the confirmation error for a request, or `undefined` when it may run.
|
|
130
|
+
*/
|
|
131
|
+
export function createHttpConfirmationCheck(gate) {
|
|
132
|
+
return async (req, steps) => {
|
|
133
|
+
if (!gate || gate.mode === 'off')
|
|
134
|
+
return undefined;
|
|
135
|
+
const names = [...new Set(gate.gated(steps).map((step) => step.command))];
|
|
136
|
+
const outcome = await gate.check(steps, headerAcknowledgement(req), undefined, `repeat the request with the header 'Afd-Confirm: ${names.join(', ')}'`);
|
|
137
|
+
return outcome.kind === 'error' ? outcome.error : undefined;
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=confirmation-gate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"confirmation-gate.js","sourceRoot":"","sources":["../src/confirmation-gate.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAKN,mBAAmB,GACnB,MAAM,oBAAoB,CAAC;AAG5B,wEAAwE;AACxE,SAAS,aAAa,CAAC,KAAc;IACpC,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,KAAK,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;IAC3E,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACxC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,KAAgC,CAAC;aAC9D,MAAM,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,IAAI,KAAK,SAAS,CAAC;aACxC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACnD,OAAO,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,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B,EACW,EAAE;QACtC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,KAAK,KAAK;YAAE,OAAO,SAAS,CAAC;QACnD,MAAM,KAAK,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QAC1E,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,KAAK,CAC/B,KAAK,EACL,qBAAqB,CAAC,GAAG,CAAC,EAC1B,SAAS,EACT,oDAAoD,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CACvE,CAAC;QACF,OAAO,OAAO,CAAC,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;IAC7D,CAAC,CAAC;AACH,CAAC"}
|
package/dist/confirmations.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview Confirmation enforcement for commands that declare `confirmPrompt`
|
|
3
|
-
* (`spec/command-metadata.md`, "Confirmations").
|
|
3
|
+
* (`spec/command-metadata.md`, "Confirmations"): the types, constants and plain helpers.
|
|
4
4
|
*
|
|
5
5
|
* The server option `confirmations` turns it on. A call that reaches a confirm-gated command runs
|
|
6
6
|
* only when it carries a confirmation:
|
|
7
7
|
*
|
|
8
8
|
* - an accepted MCP elicitation, through the `input_required` round trip of MCP 2026-07-28 (or the
|
|
9
9
|
* SDK's server-side shim on a 2025-era stdio connection), bound by a signed `requestState` to
|
|
10
|
-
* the commands and
|
|
10
|
+
* the commands, their input and the call's principal binding (`principal-binding.ts`, #532);
|
|
11
|
+
* or
|
|
11
12
|
* - under `enforce`, an acknowledgement: a `confirm` argument, or `_meta["dev.afd/confirm"]`.
|
|
12
13
|
*
|
|
13
14
|
* Otherwise the call fails with `CONFIRMATION_REQUIRED` or `CONFIRMATION_DECLINED` before any
|
|
@@ -16,8 +17,12 @@
|
|
|
16
17
|
* The lane that received a request describes what that request can carry with a
|
|
17
18
|
* {@link ConfirmationChannel}, bound to the `CommandContext` under a private symbol (as context
|
|
18
19
|
* state is), so `createContext` cannot forge one.
|
|
20
|
+
*
|
|
21
|
+
* Module-graph rule: this module loads no Node.js built-in, because the tool router and the tool
|
|
22
|
+
* list import it and `createDirectToolSurface` bundles them for the browser. The gate itself,
|
|
23
|
+
* whose binding digest uses `node:crypto`, is in `confirmation-gate.ts`, which only the MCP
|
|
24
|
+
* server and its HTTP handler import.
|
|
19
25
|
*/
|
|
20
|
-
import type { IncomingMessage } from 'node:http';
|
|
21
26
|
import type { CommandContext, CommandError } from '@lushly-dev/afd-core';
|
|
22
27
|
import type { ZodCommandDefinition } from './schema.js';
|
|
23
28
|
/** How a server treats `confirmPrompt`: not at all, or as a required confirmation. */
|
|
@@ -28,15 +33,15 @@ export declare const AFD_CONFIRM_META_KEY = "dev.afd/confirm";
|
|
|
28
33
|
export declare const CONFIRM_INPUT_KEY = "confirm";
|
|
29
34
|
/** The elicitation form: one required boolean. */
|
|
30
35
|
export declare const CONFIRM_REQUESTED_SCHEMA: {
|
|
31
|
-
readonly type:
|
|
36
|
+
readonly type: 'object';
|
|
32
37
|
readonly properties: {
|
|
33
38
|
readonly confirm: {
|
|
34
|
-
readonly type:
|
|
35
|
-
readonly title:
|
|
36
|
-
readonly description:
|
|
39
|
+
readonly type: 'boolean';
|
|
40
|
+
readonly title: 'Confirm';
|
|
41
|
+
readonly description: 'Run the command';
|
|
37
42
|
};
|
|
38
43
|
};
|
|
39
|
-
readonly required: readonly [
|
|
44
|
+
readonly required: readonly ['confirm'];
|
|
40
45
|
};
|
|
41
46
|
/** One command a call would run, with its input as sent. */
|
|
42
47
|
export interface ConfirmationStep {
|
|
@@ -51,7 +56,8 @@ export interface ConfirmationChannel {
|
|
|
51
56
|
acknowledged: boolean;
|
|
52
57
|
/**
|
|
53
58
|
* The response to an earlier confirmation elicitation, if this request is its retry and its
|
|
54
|
-
* `requestState` verifies for `binding
|
|
59
|
+
* `requestState` verifies for `binding` and for this call's principal. A stale, altered or
|
|
60
|
+
* mismatched state, or one issued to another principal, is `missing`.
|
|
55
61
|
*/
|
|
56
62
|
response(binding: string): Promise<'accept' | 'decline' | 'missing'>;
|
|
57
63
|
/** The `input_required` result that asks `message`, bound to `binding`. */
|
|
@@ -81,8 +87,6 @@ export type Acknowledgement = 'all' | ReadonlySet<string> | undefined;
|
|
|
81
87
|
export declare function bindConfirmationChannel(context: CommandContext, channel: ConfirmationChannel): CommandContext;
|
|
82
88
|
/** The channel bound to `context`, if any. */
|
|
83
89
|
export declare function confirmationChannel(context: CommandContext | undefined): ConfirmationChannel | undefined;
|
|
84
|
-
/** The binding of a confirmation: a digest of the gated commands and their input. */
|
|
85
|
-
export declare function confirmationBinding(steps: readonly ConfirmationStep[]): string;
|
|
86
90
|
/** Whether a command's own input schema declares a `confirm` property. */
|
|
87
91
|
export declare function declaresConfirmInput(command: ZodCommandDefinition): boolean;
|
|
88
92
|
export interface ConfirmationGate {
|
|
@@ -100,16 +104,4 @@ export interface ConfirmationGate {
|
|
|
100
104
|
*/
|
|
101
105
|
check(steps: readonly ConfirmationStep[], acknowledged: Acknowledgement, context: CommandContext | undefined, howToAcknowledge: string): Promise<ConfirmationOutcome>;
|
|
102
106
|
}
|
|
103
|
-
/**
|
|
104
|
-
* Create the gate for `commands` (the remotely callable ones). With mode `off` every call
|
|
105
|
-
* proceeds.
|
|
106
|
-
*/
|
|
107
|
-
export declare function createConfirmationGate(mode: ConfirmationMode, commands: readonly ZodCommandDefinition[]): ConfirmationGate;
|
|
108
|
-
/** Header that acknowledges confirm-gated commands on the HTTP routes: their names, comma-separated. */
|
|
109
|
-
export declare const AFD_CONFIRM_HEADER = "afd-confirm";
|
|
110
|
-
/**
|
|
111
|
-
* The check of the HTTP routes (`/rpc`, `/batch`, `/stream`), which have no elicitation channel:
|
|
112
|
-
* the confirmation error for a request, or `undefined` when it may run.
|
|
113
|
-
*/
|
|
114
|
-
export declare function createHttpConfirmationCheck(gate: ConfirmationGate | undefined): (req: IncomingMessage, steps: ConfirmationStep[]) => Promise<CommandError | undefined>;
|
|
115
107
|
//# sourceMappingURL=confirmations.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"confirmations.d.ts","sourceRoot":"","sources":["../src/confirmations.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"confirmations.d.ts","sourceRoot":"","sources":["../src/confirmations.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACzE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAExD,sFAAsF;AACtF,MAAM,MAAM,gBAAgB,GAAG,KAAK,GAAG,SAAS,GAAG,eAAe,CAAC;AAEnE,gGAAgG;AAChG,eAAO,MAAM,oBAAoB,oBAAoB,CAAC;AAEtD,+DAA+D;AAC/D,eAAO,MAAM,iBAAiB,YAAY,CAAC;AAE3C,kDAAkD;AAClD,eAAO,MAAM,wBAAwB;aACpC,IAAI,EAAE,QAAQ;aACd,UAAU;iBACT,OAAO;qBACN,IAAI,EAAE,SAAS;qBACf,KAAK,EAAE,SAAS;qBAChB,WAAW,EAAE,iBAAiB;;;aAGhC,QAAQ,YAAG,SAAS;CACX,CAAC;AAEX,4DAA4D;AAC5D,MAAM,WAAW,gBAAgB;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,OAAO,CAAC;CACf;AAED,qFAAqF;AACrF,MAAM,WAAW,mBAAmB;IACnC,8EAA8E;IAC9E,SAAS,EAAE,OAAO,CAAC;IACnB,4CAA4C;IAC5C,YAAY,EAAE,OAAO,CAAC;IACtB;;;;OAIG;IACH,QAAQ,CAAC,OAAO,EAAE,M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EACnC,gBAAgB,EAAE,MAAM,GACtB,OAAO,CAAC,mBAAmB,CAAC,CAAC;CAChC"}
|
package/dist/confirmations.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview Confirmation enforcement for commands that declare `confirmPrompt`
|
|
3
|
-
* (`spec/command-metadata.md`, "Confirmations").
|
|
3
|
+
* (`spec/command-metadata.md`, "Confirmations"): the types, constants and plain helpers.
|
|
4
4
|
*
|
|
5
5
|
* The server option `confirmations` turns it on. A call that reaches a confirm-gated command runs
|
|
6
6
|
* only when it carries a confirmation:
|
|
7
7
|
*
|
|
8
8
|
* - an accepted MCP elicitation, through the `input_required` round trip of MCP 2026-07-28 (or the
|
|
9
9
|
* SDK's server-side shim on a 2025-era stdio connection), bound by a signed `requestState` to
|
|
10
|
-
* the commands and
|
|
10
|
+
* the commands, their input and the call's principal binding (`principal-binding.ts`, #532);
|
|
11
|
+
* or
|
|
11
12
|
* - under `enforce`, an acknowledgement: a `confirm` argument, or `_meta["dev.afd/confirm"]`.
|
|
12
13
|
*
|
|
13
14
|
* Otherwise the call fails with `CONFIRMATION_REQUIRED` or `CONFIRMATION_DECLINED` before any
|
|
@@ -16,9 +17,12 @@
|
|
|
16
17
|
* The lane that received a request describes what that request can carry with a
|
|
17
18
|
* {@link ConfirmationChannel}, bound to the `CommandContext` under a private symbol (as context
|
|
18
19
|
* state is), so `createContext` cannot forge one.
|
|
20
|
+
*
|
|
21
|
+
* Module-graph rule: this module loads no Node.js built-in, because the tool router and the tool
|
|
22
|
+
* list import it and `createDirectToolSurface` bundles them for the browser. The gate itself,
|
|
23
|
+
* whose binding digest uses `node:crypto`, is in `confirmation-gate.ts`, which only the MCP
|
|
24
|
+
* server and its HTTP handler import.
|
|
19
25
|
*/
|
|
20
|
-
import { createHash } from 'node:crypto';
|
|
21
|
-
import { ErrorCodes } from '@lushly-dev/afd-core';
|
|
22
26
|
/** `_meta` key a client sets to `true` to acknowledge every confirm-gated command in a call. */
|
|
23
27
|
export const AFD_CONFIRM_META_KEY = 'dev.afd/confirm';
|
|
24
28
|
/** The `inputRequests` key of the confirmation elicitation. */
|
|
@@ -55,132 +59,9 @@ export function bindConfirmationChannel(context, channel) {
|
|
|
55
59
|
export function confirmationChannel(context) {
|
|
56
60
|
return context?.[CHANNEL];
|
|
57
61
|
}
|
|
58
|
-
/** JSON with object keys sorted, so equal inputs give equal digests. */
|
|
59
|
-
function canonicalJson(value) {
|
|
60
|
-
if (Array.isArray(value))
|
|
61
|
-
return `[${value.map(canonicalJson).join(',')}]`;
|
|
62
|
-
if (value && typeof value === 'object') {
|
|
63
|
-
const entries = Object.entries(value)
|
|
64
|
-
.filter(([, item]) => item !== undefined)
|
|
65
|
-
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
66
|
-
return `{${entries.map(([key, item]) => `${JSON.stringify(key)}:${canonicalJson(item)}`).join(',')}}`;
|
|
67
|
-
}
|
|
68
|
-
return JSON.stringify(value ?? null);
|
|
69
|
-
}
|
|
70
|
-
/** The binding of a confirmation: a digest of the gated commands and their input. */
|
|
71
|
-
export function confirmationBinding(steps) {
|
|
72
|
-
return createHash('sha256')
|
|
73
|
-
.update(canonicalJson(steps.map((step) => [step.command, step.input ?? {}])))
|
|
74
|
-
.digest('base64url');
|
|
75
|
-
}
|
|
76
62
|
/** Whether a command's own input schema declares a `confirm` property. */
|
|
77
63
|
export function declaresConfirmInput(command) {
|
|
78
64
|
const properties = command.jsonSchema.properties;
|
|
79
65
|
return !!properties && Object.hasOwn(properties, 'confirm');
|
|
80
66
|
}
|
|
81
|
-
function prompted(steps, commands) {
|
|
82
|
-
return steps.map((step) => ({
|
|
83
|
-
...step,
|
|
84
|
-
prompt: commands.get(step.command)?.confirmPrompt ?? '',
|
|
85
|
-
}));
|
|
86
|
-
}
|
|
87
|
-
function elicitationMessage(steps) {
|
|
88
|
-
const [first] = steps;
|
|
89
|
-
if (steps.length === 1 && first)
|
|
90
|
-
return first.prompt;
|
|
91
|
-
return [
|
|
92
|
-
'This request runs commands that need confirmation:',
|
|
93
|
-
...steps.map((step) => `- ${step.command}: ${step.prompt}`),
|
|
94
|
-
].join('\n');
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Create the gate for `commands` (the remotely callable ones). With mode `off` every call
|
|
98
|
-
* proceeds.
|
|
99
|
-
*/
|
|
100
|
-
export function createConfirmationGate(mode, commands) {
|
|
101
|
-
if (mode !== 'off' && mode !== 'enforce' && mode !== 'require-human') {
|
|
102
|
-
throw new Error("confirmations must be 'off', 'enforce' or 'require-human'");
|
|
103
|
-
}
|
|
104
|
-
const byName = new Map(commands.map((command) => [command.name, command]));
|
|
105
|
-
const gated = (steps) => mode === 'off' ? [] : steps.filter((step) => !!byName.get(step.command)?.confirmPrompt);
|
|
106
|
-
return {
|
|
107
|
-
mode,
|
|
108
|
-
gated,
|
|
109
|
-
async check(steps, acknowledged, context, howToAcknowledge) {
|
|
110
|
-
const gatedSteps = gated(steps);
|
|
111
|
-
if (gatedSteps.length === 0)
|
|
112
|
-
return { kind: 'proceed' };
|
|
113
|
-
const names = [...new Set(gatedSteps.map((step) => step.command))];
|
|
114
|
-
const binding = confirmationBinding(gatedSteps);
|
|
115
|
-
const channel = confirmationChannel(context);
|
|
116
|
-
const response = channel ? await channel.response(binding) : 'missing';
|
|
117
|
-
if (response === 'accept')
|
|
118
|
-
return { kind: 'proceed' };
|
|
119
|
-
if (response === 'decline') {
|
|
120
|
-
return {
|
|
121
|
-
kind: 'error',
|
|
122
|
-
error: {
|
|
123
|
-
code: ErrorCodes.CONFIRMATION_DECLINED,
|
|
124
|
-
message: `The user did not confirm ${names.map((name) => `'${name}'`).join(', ')}`,
|
|
125
|
-
suggestion: 'Do not run this again without asking the user first; the user chose not to proceed.',
|
|
126
|
-
retryable: false,
|
|
127
|
-
details: { commands: names },
|
|
128
|
-
},
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
if (mode === 'enforce') {
|
|
132
|
-
const covered = channel?.acknowledged === true ||
|
|
133
|
-
acknowledged === 'all' ||
|
|
134
|
-
(acknowledged instanceof Set && names.every((name) => acknowledged.has(name)));
|
|
135
|
-
if (covered)
|
|
136
|
-
return { kind: 'proceed' };
|
|
137
|
-
}
|
|
138
|
-
if (channel?.canElicit) {
|
|
139
|
-
const message = elicitationMessage(prompted(gatedSteps, byName));
|
|
140
|
-
return { kind: 'input-required', result: await channel.request(message, binding) };
|
|
141
|
-
}
|
|
142
|
-
const [firstStep] = prompted(gatedSteps, byName);
|
|
143
|
-
return {
|
|
144
|
-
kind: 'error',
|
|
145
|
-
error: {
|
|
146
|
-
code: ErrorCodes.CONFIRMATION_REQUIRED,
|
|
147
|
-
message: names.length === 1 && firstStep
|
|
148
|
-
? `'${firstStep.command}' needs confirmation: ${firstStep.prompt}`
|
|
149
|
-
: `${names.map((name) => `'${name}'`).join(', ')} need confirmation`,
|
|
150
|
-
suggestion: mode === 'enforce'
|
|
151
|
-
? `Make sure the user intends this, then ${howToAcknowledge}.`
|
|
152
|
-
: 'This server accepts only a confirmation from the user: use an MCP client that supports elicitation.',
|
|
153
|
-
retryable: false,
|
|
154
|
-
details: { commands: names },
|
|
155
|
-
},
|
|
156
|
-
};
|
|
157
|
-
},
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
/** Header that acknowledges confirm-gated commands on the HTTP routes: their names, comma-separated. */
|
|
161
|
-
export const AFD_CONFIRM_HEADER = 'afd-confirm';
|
|
162
|
-
/** The command names an `Afd-Confirm` header acknowledges. */
|
|
163
|
-
function headerAcknowledgement(req) {
|
|
164
|
-
const raw = req.headers[AFD_CONFIRM_HEADER];
|
|
165
|
-
const value = Array.isArray(raw) ? raw.join(',') : raw;
|
|
166
|
-
if (!value)
|
|
167
|
-
return undefined;
|
|
168
|
-
return new Set(value
|
|
169
|
-
.split(',')
|
|
170
|
-
.map((name) => name.trim())
|
|
171
|
-
.filter(Boolean));
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* The check of the HTTP routes (`/rpc`, `/batch`, `/stream`), which have no elicitation channel:
|
|
175
|
-
* the confirmation error for a request, or `undefined` when it may run.
|
|
176
|
-
*/
|
|
177
|
-
export function createHttpConfirmationCheck(gate) {
|
|
178
|
-
return async (req, steps) => {
|
|
179
|
-
if (!gate || gate.mode === 'off')
|
|
180
|
-
return undefined;
|
|
181
|
-
const names = [...new Set(gate.gated(steps).map((step) => step.command))];
|
|
182
|
-
const outcome = await gate.check(steps, headerAcknowledgement(req), undefined, `repeat the request with the header 'Afd-Confirm: ${names.join(', ')}'`);
|
|
183
|
-
return outcome.kind === 'error' ? outcome.error : undefined;
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
67
|
//# sourceMappingURL=confirmations.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"confirmations.js","sourceRoot":"","sources":["../src/confirmations.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"confirmations.js","sourceRoot":"","sources":["../src/confirmations.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAQH,gGAAgG;AAChG,MAAM,CAAC,MAAM,oBAAoB,GAAG,iBAAiB,CAAC;AAEtD,+DAA+D;AAC/D,MAAM,CAAC,MAAM,iBAAiB,GAAG,SAAS,CAAC;AAE3C,kDAAkD;AAClD,MAAM,CAAC,MAAM,wBAAwB,GAAG;IACvC,IAAI,EAAE,QAAQ;IACd,UAAU,EAAE;QACX,OAAO,EAAE;YACR,IAAI,EAAE,SAAS;YACf,KAAK,EAAE,SAAS;YAChB,WAAW,EAAE,iBAAiB;SAC9B;KACD;IACD,QAAQ,EAAE,CAAC,SAAS,CAAC;CACZ,CAAC;AA8BX;;;GAGG;AACH,MAAM,OAAO,mBAAmB;IACV,MAAM;IAA3B,YAAqB,MAAe;sBAAf,MAAM;IAAY,CAAC;CACxC;AAKD,MAAM,OAAO,GAAG,MAAM,CAAC,yBAAyB,CAAC,CAAC;AAIlD,qDAAqD;AACrD,MAAM,UAAU,uBAAuB,CACtC,OAAuB,EACvB,OAA4B;IAE5B,MAAM,KAAK,GAAmB,EAAE,GAAG,OAAO,EAAE,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,CAAC;IACjE,OAAO,KAAK,CAAC;AACd,CAAC;AAED,8CAA8C;AAC9C,MAAM,UAAU,mBAAmB,CAClC,OAAmC;IAEnC,OAAQ,OAAsC,EAAE,CAAC,OAAO,CAAC,CAAC;AAC3D,CAAC;AAED,0EAA0E;AAC1E,MAAM,UAAU,oBAAoB,CAAC,OAA6B;IACjE,MAAM,UAAU,GAAI,OAAO,CAAC,UAAuD,CAAC,UAAU,CAAC;IAC/F,OAAO,CAAC,CAAC,UAAU,IAAI,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;AAC7D,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Declared context fields as discovery describes them. Apart from `context-fields.ts`,
|
|
3
|
+
* since this is what needs `toJSONSchema`: a registry validates a field and never converts it, and
|
|
4
|
+
* only `createDirectToolSurface` and the servers list one.
|
|
5
|
+
*/
|
|
6
|
+
import type { JsonSchema } from '@lushly-dev/afd-core';
|
|
7
|
+
import type { ContextField } from './context-fields.js';
|
|
8
|
+
/** A declared field as discovery describes it, with its schema as JSON Schema. */
|
|
9
|
+
export interface DescribedContextField {
|
|
10
|
+
name: string;
|
|
11
|
+
meta: string;
|
|
12
|
+
schema: JsonSchema;
|
|
13
|
+
required: boolean;
|
|
14
|
+
journal: boolean;
|
|
15
|
+
}
|
|
16
|
+
/** The declared fields as `server/discover` and `afd-help` list them. */
|
|
17
|
+
export declare function describeContextFields(fields: readonly ContextField[]): DescribedContextField[];
|
|
18
|
+
//# sourceMappingURL=context-fields-describe.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-fields-describe.d.ts","sourceRoot":"","sources":["../src/context-fields-describe.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAGxD,kFAAkF;AAClF,MAAM,WAAW,qBAAqB;IACrC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,UAAU,CAAC;IACnB,QAAQ,EAAE,OAAO,CAAC;IAClB,OAAO,EAAE,OAAO,CAAC;CACjB;AAED,yEAAyE;AACzE,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,SAAS,YAAY,EAAE,GAAG,qBAAqB,EAAE,CAQ9F"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Declared context fields as discovery describes them. Apart from `context-fields.ts`,
|
|
3
|
+
* since this is what needs `toJSONSchema`: a registry validates a field and never converts it, and
|
|
4
|
+
* only `createDirectToolSurface` and the servers list one.
|
|
5
|
+
*/
|
|
6
|
+
import { zodToJsonSchema } from './json-schema.js';
|
|
7
|
+
/** The declared fields as `server/discover` and `afd-help` list them. */
|
|
8
|
+
export function describeContextFields(fields) {
|
|
9
|
+
return fields.map((field) => ({
|
|
10
|
+
name: field.name,
|
|
11
|
+
meta: field.meta,
|
|
12
|
+
schema: zodToJsonSchema(field.schema),
|
|
13
|
+
required: field.required === true,
|
|
14
|
+
journal: field.journal === true,
|
|
15
|
+
}));
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=context-fields-describe.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-fields-describe.js","sourceRoot":"","sources":["../src/context-fields-describe.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAWnD,yEAAyE;AACzE,MAAM,UAAU,qBAAqB,CAAC,MAA+B;IACpE,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QAC7B,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,MAAM,EAAE,eAAe,CAAC,KAAK,CAAC,MAAM,CAAC;QACrC,QAAQ,EAAE,KAAK,CAAC,QAAQ,KAAK,IAAI;QACjC,OAAO,EAAE,KAAK,CAAC,OAAO,KAAK,IAAI;KAC/B,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Declared context fields (`spec/context-fields.md`): named, schema-validated request
|
|
3
|
+
* values a host declares, which reach the command context on every surface and, when declared
|
|
4
|
+
* `journal: true`, the journal.
|
|
5
|
+
*/
|
|
6
|
+
import type { CommandContext, CommandResult } from '@lushly-dev/afd-core';
|
|
7
|
+
import { type $ZodType } from 'zod/v4/core';
|
|
8
|
+
/** A request value a host declares: where it lands in the context and how it is validated. */
|
|
9
|
+
export interface ContextField<T = unknown> {
|
|
10
|
+
/** The command-context key the value lands under. Not a key AFD uses itself. */
|
|
11
|
+
name: string;
|
|
12
|
+
/** The MCP `_meta` key a caller sends the value in: a reverse-DNS prefix, `dev.example/scope`. */
|
|
13
|
+
meta: string;
|
|
14
|
+
/** Validates the value; its output is what the handler sees. */
|
|
15
|
+
schema: $ZodType<T>;
|
|
16
|
+
/** Record the value in the journal. Default `false`: only for a field that never holds a secret. */
|
|
17
|
+
journal?: boolean;
|
|
18
|
+
/** Fail a call that has no value for this field. Default `false`. */
|
|
19
|
+
required?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* On a context the engine validated: the names of the declared fields marked `journal: true`, for
|
|
23
|
+
* the journal middleware. A symbol, so it never shows up among the context's keys.
|
|
24
|
+
*/
|
|
25
|
+
export declare const JOURNALED_CONTEXT_FIELDS: unique symbol;
|
|
26
|
+
/** The `_meta` key under which `server/discover` lists the declared fields. */
|
|
27
|
+
export declare const AFD_CONTEXT_FIELDS_META_KEY = "dev.afd/context-fields";
|
|
28
|
+
/**
|
|
29
|
+
* The declared fields' values in a request's `_meta`, by field name. Undeclared keys are left out.
|
|
30
|
+
* Callers spread trusted values (from `createContext`) after these, so trusted code wins.
|
|
31
|
+
*/
|
|
32
|
+
export declare function contextFieldValues(fields: readonly ContextField[] | undefined, meta: unknown): Record<string, unknown>;
|
|
33
|
+
/**
|
|
34
|
+
* Check declarations when a server or registry is created.
|
|
35
|
+
*
|
|
36
|
+
* @throws TypeError naming the field for a reserved or repeated name, a `meta` key without a
|
|
37
|
+
* reverse-DNS prefix or with a reserved one, a repeated `meta` key, or a missing schema
|
|
38
|
+
*/
|
|
39
|
+
export declare function checkContextFields(fields: readonly ContextField[] | undefined): readonly ContextField[];
|
|
40
|
+
/**
|
|
41
|
+
* Validate a call's declared fields on its context, in place: each value is replaced by its
|
|
42
|
+
* schema's output, and the journaled names are marked for the journal. An absent or `null` value
|
|
43
|
+
* is no value. A required field is not enforced for AFD's own `afd-*` commands, so a client can
|
|
44
|
+
* discover the fields before it sends them.
|
|
45
|
+
*
|
|
46
|
+
* @returns a `VALIDATION_ERROR` result for an invalid value or a missing required one, else
|
|
47
|
+
* `undefined`
|
|
48
|
+
*/
|
|
49
|
+
export declare function applyContextFields(fields: readonly ContextField[], context: CommandContext, commandName: string): CommandResult | undefined;
|
|
50
|
+
//# sourceMappingURL=context-fields.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-fields.d.ts","sourceRoot":"","sources":["../src/context-fields.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAE1E,OAAO,EAAE,KAAK,QAAQ,EAAoC,MAAM,aAAa,CAAC;AAG9E,8FAA8F;AAC9F,MAAM,WAAW,YAAY,CAAC,CAAC,GAAG,OAAO;IACxC,gFAAgF;IAChF,IAAI,EAAE,MAAM,CAAC;IACb,kGAAkG;IAClG,IAAI,EAAE,MAAM,CAAC;IACb,gEAAgE;IAChE,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;IACpB,oGAAoG;IACpG,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,qEAAqE;IACrE,QAAQ,CAAC,EAAE,OAAO,CAAC;CACnB;AAwBD;;;GAGG;AACH,eAAO,MAAM,wBAAwB,EAAE,OAAO,MAA6C,CAAC;AAE5F,+EAA+E;AAC/E,eAAO,MAAM,2BAA2B,2BAA2B,CAAC;AAEpE;;;GAGG;AACH,wBAAgB,kBAAkB,CACjC,MAAM,EAAE,SAAS,YAAY,EAAE,GAAG,SAAS,EAC3C,IAAI,EAAE,OAAO,GACX,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAQzB;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CACjC,MAAM,EAAE,SAAS,YAAY,EAAE,GAAG,SAAS,GACzC,SAAS,YAAY,EAAE,CAmCzB;AAMD;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CACjC,MAAM,EAAE,SAAS,YAAY,EAAE,EAC/B,OAAO,EAAE,cAAc,EACvB,WAAW,EAAE,MAAM,GACjB,aAAa,GAAG,SAAS,CAsC3B"}
|