@dudko.dev/agent-web 0.0.39 → 0.0.40
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 +117 -2
- package/dist/agent-web.js +1 -1
- package/dist/agent-web.js.map +1 -1
- package/dist/index.cjs +6 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/mcp.cjs +112 -53
- package/dist/mcp.cjs.map +1 -1
- package/dist/mcp.d.cts +18 -12
- package/dist/mcp.d.ts +18 -12
- package/dist/mcp.js +109 -50
- package/dist/mcp.js.map +1 -1
- package/package.json +5 -5
package/dist/mcp.cjs
CHANGED
|
@@ -27,7 +27,7 @@ __export(mcp_exports, {
|
|
|
27
27
|
MCP_INSTRUCTIONS_MAX_CHARS_PER_SERVER: () => MCP_INSTRUCTIONS_MAX_CHARS_PER_SERVER,
|
|
28
28
|
MCP_INSTRUCTIONS_MAX_CHARS_TOTAL: () => MCP_INSTRUCTIONS_MAX_CHARS_TOTAL,
|
|
29
29
|
MemoryOAuthStorage: () => MemoryOAuthStorage,
|
|
30
|
-
UnauthorizedError: () =>
|
|
30
|
+
UnauthorizedError: () => import_client4.UnauthorizedError,
|
|
31
31
|
VaultOAuthStorage: () => VaultOAuthStorage,
|
|
32
32
|
beginMcpOAuth: () => beginMcpOAuth,
|
|
33
33
|
checkAuthorizationResponseIssuer: () => checkAuthorizationResponseIssuer,
|
|
@@ -42,14 +42,12 @@ __export(mcp_exports, {
|
|
|
42
42
|
module.exports = __toCommonJS(mcp_exports);
|
|
43
43
|
|
|
44
44
|
// src/mcp/http.ts
|
|
45
|
-
var
|
|
46
|
-
var
|
|
47
|
-
var import_streamableHttp = require("@modelcontextprotocol/sdk/client/streamableHttp.js");
|
|
48
|
-
var import_types = require("@modelcontextprotocol/sdk/types.js");
|
|
45
|
+
var import_client2 = require("@modelcontextprotocol/client");
|
|
46
|
+
var import_cf_worker = require("@modelcontextprotocol/client/validators/cf-worker");
|
|
49
47
|
var import_ai = require("ai");
|
|
50
48
|
|
|
51
49
|
// src/mcp/oauth.ts
|
|
52
|
-
var
|
|
50
|
+
var import_client = require("@modelcontextprotocol/client");
|
|
53
51
|
|
|
54
52
|
// src/storage/db.ts
|
|
55
53
|
var import_idb = require("idb");
|
|
@@ -383,11 +381,13 @@ var BrowserOAuthProvider = class {
|
|
|
383
381
|
}
|
|
384
382
|
/**
|
|
385
383
|
* Called by the SDK when the server rejects what we hold: `'tokens'` after an
|
|
386
|
-
* `invalid_grant` (dead refresh token)
|
|
387
|
-
* (the AS forgot our dynamic registration)
|
|
388
|
-
*
|
|
389
|
-
*
|
|
390
|
-
*
|
|
384
|
+
* `invalid_grant` (dead refresh token); `'client'` and then `'tokens'` after
|
|
385
|
+
* an `invalid_client` (the AS forgot our dynamic registration) - what
|
|
386
|
+
* discovery found is kept on purpose, the SDK checks the next callback
|
|
387
|
+
* against it. `'verifier'` and `'discovery'` are in its type and handled the
|
|
388
|
+
* same way: one key each. `'all'` is what {@link reset} asks for. Clearing
|
|
389
|
+
* lets the SDK re-register and re-authorize without the user clearing site
|
|
390
|
+
* data.
|
|
391
391
|
*/
|
|
392
392
|
async invalidateCredentials(scope) {
|
|
393
393
|
const targets = scope === "all" ? ["tokens", "client", "verifier", "state", "discovery"] : [scope];
|
|
@@ -450,14 +450,23 @@ var finishMcpOAuth = async (provider, callback, opts = {}) => {
|
|
|
450
450
|
if (!await provider.verifyState(callback.state)) {
|
|
451
451
|
throw new Error("Authorization state mismatch; refusing to exchange the code (possible CSRF)");
|
|
452
452
|
}
|
|
453
|
+
const discovery = await provider.discoveryState();
|
|
453
454
|
checkAuthorizationResponseIssuer(
|
|
454
455
|
callback.iss,
|
|
455
|
-
|
|
456
|
+
discovery,
|
|
456
457
|
(await provider.clientInformation())?.issuer
|
|
457
458
|
);
|
|
458
|
-
|
|
459
|
+
if (!discovery) {
|
|
460
|
+
throw new Error(
|
|
461
|
+
"Authorization cannot be completed: the record of which authorization server it was started with is not in the store (started by an older version, or cleared in between); start the authorization again"
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
const result = await (0, import_client.auth)(provider, {
|
|
459
465
|
serverUrl: provider.serverUrl,
|
|
460
466
|
authorizationCode: callback.code,
|
|
467
|
+
// The SDK runs its own RFC 9207 check inside `auth()`, after ours: without
|
|
468
|
+
// the value it would read a response that carried `iss` as one that did not.
|
|
469
|
+
iss: callback.iss,
|
|
461
470
|
fetchFn: opts.fetch
|
|
462
471
|
});
|
|
463
472
|
if (result !== "AUTHORIZED") {
|
|
@@ -466,7 +475,7 @@ var finishMcpOAuth = async (provider, callback, opts = {}) => {
|
|
|
466
475
|
};
|
|
467
476
|
var beginMcpOAuth = async (provider, opts = {}) => {
|
|
468
477
|
provider.authorizationUrl = void 0;
|
|
469
|
-
return (0,
|
|
478
|
+
return (0, import_client.auth)(provider, { serverUrl: provider.serverUrl, fetchFn: opts.fetch });
|
|
470
479
|
};
|
|
471
480
|
|
|
472
481
|
// src/mcp/shared.ts
|
|
@@ -570,16 +579,23 @@ var outputSchemaNote = (toolName, detail) => {
|
|
|
570
579
|
};
|
|
571
580
|
|
|
572
581
|
// src/mcp/http.ts
|
|
573
|
-
var
|
|
574
|
-
var listAllTools = async (client) => {
|
|
582
|
+
var MAX_TOOL_LIST_PAGES = 100;
|
|
583
|
+
var listAllTools = async (client, onTruncated) => {
|
|
575
584
|
const all = [];
|
|
576
585
|
let cursor;
|
|
577
|
-
|
|
578
|
-
|
|
586
|
+
const seen = /* @__PURE__ */ new Set();
|
|
587
|
+
for (let page = 0; page < MAX_TOOL_LIST_PAGES; page += 1) {
|
|
588
|
+
const res = await client.request({
|
|
589
|
+
method: "tools/list",
|
|
590
|
+
params: cursor ? { cursor } : void 0
|
|
591
|
+
});
|
|
579
592
|
all.push(...res.tools);
|
|
580
|
-
|
|
581
|
-
if (!
|
|
593
|
+
const next = typeof res.nextCursor === "string" && res.nextCursor ? res.nextCursor : void 0;
|
|
594
|
+
if (!next || seen.has(next)) return all;
|
|
595
|
+
seen.add(next);
|
|
596
|
+
cursor = next;
|
|
582
597
|
}
|
|
598
|
+
onTruncated?.();
|
|
583
599
|
return all;
|
|
584
600
|
};
|
|
585
601
|
var ConnectTimeoutError = class extends Error {
|
|
@@ -621,10 +637,19 @@ var flattenContent = (content) => {
|
|
|
621
637
|
if (allText) return parts.map((p) => p.text).join("\n");
|
|
622
638
|
return parts.map((p) => p?.type === "text" && typeof p.text === "string" ? p.text : p);
|
|
623
639
|
};
|
|
624
|
-
var
|
|
625
|
-
|
|
626
|
-
return
|
|
627
|
-
|
|
640
|
+
var OUTPUT_SCHEMA_ERROR_MAX_CHARS = 200;
|
|
641
|
+
var outputSchemaViolation = (toolName, result, validate) => {
|
|
642
|
+
if (result.isError === true) return void 0;
|
|
643
|
+
if (result.structuredContent === void 0) {
|
|
644
|
+
return `Tool ${toolName} has an output schema but did not return structured content`;
|
|
645
|
+
}
|
|
646
|
+
try {
|
|
647
|
+
const checked = validate(result.structuredContent);
|
|
648
|
+
return checked.valid ? void 0 : `Structured content does not match the tool's output schema: ${checked.errorMessage}`;
|
|
649
|
+
} catch (err) {
|
|
650
|
+
return `Failed to validate structured content: ${err instanceof Error ? err.message : String(err)}`;
|
|
651
|
+
}
|
|
652
|
+
};
|
|
628
653
|
var asEnterpriseProvider = (provider) => {
|
|
629
654
|
const p = provider;
|
|
630
655
|
return typeof p.extension === "string" && typeof p.authorize === "function" && typeof p.transportFetch === "function" ? provider : void 0;
|
|
@@ -640,13 +665,21 @@ var refreshOnly = (p) => ({
|
|
|
640
665
|
get clientMetadata() {
|
|
641
666
|
return p.clientMetadata;
|
|
642
667
|
},
|
|
643
|
-
|
|
668
|
+
get clientMetadataUrl() {
|
|
669
|
+
return p.clientMetadataUrl;
|
|
670
|
+
},
|
|
671
|
+
// The second argument is the authorization server the SDK resolved: a
|
|
672
|
+
// provider that keeps credentials per issuer reads and writes by it.
|
|
673
|
+
clientInformation: (ctx) => p.clientInformation(ctx),
|
|
644
674
|
saveClientInformation: p.saveClientInformation?.bind(p),
|
|
645
|
-
tokens: () => p.tokens(),
|
|
646
|
-
saveTokens: (t) => p.saveTokens(t),
|
|
675
|
+
tokens: (ctx) => p.tokens(ctx),
|
|
676
|
+
saveTokens: (t, ctx) => p.saveTokens(t, ctx),
|
|
647
677
|
invalidateCredentials: p.invalidateCredentials?.bind(p),
|
|
648
678
|
addClientAuthentication: p.addClientAuthentication?.bind(p),
|
|
649
679
|
validateResourceURL: p.validateResourceURL?.bind(p),
|
|
680
|
+
// What a refresh request is made of, when the provider says so.
|
|
681
|
+
scopeForRefresh: p.scopeForRefresh?.bind(p),
|
|
682
|
+
dpop: p.dpop?.bind(p),
|
|
650
683
|
codeVerifier: () => p.codeVerifier(),
|
|
651
684
|
state: () => {
|
|
652
685
|
throw new RefreshOnlyAbort();
|
|
@@ -675,7 +708,7 @@ var refreshIfExpired = async (provider, serverUrl, fetchFn, log, name, detailLog
|
|
|
675
708
|
const tokens = await provider.tokens();
|
|
676
709
|
if (!tokens?.refresh_token) return;
|
|
677
710
|
try {
|
|
678
|
-
await (0,
|
|
711
|
+
await (0, import_client2.auth)(refreshOnly(provider), { serverUrl, fetchFn });
|
|
679
712
|
} catch (err) {
|
|
680
713
|
log(
|
|
681
714
|
"info",
|
|
@@ -705,6 +738,10 @@ var diagnoseMcpCors = async (url, fetchFn = globalThis.fetch) => {
|
|
|
705
738
|
}
|
|
706
739
|
return hints.join(" ");
|
|
707
740
|
};
|
|
741
|
+
var warnTruncated = (log, name) => log(
|
|
742
|
+
"warn",
|
|
743
|
+
`[mcp] ${name}: tools/list still paginating after ${MAX_TOOL_LIST_PAGES} pages; the rest of the tools are not mounted`
|
|
744
|
+
);
|
|
708
745
|
var openConnection = async (name, cfg, opts, log) => {
|
|
709
746
|
let client;
|
|
710
747
|
let timedOut = false;
|
|
@@ -728,7 +765,7 @@ var openConnection = async (name, cfg, opts, log) => {
|
|
|
728
765
|
if (err instanceof ConnectTimeoutError) timedOut = true;
|
|
729
766
|
if (client) await client.close().catch(() => {
|
|
730
767
|
});
|
|
731
|
-
const needsAuthorization = err instanceof
|
|
768
|
+
const needsAuthorization = err instanceof import_client2.UnauthorizedError;
|
|
732
769
|
let message = err instanceof Error ? err.message : String(err);
|
|
733
770
|
if (!needsAuthorization && !(err instanceof ConnectTimeoutError) && isNetworkLevelFailure(err)) {
|
|
734
771
|
const hint = await diagnoseMcpCors(cfg.url, cfg.fetch).catch(() => void 0);
|
|
@@ -756,9 +793,18 @@ var connectAndList = async (name, cfg, opts, log, onClient) => {
|
|
|
756
793
|
assertSecureOAuthUrl(cfg.url, name);
|
|
757
794
|
await refreshIfExpired(cfg.authProvider, cfg.url, cfg.fetch, log, name, opts.onLog);
|
|
758
795
|
}
|
|
759
|
-
const transport = new
|
|
796
|
+
const transport = new import_client2.StreamableHTTPClientTransport(new URL(cfg.url), {
|
|
760
797
|
requestInit: headers ? { headers } : void 0,
|
|
761
798
|
authProvider: cfg.authProvider,
|
|
799
|
+
// A 403 `insufficient_scope` in the middle of a session. A provider
|
|
800
|
+
// without a human step (no `redirectUrl`: the enterprise one) may ask
|
|
801
|
+
// again for the wider scope, once per request - the SDK's cap. One WITH a
|
|
802
|
+
// human step - in a browser nearly all of them - never does: the SDK would
|
|
803
|
+
// skip the refresh and start a new authorization from inside a tool call,
|
|
804
|
+
// replacing the stored state and PKCE verifier each time the model tried
|
|
805
|
+
// the tool again. The call fails with the scope the server asked for, and
|
|
806
|
+
// the host decides. The Node sibling's rule.
|
|
807
|
+
onInsufficientScope: cfg.authProvider?.redirectUrl ? "throw" : "reauthorize",
|
|
762
808
|
// The enterprise provider reports a refused grant itself: a short typed
|
|
763
809
|
// error for the caller, the server's words (redacted) to the log.
|
|
764
810
|
fetch: cfg.authProvider && asEnterpriseProvider(cfg.authProvider)?.transportFetch(cfg.fetch, opts.onLog) || cfg.fetch,
|
|
@@ -767,7 +813,7 @@ var connectAndList = async (name, cfg, opts, log, onClient) => {
|
|
|
767
813
|
transport.onerror = (err) => log("warn", `[mcp] ${name}: transport error - ${err.message}`);
|
|
768
814
|
transport.onclose = () => log("info", `[mcp] ${name}: transport closed`);
|
|
769
815
|
const enterprise = cfg.authProvider ? asEnterpriseProvider(cfg.authProvider) : void 0;
|
|
770
|
-
const client = new
|
|
816
|
+
const client = new import_client2.Client(
|
|
771
817
|
{ name: opts.clientName ?? "agent-web", version: opts.clientVersion ?? "0.0.0" },
|
|
772
818
|
// An extension is opt-in: it is declared only by the client that uses it.
|
|
773
819
|
enterprise ? { capabilities: { extensions: { [enterprise.extension]: {} } } } : void 0
|
|
@@ -776,9 +822,9 @@ var connectAndList = async (name, cfg, opts, log, onClient) => {
|
|
|
776
822
|
await client.connect(transport);
|
|
777
823
|
if (opts.onToolsChanged) {
|
|
778
824
|
const notify = opts.onToolsChanged;
|
|
779
|
-
client.setNotificationHandler(
|
|
825
|
+
client.setNotificationHandler("notifications/tools/list_changed", () => notify(name));
|
|
780
826
|
}
|
|
781
|
-
const listed = await listAllTools(client);
|
|
827
|
+
const listed = await listAllTools(client, () => warnTruncated(log, name));
|
|
782
828
|
return { name, client, listed, instructions: cleanMcpInstructions(client.getInstructions()) };
|
|
783
829
|
};
|
|
784
830
|
var connectMcpHttp = async (servers, opts = {}) => {
|
|
@@ -800,6 +846,7 @@ var connectMcpHttp = async (servers, opts = {}) => {
|
|
|
800
846
|
if (catalog[i].server === name) catalog.splice(i, 1);
|
|
801
847
|
}
|
|
802
848
|
const keys = [];
|
|
849
|
+
let schemaValidator;
|
|
803
850
|
let mounted = 0;
|
|
804
851
|
for (const t of listed) {
|
|
805
852
|
const prefixed = prefix + sanitizeName(t.name);
|
|
@@ -820,28 +867,40 @@ var connectMcpHttp = async (servers, opts = {}) => {
|
|
|
820
867
|
}
|
|
821
868
|
const description = t.description ?? "";
|
|
822
869
|
const readOnly = t.annotations?.readOnlyHint === true;
|
|
870
|
+
let outputValidator;
|
|
871
|
+
const outputValidatorOf = () => {
|
|
872
|
+
if (!t.outputSchema || outputValidator) return outputValidator;
|
|
873
|
+
try {
|
|
874
|
+
schemaValidator ??= new import_cf_worker.CfWorkerJsonSchemaValidator();
|
|
875
|
+
outputValidator = schemaValidator.getValidator(t.outputSchema);
|
|
876
|
+
} catch (err) {
|
|
877
|
+
const reason = (err instanceof Error ? err.message : String(err)).slice(
|
|
878
|
+
0,
|
|
879
|
+
OUTPUT_SCHEMA_ERROR_MAX_CHARS
|
|
880
|
+
);
|
|
881
|
+
throw new Error(`Tool '${t.name}' has an invalid outputSchema: ${reason}`);
|
|
882
|
+
}
|
|
883
|
+
return outputValidator;
|
|
884
|
+
};
|
|
823
885
|
tools[key] = (0, import_ai.dynamicTool)({
|
|
824
886
|
description,
|
|
825
887
|
inputSchema: (0, import_ai.jsonSchema)(t.inputSchema),
|
|
826
888
|
execute: async (args, options) => {
|
|
827
|
-
|
|
889
|
+
const validate = outputValidatorOf();
|
|
890
|
+
const res = await client.request(
|
|
891
|
+
{
|
|
892
|
+
method: "tools/call",
|
|
893
|
+
params: { name: t.name, arguments: args ?? {} }
|
|
894
|
+
},
|
|
895
|
+
options?.abortSignal ? { signal: options.abortSignal } : void 0
|
|
896
|
+
);
|
|
828
897
|
let schemaNote;
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
{ name: t.name, arguments: args ?? {} },
|
|
833
|
-
capturingResultSchema((r) => {
|
|
834
|
-
received = r;
|
|
835
|
-
}),
|
|
836
|
-
options?.abortSignal ? { signal: options.abortSignal } : void 0
|
|
837
|
-
);
|
|
838
|
-
} catch (err) {
|
|
839
|
-
if (!received) throw err;
|
|
840
|
-
res = received;
|
|
841
|
-
schemaNote = outputSchemaNote(t.name, err);
|
|
898
|
+
const violation = validate && outputSchemaViolation(t.name, res, validate);
|
|
899
|
+
if (violation) {
|
|
900
|
+
schemaNote = outputSchemaNote(t.name, violation);
|
|
842
901
|
log(
|
|
843
902
|
"warn",
|
|
844
|
-
`[mcp] ${key}: result does not follow the declared outputSchema - ${
|
|
903
|
+
`[mcp] ${key}: result does not follow the declared outputSchema - ${violation}; passed on with a note`
|
|
845
904
|
);
|
|
846
905
|
}
|
|
847
906
|
if (res.isError) {
|
|
@@ -897,7 +956,7 @@ var connectMcpHttp = async (servers, opts = {}) => {
|
|
|
897
956
|
refreshServer: async (name) => {
|
|
898
957
|
const client = clients.get(name);
|
|
899
958
|
if (!client) throw new Error(`MCP server "${name}" is not connected`);
|
|
900
|
-
mountTools(name, client, await listAllTools(client));
|
|
959
|
+
mountTools(name, client, await listAllTools(client, () => warnTruncated(log, name)));
|
|
901
960
|
},
|
|
902
961
|
close: async () => {
|
|
903
962
|
await Promise.allSettled([...clients.values()].map((c) => c.close()));
|
|
@@ -906,10 +965,10 @@ var connectMcpHttp = async (servers, opts = {}) => {
|
|
|
906
965
|
};
|
|
907
966
|
|
|
908
967
|
// src/mcp/index.ts
|
|
909
|
-
var
|
|
968
|
+
var import_client4 = require("@modelcontextprotocol/client");
|
|
910
969
|
|
|
911
970
|
// src/mcp/enterprise-oauth.ts
|
|
912
|
-
var
|
|
971
|
+
var import_client3 = require("@modelcontextprotocol/client");
|
|
913
972
|
|
|
914
973
|
// src/logger.ts
|
|
915
974
|
var WEIGHT = {
|
|
@@ -1639,7 +1698,7 @@ var BrowserEnterpriseOAuthProvider = class {
|
|
|
1639
1698
|
*/
|
|
1640
1699
|
async authorize(opts = {}) {
|
|
1641
1700
|
try {
|
|
1642
|
-
await (0,
|
|
1701
|
+
await (0, import_client3.auth)(this, {
|
|
1643
1702
|
serverUrl: this.serverUrl,
|
|
1644
1703
|
fetchFn: this.transportFetch(opts.fetch, opts.onLog)
|
|
1645
1704
|
});
|
|
@@ -1663,7 +1722,7 @@ var BrowserEnterpriseOAuthProvider = class {
|
|
|
1663
1722
|
throw new EnterpriseOAuthError(
|
|
1664
1723
|
"discovery",
|
|
1665
1724
|
`Enterprise MCP authorization: discovery failed (${message})`,
|
|
1666
|
-
oauthCode(err
|
|
1725
|
+
err instanceof import_client3.OAuthError ? oauthCode(err.code) : void 0
|
|
1667
1726
|
);
|
|
1668
1727
|
}
|
|
1669
1728
|
}
|