@jangmanj/mcp 0.0.0-stage → 0.12.3
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/LICENSE +21 -0
- package/README.md +337 -2
- package/adapters/pi/HOST-NOTES.md +65 -0
- package/adapters/pi/bridge.ts +721 -0
- package/adapters/pi/config.ts +364 -0
- package/adapters/pi/content-offloaded.ts +53 -0
- package/adapters/pi/content.ts +78 -0
- package/adapters/pi/diagnostics.ts +26 -0
- package/adapters/pi/elicitation.ts +133 -0
- package/adapters/pi/index.ts +210 -0
- package/adapters/pi/oauth.ts +219 -0
- package/adapters/pi/pi-api.ts +133 -0
- package/adapters/pi/resources.ts +177 -0
- package/adapters/pi/result-files.ts +32 -0
- package/adapters/pi/tasks.ts +49 -0
- package/dist/auth/client-credentials.d.ts +65 -0
- package/dist/auth/client-credentials.js +388 -0
- package/dist/auth/discovery.d.ts +28 -0
- package/dist/auth/discovery.js +125 -0
- package/dist/auth/index.d.ts +9 -0
- package/dist/auth/index.js +5 -0
- package/dist/auth/node-ports.d.ts +8 -0
- package/dist/auth/node-ports.js +67 -0
- package/dist/auth/protocol.d.ts +12 -0
- package/dist/auth/protocol.js +39 -0
- package/dist/auth/provider.d.ts +114 -0
- package/dist/auth/provider.js +407 -0
- package/dist/auth/safe-fetch.d.ts +14 -0
- package/dist/auth/safe-fetch.js +130 -0
- package/dist/client/events.d.ts +33 -0
- package/dist/client/events.js +84 -0
- package/dist/client/index.d.ts +65 -0
- package/dist/client/index.js +128 -0
- package/dist/core/auth-types.d.ts +36 -0
- package/dist/core/auth-types.js +11 -0
- package/dist/core/bearer-challenge.d.ts +3 -0
- package/dist/core/bearer-challenge.js +63 -0
- package/dist/core/cancel.d.ts +70 -0
- package/dist/core/cancel.js +177 -0
- package/dist/core/extension.d.ts +33 -0
- package/dist/core/extension.js +145 -0
- package/dist/core/index.d.ts +73 -0
- package/dist/core/index.js +129 -0
- package/dist/core/jsonrpc.d.ts +49 -0
- package/dist/core/jsonrpc.js +80 -0
- package/dist/core/lifecycle.d.ts +128 -0
- package/dist/core/lifecycle.js +251 -0
- package/dist/core/outcome.d.ts +98 -0
- package/dist/core/outcome.js +32 -0
- package/dist/core/ports.d.ts +144 -0
- package/dist/core/ports.js +67 -0
- package/dist/core/progress.d.ts +81 -0
- package/dist/core/progress.js +205 -0
- package/dist/core/request.d.ts +108 -0
- package/dist/core/request.js +578 -0
- package/dist/core/schema/index.d.ts +1 -0
- package/dist/core/schema/index.js +1 -0
- package/dist/core/schema/types.d.ts +4154 -0
- package/dist/core/schema/types.js +6 -0
- package/dist/core/telemetry.d.ts +25 -0
- package/dist/core/telemetry.js +37 -0
- package/dist/ext-events/index.d.ts +79 -0
- package/dist/ext-events/index.js +218 -0
- package/dist/ext-events/model.d.ts +10 -0
- package/dist/ext-events/model.js +6 -0
- package/dist/ext-events/push.d.ts +18 -0
- package/dist/ext-events/push.js +101 -0
- package/dist/ext-events/stream.d.ts +6 -0
- package/dist/ext-events/stream.js +60 -0
- package/dist/ext-server-cards/index.d.ts +107 -0
- package/dist/ext-server-cards/index.js +249 -0
- package/dist/ext-skills/cache.d.ts +20 -0
- package/dist/ext-skills/cache.js +101 -0
- package/dist/ext-skills/client.d.ts +82 -0
- package/dist/ext-skills/client.js +292 -0
- package/dist/ext-skills/extension.d.ts +16 -0
- package/dist/ext-skills/extension.js +29 -0
- package/dist/ext-skills/index.d.ts +4 -0
- package/dist/ext-skills/index.js +4 -0
- package/dist/ext-skills/verify.d.ts +57 -0
- package/dist/ext-skills/verify.js +168 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/mrtr-cache/cache.d.ts +51 -0
- package/dist/mrtr-cache/cache.js +295 -0
- package/dist/mrtr-cache/client.d.ts +24 -0
- package/dist/mrtr-cache/client.js +44 -0
- package/dist/mrtr-cache/common.d.ts +10 -0
- package/dist/mrtr-cache/common.js +9 -0
- package/dist/mrtr-cache/elicitation.d.ts +42 -0
- package/dist/mrtr-cache/elicitation.js +221 -0
- package/dist/mrtr-cache/index.d.ts +9 -0
- package/dist/mrtr-cache/index.js +8 -0
- package/dist/mrtr-cache/mrtr.d.ts +37 -0
- package/dist/mrtr-cache/mrtr.js +250 -0
- package/dist/mrtr-cache/refresh.d.ts +8 -0
- package/dist/mrtr-cache/refresh.js +30 -0
- package/dist/mrtr-cache/stream.d.ts +5 -0
- package/dist/mrtr-cache/stream.js +18 -0
- package/dist/mrtr-cache/subscriptions.d.ts +96 -0
- package/dist/mrtr-cache/subscriptions.js +288 -0
- package/dist/mrtr-cache/tasks.d.ts +103 -0
- package/dist/mrtr-cache/tasks.js +444 -0
- package/dist/ops/common.d.ts +18 -0
- package/dist/ops/common.js +39 -0
- package/dist/ops/completion.d.ts +44 -0
- package/dist/ops/completion.js +65 -0
- package/dist/ops/content.d.ts +40 -0
- package/dist/ops/content.js +117 -0
- package/dist/ops/icons.d.ts +29 -0
- package/dist/ops/icons.js +82 -0
- package/dist/ops/index.d.ts +30 -0
- package/dist/ops/index.js +28 -0
- package/dist/ops/pagination.d.ts +32 -0
- package/dist/ops/pagination.js +65 -0
- package/dist/ops/prompts.d.ts +32 -0
- package/dist/ops/prompts.js +68 -0
- package/dist/ops/resources.d.ts +42 -0
- package/dist/ops/resources.js +104 -0
- package/dist/ops/schema.d.ts +39 -0
- package/dist/ops/schema.js +163 -0
- package/dist/ops/tools.d.ts +88 -0
- package/dist/ops/tools.js +169 -0
- package/dist/ops/uri-template.d.ts +3 -0
- package/dist/ops/uri-template.js +101 -0
- package/dist/server/index.d.ts +5 -0
- package/dist/server/index.js +5 -0
- package/dist/server/mrtr.d.ts +38 -0
- package/dist/server/mrtr.js +99 -0
- package/dist/server/protocol.d.ts +21 -0
- package/dist/server/protocol.js +49 -0
- package/dist/server/server.d.ts +105 -0
- package/dist/server/server.js +383 -0
- package/dist/server/skills.d.ts +45 -0
- package/dist/server/skills.js +73 -0
- package/dist/server/uri.d.ts +2 -0
- package/dist/server/uri.js +27 -0
- package/dist/server-events/bus.d.ts +40 -0
- package/dist/server-events/bus.js +84 -0
- package/dist/server-events/events.d.ts +36 -0
- package/dist/server-events/events.js +108 -0
- package/dist/server-events/index.d.ts +6 -0
- package/dist/server-events/index.js +6 -0
- package/dist/server-events/notices.d.ts +24 -0
- package/dist/server-events/notices.js +31 -0
- package/dist/server-events/subscriptions.d.ts +33 -0
- package/dist/server-events/subscriptions.js +77 -0
- package/dist/server-events/tasks.d.ts +43 -0
- package/dist/server-events/tasks.js +101 -0
- package/dist/server-events/types.d.ts +25 -0
- package/dist/server-events/types.js +34 -0
- package/dist/server-transport/auth.d.ts +20 -0
- package/dist/server-transport/auth.js +29 -0
- package/dist/server-transport/channel.d.ts +17 -0
- package/dist/server-transport/channel.js +47 -0
- package/dist/server-transport/http.d.ts +19 -0
- package/dist/server-transport/http.js +209 -0
- package/dist/server-transport/index.d.ts +4 -0
- package/dist/server-transport/index.js +4 -0
- package/dist/server-transport/stdio.d.ts +16 -0
- package/dist/server-transport/stdio.js +83 -0
- package/dist/telemetry-otel/index.d.ts +64 -0
- package/dist/telemetry-otel/index.js +196 -0
- package/dist/testing/index.d.ts +36 -0
- package/dist/testing/index.js +140 -0
- package/dist/transport-http/headers.d.ts +24 -0
- package/dist/transport-http/headers.js +140 -0
- package/dist/transport-http/index.d.ts +3 -0
- package/dist/transport-http/index.js +3 -0
- package/dist/transport-http/sse.d.ts +33 -0
- package/dist/transport-http/sse.js +152 -0
- package/dist/transport-http/transport.d.ts +66 -0
- package/dist/transport-http/transport.js +854 -0
- package/dist/transport-stdio/framing.d.ts +50 -0
- package/dist/transport-stdio/framing.js +154 -0
- package/dist/transport-stdio/index.d.ts +4 -0
- package/dist/transport-stdio/index.js +4 -0
- package/dist/transport-stdio/stderr-ring.d.ts +27 -0
- package/dist/transport-stdio/stderr-ring.js +105 -0
- package/dist/transport-stdio/transport.d.ts +142 -0
- package/dist/transport-stdio/transport.js +743 -0
- package/dist/transport-stdio/write-queue.d.ts +48 -0
- package/dist/transport-stdio/write-queue.js +147 -0
- package/package.json +134 -4
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import { object, withAbort } from "./common.js";
|
|
2
|
+
const methods = new Set([
|
|
3
|
+
"server/discover",
|
|
4
|
+
"tools/list",
|
|
5
|
+
"prompts/list",
|
|
6
|
+
"resources/list",
|
|
7
|
+
"resources/templates/list",
|
|
8
|
+
"resources/read",
|
|
9
|
+
]);
|
|
10
|
+
const lists = (method) => method.endsWith("/list");
|
|
11
|
+
const WAITER_ABORTED = Symbol("cache-waiter-aborted");
|
|
12
|
+
function canonical(params) {
|
|
13
|
+
const { _meta, ...rest } = params;
|
|
14
|
+
return JSON.stringify(rest, (_k, v) => object(v)
|
|
15
|
+
? Object.fromEntries(Object.keys(v)
|
|
16
|
+
.sort()
|
|
17
|
+
.map((k) => [k, v[k]]))
|
|
18
|
+
: v);
|
|
19
|
+
}
|
|
20
|
+
const mark = (o, cache) => ({ ...o, meta: { ...o.meta, cache } });
|
|
21
|
+
export class ResultCache {
|
|
22
|
+
options;
|
|
23
|
+
memory = new Map();
|
|
24
|
+
stores;
|
|
25
|
+
flights = new Map();
|
|
26
|
+
now;
|
|
27
|
+
constructor(options = {}) {
|
|
28
|
+
this.options = options;
|
|
29
|
+
this.stores = options.store ? [this.memory, options.store] : [this.memory];
|
|
30
|
+
this.now = options.now ?? (() => performance.now());
|
|
31
|
+
for (const n of [
|
|
32
|
+
options.maxEntries,
|
|
33
|
+
options.maxBytes,
|
|
34
|
+
options.maxEntryBytes,
|
|
35
|
+
options.maxTtlMs,
|
|
36
|
+
])
|
|
37
|
+
if (n !== undefined && (!Number.isFinite(n) || n < 0))
|
|
38
|
+
throw RangeError("invalid cache bound");
|
|
39
|
+
}
|
|
40
|
+
purge(match) {
|
|
41
|
+
for (const s of this.stores)
|
|
42
|
+
for (const [k, e] of s)
|
|
43
|
+
if (match(e))
|
|
44
|
+
s.delete(k);
|
|
45
|
+
for (const fs of this.flights.values())
|
|
46
|
+
for (const f of fs)
|
|
47
|
+
if (match(f))
|
|
48
|
+
f.valid = false;
|
|
49
|
+
}
|
|
50
|
+
invalidate(serverRef, method, uri) {
|
|
51
|
+
this.purge((e) => e.serverRef === serverRef &&
|
|
52
|
+
e.method === method &&
|
|
53
|
+
(uri === undefined || e.uri === uri));
|
|
54
|
+
}
|
|
55
|
+
notification(serverRef, method, params = {}) {
|
|
56
|
+
switch (method) {
|
|
57
|
+
case "notifications/tools/list_changed":
|
|
58
|
+
this.invalidate(serverRef, "tools/list");
|
|
59
|
+
break;
|
|
60
|
+
case "notifications/prompts/list_changed":
|
|
61
|
+
this.invalidate(serverRef, "prompts/list");
|
|
62
|
+
break;
|
|
63
|
+
case "notifications/resources/list_changed":
|
|
64
|
+
this.invalidate(serverRef, "resources/list");
|
|
65
|
+
break;
|
|
66
|
+
case "notifications/resources/updated":
|
|
67
|
+
if (typeof params.uri === "string")
|
|
68
|
+
this.invalidate(serverRef, "resources/read", params.uri);
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
insert(entry) {
|
|
73
|
+
const external = this.options.store;
|
|
74
|
+
const store = external && (entry.shared || external.perPrincipalIsolation)
|
|
75
|
+
? external
|
|
76
|
+
: this.memory;
|
|
77
|
+
// Both stores share one memory budget; compare insertion recency via Map ordering inside each partition.
|
|
78
|
+
store.delete(entry.key);
|
|
79
|
+
store.set(entry.key, entry);
|
|
80
|
+
let bytes = 0, count = 0;
|
|
81
|
+
for (const s of this.stores)
|
|
82
|
+
for (const [, e] of s) {
|
|
83
|
+
bytes += e.size;
|
|
84
|
+
count++;
|
|
85
|
+
}
|
|
86
|
+
while (count > (this.options.maxEntries ?? 500) ||
|
|
87
|
+
bytes > (this.options.maxBytes ?? 32 * 1024 * 1024)) {
|
|
88
|
+
const candidate = this.stores.find((s) => s.size > 0);
|
|
89
|
+
const [key, old] = candidate[Symbol.iterator]().next().value;
|
|
90
|
+
candidate.delete(key);
|
|
91
|
+
bytes -= old.size;
|
|
92
|
+
count--;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async run(ctx, method, params, opts, request) {
|
|
96
|
+
const { serverRef, authContextId } = ctx;
|
|
97
|
+
const inspect = (out) => {
|
|
98
|
+
if ((out.kind === "transport-failure" &&
|
|
99
|
+
[401, 403].includes(out.failure.status ?? 0)) ||
|
|
100
|
+
(out.kind === "transport-failure" &&
|
|
101
|
+
out.failure.kind === "forbidden") ||
|
|
102
|
+
out.kind === "auth-failure")
|
|
103
|
+
this.purge((e) => e.serverRef === serverRef && e.authContextId === authContextId);
|
|
104
|
+
if (out.kind === "unsupported-version")
|
|
105
|
+
this.invalidate(serverRef, "server/discover");
|
|
106
|
+
if (out.kind === "jsonrpc-error") {
|
|
107
|
+
if (out.code === -32021 || out.code === -32022)
|
|
108
|
+
this.invalidate(serverRef, "server/discover");
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
if (opts.signal?.aborted ||
|
|
112
|
+
!methods.has(method) ||
|
|
113
|
+
"requestState" in params ||
|
|
114
|
+
"inputResponses" in params) {
|
|
115
|
+
const out = await request(method, params, opts);
|
|
116
|
+
inspect(out);
|
|
117
|
+
return mark(out, "bypass");
|
|
118
|
+
}
|
|
119
|
+
const base = [serverRef, method, canonical(params)], key = JSON.stringify([...base, authContextId]), pub = JSON.stringify([...base, null]);
|
|
120
|
+
let entry;
|
|
121
|
+
for (const s of this.stores) {
|
|
122
|
+
entry = s.get(key) ?? (this.options.sharePublic ? s.get(pub) : undefined);
|
|
123
|
+
if (entry && entry.expires > this.now()) {
|
|
124
|
+
s.delete(entry.key);
|
|
125
|
+
s.set(entry.key, entry);
|
|
126
|
+
return mark(structuredClone(entry.outcome), "hit");
|
|
127
|
+
}
|
|
128
|
+
if (entry)
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
const active = this.flights.get(key) ?? new Set();
|
|
132
|
+
let flight;
|
|
133
|
+
for (const f of active) {
|
|
134
|
+
if (f.valid && !f.aborted) {
|
|
135
|
+
flight = f;
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const isCreator = flight === undefined;
|
|
140
|
+
if (!flight) {
|
|
141
|
+
const controller = new AbortController();
|
|
142
|
+
const newFlight = {
|
|
143
|
+
...ctx,
|
|
144
|
+
method,
|
|
145
|
+
uri: params.uri,
|
|
146
|
+
valid: true,
|
|
147
|
+
waiters: 0,
|
|
148
|
+
aborted: false,
|
|
149
|
+
controller,
|
|
150
|
+
result: null,
|
|
151
|
+
};
|
|
152
|
+
const flightOpts = { ...opts, signal: controller.signal };
|
|
153
|
+
newFlight.result = Promise.resolve()
|
|
154
|
+
.then(async () => {
|
|
155
|
+
if (newFlight.aborted) {
|
|
156
|
+
return {
|
|
157
|
+
kind: "cancelled",
|
|
158
|
+
wasSent: false,
|
|
159
|
+
phase: "queue",
|
|
160
|
+
meta: { requestId: 0, method, binding: "local", attempts: 0 },
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
const out = await request(method, params, flightOpts);
|
|
164
|
+
inspect(out);
|
|
165
|
+
if (out.kind === "jsonrpc-error" &&
|
|
166
|
+
out.code === -32602 &&
|
|
167
|
+
lists(method) &&
|
|
168
|
+
"cursor" in params) {
|
|
169
|
+
this.invalidate(serverRef, method);
|
|
170
|
+
const { cursor, ...first } = params;
|
|
171
|
+
return this.run(ctx, method, first, flightOpts, request);
|
|
172
|
+
}
|
|
173
|
+
if (newFlight.valid &&
|
|
174
|
+
entry &&
|
|
175
|
+
this.options.staleIfError &&
|
|
176
|
+
out.kind === "transport-failure" &&
|
|
177
|
+
(!out.failure.status || out.failure.status >= 500)) {
|
|
178
|
+
const result = {
|
|
179
|
+
...structuredClone(entry.outcome),
|
|
180
|
+
stale: true,
|
|
181
|
+
staleReason: out.failure.kind,
|
|
182
|
+
};
|
|
183
|
+
return mark(result, "stale");
|
|
184
|
+
}
|
|
185
|
+
if (!newFlight.valid ||
|
|
186
|
+
out.kind !== "ok" ||
|
|
187
|
+
!object(out.value) ||
|
|
188
|
+
out.value.resultType !== "complete" ||
|
|
189
|
+
out.meta.trace?.length)
|
|
190
|
+
return mark(out, "miss");
|
|
191
|
+
const scope = out.value.cacheScope === "public" ? "public" : "private";
|
|
192
|
+
let mixed = false;
|
|
193
|
+
if (lists(method)) {
|
|
194
|
+
for (const s of this.stores)
|
|
195
|
+
for (const [, e] of s)
|
|
196
|
+
if (e.serverRef === serverRef &&
|
|
197
|
+
e.method === method &&
|
|
198
|
+
(e.authContextId === authContextId || e.shared) &&
|
|
199
|
+
(e.scope !== scope || e.mixed))
|
|
200
|
+
mixed = true;
|
|
201
|
+
if (mixed) {
|
|
202
|
+
this.options.diagnostic?.("mixed-cache-scope", { method });
|
|
203
|
+
for (const s of this.stores)
|
|
204
|
+
for (const [k, e] of s)
|
|
205
|
+
if (e.serverRef === serverRef &&
|
|
206
|
+
e.method === method &&
|
|
207
|
+
(e.authContextId === authContextId || e.shared)) {
|
|
208
|
+
if (e.shared)
|
|
209
|
+
s.delete(k);
|
|
210
|
+
else
|
|
211
|
+
e.mixed = true;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const shared = scope === "public" && this.options.sharePublic === true && !mixed;
|
|
216
|
+
// A replacement that is stale or too large must not resurrect the previous value.
|
|
217
|
+
for (const s of this.stores) {
|
|
218
|
+
s.delete(key);
|
|
219
|
+
s.delete(pub);
|
|
220
|
+
}
|
|
221
|
+
const ttl = typeof out.value.ttlMs === "number" &&
|
|
222
|
+
Number.isFinite(out.value.ttlMs)
|
|
223
|
+
? Math.min(this.options.maxTtlMs ?? 86_400_000, Math.floor(out.value.ttlMs))
|
|
224
|
+
: 0;
|
|
225
|
+
if (ttl > 0) {
|
|
226
|
+
const size = new TextEncoder().encode(JSON.stringify([base, out])).length;
|
|
227
|
+
if (size <= (this.options.maxEntryBytes ?? 4 * 1024 * 1024))
|
|
228
|
+
this.insert({
|
|
229
|
+
...ctx,
|
|
230
|
+
method,
|
|
231
|
+
uri: params.uri,
|
|
232
|
+
key: shared ? pub : key,
|
|
233
|
+
shared,
|
|
234
|
+
scope,
|
|
235
|
+
mixed,
|
|
236
|
+
expires: this.now() + ttl,
|
|
237
|
+
size,
|
|
238
|
+
outcome: structuredClone(out),
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
return mark(out, "miss");
|
|
242
|
+
})
|
|
243
|
+
.finally(() => {
|
|
244
|
+
active.delete(newFlight);
|
|
245
|
+
if (active.size === 0 && this.flights.get(key) === active) {
|
|
246
|
+
this.flights.delete(key);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
// Prevent unhandled rejection when all attached callers leave early via withAbort.
|
|
250
|
+
newFlight.result.catch(() => { });
|
|
251
|
+
active.add(newFlight);
|
|
252
|
+
this.flights.set(key, active);
|
|
253
|
+
flight = newFlight;
|
|
254
|
+
}
|
|
255
|
+
flight.waiters += 1;
|
|
256
|
+
let detached = false;
|
|
257
|
+
const detach = (aborted) => {
|
|
258
|
+
if (detached)
|
|
259
|
+
return;
|
|
260
|
+
detached = true;
|
|
261
|
+
flight.waiters -= 1;
|
|
262
|
+
if (aborted && flight.waiters === 0) {
|
|
263
|
+
flight.aborted = true;
|
|
264
|
+
active.delete(flight);
|
|
265
|
+
if (active.size === 0 && this.flights.get(key) === active) {
|
|
266
|
+
this.flights.delete(key);
|
|
267
|
+
}
|
|
268
|
+
flight.controller.abort(opts.signal?.reason);
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
try {
|
|
272
|
+
const out = opts.signal
|
|
273
|
+
? await withAbort(flight.result, opts.signal, () => {
|
|
274
|
+
detach(true);
|
|
275
|
+
return WAITER_ABORTED;
|
|
276
|
+
})
|
|
277
|
+
: await flight.result;
|
|
278
|
+
detach(false);
|
|
279
|
+
return !isCreator && out.kind === "ok" ? structuredClone(out) : out;
|
|
280
|
+
}
|
|
281
|
+
catch (err) {
|
|
282
|
+
if (err === WAITER_ABORTED) {
|
|
283
|
+
detach(true);
|
|
284
|
+
return {
|
|
285
|
+
kind: "cancelled",
|
|
286
|
+
wasSent: false,
|
|
287
|
+
phase: "queue",
|
|
288
|
+
meta: { requestId: 0, method, binding: "local", attempts: 0 },
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
detach(false);
|
|
292
|
+
throw err;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { AuthProvider, Extension, RequestOptions, TelemetrySpan } from "../core/index.js";
|
|
2
|
+
import { MrtrSession, type MrtrOptions } from "./mrtr.js";
|
|
3
|
+
import { ResultCache, type CacheOptions } from "./cache.js";
|
|
4
|
+
import { type ElicitationOptions } from "./elicitation.js";
|
|
5
|
+
import { SubscriptionManager, type ListenOptions, type ListenFilter } from "./subscriptions.js";
|
|
6
|
+
import type { RequestFn, Obj } from "./common.js";
|
|
7
|
+
/** Register on the wire client instead of its generic consent port when using separate form/url ports. */
|
|
8
|
+
export declare function elicitationExtension(options: ElicitationOptions): Extension;
|
|
9
|
+
export interface StatefulClientOptions extends MrtrOptions {
|
|
10
|
+
request: RequestFn;
|
|
11
|
+
serverRef: string;
|
|
12
|
+
auth?: Pick<AuthProvider, "contextId">;
|
|
13
|
+
cache?: CacheOptions;
|
|
14
|
+
subscriptions?: Omit<ListenOptions, "serverRef" | "cache">;
|
|
15
|
+
activeSpan?: () => TelemetrySpan | undefined;
|
|
16
|
+
}
|
|
17
|
+
export declare class StatefulClient {
|
|
18
|
+
readonly cache: ResultCache;
|
|
19
|
+
readonly subscriptions?: SubscriptionManager;
|
|
20
|
+
readonly mrtr: MrtrSession;
|
|
21
|
+
constructor(options: StatefulClientOptions);
|
|
22
|
+
request(method: string, params?: Obj, opts?: RequestOptions): Promise<import("../core/index.js").Outcome<unknown>>;
|
|
23
|
+
listen(filter: ListenFilter): import("./subscriptions.js").Subscription;
|
|
24
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { MrtrSession } from "./mrtr.js";
|
|
2
|
+
import { ResultCache } from "./cache.js";
|
|
3
|
+
import { elicitationCapabilities, } from "./elicitation.js";
|
|
4
|
+
import { SubscriptionManager, } from "./subscriptions.js";
|
|
5
|
+
/** Register on the wire client instead of its generic consent port when using separate form/url ports. */
|
|
6
|
+
export function elicitationExtension(options) {
|
|
7
|
+
return {
|
|
8
|
+
id: "elicitation",
|
|
9
|
+
capabilities: (working) => working
|
|
10
|
+
? elicitationCapabilities(options)
|
|
11
|
+
: {},
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export class StatefulClient {
|
|
15
|
+
cache;
|
|
16
|
+
subscriptions;
|
|
17
|
+
mrtr;
|
|
18
|
+
constructor(options) {
|
|
19
|
+
this.cache = new ResultCache(options.cache);
|
|
20
|
+
if (options.subscriptions)
|
|
21
|
+
this.subscriptions = new SubscriptionManager({
|
|
22
|
+
...options.subscriptions,
|
|
23
|
+
serverRef: options.serverRef,
|
|
24
|
+
cache: this.cache,
|
|
25
|
+
});
|
|
26
|
+
this.mrtr = new MrtrSession(async (method, params, opts) => {
|
|
27
|
+
const outcome = await this.cache.run({
|
|
28
|
+
serverRef: options.serverRef,
|
|
29
|
+
authContextId: options.auth?.contextId() ?? "anonymous",
|
|
30
|
+
}, method, params, opts, options.request);
|
|
31
|
+
if (outcome.meta.cache)
|
|
32
|
+
options.activeSpan?.()?.setAttribute("cache", outcome.meta.cache);
|
|
33
|
+
return outcome;
|
|
34
|
+
}, options);
|
|
35
|
+
}
|
|
36
|
+
request(method, params = {}, opts = {}) {
|
|
37
|
+
return this.mrtr.run(method, params, opts);
|
|
38
|
+
}
|
|
39
|
+
listen(filter) {
|
|
40
|
+
if (!this.subscriptions)
|
|
41
|
+
throw Error("no listen stream port configured");
|
|
42
|
+
return this.subscriptions.listen(filter);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Outcome, RequestOptions } from "../core/index.js";
|
|
2
|
+
export { withAbort } from "../core/index.js";
|
|
3
|
+
export type Obj = Record<string, unknown>;
|
|
4
|
+
export declare const object: (v: unknown) => v is Obj;
|
|
5
|
+
export type RequestFn = (method: string, params: Obj, options: RequestOptions) => PromiseLike<Outcome<unknown>>;
|
|
6
|
+
export type Diagnostic = (reason: string, detail?: Obj) => void;
|
|
7
|
+
export declare class Violation extends Error {
|
|
8
|
+
readonly context: Obj;
|
|
9
|
+
constructor(reason: string, context?: Obj);
|
|
10
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { withAbort } from "../core/index.js";
|
|
2
|
+
export const object = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
3
|
+
export class Violation extends Error {
|
|
4
|
+
context;
|
|
5
|
+
constructor(reason, context = {}) {
|
|
6
|
+
super(reason);
|
|
7
|
+
this.context = context;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { BrowserPort } from "../core/index.js";
|
|
2
|
+
import { type Diagnostic, type Obj } from "./common.js";
|
|
3
|
+
export type Decision = "accept" | "cancel" | "decline";
|
|
4
|
+
export type FormResponse = {
|
|
5
|
+
action: "accept";
|
|
6
|
+
content: Obj;
|
|
7
|
+
} | {
|
|
8
|
+
action: "cancel" | "decline";
|
|
9
|
+
};
|
|
10
|
+
export interface FormRequest {
|
|
11
|
+
serverLabel: string;
|
|
12
|
+
message: string;
|
|
13
|
+
schema: Obj;
|
|
14
|
+
defaults: Obj;
|
|
15
|
+
suspectSensitiveFields: string[];
|
|
16
|
+
invalidPrevious: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface UrlConsentRequest {
|
|
19
|
+
serverLabel: string;
|
|
20
|
+
message: string;
|
|
21
|
+
url: string;
|
|
22
|
+
hostHighlight: string;
|
|
23
|
+
punycodeWarning: boolean;
|
|
24
|
+
}
|
|
25
|
+
export interface ElicitationPort {
|
|
26
|
+
form?(request: FormRequest, signal: AbortSignal): Promise<FormResponse>;
|
|
27
|
+
url?(request: UrlConsentRequest, signal: AbortSignal): Promise<Decision>;
|
|
28
|
+
}
|
|
29
|
+
export interface ElicitationOptions {
|
|
30
|
+
serverLabel: string;
|
|
31
|
+
port?: ElicitationPort;
|
|
32
|
+
browser?: BrowserPort;
|
|
33
|
+
allowInsecureDev?: boolean;
|
|
34
|
+
diagnostic?: Diagnostic;
|
|
35
|
+
}
|
|
36
|
+
export declare function elicitationCapabilities({ port, browser, }: ElicitationOptions): Obj;
|
|
37
|
+
export declare function assertSupported(method: unknown, params: unknown, options: ElicitationOptions, key: string): asserts params is Obj;
|
|
38
|
+
export declare function elicit(params: Obj, options: ElicitationOptions, signal: AbortSignal): Promise<{
|
|
39
|
+
action: Decision;
|
|
40
|
+
content?: Obj;
|
|
41
|
+
}>;
|
|
42
|
+
export declare function removedElicitationNotification(method: string, diagnostic?: Diagnostic): boolean;
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { object, Violation, withAbort, } from "./common.js";
|
|
2
|
+
export function elicitationCapabilities({ port, browser, }) {
|
|
3
|
+
const modes = {
|
|
4
|
+
...(port?.form && { form: {} }),
|
|
5
|
+
...(port?.url && browser && { url: {} }),
|
|
6
|
+
};
|
|
7
|
+
return Object.keys(modes).length ? { elicitation: modes } : {};
|
|
8
|
+
}
|
|
9
|
+
const bad = () => {
|
|
10
|
+
throw new Violation("unsupported-elicitation-schema");
|
|
11
|
+
};
|
|
12
|
+
const strings = (v) => Array.isArray(v) && v.every((x) => typeof x === "string");
|
|
13
|
+
function keys(o, allowed) {
|
|
14
|
+
if (Object.keys(o).some((k) => !allowed.includes(k)))
|
|
15
|
+
bad();
|
|
16
|
+
}
|
|
17
|
+
function choices(o, keyword) {
|
|
18
|
+
if (o.enum !== undefined) {
|
|
19
|
+
if (!strings(o.enum) || !o.enum.length || o[keyword] !== undefined)
|
|
20
|
+
return bad();
|
|
21
|
+
return o.enum;
|
|
22
|
+
}
|
|
23
|
+
const a = o[keyword];
|
|
24
|
+
if (a === undefined)
|
|
25
|
+
return undefined;
|
|
26
|
+
if (!Array.isArray(a) || !a.length)
|
|
27
|
+
return bad();
|
|
28
|
+
return a.map((item) => {
|
|
29
|
+
if (!object(item) ||
|
|
30
|
+
typeof item.const !== "string" ||
|
|
31
|
+
typeof item.title !== "string")
|
|
32
|
+
return bad();
|
|
33
|
+
keys(item, ["const", "title"]);
|
|
34
|
+
return item.const;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function formatOk(s, f) {
|
|
38
|
+
if (f === "email")
|
|
39
|
+
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/u.test(s);
|
|
40
|
+
if (f === "date")
|
|
41
|
+
return (/^\d{4}-\d\d-\d\d$/.test(s) &&
|
|
42
|
+
!Number.isNaN(Date.parse(s)) &&
|
|
43
|
+
new Date(s).toISOString().startsWith(s));
|
|
44
|
+
if (f === "date-time")
|
|
45
|
+
return (/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.\d+)?(?:Z|[+-]\d\d:\d\d)$/i.test(s) && !Number.isNaN(Date.parse(s)));
|
|
46
|
+
if (f === "uri")
|
|
47
|
+
return URL.canParse(s);
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
function validator(p) {
|
|
51
|
+
const base = ["type", "default", "description", "title"];
|
|
52
|
+
const limits = (lo, hi, count) => {
|
|
53
|
+
for (const k of [lo, hi])
|
|
54
|
+
if (k in p &&
|
|
55
|
+
(typeof p[k] !== "number" ||
|
|
56
|
+
!Number.isFinite(p[k]) ||
|
|
57
|
+
(count && (!Number.isInteger(p[k]) || Number(p[k]) < 0))))
|
|
58
|
+
bad();
|
|
59
|
+
return (n) => n >= Number(p[lo] ?? -Infinity) && n <= Number(p[hi] ?? Infinity);
|
|
60
|
+
};
|
|
61
|
+
switch (p.type) {
|
|
62
|
+
case "string": {
|
|
63
|
+
keys(p, [
|
|
64
|
+
...base,
|
|
65
|
+
"format",
|
|
66
|
+
"pattern",
|
|
67
|
+
"minLength",
|
|
68
|
+
"maxLength",
|
|
69
|
+
"enum",
|
|
70
|
+
"enumNames",
|
|
71
|
+
"oneOf",
|
|
72
|
+
]);
|
|
73
|
+
const e = choices(p, "oneOf"), fits = limits("minLength", "maxLength", true);
|
|
74
|
+
if (p.format !== undefined &&
|
|
75
|
+
!["date", "date-time", "email", "uri"].includes(String(p.format)))
|
|
76
|
+
bad();
|
|
77
|
+
let re;
|
|
78
|
+
if (p.pattern !== undefined) {
|
|
79
|
+
if (typeof p.pattern !== "string")
|
|
80
|
+
return bad();
|
|
81
|
+
try {
|
|
82
|
+
re = new RegExp(p.pattern, "u");
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
return bad();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return (v) => typeof v === "string" &&
|
|
89
|
+
fits([...v].length) &&
|
|
90
|
+
formatOk(v, p.format) &&
|
|
91
|
+
(!re || re.test(v)) &&
|
|
92
|
+
(!e || e.includes(v));
|
|
93
|
+
}
|
|
94
|
+
case "boolean":
|
|
95
|
+
keys(p, base);
|
|
96
|
+
return (v) => typeof v === "boolean";
|
|
97
|
+
case "number":
|
|
98
|
+
case "integer": {
|
|
99
|
+
keys(p, [...base, "minimum", "maximum"]);
|
|
100
|
+
const fits = limits("minimum", "maximum", false);
|
|
101
|
+
return (v) => typeof v === "number" &&
|
|
102
|
+
Number.isFinite(v) &&
|
|
103
|
+
fits(v) &&
|
|
104
|
+
(p.type !== "integer" || Number.isInteger(v));
|
|
105
|
+
}
|
|
106
|
+
case "array": {
|
|
107
|
+
keys(p, [...base, "minItems", "maxItems", "items"]);
|
|
108
|
+
if (!object(p.items))
|
|
109
|
+
return bad();
|
|
110
|
+
keys(p.items, ["type", "enum", "anyOf"]);
|
|
111
|
+
if (p.items.type !== undefined && p.items.type !== "string")
|
|
112
|
+
return bad();
|
|
113
|
+
const e = choices(p.items, "anyOf") ?? bad(), fits = limits("minItems", "maxItems", true);
|
|
114
|
+
return (v) => strings(v) && fits(v.length) && v.every((x) => e.includes(x));
|
|
115
|
+
}
|
|
116
|
+
default:
|
|
117
|
+
return bad();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
export function assertSupported(method, params, options, key) {
|
|
121
|
+
const mode = object(params) ? (params.mode ?? "form") : null;
|
|
122
|
+
if (method !== "elicitation/create" ||
|
|
123
|
+
!((mode === "form" && options.port?.form) ||
|
|
124
|
+
(mode === "url" && options.port?.url && options.browser)))
|
|
125
|
+
throw new Violation("unsupported-input-request", {
|
|
126
|
+
key,
|
|
127
|
+
method,
|
|
128
|
+
serverLabel: options.serverLabel,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
export async function elicit(params, options, signal) {
|
|
132
|
+
assertSupported("elicitation/create", params, options, "");
|
|
133
|
+
signal.throwIfAborted();
|
|
134
|
+
const { serverLabel, port, browser } = options, message = params.message;
|
|
135
|
+
if (typeof message !== "string")
|
|
136
|
+
throw new Violation("invalid-elicitation-message");
|
|
137
|
+
if (params.mode === "url") {
|
|
138
|
+
const url = params.url;
|
|
139
|
+
if (typeof url !== "string" ||
|
|
140
|
+
!URL.canParse(url) ||
|
|
141
|
+
/[\x00-\x20\x7f\\]/.test(url))
|
|
142
|
+
throw new Violation("unsafe-elicitation-url");
|
|
143
|
+
const u = new URL(url), loopback = u.hostname === "localhost" ||
|
|
144
|
+
u.hostname === "[::1]" ||
|
|
145
|
+
/^127\.\d+\.\d+\.\d+$/.test(u.hostname);
|
|
146
|
+
if (u.username ||
|
|
147
|
+
u.password ||
|
|
148
|
+
!(u.protocol === "https:" ||
|
|
149
|
+
(u.protocol === "http:" && options.allowInsecureDev && loopback)))
|
|
150
|
+
throw new Violation("unsafe-elicitation-url");
|
|
151
|
+
const action = await withAbort(port.url({
|
|
152
|
+
serverLabel,
|
|
153
|
+
message,
|
|
154
|
+
url,
|
|
155
|
+
hostHighlight: u.host,
|
|
156
|
+
punycodeWarning: u.hostname
|
|
157
|
+
.split(".")
|
|
158
|
+
.some((s) => s.startsWith("xn--")),
|
|
159
|
+
}, signal), signal);
|
|
160
|
+
signal.throwIfAborted();
|
|
161
|
+
if (action === "accept")
|
|
162
|
+
await withAbort(browser.open(url, signal), signal);
|
|
163
|
+
else if (action !== "decline" && action !== "cancel")
|
|
164
|
+
throw Error("invalid consent decision");
|
|
165
|
+
return { action };
|
|
166
|
+
}
|
|
167
|
+
const schema = params.requestedSchema;
|
|
168
|
+
if (!object(schema) || schema.type !== "object" || !object(schema.properties))
|
|
169
|
+
return bad();
|
|
170
|
+
keys(schema, [
|
|
171
|
+
"$schema",
|
|
172
|
+
"type",
|
|
173
|
+
"properties",
|
|
174
|
+
"required",
|
|
175
|
+
"title",
|
|
176
|
+
"description",
|
|
177
|
+
]);
|
|
178
|
+
const fields = new Map();
|
|
179
|
+
const defaults = Object.create(null);
|
|
180
|
+
for (const [k, p] of Object.entries(schema.properties)) {
|
|
181
|
+
if (!object(p))
|
|
182
|
+
return bad();
|
|
183
|
+
const check = validator(p);
|
|
184
|
+
fields.set(k, check);
|
|
185
|
+
if ("default" in p) {
|
|
186
|
+
if (!check(p.default))
|
|
187
|
+
return bad();
|
|
188
|
+
defaults[k] = p.default;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const required = schema.required ?? [];
|
|
192
|
+
if (!strings(required) || required.some((k) => !fields.has(k)))
|
|
193
|
+
return bad();
|
|
194
|
+
const suspectSensitiveFields = [...fields.keys()].filter((k) => /password|token|secret|api[_-]?key|card/i.test(k));
|
|
195
|
+
for (const invalidPrevious of [false, true]) {
|
|
196
|
+
signal.throwIfAborted();
|
|
197
|
+
const r = await withAbort(port.form({
|
|
198
|
+
serverLabel,
|
|
199
|
+
message,
|
|
200
|
+
schema,
|
|
201
|
+
defaults,
|
|
202
|
+
suspectSensitiveFields,
|
|
203
|
+
invalidPrevious,
|
|
204
|
+
}, signal), signal);
|
|
205
|
+
if (r.action === "decline" || r.action === "cancel")
|
|
206
|
+
return { action: r.action };
|
|
207
|
+
if (r.action === "accept" &&
|
|
208
|
+
object(r.content) &&
|
|
209
|
+
required.every((k) => Object.hasOwn(r.content, k)) &&
|
|
210
|
+
Object.entries(r.content).every(([k, v]) => fields.get(k)?.(v)))
|
|
211
|
+
return { action: "accept", content: structuredClone(r.content) };
|
|
212
|
+
}
|
|
213
|
+
options.diagnostic?.("invalid-elicitation-response");
|
|
214
|
+
return { action: "cancel" };
|
|
215
|
+
}
|
|
216
|
+
export function removedElicitationNotification(method, diagnostic) {
|
|
217
|
+
if (method !== "notifications/elicitation/complete")
|
|
218
|
+
return false;
|
|
219
|
+
diagnostic?.("removed-elicitation-notification");
|
|
220
|
+
return true;
|
|
221
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./mrtr.js";
|
|
2
|
+
export * from "./elicitation.js";
|
|
3
|
+
export * from "./subscriptions.js";
|
|
4
|
+
export * from "./cache.js";
|
|
5
|
+
export * from "./client.js";
|
|
6
|
+
export * from "./stream.js";
|
|
7
|
+
export * from "./refresh.js";
|
|
8
|
+
export * from "./tasks.js";
|
|
9
|
+
export type { RequestFn, Diagnostic } from "./common.js";
|