@dudko.dev/agent-web-react 0.0.28 → 0.0.30
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 +368 -24
- package/dist/{chunk-UUB7CE6N.js → chunk-EMF7DTGS.js} +69 -1
- package/dist/chunk-EMF7DTGS.js.map +1 -0
- package/dist/chunk-FXHQXIOU.js +94 -0
- package/dist/chunk-FXHQXIOU.js.map +1 -0
- package/dist/chunk-VMCWL6VH.js +294 -0
- package/dist/chunk-VMCWL6VH.js.map +1 -0
- package/dist/elicitation-CcZe5-EK.d.cts +1535 -0
- package/dist/elicitation-CcZe5-EK.d.ts +1535 -0
- package/dist/index.cjs +4360 -618
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +705 -433
- package/dist/index.d.ts +705 -433
- package/dist/index.js +4029 -628
- package/dist/index.js.map +1 -1
- package/dist/mcp-app-policy-DIUmGPJP.d.cts +55 -0
- package/dist/mcp-app-policy-DIUmGPJP.d.ts +55 -0
- package/dist/mcp-app-sandbox.cjs +313 -0
- package/dist/mcp-app-sandbox.cjs.map +1 -0
- package/dist/mcp-app-sandbox.d.cts +65 -0
- package/dist/mcp-app-sandbox.d.ts +65 -0
- package/dist/mcp-app-sandbox.js +201 -0
- package/dist/mcp-app-sandbox.js.map +1 -0
- package/dist/mcp.cjs +316 -42
- package/dist/mcp.cjs.map +1 -1
- package/dist/mcp.d.cts +195 -100
- package/dist/mcp.d.ts +195 -100
- package/dist/mcp.js +234 -46
- package/dist/mcp.js.map +1 -1
- package/dist/pipeline.cjs.map +1 -1
- package/dist/pipeline.js +1 -1
- package/dist/styles.css +1143 -1
- package/package.json +13 -3
- package/dist/chunk-UUB7CE6N.js.map +0 -1
package/dist/mcp.cjs
CHANGED
|
@@ -31,10 +31,21 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var mcp_exports = {};
|
|
32
32
|
__export(mcp_exports, {
|
|
33
33
|
claimOAuthCallback: () => claimOAuthCallback,
|
|
34
|
+
createElicitationQueue: () => createElicitationQueue,
|
|
34
35
|
describeMcpResult: () => describeMcpResult,
|
|
36
|
+
elicitationConnectOptions: () => elicitationConnectOptions,
|
|
37
|
+
elicitationSettings: () => elicitationSettings,
|
|
38
|
+
elicitationTimeoutOf: () => elicitationTimeoutOf,
|
|
35
39
|
isUnauthorizedError: () => isUnauthorizedError,
|
|
40
|
+
mcpAppsRouter: () => mcpAppsRouter,
|
|
36
41
|
mcpInstructionsOf: () => mcpInstructionsOf,
|
|
42
|
+
mcpOAuthOptionsOfRecord: () => mcpOAuthOptionsOfRecord,
|
|
37
43
|
mcpOAuthProviderOptions: () => mcpOAuthProviderOptions,
|
|
44
|
+
mcpOAuthRecordOf: () => mcpOAuthRecordOf,
|
|
45
|
+
mcpOAuthRegistrationOf: () => mcpOAuthRegistrationOf,
|
|
46
|
+
mcpProtocolOf: () => mcpProtocolOf,
|
|
47
|
+
mcpSkillsSettingOf: () => mcpSkillsSettingOf,
|
|
48
|
+
newMcpOAuthProvider: () => newMcpOAuthProvider,
|
|
38
49
|
readCallbackParams: () => readCallbackParams,
|
|
39
50
|
stripOAuthParams: () => stripOAuthParams,
|
|
40
51
|
useMcp: () => useMcp,
|
|
@@ -44,11 +55,162 @@ module.exports = __toCommonJS(mcp_exports);
|
|
|
44
55
|
|
|
45
56
|
// src/hooks/use-mcp.ts
|
|
46
57
|
var import_react = require("react");
|
|
58
|
+
|
|
59
|
+
// src/elicitation.ts
|
|
60
|
+
var ELICITATION_TIMEOUT_MS = 3e5;
|
|
61
|
+
var elicitationSettings = (option) => {
|
|
62
|
+
if (option === true) return {};
|
|
63
|
+
if (typeof option === "object" && option !== null) return { ...option };
|
|
64
|
+
return void 0;
|
|
65
|
+
};
|
|
66
|
+
var elicitationConnectOptions = (option, handler) => {
|
|
67
|
+
const settings = elicitationSettings(option);
|
|
68
|
+
return settings ? { onElicitation: handler, elicitation: settings } : {};
|
|
69
|
+
};
|
|
70
|
+
var TIMER_MAX_MS = 2147483647;
|
|
71
|
+
var elicitationTimeoutOf = (settings) => {
|
|
72
|
+
const raw = settings.timeoutMs;
|
|
73
|
+
if (raw === void 0 || raw === null) return ELICITATION_TIMEOUT_MS;
|
|
74
|
+
if (typeof raw !== "number" || !(raw > 0) || raw > TIMER_MAX_MS) return void 0;
|
|
75
|
+
return Math.max(1, Math.floor(raw));
|
|
76
|
+
};
|
|
77
|
+
var createElicitationQueue = (options = {}) => {
|
|
78
|
+
const now = options.now ?? Date.now;
|
|
79
|
+
const waiting = /* @__PURE__ */ new Map();
|
|
80
|
+
let disposed = false;
|
|
81
|
+
const list = () => [...waiting.values()].map((w) => w.request);
|
|
82
|
+
const changed = () => {
|
|
83
|
+
if (!disposed) options.onChange?.(list());
|
|
84
|
+
};
|
|
85
|
+
const handler = (request, ctx) => new Promise((resolve) => {
|
|
86
|
+
const signal = ctx?.signal;
|
|
87
|
+
if (disposed || signal?.aborted || waiting.has(request.id)) {
|
|
88
|
+
resolve({ action: "cancel" });
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const onAbort = () => {
|
|
92
|
+
if (waiting.get(request.id)?.settle !== settle) return;
|
|
93
|
+
settle({ action: "cancel" });
|
|
94
|
+
changed();
|
|
95
|
+
};
|
|
96
|
+
const settle = (result) => {
|
|
97
|
+
if (waiting.get(request.id)?.settle !== settle) return;
|
|
98
|
+
waiting.delete(request.id);
|
|
99
|
+
signal?.removeEventListener("abort", onAbort);
|
|
100
|
+
resolve(result);
|
|
101
|
+
};
|
|
102
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
103
|
+
const timeoutMs = typeof options.timeoutMs === "function" ? options.timeoutMs() : options.timeoutMs;
|
|
104
|
+
waiting.set(request.id, {
|
|
105
|
+
request: {
|
|
106
|
+
...request,
|
|
107
|
+
...timeoutMs !== void 0 ? { expiresAt: now() + timeoutMs } : {}
|
|
108
|
+
},
|
|
109
|
+
settle
|
|
110
|
+
});
|
|
111
|
+
changed();
|
|
112
|
+
});
|
|
113
|
+
return {
|
|
114
|
+
handler,
|
|
115
|
+
pending: list,
|
|
116
|
+
answer: (id, result) => {
|
|
117
|
+
const entry = waiting.get(id);
|
|
118
|
+
if (!entry) return false;
|
|
119
|
+
entry.settle(result);
|
|
120
|
+
changed();
|
|
121
|
+
return true;
|
|
122
|
+
},
|
|
123
|
+
cancel: (match) => {
|
|
124
|
+
let any = false;
|
|
125
|
+
for (const entry of [...waiting.values()]) {
|
|
126
|
+
if (match && !match(entry.request)) continue;
|
|
127
|
+
entry.settle({ action: "cancel" });
|
|
128
|
+
any = true;
|
|
129
|
+
}
|
|
130
|
+
if (any) changed();
|
|
131
|
+
},
|
|
132
|
+
dispose: () => {
|
|
133
|
+
disposed = true;
|
|
134
|
+
for (const entry of [...waiting.values()]) entry.settle({ action: "cancel" });
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// src/hooks/use-mcp.ts
|
|
140
|
+
var mcpSkillsSettingOf = (value) => {
|
|
141
|
+
if (value === true) return true;
|
|
142
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return void 0;
|
|
143
|
+
const consent = value.consent;
|
|
144
|
+
return consent === "ask" || consent === "once-per-skill" || consent === "never" ? { consent } : consent === void 0 ? true : void 0;
|
|
145
|
+
};
|
|
146
|
+
var mcpAppsRouter = (connectionOf) => {
|
|
147
|
+
const appsOf = (server) => connectionOf(server)?.apps;
|
|
148
|
+
const notConnected = (server) => new Error(`the server \u201C${server}\u201D is not connected`);
|
|
149
|
+
return {
|
|
150
|
+
readUiResource: (server, uri, options) => {
|
|
151
|
+
const apps = appsOf(server);
|
|
152
|
+
return apps ? apps.readUiResource(server, uri, options) : Promise.reject(notConnected(server));
|
|
153
|
+
},
|
|
154
|
+
appTool: (server, name) => appsOf(server)?.appTool(server, name),
|
|
155
|
+
readResource: (server, uri, options) => {
|
|
156
|
+
const apps = appsOf(server);
|
|
157
|
+
if (!apps) return Promise.reject(notConnected(server));
|
|
158
|
+
if (!apps.readResource) {
|
|
159
|
+
return Promise.reject(new Error("this version of the core cannot read a linked file"));
|
|
160
|
+
}
|
|
161
|
+
return apps.readResource(server, uri, options);
|
|
162
|
+
},
|
|
163
|
+
callAppTool: (server, name, args, options) => {
|
|
164
|
+
const apps = appsOf(server);
|
|
165
|
+
return apps ? apps.callAppTool(server, name, args, options) : Promise.reject(notConnected(server));
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
};
|
|
47
169
|
var mcpInstructionsOf = (connection, name) => {
|
|
48
170
|
const list = Array.isArray(connection.instructions) ? connection.instructions : [];
|
|
49
171
|
const text = list.find((i) => i?.server === name)?.instructions;
|
|
50
172
|
return typeof text === "string" && text.trim() ? text : void 0;
|
|
51
173
|
};
|
|
174
|
+
var mcpProtocolOf = (connection, name) => {
|
|
175
|
+
const list = Array.isArray(connection.protocols) ? connection.protocols : [];
|
|
176
|
+
const found = list.find((p) => p?.server === name);
|
|
177
|
+
return found && typeof found.version === "string" ? { server: name, version: found.version, era: found.era } : void 0;
|
|
178
|
+
};
|
|
179
|
+
var useElicitationQueue = (optionsRef) => {
|
|
180
|
+
const [pendingElicitations, setPending] = (0, import_react.useState)([]);
|
|
181
|
+
const mountedRef = (0, import_react.useRef)(true);
|
|
182
|
+
const queueRef = (0, import_react.useRef)(void 0);
|
|
183
|
+
queueRef.current ??= createElicitationQueue({
|
|
184
|
+
timeoutMs: () => {
|
|
185
|
+
const settings = elicitationSettings(optionsRef.current.elicitation);
|
|
186
|
+
return settings ? elicitationTimeoutOf(settings) : void 0;
|
|
187
|
+
},
|
|
188
|
+
onChange: (pending) => {
|
|
189
|
+
if (mountedRef.current) setPending(pending);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
const queue = queueRef.current;
|
|
193
|
+
const handler = (0, import_react.useCallback)(
|
|
194
|
+
(request, ctx) => mountedRef.current ? queue.handler(request, ctx) : { action: "cancel" },
|
|
195
|
+
[queue]
|
|
196
|
+
);
|
|
197
|
+
const answerElicitation = (0, import_react.useCallback)(
|
|
198
|
+
(id, result) => queue.answer(id, result),
|
|
199
|
+
[queue]
|
|
200
|
+
);
|
|
201
|
+
const cancel = (0, import_react.useCallback)(
|
|
202
|
+
(server) => queue.cancel(server === void 0 ? void 0 : (request) => request.server === server),
|
|
203
|
+
[queue]
|
|
204
|
+
);
|
|
205
|
+
(0, import_react.useEffect)(() => {
|
|
206
|
+
mountedRef.current = true;
|
|
207
|
+
return () => {
|
|
208
|
+
mountedRef.current = false;
|
|
209
|
+
queue.cancel();
|
|
210
|
+
};
|
|
211
|
+
}, [queue]);
|
|
212
|
+
return { pendingElicitations, answerElicitation, handler, cancel };
|
|
213
|
+
};
|
|
52
214
|
var OAUTH_UNSUPPORTED = "The installed @dudko.dev/agent-web has no MCP OAuth support. Upgrade the core to a version exporting BrowserOAuthProvider, or use a static Authorization header.";
|
|
53
215
|
var PENDING_KEY = "agent-web-react:mcp:pending";
|
|
54
216
|
var OAUTH_KEY = "agent-web-react:mcp:oauth";
|
|
@@ -142,12 +304,48 @@ var mcpOAuthProviderOptions = (serverUrl, oauth = {}, defaults = {}) => {
|
|
|
142
304
|
clientName: oauth.clientName ?? defaults.clientName,
|
|
143
305
|
scope: oauth.scope,
|
|
144
306
|
// Left out when unset: a provider built without it keeps registering dynamically.
|
|
145
|
-
...clientMetadataUrl !== void 0 ? { clientMetadataUrl } : {}
|
|
307
|
+
...clientMetadataUrl !== void 0 ? { clientMetadataUrl } : {},
|
|
308
|
+
// Left out when unset, too: the core's default is 'auto'.
|
|
309
|
+
...oauth.registration !== void 0 ? { registration: oauth.registration } : {}
|
|
146
310
|
};
|
|
147
311
|
};
|
|
312
|
+
var mcpOAuthRecordOf = (resolved) => ({
|
|
313
|
+
redirectUrl: resolved.redirectUrl,
|
|
314
|
+
scope: resolved.scope,
|
|
315
|
+
clientName: resolved.clientName,
|
|
316
|
+
clientMetadataUrl: resolved.clientMetadataUrl,
|
|
317
|
+
...resolved.registration !== void 0 ? { registration: resolved.registration } : {}
|
|
318
|
+
});
|
|
319
|
+
var mcpOAuthOptionsOfRecord = (record) => ({
|
|
320
|
+
redirectUrl: record.redirectUrl,
|
|
321
|
+
scope: record.scope,
|
|
322
|
+
clientName: record.clientName,
|
|
323
|
+
clientMetadataUrl: record.clientMetadataUrl,
|
|
324
|
+
...record.registration !== void 0 ? { registration: record.registration } : {}
|
|
325
|
+
});
|
|
326
|
+
var registrationUnsupported = (registration) => `The installed @dudko.dev/agent-web does not support oauth.registration ('${registration}'): it would register as it always did. Upgrade the core, or leave registration out.`;
|
|
327
|
+
var newMcpOAuthProvider = (mod, options) => {
|
|
328
|
+
if (!mod.BrowserOAuthProvider) throw new Error(OAUTH_UNSUPPORTED);
|
|
329
|
+
const provider = new mod.BrowserOAuthProvider(options);
|
|
330
|
+
const asked = options.registration;
|
|
331
|
+
if (asked !== void 0 && asked !== "auto" && provider.registration !== asked) {
|
|
332
|
+
throw new Error(registrationUnsupported(asked));
|
|
333
|
+
}
|
|
334
|
+
return provider;
|
|
335
|
+
};
|
|
336
|
+
var mcpOAuthRegistrationOf = async (provider) => {
|
|
337
|
+
if (typeof provider?.registrationKind !== "function") return void 0;
|
|
338
|
+
try {
|
|
339
|
+
const kind = await provider.registrationKind();
|
|
340
|
+
return kind === "cimd" || kind === "dcr" ? kind : void 0;
|
|
341
|
+
} catch {
|
|
342
|
+
return void 0;
|
|
343
|
+
}
|
|
344
|
+
};
|
|
148
345
|
var isUnauthorizedError = (err, mod) => {
|
|
149
346
|
if (!(err instanceof Error)) return false;
|
|
150
347
|
if (mod?.UnauthorizedError && err instanceof mod.UnauthorizedError) return true;
|
|
348
|
+
if (err.name === "UnauthorizedError") return true;
|
|
151
349
|
return err.constructor?.name === "UnauthorizedError";
|
|
152
350
|
};
|
|
153
351
|
var watchAuthorization = (tools, mod, onUnauthorized) => {
|
|
@@ -179,8 +377,12 @@ var useMcp = (options = {}) => {
|
|
|
179
377
|
const [tools, setTools] = (0, import_react.useState)(void 0);
|
|
180
378
|
const [catalog, setCatalog] = (0, import_react.useState)([]);
|
|
181
379
|
const [instructions, setInstructions] = (0, import_react.useState)(void 0);
|
|
380
|
+
const [protocol, setProtocol] = (0, import_react.useState)(void 0);
|
|
182
381
|
const [error, setError] = (0, import_react.useState)(void 0);
|
|
183
382
|
const [authorizationUrl, setAuthorizationUrl] = (0, import_react.useState)(void 0);
|
|
383
|
+
const [oauthRegistration, setOauthRegistration] = (0, import_react.useState)(
|
|
384
|
+
void 0
|
|
385
|
+
);
|
|
184
386
|
const [oauthSupported, setOauthSupported] = (0, import_react.useState)(void 0);
|
|
185
387
|
const [completingAuthorization, setCompletingAuthorization] = (0, import_react.useState)(
|
|
186
388
|
() => readCallbackParams() !== void 0
|
|
@@ -190,12 +392,15 @@ var useMcp = (options = {}) => {
|
|
|
190
392
|
const epochRef = (0, import_react.useRef)(0);
|
|
191
393
|
const optionsRef = (0, import_react.useRef)(options);
|
|
192
394
|
optionsRef.current = options;
|
|
395
|
+
const elicitation = useElicitationQueue(optionsRef);
|
|
396
|
+
const cancelElicitations = elicitation.cancel;
|
|
193
397
|
const closeConnection = (0, import_react.useCallback)(async () => {
|
|
194
398
|
const open = connectionRef.current;
|
|
195
399
|
connectionRef.current = void 0;
|
|
400
|
+
cancelElicitations();
|
|
196
401
|
if (open) await open.close().catch(() => {
|
|
197
402
|
});
|
|
198
|
-
}, []);
|
|
403
|
+
}, [cancelElicitations]);
|
|
199
404
|
const checkOAuthSupport = (0, import_react.useCallback)(async () => {
|
|
200
405
|
try {
|
|
201
406
|
const mod = await loadMcp();
|
|
@@ -216,9 +421,11 @@ var useMcp = (options = {}) => {
|
|
|
216
421
|
setStatus("connecting");
|
|
217
422
|
setError(void 0);
|
|
218
423
|
setAuthorizationUrl(void 0);
|
|
424
|
+
setOauthRegistration(void 0);
|
|
219
425
|
setTools(void 0);
|
|
220
426
|
setCatalog([]);
|
|
221
427
|
setInstructions(void 0);
|
|
428
|
+
setProtocol(void 0);
|
|
222
429
|
try {
|
|
223
430
|
const mod = await loadMcp();
|
|
224
431
|
setOauthSupported(typeof mod.BrowserOAuthProvider === "function");
|
|
@@ -234,13 +441,12 @@ var useMcp = (options = {}) => {
|
|
|
234
441
|
const record = {
|
|
235
442
|
url: opts.url,
|
|
236
443
|
name,
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
clientMetadataUrl: providerOptions.clientMetadataUrl,
|
|
444
|
+
...mcpOAuthRecordOf(providerOptions),
|
|
445
|
+
...opts.protocol ? { protocol: opts.protocol } : {},
|
|
446
|
+
...mcpSkillsSettingOf(opts.skills) ? { skills: mcpSkillsSettingOf(opts.skills) } : {},
|
|
241
447
|
startedAt: Date.now()
|
|
242
448
|
};
|
|
243
|
-
authProvider =
|
|
449
|
+
authProvider = newMcpOAuthProvider(mod, providerOptions);
|
|
244
450
|
providerRef.current = authProvider;
|
|
245
451
|
writeRecord(PENDING_KEY, record);
|
|
246
452
|
writeRecord(OAUTH_KEY, record);
|
|
@@ -252,10 +458,19 @@ var useMcp = (options = {}) => {
|
|
|
252
458
|
[name]: {
|
|
253
459
|
url: opts.url,
|
|
254
460
|
headers: opts.oauth ? void 0 : opts.headers,
|
|
255
|
-
authProvider
|
|
461
|
+
authProvider,
|
|
462
|
+
// Only when asked for: without it the connection is built as it always was.
|
|
463
|
+
...opts.protocol ? { protocol: opts.protocol } : {},
|
|
464
|
+
...mcpSkillsSettingOf(opts.skills) ? { skills: mcpSkillsSettingOf(opts.skills) } : {}
|
|
256
465
|
}
|
|
257
466
|
},
|
|
258
|
-
{
|
|
467
|
+
{
|
|
468
|
+
clientName: optionsRef.current.clientName,
|
|
469
|
+
onLog: optionsRef.current.onLog,
|
|
470
|
+
...optionsRef.current.apps ? { apps: true } : {},
|
|
471
|
+
// Off: neither key is passed, so the core declares no capability.
|
|
472
|
+
...elicitationConnectOptions(optionsRef.current.elicitation, elicitation.handler)
|
|
473
|
+
}
|
|
259
474
|
);
|
|
260
475
|
if (!current()) {
|
|
261
476
|
await connection.close().catch(() => {
|
|
@@ -286,6 +501,10 @@ var useMcp = (options = {}) => {
|
|
|
286
501
|
);
|
|
287
502
|
setCatalog(connection.catalog);
|
|
288
503
|
setInstructions(mcpInstructionsOf(connection, name));
|
|
504
|
+
setProtocol(mcpProtocolOf(connection, name));
|
|
505
|
+
const registered = await mcpOAuthRegistrationOf(authProvider);
|
|
506
|
+
if (!current()) return;
|
|
507
|
+
setOauthRegistration(registered);
|
|
289
508
|
setStatus("connected");
|
|
290
509
|
} catch (err) {
|
|
291
510
|
if (!current()) return;
|
|
@@ -293,7 +512,7 @@ var useMcp = (options = {}) => {
|
|
|
293
512
|
setStatus("error");
|
|
294
513
|
}
|
|
295
514
|
},
|
|
296
|
-
[closeConnection]
|
|
515
|
+
[closeConnection, elicitation.handler]
|
|
297
516
|
);
|
|
298
517
|
const disconnect = (0, import_react.useCallback)(async () => {
|
|
299
518
|
epochRef.current++;
|
|
@@ -301,7 +520,9 @@ var useMcp = (options = {}) => {
|
|
|
301
520
|
setTools(void 0);
|
|
302
521
|
setCatalog([]);
|
|
303
522
|
setInstructions(void 0);
|
|
523
|
+
setProtocol(void 0);
|
|
304
524
|
setAuthorizationUrl(void 0);
|
|
525
|
+
setOauthRegistration(void 0);
|
|
305
526
|
setError(void 0);
|
|
306
527
|
setStatus("idle");
|
|
307
528
|
}, [closeConnection]);
|
|
@@ -354,7 +575,8 @@ var useMcp = (options = {}) => {
|
|
|
354
575
|
const mod = await loadMcp();
|
|
355
576
|
setOauthSupported(typeof mod.BrowserOAuthProvider === "function");
|
|
356
577
|
if (!mod.BrowserOAuthProvider || !mod.finishMcpOAuth) throw new Error(OAUTH_UNSUPPORTED);
|
|
357
|
-
const
|
|
578
|
+
const oauth = mcpOAuthOptionsOfRecord(pending);
|
|
579
|
+
const provider = newMcpOAuthProvider(mod, mcpOAuthProviderOptions(pending.url, oauth));
|
|
358
580
|
providerRef.current = provider;
|
|
359
581
|
await mod.finishMcpOAuth(provider, callback);
|
|
360
582
|
writeRecord(PENDING_KEY, void 0);
|
|
@@ -362,12 +584,9 @@ var useMcp = (options = {}) => {
|
|
|
362
584
|
await connect({
|
|
363
585
|
url: pending.url,
|
|
364
586
|
name: pending.name,
|
|
365
|
-
oauth
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
clientName: pending.clientName,
|
|
369
|
-
clientMetadataUrl: pending.clientMetadataUrl
|
|
370
|
-
}
|
|
587
|
+
oauth,
|
|
588
|
+
...pending.protocol ? { protocol: pending.protocol } : {},
|
|
589
|
+
...mcpSkillsSettingOf(pending.skills) ? { skills: mcpSkillsSettingOf(pending.skills) } : {}
|
|
371
590
|
});
|
|
372
591
|
} catch (err) {
|
|
373
592
|
writeRecord(PENDING_KEY, void 0);
|
|
@@ -385,15 +604,25 @@ var useMcp = (options = {}) => {
|
|
|
385
604
|
},
|
|
386
605
|
[closeConnection]
|
|
387
606
|
);
|
|
607
|
+
const appsOn = options.apps === true;
|
|
608
|
+
const apps = (0, import_react.useMemo)(
|
|
609
|
+
() => appsOn ? mcpAppsRouter(() => connectionRef.current) : void 0,
|
|
610
|
+
[appsOn]
|
|
611
|
+
);
|
|
388
612
|
return {
|
|
613
|
+
apps,
|
|
389
614
|
status,
|
|
390
615
|
tools,
|
|
391
616
|
catalog,
|
|
392
617
|
instructions,
|
|
618
|
+
protocol,
|
|
393
619
|
error,
|
|
394
620
|
authorizationUrl,
|
|
621
|
+
oauthRegistration,
|
|
395
622
|
oauthSupported,
|
|
396
623
|
completingAuthorization,
|
|
624
|
+
pendingElicitations: elicitation.pendingElicitations,
|
|
625
|
+
answerElicitation: elicitation.answerElicitation,
|
|
397
626
|
connect,
|
|
398
627
|
disconnect,
|
|
399
628
|
authorize,
|
|
@@ -406,10 +635,14 @@ var useMcp = (options = {}) => {
|
|
|
406
635
|
var import_react2 = require("react");
|
|
407
636
|
var DEFAULT_KEY = "agent-web-react:mcp-servers";
|
|
408
637
|
var sanitize = (s) => s.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 32) || "mcp";
|
|
638
|
+
var isProtocolMode = (value) => value === "legacy" || value === "auto" || value === "modern";
|
|
409
639
|
var view = (s, patch = {}) => ({
|
|
410
640
|
name: s.name,
|
|
411
641
|
url: s.url,
|
|
412
642
|
auth: s.auth,
|
|
643
|
+
...isProtocolMode(s.protocol) ? { protocolMode: s.protocol } : {},
|
|
644
|
+
...mcpSkillsSettingOf(s.skills) ? { skills: mcpSkillsSettingOf(s.skills) } : {},
|
|
645
|
+
...typeof s.apps === "boolean" ? { apps: s.apps } : {},
|
|
413
646
|
status: "idle",
|
|
414
647
|
catalog: [],
|
|
415
648
|
...patch
|
|
@@ -436,13 +669,18 @@ var useMcpServers = (options = {}) => {
|
|
|
436
669
|
name: s.name,
|
|
437
670
|
url: s.url,
|
|
438
671
|
auth: s.auth,
|
|
439
|
-
...oauth && typeof oauth === "object" ? { oauth } : {}
|
|
672
|
+
...oauth && typeof oauth === "object" ? { oauth } : {},
|
|
673
|
+
...s.protocolMode ? { protocol: s.protocolMode } : {},
|
|
674
|
+
...mcpSkillsSettingOf(s.skills) ? { skills: mcpSkillsSettingOf(s.skills) } : {},
|
|
675
|
+
...typeof s.apps === "boolean" ? { apps: s.apps } : {}
|
|
440
676
|
});
|
|
441
677
|
}
|
|
442
678
|
}
|
|
443
679
|
const optionsRef = (0, import_react2.useRef)(options);
|
|
444
680
|
optionsRef.current = options;
|
|
445
681
|
const unmountedRef = (0, import_react2.useRef)(false);
|
|
682
|
+
const elicitation = useElicitationQueue(optionsRef);
|
|
683
|
+
const cancelElicitations = elicitation.cancel;
|
|
446
684
|
const saveList = (0, import_react2.useCallback)(() => {
|
|
447
685
|
if (!persist) return;
|
|
448
686
|
writeRecord(storageKey, [...specsRef.current.values()]);
|
|
@@ -469,15 +707,11 @@ var useMcpServers = (options = {}) => {
|
|
|
469
707
|
l.tools = void 0;
|
|
470
708
|
setToolsVersion((v) => v + 1);
|
|
471
709
|
}
|
|
710
|
+
cancelElicitations(name);
|
|
472
711
|
if (open) await open.close().catch(() => {
|
|
473
712
|
});
|
|
474
713
|
}, []);
|
|
475
|
-
const providerFor = (mod, spec) =>
|
|
476
|
-
if (!mod.BrowserOAuthProvider) throw new Error(OAUTH_UNSUPPORTED);
|
|
477
|
-
return new mod.BrowserOAuthProvider(
|
|
478
|
-
mcpOAuthProviderOptions(spec.url, spec.oauth, optionsRef.current)
|
|
479
|
-
);
|
|
480
|
-
};
|
|
714
|
+
const providerFor = (mod, spec) => newMcpOAuthProvider(mod, mcpOAuthProviderOptions(spec.url, spec.oauth, optionsRef.current));
|
|
481
715
|
const connectServer = (0, import_react2.useCallback)(
|
|
482
716
|
async (name) => {
|
|
483
717
|
const spec = specsRef.current.get(name);
|
|
@@ -494,8 +728,10 @@ var useMcpServers = (options = {}) => {
|
|
|
494
728
|
status: "connecting",
|
|
495
729
|
error: void 0,
|
|
496
730
|
authorizationUrl: void 0,
|
|
731
|
+
oauthRegistration: void 0,
|
|
497
732
|
catalog: [],
|
|
498
|
-
instructions: void 0
|
|
733
|
+
instructions: void 0,
|
|
734
|
+
protocol: void 0
|
|
499
735
|
});
|
|
500
736
|
try {
|
|
501
737
|
const mod = await loadMcp();
|
|
@@ -511,13 +747,21 @@ var useMcpServers = (options = {}) => {
|
|
|
511
747
|
url: spec.url,
|
|
512
748
|
headers: spec.auth === "bearer" && l.token ? { Authorization: `Bearer ${l.token}` } : void 0,
|
|
513
749
|
authProvider: provider,
|
|
514
|
-
connectTimeoutMs: optionsRef.current.connectTimeoutMs
|
|
750
|
+
connectTimeoutMs: optionsRef.current.connectTimeoutMs,
|
|
751
|
+
// Only when asked for: without it the connection is built as it always was.
|
|
752
|
+
...spec.protocol ? { protocol: spec.protocol } : {},
|
|
753
|
+
...mcpSkillsSettingOf(spec.skills) ? { skills: mcpSkillsSettingOf(spec.skills) } : {},
|
|
754
|
+
// Only when given: without it the hook's `apps` decides, as before.
|
|
755
|
+
...typeof spec.apps === "boolean" ? { apps: spec.apps } : {}
|
|
515
756
|
}
|
|
516
757
|
},
|
|
517
758
|
{
|
|
518
759
|
clientName: optionsRef.current.clientName,
|
|
519
760
|
onLog: optionsRef.current.onLog,
|
|
520
|
-
connectTimeoutMs: optionsRef.current.connectTimeoutMs
|
|
761
|
+
connectTimeoutMs: optionsRef.current.connectTimeoutMs,
|
|
762
|
+
...optionsRef.current.apps ? { apps: true } : {},
|
|
763
|
+
// Off: neither key is passed, so the core declares no capability.
|
|
764
|
+
...elicitationConnectOptions(optionsRef.current.elicitation, elicitation.handler)
|
|
521
765
|
}
|
|
522
766
|
);
|
|
523
767
|
if (!current()) {
|
|
@@ -549,10 +793,15 @@ var useMcpServers = (options = {}) => {
|
|
|
549
793
|
});
|
|
550
794
|
});
|
|
551
795
|
setToolsVersion((v) => v + 1);
|
|
796
|
+
const negotiated = mcpProtocolOf(connection, name);
|
|
797
|
+
const oauthRegistration = await mcpOAuthRegistrationOf(provider);
|
|
798
|
+
if (!current()) return;
|
|
552
799
|
patch(name, {
|
|
553
800
|
status: "connected",
|
|
801
|
+
oauthRegistration,
|
|
554
802
|
catalog: connection.catalog,
|
|
555
|
-
instructions: mcpInstructionsOf(connection, name)
|
|
803
|
+
instructions: mcpInstructionsOf(connection, name),
|
|
804
|
+
protocol: negotiated ? { version: negotiated.version, era: negotiated.era } : void 0
|
|
556
805
|
});
|
|
557
806
|
} catch (err) {
|
|
558
807
|
if (!current()) return;
|
|
@@ -575,7 +824,10 @@ var useMcpServers = (options = {}) => {
|
|
|
575
824
|
name,
|
|
576
825
|
url: input.url,
|
|
577
826
|
auth: input.auth ?? "none",
|
|
578
|
-
...input.oauth ? { oauth: input.oauth } : {}
|
|
827
|
+
...input.oauth ? { oauth: input.oauth } : {},
|
|
828
|
+
...isProtocolMode(input.protocol) ? { protocol: input.protocol } : {},
|
|
829
|
+
...mcpSkillsSettingOf(input.skills) ? { skills: mcpSkillsSettingOf(input.skills) } : {},
|
|
830
|
+
...typeof input.apps === "boolean" ? { apps: input.apps } : {}
|
|
579
831
|
};
|
|
580
832
|
specsRef.current.set(name, spec);
|
|
581
833
|
live(name).token = input.auth === "bearer" ? input.token : void 0;
|
|
@@ -613,10 +865,7 @@ var useMcpServers = (options = {}) => {
|
|
|
613
865
|
const pending = {
|
|
614
866
|
name,
|
|
615
867
|
url: spec.url,
|
|
616
|
-
|
|
617
|
-
scope: spec.oauth?.scope,
|
|
618
|
-
clientName: spec.oauth?.clientName ?? optionsRef.current.clientName,
|
|
619
|
-
clientMetadataUrl: spec.oauth?.clientMetadataUrl ?? optionsRef.current.clientMetadataUrl,
|
|
868
|
+
...mcpOAuthRecordOf(mcpOAuthProviderOptions(spec.url, spec.oauth, optionsRef.current)),
|
|
620
869
|
startedAt: Date.now()
|
|
621
870
|
};
|
|
622
871
|
writeRecord(pendingKey, pending);
|
|
@@ -647,8 +896,10 @@ var useMcpServers = (options = {}) => {
|
|
|
647
896
|
patch(name, {
|
|
648
897
|
status: "idle",
|
|
649
898
|
authorizationUrl: void 0,
|
|
899
|
+
oauthRegistration: void 0,
|
|
650
900
|
catalog: [],
|
|
651
901
|
instructions: void 0,
|
|
902
|
+
protocol: void 0,
|
|
652
903
|
error: void 0
|
|
653
904
|
});
|
|
654
905
|
},
|
|
@@ -693,15 +944,7 @@ var useMcpServers = (options = {}) => {
|
|
|
693
944
|
setServers((list) => [...list, view(spec)]);
|
|
694
945
|
saveList();
|
|
695
946
|
}
|
|
696
|
-
const provider = providerFor(mod, {
|
|
697
|
-
...spec,
|
|
698
|
-
oauth: {
|
|
699
|
-
redirectUrl: pending.redirectUrl,
|
|
700
|
-
scope: pending.scope,
|
|
701
|
-
clientName: pending.clientName,
|
|
702
|
-
clientMetadataUrl: pending.clientMetadataUrl
|
|
703
|
-
}
|
|
704
|
-
});
|
|
947
|
+
const provider = providerFor(mod, { ...spec, oauth: mcpOAuthOptionsOfRecord(pending) });
|
|
705
948
|
live(spec.name).provider = provider;
|
|
706
949
|
await mod.finishMcpOAuth(provider, callback);
|
|
707
950
|
writeRecord(pendingKey, void 0);
|
|
@@ -734,10 +977,30 @@ var useMcpServers = (options = {}) => {
|
|
|
734
977
|
() => servers.filter((s) => s.status === "connected").flatMap((s) => s.catalog),
|
|
735
978
|
[servers]
|
|
736
979
|
);
|
|
980
|
+
const appsOn = options.apps === true || servers.some((s) => s.apps === true);
|
|
981
|
+
const hookApps = options.apps === true;
|
|
982
|
+
const apps = (0, import_react2.useMemo)(
|
|
983
|
+
() => appsOn ? mcpAppsRouter((server) => {
|
|
984
|
+
const spec = specsRef.current.get(server);
|
|
985
|
+
if (!spec || (spec.apps ?? hookApps) !== true) return void 0;
|
|
986
|
+
return liveRef.current.get(server)?.connection;
|
|
987
|
+
}) : void 0,
|
|
988
|
+
[appsOn, hookApps]
|
|
989
|
+
);
|
|
990
|
+
const protocols = (0, import_react2.useMemo)(
|
|
991
|
+
() => servers.flatMap(
|
|
992
|
+
(s) => s.status === "connected" && s.protocol ? [{ server: s.name, ...s.protocol }] : []
|
|
993
|
+
),
|
|
994
|
+
[servers]
|
|
995
|
+
);
|
|
737
996
|
return {
|
|
997
|
+
apps,
|
|
738
998
|
servers,
|
|
739
999
|
tools,
|
|
740
1000
|
catalog,
|
|
1001
|
+
protocols,
|
|
1002
|
+
pendingElicitations: elicitation.pendingElicitations,
|
|
1003
|
+
answerElicitation: elicitation.answerElicitation,
|
|
741
1004
|
connectedCount: servers.filter((s) => s.status === "connected").length,
|
|
742
1005
|
completingAuthorization,
|
|
743
1006
|
oauthSupported,
|
|
@@ -752,10 +1015,21 @@ var useMcpServers = (options = {}) => {
|
|
|
752
1015
|
// Annotate the CommonJS export names for ESM import in node:
|
|
753
1016
|
0 && (module.exports = {
|
|
754
1017
|
claimOAuthCallback,
|
|
1018
|
+
createElicitationQueue,
|
|
755
1019
|
describeMcpResult,
|
|
1020
|
+
elicitationConnectOptions,
|
|
1021
|
+
elicitationSettings,
|
|
1022
|
+
elicitationTimeoutOf,
|
|
756
1023
|
isUnauthorizedError,
|
|
1024
|
+
mcpAppsRouter,
|
|
757
1025
|
mcpInstructionsOf,
|
|
1026
|
+
mcpOAuthOptionsOfRecord,
|
|
758
1027
|
mcpOAuthProviderOptions,
|
|
1028
|
+
mcpOAuthRecordOf,
|
|
1029
|
+
mcpOAuthRegistrationOf,
|
|
1030
|
+
mcpProtocolOf,
|
|
1031
|
+
mcpSkillsSettingOf,
|
|
1032
|
+
newMcpOAuthProvider,
|
|
759
1033
|
readCallbackParams,
|
|
760
1034
|
stripOAuthParams,
|
|
761
1035
|
useMcp,
|