@lushly-dev/afd-server 2.4.0 → 3.0.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 +239 -56
- package/dist/admission.d.ts +137 -0
- package/dist/admission.d.ts.map +1 -0
- package/dist/admission.js +144 -0
- package/dist/admission.js.map +1 -0
- package/dist/app-call-registry.d.ts +31 -0
- package/dist/app-call-registry.d.ts.map +1 -0
- package/dist/app-call-registry.js +64 -0
- package/dist/app-call-registry.js.map +1 -0
- package/dist/approval-admission.d.ts +139 -0
- package/dist/approval-admission.d.ts.map +1 -0
- package/dist/approval-admission.js +252 -0
- package/dist/approval-admission.js.map +1 -0
- package/dist/approval-crypto.d.ts +37 -17
- package/dist/approval-crypto.d.ts.map +1 -1
- package/dist/approval-crypto.js +84 -75
- package/dist/approval-crypto.js.map +1 -1
- package/dist/approval-process-state.d.ts +21 -0
- package/dist/approval-process-state.d.ts.map +1 -0
- package/dist/approval-process-state.js +29 -0
- package/dist/approval-process-state.js.map +1 -0
- package/dist/approval-relay.d.ts +192 -9
- package/dist/approval-relay.d.ts.map +1 -1
- package/dist/approval-relay.js +45 -9
- package/dist/approval-relay.js.map +1 -1
- package/dist/approval-up-front.d.ts +40 -0
- package/dist/approval-up-front.d.ts.map +1 -0
- package/dist/approval-up-front.js +409 -0
- package/dist/approval-up-front.js.map +1 -0
- package/dist/approval.d.ts +65 -12
- package/dist/approval.d.ts.map +1 -1
- package/dist/approval.js +311 -47
- package/dist/approval.js.map +1 -1
- package/dist/approver.d.ts +84 -0
- package/dist/approver.d.ts.map +1 -0
- package/dist/approver.js +107 -0
- package/dist/approver.js.map +1 -0
- package/dist/auth-middleware-check.d.ts +23 -0
- package/dist/auth-middleware-check.d.ts.map +1 -0
- package/dist/auth-middleware-check.js +48 -0
- package/dist/auth-middleware-check.js.map +1 -0
- package/dist/bootstrap/afd-context.d.ts.map +1 -1
- package/dist/bootstrap/afd-context.js +27 -3
- package/dist/bootstrap/afd-context.js.map +1 -1
- package/dist/bootstrap/afd-docs.d.ts.map +1 -1
- package/dist/bootstrap/afd-docs.js +4 -0
- package/dist/bootstrap/afd-docs.js.map +1 -1
- package/dist/bootstrap/afd-help.d.ts +4 -1
- package/dist/bootstrap/afd-help.d.ts.map +1 -1
- package/dist/bootstrap/afd-help.js +19 -4
- package/dist/bootstrap/afd-help.js.map +1 -1
- package/dist/bootstrap/described-command.d.ts +3 -1
- package/dist/bootstrap/described-command.d.ts.map +1 -1
- package/dist/bootstrap/described-command.js.map +1 -1
- package/dist/command-allow.d.ts +4 -4
- package/dist/command-allow.d.ts.map +1 -1
- package/dist/command-allow.js +10 -15
- package/dist/command-allow.js.map +1 -1
- package/dist/command-routing.d.ts +56 -8
- package/dist/command-routing.d.ts.map +1 -1
- package/dist/command-routing.js +82 -16
- package/dist/command-routing.js.map +1 -1
- package/dist/command-timeout.d.ts +17 -0
- package/dist/command-timeout.d.ts.map +1 -0
- package/dist/command-timeout.js +99 -0
- package/dist/command-timeout.js.map +1 -0
- package/dist/confirmation-gate.d.ts +7 -7
- package/dist/confirmation-gate.d.ts.map +1 -1
- package/dist/confirmation-gate.js +26 -12
- package/dist/confirmation-gate.js.map +1 -1
- package/dist/confirmations.d.ts +28 -3
- package/dist/confirmations.d.ts.map +1 -1
- package/dist/confirmations.js +6 -7
- package/dist/confirmations.js.map +1 -1
- package/dist/context-fields.d.ts +16 -1
- package/dist/context-fields.d.ts.map +1 -1
- package/dist/context-fields.js +30 -13
- package/dist/context-fields.js.map +1 -1
- package/dist/context-scope.d.ts +3 -3
- package/dist/context-scope.d.ts.map +1 -1
- package/dist/context-scope.js +7 -8
- package/dist/context-scope.js.map +1 -1
- package/dist/define-core.d.ts +63 -1
- package/dist/define-core.d.ts.map +1 -1
- package/dist/define-core.js +27 -4
- package/dist/define-core.js.map +1 -1
- package/dist/define.d.ts +4 -3
- package/dist/define.d.ts.map +1 -1
- package/dist/define.js +1 -1
- package/dist/define.js.map +1 -1
- package/dist/direct-registry.d.ts +54 -8
- package/dist/direct-registry.d.ts.map +1 -1
- package/dist/direct-registry.js +64 -8
- package/dist/direct-registry.js.map +1 -1
- package/dist/direct-tool-surface.d.ts +1 -1
- package/dist/direct-tool-surface.d.ts.map +1 -1
- package/dist/direct-tool-surface.js +28 -6
- package/dist/direct-tool-surface.js.map +1 -1
- package/dist/direct.d.ts +6 -2
- package/dist/direct.d.ts.map +1 -1
- package/dist/direct.js +4 -1
- package/dist/direct.js.map +1 -1
- package/dist/elicitation-state.d.ts +5 -1
- package/dist/elicitation-state.d.ts.map +1 -1
- package/dist/elicitation-state.js +4 -1
- package/dist/elicitation-state.js.map +1 -1
- package/dist/execution.d.ts +52 -6
- package/dist/execution.d.ts.map +1 -1
- package/dist/execution.js +183 -19
- package/dist/execution.js.map +1 -1
- package/dist/host-state.d.ts +81 -0
- package/dist/host-state.d.ts.map +1 -0
- package/dist/host-state.js +62 -0
- package/dist/host-state.js.map +1 -0
- package/dist/http-handler.d.ts +48 -11
- package/dist/http-handler.d.ts.map +1 -1
- package/dist/http-handler.js +207 -58
- package/dist/http-handler.js.map +1 -1
- package/dist/http-security.d.ts +14 -1
- package/dist/http-security.d.ts.map +1 -1
- package/dist/http-security.js +79 -17
- package/dist/http-security.js.map +1 -1
- package/dist/http-sessions.d.ts +45 -5
- package/dist/http-sessions.d.ts.map +1 -1
- package/dist/http-sessions.js +52 -10
- package/dist/http-sessions.js.map +1 -1
- package/dist/http-sse.d.ts +9 -0
- package/dist/http-sse.d.ts.map +1 -1
- package/dist/http-sse.js +12 -3
- package/dist/http-sse.js.map +1 -1
- package/dist/human-relay.d.ts +195 -0
- package/dist/human-relay.d.ts.map +1 -0
- package/dist/human-relay.js +670 -0
- package/dist/human-relay.js.map +1 -0
- package/dist/idempotency-step.d.ts +231 -0
- package/dist/idempotency-step.d.ts.map +1 -0
- package/dist/idempotency-step.js +389 -0
- package/dist/idempotency-step.js.map +1 -0
- package/dist/idempotency-store.d.ts +107 -0
- package/dist/idempotency-store.d.ts.map +1 -0
- package/dist/idempotency-store.js +100 -0
- package/dist/idempotency-store.js.map +1 -0
- package/dist/index.d.ts +18 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -4
- package/dist/index.js.map +1 -1
- package/dist/instance-state.d.ts +53 -0
- package/dist/instance-state.d.ts.map +1 -0
- package/dist/instance-state.js +94 -0
- package/dist/instance-state.js.map +1 -0
- package/dist/journal-off-tape.d.ts +52 -0
- package/dist/journal-off-tape.d.ts.map +1 -0
- package/dist/journal-off-tape.js +150 -0
- package/dist/journal-off-tape.js.map +1 -0
- package/dist/journal-replay.d.ts.map +1 -1
- package/dist/journal-replay.js +48 -3
- package/dist/journal-replay.js.map +1 -1
- package/dist/journal-view.d.ts +50 -0
- package/dist/journal-view.d.ts.map +1 -0
- package/dist/journal-view.js +214 -0
- package/dist/journal-view.js.map +1 -0
- package/dist/journal.d.ts +34 -6
- package/dist/journal.d.ts.map +1 -1
- package/dist/journal.js +183 -47
- package/dist/journal.js.map +1 -1
- package/dist/json-rpc.d.ts +3 -2
- package/dist/json-rpc.d.ts.map +1 -1
- package/dist/json-rpc.js +5 -3
- package/dist/json-rpc.js.map +1 -1
- package/dist/lazy-tools.d.ts +27 -2
- package/dist/lazy-tools.d.ts.map +1 -1
- package/dist/lazy-tools.js +22 -4
- package/dist/lazy-tools.js.map +1 -1
- package/dist/mcp-auth-bearer.d.ts +2 -0
- package/dist/mcp-auth-bearer.d.ts.map +1 -1
- package/dist/mcp-auth-bearer.js +1 -1
- package/dist/mcp-auth-bearer.js.map +1 -1
- package/dist/mcp-auth-dispatch.d.ts +38 -0
- package/dist/mcp-auth-dispatch.d.ts.map +1 -0
- package/dist/mcp-auth-dispatch.js +113 -0
- package/dist/mcp-auth-dispatch.js.map +1 -0
- package/dist/mcp-auth-jwt.d.ts +3 -2
- package/dist/mcp-auth-jwt.d.ts.map +1 -1
- package/dist/mcp-auth-jwt.js.map +1 -1
- package/dist/mcp-auth-resource.d.ts +6 -2
- package/dist/mcp-auth-resource.d.ts.map +1 -1
- package/dist/mcp-auth-resource.js +6 -5
- package/dist/mcp-auth-resource.js.map +1 -1
- package/dist/mcp-auth-scopes.d.ts +84 -0
- package/dist/mcp-auth-scopes.d.ts.map +1 -0
- package/dist/mcp-auth-scopes.js +125 -0
- package/dist/mcp-auth-scopes.js.map +1 -0
- package/dist/mcp-auth-view.d.ts +50 -0
- package/dist/mcp-auth-view.d.ts.map +1 -0
- package/dist/mcp-auth-view.js +179 -0
- package/dist/mcp-auth-view.js.map +1 -0
- package/dist/mcp-auth.d.ts +94 -3
- package/dist/mcp-auth.d.ts.map +1 -1
- package/dist/mcp-auth.js +241 -23
- package/dist/mcp-auth.js.map +1 -1
- package/dist/mcp-modern.d.ts +80 -6
- package/dist/mcp-modern.d.ts.map +1 -1
- package/dist/mcp-modern.js +378 -91
- package/dist/mcp-modern.js.map +1 -1
- package/dist/meta-argument.d.ts +9 -8
- package/dist/meta-argument.d.ts.map +1 -1
- package/dist/meta-argument.js +28 -10
- package/dist/meta-argument.js.map +1 -1
- package/dist/meta-tools.js +2 -2
- package/dist/meta-tools.js.map +1 -1
- package/dist/middleware.d.ts +16 -1
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +57 -10
- package/dist/middleware.js.map +1 -1
- package/dist/output-budget.d.ts +77 -0
- package/dist/output-budget.d.ts.map +1 -0
- package/dist/output-budget.js +202 -0
- package/dist/output-budget.js.map +1 -0
- package/dist/principal-binding.js +1 -1
- package/dist/principal-binding.js.map +1 -1
- package/dist/relay-evidence.d.ts +74 -0
- package/dist/relay-evidence.d.ts.map +1 -0
- package/dist/relay-evidence.js +143 -0
- package/dist/relay-evidence.js.map +1 -0
- package/dist/resources.d.ts +35 -10
- package/dist/resources.d.ts.map +1 -1
- package/dist/resources.js +59 -18
- package/dist/resources.js.map +1 -1
- package/dist/schema.d.ts +4 -3
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +3 -3
- package/dist/schema.js.map +1 -1
- package/dist/scope-view.d.ts +65 -0
- package/dist/scope-view.d.ts.map +1 -0
- package/dist/scope-view.js +45 -0
- package/dist/scope-view.js.map +1 -0
- package/dist/server-types.d.ts +182 -27
- package/dist/server-types.d.ts.map +1 -1
- package/dist/server-types.js.map +1 -1
- package/dist/server.d.ts +26 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +403 -61
- package/dist/server.js.map +1 -1
- package/dist/skills.d.ts.map +1 -1
- package/dist/skills.js +99 -3
- package/dist/skills.js.map +1 -1
- package/dist/task-dispatch.d.ts +35 -0
- package/dist/task-dispatch.d.ts.map +1 -0
- package/dist/task-dispatch.js +25 -0
- package/dist/task-dispatch.js.map +1 -0
- package/dist/tasks-runtime.d.ts +115 -0
- package/dist/tasks-runtime.d.ts.map +1 -0
- package/dist/tasks-runtime.js +760 -0
- package/dist/tasks-runtime.js.map +1 -0
- package/dist/tasks.d.ts +237 -0
- package/dist/tasks.d.ts.map +1 -0
- package/dist/tasks.js +321 -0
- package/dist/tasks.js.map +1 -0
- package/dist/tool-router.d.ts +68 -3
- package/dist/tool-router.d.ts.map +1 -1
- package/dist/tool-router.js +293 -70
- package/dist/tool-router.js.map +1 -1
- package/dist/tools.d.ts +32 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +162 -55
- package/dist/tools.js.map +1 -1
- package/dist/up-front-steps.d.ts +35 -0
- package/dist/up-front-steps.d.ts.map +1 -0
- package/dist/up-front-steps.js +159 -0
- package/dist/up-front-steps.js.map +1 -0
- package/dist/validation.d.ts +38 -3
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +118 -23
- package/dist/validation.js.map +1 -1
- package/dist/verified-credentials.d.ts +13 -5
- package/dist/verified-credentials.d.ts.map +1 -1
- package/dist/verified-credentials.js +43 -10
- package/dist/verified-credentials.js.map +1 -1
- package/package.json +15 -11
package/dist/http-security.js
CHANGED
|
@@ -12,6 +12,30 @@ export class HttpRequestError extends Error {
|
|
|
12
12
|
this.suggestion = options.suggestion;
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Refuse an `allowedOrigins` entry that is not a serialized http(s) origin, as a browser sends it:
|
|
17
|
+
* `scheme://host[:port]`, lower case, with no path, no trailing slash and no default port. `*`
|
|
18
|
+
* and `null` are refused: neither names one origin.
|
|
19
|
+
*/
|
|
20
|
+
export function assertAllowedOrigins(origins) {
|
|
21
|
+
if (origins === undefined)
|
|
22
|
+
return;
|
|
23
|
+
if (!Array.isArray(origins))
|
|
24
|
+
throw new Error('allowedOrigins must be an array of origins');
|
|
25
|
+
for (const origin of origins) {
|
|
26
|
+
let valid = false;
|
|
27
|
+
if (typeof origin === 'string') {
|
|
28
|
+
try {
|
|
29
|
+
const url = new URL(origin);
|
|
30
|
+
valid = (url.protocol === 'http:' || url.protocol === 'https:') && url.origin === origin;
|
|
31
|
+
}
|
|
32
|
+
catch { }
|
|
33
|
+
}
|
|
34
|
+
if (!valid) {
|
|
35
|
+
throw new Error(`allowedOrigins entry ${JSON.stringify(origin)} must be an exact http(s) origin such as http://localhost:5173: scheme, host and port only, with no path or trailing slash ("*" and "null" are not allowed)`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
15
39
|
export function validateHttpRequest(req, options) {
|
|
16
40
|
const host = req.headers.host;
|
|
17
41
|
if (!host || /[\s/@\\?#]/.test(host))
|
|
@@ -27,20 +51,15 @@ export function validateHttpRequest(req, options) {
|
|
|
27
51
|
if (!allowedHosts.some((allowed) => allowed.toLowerCase() === base.hostname.toLowerCase())) {
|
|
28
52
|
throw new HttpRequestError(403, 'Host is not allowed; configure allowedHosts for this host');
|
|
29
53
|
}
|
|
54
|
+
// `devMode` relaxes none of these checks and sends no CORS header: a browser origin other
|
|
55
|
+
// than the server's own is refused unless it is listed in `allowedOrigins`, in either mode.
|
|
30
56
|
const origin = req.headers.origin;
|
|
31
|
-
if (!options.
|
|
32
|
-
origin &&
|
|
33
|
-
origin !== base.origin &&
|
|
34
|
-
!options.allowedOrigins?.includes(origin)) {
|
|
57
|
+
if (origin && origin !== base.origin && !options.allowedOrigins?.includes(origin)) {
|
|
35
58
|
throw new HttpRequestError(403, 'Origin is not allowed; configure allowedOrigins for browser access');
|
|
36
59
|
}
|
|
37
60
|
// Covers navigation and no-cors GET requests that omit Origin, including legacy streams.
|
|
38
61
|
const fetchSite = req.headers['sec-fetch-site'];
|
|
39
|
-
if (!
|
|
40
|
-
!origin &&
|
|
41
|
-
fetchSite &&
|
|
42
|
-
fetchSite !== 'same-origin' &&
|
|
43
|
-
fetchSite !== 'none') {
|
|
62
|
+
if (!origin && fetchSite && fetchSite !== 'same-origin' && fetchSite !== 'none') {
|
|
44
63
|
throw new HttpRequestError(403, 'Cross-origin browser requests require an allowed Origin');
|
|
45
64
|
}
|
|
46
65
|
const target = req.url ?? '/';
|
|
@@ -48,12 +67,58 @@ export function validateHttpRequest(req, options) {
|
|
|
48
67
|
throw new HttpRequestError(400, 'Invalid request target');
|
|
49
68
|
return new URL(target, base);
|
|
50
69
|
}
|
|
70
|
+
/** Bodies read before routing (by the authorization layer, or a host's own layer). */
|
|
71
|
+
const prereadBodies = new WeakMap();
|
|
72
|
+
/** Record `body` as `req`'s, already read: the routes read it instead of the stream. */
|
|
73
|
+
export function setPrereadBody(req, body) {
|
|
74
|
+
prereadBodies.set(req, body);
|
|
75
|
+
}
|
|
76
|
+
/** The body read before routing, if any. */
|
|
77
|
+
export function prereadBody(req) {
|
|
78
|
+
return prereadBodies.get(req);
|
|
79
|
+
}
|
|
80
|
+
/** Whether `req` declares a JSON body. */
|
|
81
|
+
export function isJsonContent(req) {
|
|
82
|
+
return req.headers['content-type']?.split(';')[0]?.trim().toLowerCase() === 'application/json';
|
|
83
|
+
}
|
|
84
|
+
/** Read at most `maxBytes` of `req`'s body, or `undefined` when it is longer. */
|
|
85
|
+
export async function readBodyBytes(req, maxBytes) {
|
|
86
|
+
const length = Number(req.headers['content-length']);
|
|
87
|
+
if (Number.isFinite(length) && length > maxBytes)
|
|
88
|
+
return undefined;
|
|
89
|
+
const chunks = [];
|
|
90
|
+
let size = 0;
|
|
91
|
+
for await (const chunk of req) {
|
|
92
|
+
size += chunk.length;
|
|
93
|
+
if (size > maxBytes)
|
|
94
|
+
return undefined;
|
|
95
|
+
chunks.push(chunk);
|
|
96
|
+
}
|
|
97
|
+
return Buffer.concat(chunks);
|
|
98
|
+
}
|
|
99
|
+
function parseJson(bytes) {
|
|
100
|
+
try {
|
|
101
|
+
return JSON.parse(bytes.toString('utf8'));
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
throw new HttpRequestError(400, 'Request body must be valid JSON', {
|
|
105
|
+
rpcCode: -32700,
|
|
106
|
+
suggestion: 'Send a UTF-8 JSON body',
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
51
110
|
export function readJsonBody(req, maxBytes) {
|
|
52
|
-
|
|
111
|
+
const preread = prereadBodies.get(req);
|
|
112
|
+
if (!preread && (req.destroyed || req.aborted))
|
|
53
113
|
throw new HttpRequestError(400, 'Request body was interrupted');
|
|
54
|
-
if (req
|
|
114
|
+
if (!isJsonContent(req)) {
|
|
55
115
|
throw new HttpRequestError(415, 'Content-Type must be application/json');
|
|
56
116
|
}
|
|
117
|
+
if (preread) {
|
|
118
|
+
if (preread.length > maxBytes)
|
|
119
|
+
throw new HttpRequestError(413, 'Request body is too large');
|
|
120
|
+
return Promise.resolve().then(() => parseJson(preread));
|
|
121
|
+
}
|
|
57
122
|
const length = req.headers['content-length'];
|
|
58
123
|
if (length && Number(length) > maxBytes)
|
|
59
124
|
throw new HttpRequestError(413, 'Request body is too large');
|
|
@@ -80,13 +145,10 @@ export function readJsonBody(req, maxBytes) {
|
|
|
80
145
|
const onEnd = () => {
|
|
81
146
|
cleanup();
|
|
82
147
|
try {
|
|
83
|
-
resolve(
|
|
148
|
+
resolve(parseJson(Buffer.concat(chunks)));
|
|
84
149
|
}
|
|
85
|
-
catch {
|
|
86
|
-
reject(
|
|
87
|
-
rpcCode: -32700,
|
|
88
|
-
suggestion: 'Send a UTF-8 JSON body',
|
|
89
|
-
}));
|
|
150
|
+
catch (error) {
|
|
151
|
+
reject(error);
|
|
90
152
|
}
|
|
91
153
|
};
|
|
92
154
|
const onError = () => fail(new HttpRequestError(400, 'Request body could not be read'));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-security.js","sourceRoot":"","sources":["../src/http-security.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAErC,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAOhC,MAAM;IANhB,8FAA8F;IACrF,OAAO,CAAS;IACzB,6DAA6D;IACpD,UAAU,CAAU;IAE7B,YACU,MAAc,EACvB,OAAe,EACf,OAAO,GAA8C,EAAE;QAEvD,KAAK,CAAC,OAAO,CAAC,CAAC;sBAJN,MAAM;QAKf,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,CAAC,KAAK,CAAC;QACzC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IACtC,CAAC;CACD;
|
|
1
|
+
{"version":3,"file":"http-security.js","sourceRoot":"","sources":["../src/http-security.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAErC,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAOhC,MAAM;IANhB,8FAA8F;IACrF,OAAO,CAAS;IACzB,6DAA6D;IACpD,UAAU,CAAU;IAE7B,YACU,MAAc,EACvB,OAAe,EACf,OAAO,GAA8C,EAAE;QAEvD,KAAK,CAAC,OAAO,CAAC,CAAC;sBAJN,MAAM;QAKf,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,CAAC,KAAK,CAAC;QACzC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IACtC,CAAC;CACD;AASD;;;;GAIG;AACH,MAAM,UAA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AAM,CAAC,CAAC;QACvB,GAAG,CAAC,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QACvB,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACzB,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;QACnD,GAAG,CAAC,IAAI,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;IAChC,CAAC,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/http-sessions.d.ts
CHANGED
|
@@ -3,17 +3,34 @@
|
|
|
3
3
|
*
|
|
4
4
|
* `initialize` creates a session and returns its ID in the `Mcp-Session-Id` response header.
|
|
5
5
|
* Later requests carrying that header share one context stack; requests without it are
|
|
6
|
-
* stateless. Sessions expire after an idle timeout
|
|
7
|
-
*
|
|
6
|
+
* stateless. Sessions expire after an idle timeout. Each session has an owner, who alone may
|
|
7
|
+
* use it, and a quota key (with MCP authorization, the issuer and subject: one person or service
|
|
8
|
+
* across all its clients). A quota key holds at most `maxSessionsPerOwner` sessions: at that
|
|
9
|
+
* quota, or when the store is full, a new session displaces only the least recently used
|
|
10
|
+
* session of the same quota key. One with none is refused while the store is full, so memory
|
|
11
|
+
* stays bounded however many clients connect, no caller ends another's session, and no single
|
|
12
|
+
* subject can fill the store. Up to `maxSessions / maxSessionsPerOwner` distinct subjects can
|
|
13
|
+
* still fill it between them; a new subject is then refused until sessions expire.
|
|
8
14
|
*/
|
|
9
15
|
import { type ContextState } from './bootstrap/afd-context.js';
|
|
10
16
|
/** Default maximum number of live HTTP sessions. */
|
|
11
17
|
export declare const DEFAULT_MAX_SESSIONS = 1000;
|
|
18
|
+
/** Default number of live sessions one owner may hold (never more than `maxSessions`). */
|
|
19
|
+
export declare const DEFAULT_MAX_SESSIONS_PER_OWNER = 100;
|
|
12
20
|
/** Default idle time after which an HTTP session expires (30 minutes). */
|
|
13
21
|
export declare const DEFAULT_SESSION_IDLE_TIMEOUT_MS: number;
|
|
14
22
|
export interface SessionStoreOptions {
|
|
15
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* Maximum live sessions. Beyond it, the creating owner's least recently used session is
|
|
25
|
+
* evicted, or the new session is refused when that owner has none.
|
|
26
|
+
*/
|
|
16
27
|
maxSessions?: number;
|
|
28
|
+
/**
|
|
29
|
+
* Maximum live sessions of one quota key; at it, that key's least recently used session is
|
|
30
|
+
* evicted. Default: `DEFAULT_MAX_SESSIONS_PER_OWNER`, or `maxSessions` when smaller. Never
|
|
31
|
+
* more than `maxSessions`.
|
|
32
|
+
*/
|
|
33
|
+
maxSessionsPerOwner?: number;
|
|
17
34
|
/** Idle time in milliseconds after which a session expires. */
|
|
18
35
|
idleTimeoutMs?: number;
|
|
19
36
|
/** Clock, for tests. */
|
|
@@ -23,9 +40,12 @@ export interface SessionStore {
|
|
|
23
40
|
/**
|
|
24
41
|
* Start a session and return its ID. `owner` binds it to a caller: on a server with MCP
|
|
25
42
|
* authorization, the verified credentials' issuer, subject and client
|
|
26
|
-
* (`credentialsBinding`).
|
|
43
|
+
* (`credentialsBinding`). `quotaKey` (default: `owner`) groups the sessions that share one
|
|
44
|
+
* quota: with MCP authorization, the issuer and subject. At that key's quota, or at capacity,
|
|
45
|
+
* it evicts only that key's least recently used session, and returns `undefined` (nothing
|
|
46
|
+
* created) when the store is full and the key holds none to give up.
|
|
27
47
|
*/
|
|
28
|
-
create(owner?: string): string;
|
|
48
|
+
create(owner?: string, quotaKey?: string): string | undefined;
|
|
29
49
|
/**
|
|
30
50
|
* The context state of a live session, refreshing its idle timer; `undefined` if unknown,
|
|
31
51
|
* expired, or owned by another caller than `owner`. A caller that is not the owner neither
|
|
@@ -37,5 +57,25 @@ export interface SessionStore {
|
|
|
37
57
|
/** Drop every session. */
|
|
38
58
|
clear(): void;
|
|
39
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* `maxSessions` and the per-owner quota, defaulted and checked: the quota defaults to
|
|
62
|
+
* `DEFAULT_MAX_SESSIONS_PER_OWNER`, or `maxSessions` when smaller, and is never above it.
|
|
63
|
+
*
|
|
64
|
+
* @throws Error when either is not a positive integer, or the quota exceeds `maxSessions`
|
|
65
|
+
*/
|
|
66
|
+
export declare function sessionLimits(maxSessions?: number, maxSessionsPerOwner?: number): {
|
|
67
|
+
maxSessions: number;
|
|
68
|
+
perOwner: number;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* A session store's limits, defaults applied, as {@link createSessionStore} reads them.
|
|
72
|
+
*
|
|
73
|
+
* @throws Error as {@link sessionLimits} does, or when the idle timeout is not positive
|
|
74
|
+
*/
|
|
75
|
+
export declare function sessionStoreLimits(options?: SessionStoreOptions): {
|
|
76
|
+
maxSessions: number;
|
|
77
|
+
perOwner: number;
|
|
78
|
+
idleTimeoutMs: number;
|
|
79
|
+
};
|
|
40
80
|
export declare function createSessionStore(options?: SessionStoreOptions): SessionStore;
|
|
41
81
|
//# sourceMappingURL=http-sessions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-sessions.d.ts","sourceRoot":"","sources":["../src/http-sessions.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"http-sessions.d.ts","sourceRoot":"","sources":["../src/http-sessions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAGH,OAAO,EAAE,KAAK,YAAY,EAAsB,MAAM,4BAA4B,CAAC;AAEnF,oDAAoD;AACpD,eAAO,MAAM,oBAAoB,OAAO,CAAC;AAEzC,0FAA0F;AAC1F,eAAO,MAAM,8BAA8B,MAAM,CAAC;AAElD,0EAA0E;AAC1E,eAAO,MAAM,+BAA+B,QAAiB,CAAC;AAE9D,MAAM,WAAW,mBAAmB;IACnC;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;OAIG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,+DAA+D;IAC/D,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,wBAAwB;IACxB,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,YAAY;IAC5B;;;;;;;OAOG;IACH,MAAM,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IAC9D;;;;OAIG;IACH,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,CAAC;IAC1D,yFAAyF;IACzF,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,0BAA0B;IAC1B,KAAK,IAAI,IAAI,CAAC;CACd;AASD;;;;;GAKG;AACH,wBAAgB,aAAa,CAC5B,WAAW,SAAuB,EAClC,mBAAmB,CAAC,EAAE,MAAM,GAC1B;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAS3C;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,mBAAwB;;;;EAOnE;AAED,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,mBAAwB,GAAG,YAAY,CAmDlF"}
|
package/dist/http-sessions.js
CHANGED
|
@@ -3,23 +3,55 @@
|
|
|
3
3
|
*
|
|
4
4
|
* `initialize` creates a session and returns its ID in the `Mcp-Session-Id` response header.
|
|
5
5
|
* Later requests carrying that header share one context stack; requests without it are
|
|
6
|
-
* stateless. Sessions expire after an idle timeout
|
|
7
|
-
*
|
|
6
|
+
* stateless. Sessions expire after an idle timeout. Each session has an owner, who alone may
|
|
7
|
+
* use it, and a quota key (with MCP authorization, the issuer and subject: one person or service
|
|
8
|
+
* across all its clients). A quota key holds at most `maxSessionsPerOwner` sessions: at that
|
|
9
|
+
* quota, or when the store is full, a new session displaces only the least recently used
|
|
10
|
+
* session of the same quota key. One with none is refused while the store is full, so memory
|
|
11
|
+
* stays bounded however many clients connect, no caller ends another's session, and no single
|
|
12
|
+
* subject can fill the store. Up to `maxSessions / maxSessionsPerOwner` distinct subjects can
|
|
13
|
+
* still fill it between them; a new subject is then refused until sessions expire.
|
|
8
14
|
*/
|
|
9
15
|
import { randomUUID } from 'node:crypto';
|
|
10
16
|
import { createContextState } from './bootstrap/afd-context.js';
|
|
11
17
|
/** Default maximum number of live HTTP sessions. */
|
|
12
18
|
export const DEFAULT_MAX_SESSIONS = 1000;
|
|
19
|
+
/** Default number of live sessions one owner may hold (never more than `maxSessions`). */
|
|
20
|
+
export const DEFAULT_MAX_SESSIONS_PER_OWNER = 100;
|
|
13
21
|
/** Default idle time after which an HTTP session expires (30 minutes). */
|
|
14
22
|
export const DEFAULT_SESSION_IDLE_TIMEOUT_MS = 30 * 60 * 1000;
|
|
15
|
-
|
|
16
|
-
|
|
23
|
+
/**
|
|
24
|
+
* `maxSessions` and the per-owner quota, defaulted and checked: the quota defaults to
|
|
25
|
+
* `DEFAULT_MAX_SESSIONS_PER_OWNER`, or `maxSessions` when smaller, and is never above it.
|
|
26
|
+
*
|
|
27
|
+
* @throws Error when either is not a positive integer, or the quota exceeds `maxSessions`
|
|
28
|
+
*/
|
|
29
|
+
export function sessionLimits(maxSessions = DEFAULT_MAX_SESSIONS, maxSessionsPerOwner) {
|
|
17
30
|
if (!Number.isSafeInteger(maxSessions) || maxSessions <= 0) {
|
|
18
31
|
throw new Error('maxSessions must be a positive integer');
|
|
19
32
|
}
|
|
33
|
+
const perOwner = maxSessionsPerOwner ?? Math.min(DEFAULT_MAX_SESSIONS_PER_OWNER, maxSessions);
|
|
34
|
+
if (!Number.isSafeInteger(perOwner) || perOwner <= 0 || perOwner > maxSessions) {
|
|
35
|
+
throw new Error('maxSessionsPerOwner must be a positive integer no greater than maxSessions');
|
|
36
|
+
}
|
|
37
|
+
return { maxSessions, perOwner };
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A session store's limits, defaults applied, as {@link createSessionStore} reads them.
|
|
41
|
+
*
|
|
42
|
+
* @throws Error as {@link sessionLimits} does, or when the idle timeout is not positive
|
|
43
|
+
*/
|
|
44
|
+
export function sessionStoreLimits(options = {}) {
|
|
45
|
+
const { idleTimeoutMs = DEFAULT_SESSION_IDLE_TIMEOUT_MS } = options;
|
|
46
|
+
const { maxSessions, perOwner } = sessionLimits(options.maxSessions, options.maxSessionsPerOwner);
|
|
20
47
|
if (!Number.isFinite(idleTimeoutMs) || idleTimeoutMs <= 0) {
|
|
21
48
|
throw new Error('sessionIdleTimeoutMs must be a positive number');
|
|
22
49
|
}
|
|
50
|
+
return { maxSessions, perOwner, idleTimeoutMs };
|
|
51
|
+
}
|
|
52
|
+
export function createSessionStore(options = {}) {
|
|
53
|
+
const { now = Date.now } = options;
|
|
54
|
+
const { maxSessions, perOwner, idleTimeoutMs } = sessionStoreLimits(options);
|
|
23
55
|
// Map order is least recently used first: `get` moves a session to the end.
|
|
24
56
|
const sessions = new Map();
|
|
25
57
|
function sweep(at) {
|
|
@@ -30,16 +62,26 @@ export function createSessionStore(options = {}) {
|
|
|
30
62
|
}
|
|
31
63
|
}
|
|
32
64
|
return {
|
|
33
|
-
create(owner) {
|
|
65
|
+
create(owner, quotaKey = owner) {
|
|
34
66
|
const at = now();
|
|
35
67
|
sweep(at);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
68
|
+
// Room comes only from the caller's own sessions (its quota key's), least recently used
|
|
69
|
+
// first: one caller's `initialize` never ends another caller's session.
|
|
70
|
+
let oldestOwn;
|
|
71
|
+
let own = 0;
|
|
72
|
+
for (const [id, session] of sessions) {
|
|
73
|
+
if (session.quotaKey !== quotaKey)
|
|
74
|
+
continue;
|
|
75
|
+
oldestOwn ??= id;
|
|
76
|
+
own++;
|
|
77
|
+
}
|
|
78
|
+
if (own >= perOwner || sessions.size >= maxSessions) {
|
|
79
|
+
if (oldestOwn === undefined)
|
|
80
|
+
return undefined;
|
|
81
|
+
sessions.delete(oldestOwn);
|
|
40
82
|
}
|
|
41
83
|
const id = randomUUID();
|
|
42
|
-
sessions.set(id, { state: createContextState(), lastSeen: at, owner });
|
|
84
|
+
sessions.set(id, { state: createContextState(), lastSeen: at, owner, quotaKey });
|
|
43
85
|
return id;
|
|
44
86
|
},
|
|
45
87
|
get(id, owner) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-sessions.js","sourceRoot":"","sources":["../src/http-sessions.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"http-sessions.js","sourceRoot":"","sources":["../src/http-sessions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAqB,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAEnF,oDAAoD;AACpD,MAAM,CAAC,MAAM,oBAAoB,GAAG,IAAI,CAAC;AAEzC,0FAA0F;AAC1F,MAAM,CAAC,MAAM,8BAA8B,GAAG,GAAG,CAAC;AAElD,0EAA0E;AAC1E,MAAM,CAAC,MAAM,+BAA+B,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAiD9D;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAC5B,WAAW,GAAG,oBAAoB,EAClC,mBAA4B;IAE5B,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,WAAW,CAAC,IAAI,WAAW,IAAI,CAAC,EAAE,CAAC;QAC5D,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IAC3D,CAAC;IACD,MAAM,QAAQ,GAAG,mBAAmB,IAAI,IAAI,CAAC,GAAG,CAAC,8BAA8B,EAAE,WAAW,CAAC,CAAC;IAC9F,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,QAAQ,CAAC,IAAI,QAAQ,IAAI,CAAC,IAAI,QAAQ,GAAG,WAAW,EAAE,CAAC;QAChF,MAAM,IAAI,KAAK,CAAC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ACD,IAAI,IAAI;YACP,OAAO,QAAQ,CAAC,IAAI,CAAC;QACtB,CAAC;QACD,KAAK;YACJ,QAAQ,CAAC,KAAK,EAAE,CAAC;QAClB,CAAC;KACD,CAAC;AACH,CAAC"}
|
package/dist/http-sse.d.ts
CHANGED
|
@@ -14,6 +14,15 @@ export interface SseHubOptions {
|
|
|
14
14
|
maxConnections?: number;
|
|
15
15
|
heartbeatMs?: number;
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* The hub's limits, defaults applied.
|
|
19
|
+
*
|
|
20
|
+
* @throws Error when the connection cap is not a positive integer or the interval not positive
|
|
21
|
+
*/
|
|
22
|
+
export declare function sseHubLimits(options?: SseHubOptions): {
|
|
23
|
+
maxConnections: number;
|
|
24
|
+
heartbeatMs: number;
|
|
25
|
+
};
|
|
17
26
|
/**
|
|
18
27
|
* Track open `/sse` responses. One shared timer writes a `: ping` comment to every client so
|
|
19
28
|
* proxies keep idle streams open and dead peers are detected; it runs only while clients exist.
|
package/dist/http-sse.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-sse.d.ts","sourceRoot":"","sources":["../src/http-sse.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEhD,+DAA+D;AAC/D,eAAO,MAAM,2BAA2B,MAAM,CAAC;AAE/C,oEAAoE;AACpE,eAAO,MAAM,wBAAwB,QAAS,CAAC;AAE/C,MAAM,WAAW,SAAS;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,cAAc,CAAC;CACzB;AAED,MAAM,WAAW,aAAa;IAC7B,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE,aAAkB;;
|
|
1
|
+
{"version":3,"file":"http-sse.d.ts","sourceRoot":"","sources":["../src/http-sse.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEhD,+DAA+D;AAC/D,eAAO,MAAM,2BAA2B,MAAM,CAAC;AAE/C,oEAAoE;AACpE,eAAO,MAAM,wBAAwB,QAAS,CAAC;AAE/C,MAAM,WAAW,SAAS;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,cAAc,CAAC;CACzB;AAED,MAAM,WAAW,aAAa;IAC7B,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE,aAAkB;;;EAUvD;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE,aAAkB;;IA2BtD,uDAAuD;cAC7C,OAAO;IAGjB,sFAAsF;kBACxE,cAAc,GAAG,SAAS;IAUxC,mDAAmD;gBACvC,IAAI;EAMjB"}
|
package/dist/http-sse.js
CHANGED
|
@@ -6,10 +6,11 @@ export const DEFAULT_MAX_SSE_CONNECTIONS = 100;
|
|
|
6
6
|
/** Default interval between SSE heartbeat comments (25 seconds). */
|
|
7
7
|
export const DEFAULT_SSE_HEARTBEAT_MS = 25_000;
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
9
|
+
* The hub's limits, defaults applied.
|
|
10
|
+
*
|
|
11
|
+
* @throws Error when the connection cap is not a positive integer or the interval not positive
|
|
11
12
|
*/
|
|
12
|
-
export function
|
|
13
|
+
export function sseHubLimits(options = {}) {
|
|
13
14
|
const maxConnections = options.maxConnections ?? DEFAULT_MAX_SSE_CONNECTIONS;
|
|
14
15
|
const heartbeatMs = options.heartbeatMs ?? DEFAULT_SSE_HEARTBEAT_MS;
|
|
15
16
|
if (!Number.isSafeInteger(maxConnections) || maxConnections <= 0) {
|
|
@@ -18,6 +19,14 @@ export function createSseHub(options = {}) {
|
|
|
18
19
|
if (!Number.isFinite(heartbeatMs) || heartbeatMs <= 0) {
|
|
19
20
|
throw new Error('SSE heartbeat interval must be positive');
|
|
20
21
|
}
|
|
22
|
+
return { maxConnections, heartbeatMs };
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Track open `/sse` responses. One shared timer writes a `: ping` comment to every client so
|
|
26
|
+
* proxies keep idle streams open and dead peers are detected; it runs only while clients exist.
|
|
27
|
+
*/
|
|
28
|
+
export function createSseHub(options = {}) {
|
|
29
|
+
const { maxConnections, heartbeatMs } = sseHubLimits(options);
|
|
21
30
|
const clients = new Map();
|
|
22
31
|
let counter = 0;
|
|
23
32
|
let heartbeat;
|
package/dist/http-sse.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-sse.js","sourceRoot":"","sources":["../src/http-sse.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,+DAA+D;AAC/D,MAAM,CAAC,MAAM,2BAA2B,GAAG,GAAG,CAAC;AAE/C,oEAAoE;AACpE,MAAM,CAAC,MAAM,wBAAwB,GAAG,MAAM,CAAC;AAY/C
|
|
1
|
+
{"version":3,"file":"http-sse.js","sourceRoot":"","sources":["../src/http-sse.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,+DAA+D;AAC/D,MAAM,CAAC,MAAM,2BAA2B,GAAG,GAAG,CAAC;AAE/C,oEAAoE;AACpE,MAAM,CAAC,MAAM,wBAAwB,GAAG,MAAM,CAAC;AAY/C;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,OAAO,GAAkB,EAAE;IACvD,MAAM,cAAc,GAAG,OAAO,CAAC,cAAc,IAAI,2BAA2B,CAAC;IAC7E,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,wBAAwB,CAAC;IACpE,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,cAAc,CAAC,IAAI,cAAc,IAAI,CAAC,EAAE,CAAC;QAClE,MAAM,IAAI,KAAK,CAA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tF,GAAG,CAAC,QAAwB;YAC3B,MAAM,MAAM,GAAG,EAAE,EAAE,EAAE,UAAU,EAAE,OAAO,EAAE,EAAE,QAAQ,EAAE,CAAC;YACvD,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;YAC/B,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC;YAChD,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;gBAC7B,SAAS,GAAG,WAAW,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;gBAC3C,SAAS,CAAC,KAAK,EAAE,CAAC;YACnB,CAAC;YACD,OAAO,MAAM,CAAC;QACf,CAAC;QACD,mDAAmD;QACnD,QAAQ;YACP,KAAK,MAAM,MAAM,IAAI,OAAO,CAAC,MAAM,EAAE;gBAAE,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC;YAC7D,OAAO,CAAC,KAAK,EAAE,CAAC;YAChB,aAAa,EAAE,CAAC;QACjB,CAAC;KACD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Host-verified confirmation (`spec/confirmations.md`, "Host-verified
|
|
3
|
+
* confirmation"; `spec/approvals.md`, "Relay evidence"): the {@link HumanRelay} a host installs with
|
|
4
|
+
* `createMcpServer({ humanRelay })` or `createMcpHandler({ humanRelay })`, `inputDigest`, and the
|
|
5
|
+
* gate that enforces it on every MCP and HTTP surface regardless of `confirmations`.
|
|
6
|
+
*
|
|
7
|
+
* Node-only (uses `node:crypto` and `@modelcontextprotocol/server`); never imported by `/direct`
|
|
8
|
+
* or `/define`.
|
|
9
|
+
*/
|
|
10
|
+
import type { Actor, Clock, CommandContext, CommandError } from '@lushly-dev/afd-core';
|
|
11
|
+
import type { HumanRelayGate } from './confirmations.js';
|
|
12
|
+
import { type ContextField } from './context-fields.js';
|
|
13
|
+
import type { Credentials } from './mcp-auth-rules.js';
|
|
14
|
+
import { type PrincipalCredentials } from './principal-binding.js';
|
|
15
|
+
import type { ZodCommandDefinition } from './schema.js';
|
|
16
|
+
/** The `inputRequests` key of the URL-mode elicitation a {@link HumanRelay} prompt becomes. */
|
|
17
|
+
export declare const RELAY_INPUT_KEY = "relay";
|
|
18
|
+
/** What an MCP lane carries for host-verified confirmation (`spec/confirmations.md`). */
|
|
19
|
+
export interface HumanRelayChannel {
|
|
20
|
+
readonly modern: boolean;
|
|
21
|
+
readonly clientCapabilities?: Record<string, unknown>;
|
|
22
|
+
readonly requestState?: string;
|
|
23
|
+
readonly relayAction?: 'accept' | 'decline' | 'cancel';
|
|
24
|
+
}
|
|
25
|
+
/** Return a copy of `context` carrying the lane's host-verified relay channel. */
|
|
26
|
+
export declare function bindHumanRelayChannel(context: CommandContext, channel: HumanRelayChannel): CommandContext;
|
|
27
|
+
/** The host-verified relay channel bound to `context`, if any. */
|
|
28
|
+
export declare function humanRelayChannel(context: CommandContext | undefined): HumanRelayChannel | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Compute the relay input digest (`spec/confirmations.md`, "Host-verified confirmation",
|
|
31
|
+
* step 2): unpadded base64url SHA-256 of the canonical JSON (`spec/approvals.md`,
|
|
32
|
+
* "Approvals", rule 3) of `input`. `spec/authorization.json` (`inputDigests`) pins it.
|
|
33
|
+
*
|
|
34
|
+
* @throws TypeError when `input` has no exact canonical JSON form (e.g. `bigint`, non-finite
|
|
35
|
+
* number, cycle, `Map`, `Set`, class instance)
|
|
36
|
+
*/
|
|
37
|
+
export declare function inputDigest(input: unknown): string;
|
|
38
|
+
/** One call a {@link HumanRelay} is asked about. */
|
|
39
|
+
export interface RelayCall {
|
|
40
|
+
/** The command's name. */
|
|
41
|
+
readonly command: string;
|
|
42
|
+
/** {@link inputDigest} of the validated input (defaults applied): what the person decides on. */
|
|
43
|
+
readonly inputDigest: string;
|
|
44
|
+
/** The actor the command runs with (after `createContext` and verified credentials). */
|
|
45
|
+
readonly actor?: Actor;
|
|
46
|
+
/** The call's verified credentials, on an authorized server (or from `createContext`). */
|
|
47
|
+
readonly credentials?: Credentials | PrincipalCredentials;
|
|
48
|
+
/** The client capabilities declared on the request. */
|
|
49
|
+
readonly clientCapabilities?: Record<string, unknown>;
|
|
50
|
+
/** The validated input that {@link inputDigest} names, so the host can show it to the person. */
|
|
51
|
+
readonly input: unknown;
|
|
52
|
+
/**
|
|
53
|
+
* The context the command would run with: after `createContext` and declared context fields,
|
|
54
|
+
* with verified credentials and actor. Relay evidence is never on it.
|
|
55
|
+
*/
|
|
56
|
+
readonly context: CommandContext;
|
|
57
|
+
/**
|
|
58
|
+
* The call's principal binding (`spec/authorization.md`, M8), which the relay's state must
|
|
59
|
+
* bind.
|
|
60
|
+
*/
|
|
61
|
+
principalBinding(): string;
|
|
62
|
+
/**
|
|
63
|
+
* Whether the client declared URL-mode elicitation, without which the server never sends the
|
|
64
|
+
* relay's prompt.
|
|
65
|
+
*/
|
|
66
|
+
supportsUrlElicitation(): boolean;
|
|
67
|
+
}
|
|
68
|
+
/** What the person is asked to open, and the state that names the pending decision. */
|
|
69
|
+
export interface RelayPrompt {
|
|
70
|
+
/** An absolute `https` URL that holds no identity and is not pre-authenticated. */
|
|
71
|
+
url: string;
|
|
72
|
+
/** The elicitation's message: ids, never content. */
|
|
73
|
+
message: string;
|
|
74
|
+
/** The `requestState`: integrity-protected by the relay (expires within 24 hours). */
|
|
75
|
+
state: string;
|
|
76
|
+
}
|
|
77
|
+
/** Result of {@link HumanRelay.begin}: a prompt to send, or a refusal error. */
|
|
78
|
+
export type RelayBeginResult = {
|
|
79
|
+
ok: true;
|
|
80
|
+
prompt: RelayPrompt;
|
|
81
|
+
} | {
|
|
82
|
+
ok: false;
|
|
83
|
+
error: CommandError;
|
|
84
|
+
};
|
|
85
|
+
/** What {@link HumanRelay.resume} found for the pending decision. */
|
|
86
|
+
export type RelayOutcome = {
|
|
87
|
+
kind: 'approved';
|
|
88
|
+
decisionId: string;
|
|
89
|
+
} | {
|
|
90
|
+
kind: 'declined';
|
|
91
|
+
} | {
|
|
92
|
+
kind: 'pending';
|
|
93
|
+
prompt: RelayPrompt;
|
|
94
|
+
} | {
|
|
95
|
+
kind: 'invalid';
|
|
96
|
+
};
|
|
97
|
+
/** A pending decision's address, for a client without URL-mode elicitation. */
|
|
98
|
+
export interface RelayPending {
|
|
99
|
+
/** The decision's id: 1 to 128 ASCII letters, digits, `_` and `-`. */
|
|
100
|
+
decision: string;
|
|
101
|
+
/** The decision's page: an absolute `https` URL with no identity, as {@link RelayPrompt.url}. */
|
|
102
|
+
url: string;
|
|
103
|
+
/**
|
|
104
|
+
* The page listing the person's pending decisions, the same kind of URL. A fixed address the
|
|
105
|
+
* person can learn, so the refusal's suggestion names it first.
|
|
106
|
+
*/
|
|
107
|
+
list: string;
|
|
108
|
+
}
|
|
109
|
+
/** What {@link HumanRelay.pendingList} found for a call from a client without URL-mode elicitation. */
|
|
110
|
+
export type PendingListOutcome = {
|
|
111
|
+
kind: 'approved';
|
|
112
|
+
decisionId: string;
|
|
113
|
+
} | {
|
|
114
|
+
kind: 'declined';
|
|
115
|
+
} | ({
|
|
116
|
+
kind: 'pending';
|
|
117
|
+
} & RelayPending) | {
|
|
118
|
+
kind: 'error';
|
|
119
|
+
error: CommandError;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* The host's way to prove that a person decided a call to a host-verified command
|
|
123
|
+
* (`spec/confirmations.md`, "Host-verified confirmation").
|
|
124
|
+
*
|
|
125
|
+
* A relay guarantees:
|
|
126
|
+
* - Its `state` is integrity-protected, expires within 24 hours, and binds the decision, the
|
|
127
|
+
* command, `inputDigest` and `call.principalBinding()` (M8). `resume` returns
|
|
128
|
+
* `{ kind: 'invalid' }` for a state that does not verify, has expired, or names another
|
|
129
|
+
* command, input or principal.
|
|
130
|
+
* - `{ kind: 'approved', decisionId }` is returned at most once per decision, atomically, with
|
|
131
|
+
* a `decisionId` of 1 to 128 ASCII letters, digits, `_` and `-`, and only for a decision the
|
|
132
|
+
* person made after `begin`.
|
|
133
|
+
* - Its `url` (and `list` in `pendingList`) is an absolute `https` URL with a valid ASCII DNS
|
|
134
|
+
* host or bracketed IPv6 address, optional port 1–65535, holding no identity and not
|
|
135
|
+
* pre-authenticated.
|
|
136
|
+
*/
|
|
137
|
+
export interface HumanRelay {
|
|
138
|
+
/** Start a decision for `call`, or refuse it with a `CommandError`. */
|
|
139
|
+
begin(call: RelayCall): Promise<RelayBeginResult> | RelayBeginResult;
|
|
140
|
+
/** Check what became of the decision `state` names for `call`. */
|
|
141
|
+
resume(state: string, call: RelayCall): Promise<RelayOutcome> | RelayOutcome;
|
|
142
|
+
/**
|
|
143
|
+
* Optional host-owned context fields for an approved call, merged onto the call's context
|
|
144
|
+
* before middleware and the handler run (replacing any request-supplied field of the same
|
|
145
|
+
* name, never reserved AFD keys). Not evidence: stays on context across nested in-process
|
|
146
|
+
* calls.
|
|
147
|
+
*/
|
|
148
|
+
approvedContext?(decisionId: string, call: RelayCall): Promise<Record<string, unknown>> | Record<string, unknown>;
|
|
149
|
+
/**
|
|
150
|
+
* Optional pending-list fallback for a call from a client that did not declare URL-mode
|
|
151
|
+
* elicitation (`spec/confirmations.md`, "The pending list"). Returning `undefined` (or
|
|
152
|
+
* omitting the method) keeps the default refusal; answering `pending` fails with a retryable
|
|
153
|
+
* `CONFIRMATION_REQUIRED` naming the decision, its page and the pending list, and `approved`
|
|
154
|
+
* runs the command through the same relay evidence and `approvedContext` path as `resume`.
|
|
155
|
+
*/
|
|
156
|
+
pendingList?(call: RelayCall): Promise<PendingListOutcome | undefined> | PendingListOutcome | undefined;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Whether declared client capabilities include URL-mode elicitation (`elicitation.url`).
|
|
160
|
+
* `elicitation: {}` (form only) and `elicitation: { form: {} }` return `false`.
|
|
161
|
+
*/
|
|
162
|
+
export declare function allowsUrlElicitation(capabilities: unknown): boolean;
|
|
163
|
+
/**
|
|
164
|
+
* Whether `id` is a valid relay decision id: 1 to 128 ASCII letters, digits, `_` and `-`
|
|
165
|
+
* (`spec/confirmations.md`, "Host-verified confirmation"), so it carries no sentence, markup
|
|
166
|
+
* or link into a refusal suggestion.
|
|
167
|
+
*/
|
|
168
|
+
export declare function isDecisionId(id: unknown): id is string;
|
|
169
|
+
/**
|
|
170
|
+
* Whether `url` is an absolute `https` URL with a valid host (DNS labels of ASCII letters, digits
|
|
171
|
+
* and `-`, or a bracketed IPv6 address), an optional port from 1 to 65535, no user information,
|
|
172
|
+
* and no whitespace, control character or unpaired surrogate.
|
|
173
|
+
*/
|
|
174
|
+
export declare function isRelayUrl(url: unknown): url is string;
|
|
175
|
+
/**
|
|
176
|
+
* Refusal for a host-verified command the server cannot relay: no relay is installed, or the call
|
|
177
|
+
* is a batch/pipeline step, a 2025-era request, or an HTTP `/rpc`, `/batch` or `/stream` route.
|
|
178
|
+
*/
|
|
179
|
+
export declare function hostVerifiedUnrelayable(names: readonly string[], hasRelay: boolean): CommandError;
|
|
180
|
+
export interface HumanRelayGateOptions {
|
|
181
|
+
relay?: HumanRelay;
|
|
182
|
+
commands: readonly ZodCommandDefinition[];
|
|
183
|
+
actor: Actor;
|
|
184
|
+
devMode: boolean;
|
|
185
|
+
contextFields?: readonly ContextField[];
|
|
186
|
+
clock?: Clock;
|
|
187
|
+
maxValidationIssues?: number;
|
|
188
|
+
onError?: (error: Error) => void;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Create the host-verified confirmation gate wired into `createToolRouter` on MCP servers.
|
|
192
|
+
* Enforced on every MCP surface regardless of `confirmations` (`'off'` included).
|
|
193
|
+
*/
|
|
194
|
+
export declare function createHumanRelayGate(options: HumanRelayGateOptions): HumanRelayGate;
|
|
195
|
+
//# sourceMappingURL=human-relay.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"human-relay.d.ts","sourceRoot":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