@slashfi/agents-sdk 0.76.0 → 0.77.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/dist/adk.js +183 -111
- package/dist/adk.js.map +1 -1
- package/dist/cjs/config-store.js +493 -13
- package/dist/cjs/config-store.js.map +1 -1
- package/dist/cjs/define-config.js.map +1 -1
- package/dist/cjs/mcp-client.js +98 -0
- package/dist/cjs/mcp-client.js.map +1 -1
- package/dist/cjs/registry-consumer.js +68 -10
- package/dist/cjs/registry-consumer.js.map +1 -1
- package/dist/config-store.d.ts +36 -1
- package/dist/config-store.d.ts.map +1 -1
- package/dist/config-store.js +494 -14
- package/dist/config-store.js.map +1 -1
- package/dist/define-config.d.ts +54 -0
- package/dist/define-config.d.ts.map +1 -1
- package/dist/define-config.js.map +1 -1
- package/dist/mcp-client.d.ts +44 -0
- package/dist/mcp-client.d.ts.map +1 -1
- package/dist/mcp-client.js +95 -0
- package/dist/mcp-client.js.map +1 -1
- package/dist/registry-consumer.d.ts.map +1 -1
- package/dist/registry-consumer.js +68 -10
- package/dist/registry-consumer.js.map +1 -1
- package/package.json +1 -1
- package/src/adk.ts +71 -3
- package/src/config-store.test.ts +212 -0
- package/src/config-store.ts +572 -18
- package/src/define-config.ts +58 -0
- package/src/mcp-client.ts +121 -0
- package/src/registry-consumer.ts +74 -12
package/dist/cjs/config-store.js
CHANGED
|
@@ -509,16 +509,215 @@ function createAdk(fs, options = {}) {
|
|
|
509
509
|
// ==========================================
|
|
510
510
|
// Registry API
|
|
511
511
|
// ==========================================
|
|
512
|
+
/**
|
|
513
|
+
* Encrypt with `secret:` prefix when an encryption key is configured, so the
|
|
514
|
+
* value is readable by the existing `decryptConfigSecrets` path on the read
|
|
515
|
+
* side. Plaintext fallback preserves the "no key = dev mode" contract.
|
|
516
|
+
*/
|
|
517
|
+
async function protectSecret(value) {
|
|
518
|
+
if (!options.encryptionKey)
|
|
519
|
+
return value;
|
|
520
|
+
return `${SECRET_PREFIX}${await (0, crypto_js_1.encryptSecret)(value, options.encryptionKey)}`;
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Re-probe a registry with the current stored credentials to see whether it
|
|
524
|
+
* advertises `capabilities.registry.proxy` in its MCP `initialize` response,
|
|
525
|
+
* and persist the proxy config when it does. Safe to call after a successful
|
|
526
|
+
* `auth()` / `authLocal()` — on the add path we skip the proxy probe when
|
|
527
|
+
* auth is required, so this is the second chance to back-fill it.
|
|
528
|
+
*
|
|
529
|
+
* Respects explicit user config: if `proxy` is already set, we leave it
|
|
530
|
+
* alone. Any discovery failure is swallowed — proxy is an optimization,
|
|
531
|
+
* not a correctness requirement.
|
|
532
|
+
*/
|
|
533
|
+
async function discoverProxyAfterAuth(nameOrUrl) {
|
|
534
|
+
const config = await readConfig();
|
|
535
|
+
const target = findRegistry(config.registries ?? [], nameOrUrl);
|
|
536
|
+
if (!target || typeof target === "string")
|
|
537
|
+
return;
|
|
538
|
+
if (target.proxy)
|
|
539
|
+
return;
|
|
540
|
+
try {
|
|
541
|
+
const consumer = await buildConsumer(nameOrUrl);
|
|
542
|
+
const discovered = await consumer.discover(target.url);
|
|
543
|
+
if (!discovered.proxy?.mode)
|
|
544
|
+
return;
|
|
545
|
+
await updateRegistryEntry(nameOrUrl, (existing) => {
|
|
546
|
+
if (existing.proxy)
|
|
547
|
+
return;
|
|
548
|
+
existing.proxy = {
|
|
549
|
+
mode: discovered.proxy.mode,
|
|
550
|
+
...(discovered.proxy.agent && { agent: discovered.proxy.agent }),
|
|
551
|
+
};
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
catch {
|
|
555
|
+
// Proxy probe is best-effort — auth itself already succeeded.
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Atomic read-modify-write on a registry entry by name or URL. Used by
|
|
560
|
+
* `authLocal` to persist both `auth` and `oauth` together, which `auth()`
|
|
561
|
+
* alone can't express. Returns true when the entry was found and written.
|
|
562
|
+
*/
|
|
563
|
+
async function updateRegistryEntry(nameOrUrl, mutate) {
|
|
564
|
+
const config = await readConfig();
|
|
565
|
+
if (!config.registries?.length)
|
|
566
|
+
return false;
|
|
567
|
+
let found = false;
|
|
568
|
+
const registries = config.registries.map((r) => {
|
|
569
|
+
const rName = registryDisplayName(r);
|
|
570
|
+
if (rName !== nameOrUrl && registryUrl(r) !== nameOrUrl)
|
|
571
|
+
return r;
|
|
572
|
+
found = true;
|
|
573
|
+
const existing = typeof r === "string" ? { url: r } : { ...r };
|
|
574
|
+
mutate(existing);
|
|
575
|
+
return existing;
|
|
576
|
+
});
|
|
577
|
+
if (!found)
|
|
578
|
+
return false;
|
|
579
|
+
await writeConfig({ ...config, registries });
|
|
580
|
+
return true;
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Decrypt a `secret:`-prefixed value if we hold the encryption key. Plaintext
|
|
584
|
+
* values pass through unchanged so dev configs keep working.
|
|
585
|
+
*/
|
|
586
|
+
async function revealSecret(value) {
|
|
587
|
+
if (!value)
|
|
588
|
+
return value;
|
|
589
|
+
if (!value.startsWith(SECRET_PREFIX))
|
|
590
|
+
return value;
|
|
591
|
+
if (!options.encryptionKey)
|
|
592
|
+
return undefined;
|
|
593
|
+
return (0, crypto_js_1.decryptSecret)(value.slice(SECRET_PREFIX.length), options.encryptionKey);
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Refresh a registry's OAuth access token using the stored refresh token.
|
|
597
|
+
* Persists the new access token (encrypted) and updates `expiresAt`. If the
|
|
598
|
+
* provider rotates the refresh token, that's encrypted and stored too.
|
|
599
|
+
* Returns `true` when the refresh succeeded. Callers should catch and fall
|
|
600
|
+
* back to full re-auth on failure.
|
|
601
|
+
*/
|
|
602
|
+
async function refreshRegistryToken(nameOrUrl) {
|
|
603
|
+
const config = await readConfig();
|
|
604
|
+
const target = findRegistry(config.registries ?? [], nameOrUrl);
|
|
605
|
+
if (!target || typeof target === "string")
|
|
606
|
+
return false;
|
|
607
|
+
const oauth = target.oauth;
|
|
608
|
+
if (!oauth?.refreshToken || !oauth.tokenEndpoint || !oauth.clientId)
|
|
609
|
+
return false;
|
|
610
|
+
const refreshToken = await revealSecret(oauth.refreshToken);
|
|
611
|
+
const clientSecret = await revealSecret(oauth.clientSecret);
|
|
612
|
+
if (!refreshToken)
|
|
613
|
+
return false;
|
|
614
|
+
const refreshed = await (0, mcp_client_js_1.refreshAccessToken)(oauth.tokenEndpoint, {
|
|
615
|
+
refreshToken,
|
|
616
|
+
clientId: oauth.clientId,
|
|
617
|
+
...(clientSecret && { clientSecret }),
|
|
618
|
+
});
|
|
619
|
+
const expiresAt = refreshed.expiresIn
|
|
620
|
+
? new Date(Date.now() + refreshed.expiresIn * 1000).toISOString()
|
|
621
|
+
: undefined;
|
|
622
|
+
const encAccess = await protectSecret(refreshed.accessToken);
|
|
623
|
+
const encRefresh = refreshed.refreshToken
|
|
624
|
+
? await protectSecret(refreshed.refreshToken)
|
|
625
|
+
: undefined;
|
|
626
|
+
await updateRegistryEntry(nameOrUrl, (existing) => {
|
|
627
|
+
existing.auth = { type: "bearer", token: encAccess };
|
|
628
|
+
if (!existing.oauth)
|
|
629
|
+
return;
|
|
630
|
+
if (encRefresh)
|
|
631
|
+
existing.oauth.refreshToken = encRefresh;
|
|
632
|
+
if (expiresAt)
|
|
633
|
+
existing.oauth.expiresAt = expiresAt;
|
|
634
|
+
else
|
|
635
|
+
delete existing.oauth.expiresAt;
|
|
636
|
+
});
|
|
637
|
+
return true;
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* Run a registry op once; on 401 (`registry_auth_required`), try to refresh
|
|
641
|
+
* via the stored refresh token and retry exactly once. Any other AdkError
|
|
642
|
+
* propagates as-is.
|
|
643
|
+
*/
|
|
644
|
+
async function callWithRefresh(nameOrUrl, fn) {
|
|
645
|
+
try {
|
|
646
|
+
return await fn();
|
|
647
|
+
}
|
|
648
|
+
catch (err) {
|
|
649
|
+
if (!(err instanceof adk_error_js_1.AdkError) || err.code !== "registry_auth_required")
|
|
650
|
+
throw err;
|
|
651
|
+
let refreshed = false;
|
|
652
|
+
try {
|
|
653
|
+
refreshed = await refreshRegistryToken(nameOrUrl);
|
|
654
|
+
}
|
|
655
|
+
catch {
|
|
656
|
+
// Refresh failed — surface the original 401 below.
|
|
657
|
+
}
|
|
658
|
+
if (!refreshed)
|
|
659
|
+
throw err;
|
|
660
|
+
return fn();
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* Throw a typed error if the registry has a recorded auth challenge and
|
|
665
|
+
* no usable credentials on the entry. Callers should invoke this before
|
|
666
|
+
* running any op that talks to the registry.
|
|
667
|
+
*/
|
|
668
|
+
function assertRegistryAuthorized(entry) {
|
|
669
|
+
if (!entry.authRequirement)
|
|
670
|
+
return;
|
|
671
|
+
const hasUsableAuth = entry.auth && entry.auth.type !== "none"
|
|
672
|
+
? (entry.auth.type === "bearer" && !!entry.auth.token) ||
|
|
673
|
+
(entry.auth.type === "api-key" && !!entry.auth.key)
|
|
674
|
+
: false;
|
|
675
|
+
if (hasUsableAuth)
|
|
676
|
+
return;
|
|
677
|
+
const name = entry.name ?? entry.url;
|
|
678
|
+
const scope = entry.authRequirement.scopes?.join(" ");
|
|
679
|
+
throw new adk_error_js_1.AdkError({
|
|
680
|
+
code: "registry_auth_required",
|
|
681
|
+
message: `Registry "${name}" requires authentication.`,
|
|
682
|
+
hint: `Run: adk registry auth ${name} --token <token>${scope ? ` (scopes: ${scope})` : ""}`,
|
|
683
|
+
details: {
|
|
684
|
+
url: entry.url,
|
|
685
|
+
scheme: entry.authRequirement.scheme,
|
|
686
|
+
realm: entry.authRequirement.realm,
|
|
687
|
+
authorizationServers: entry.authRequirement.authorizationServers,
|
|
688
|
+
scopes: entry.authRequirement.scopes,
|
|
689
|
+
resourceMetadataUrl: entry.authRequirement.resourceMetadataUrl,
|
|
690
|
+
},
|
|
691
|
+
});
|
|
692
|
+
}
|
|
512
693
|
const registry = {
|
|
513
694
|
async add(entry) {
|
|
514
695
|
const config = await readConfig();
|
|
515
696
|
const alias = entry.name ?? entry.url;
|
|
516
697
|
const registries = (config.registries ?? []).filter((r) => registryDisplayName(r) !== alias);
|
|
517
|
-
// Probe the registry
|
|
518
|
-
//
|
|
519
|
-
//
|
|
698
|
+
// Probe the registry before saving. Two things fall out of the probe:
|
|
699
|
+
// 1. Auth challenge — 401 + WWW-Authenticate points at RFC 9728
|
|
700
|
+
// resource metadata; we persist it on `authRequirement` so
|
|
701
|
+
// subsequent ops can refuse early with a friendly message.
|
|
702
|
+
// 2. Proxy capability — the MCP `initialize` response may advertise
|
|
703
|
+
// `capabilities.registry.proxy`, which auto-populates `proxy`.
|
|
704
|
+
// Users who set `proxy` or `auth` explicitly on the entry always win:
|
|
705
|
+
// discovery only fills in blanks.
|
|
520
706
|
let final = entry;
|
|
521
|
-
|
|
707
|
+
let authRequirement;
|
|
708
|
+
const hasUsableAuth = entry.auth && entry.auth.type !== "none"
|
|
709
|
+
? (entry.auth.type === "bearer" && !!entry.auth.token) ||
|
|
710
|
+
(entry.auth.type === "api-key" && !!entry.auth.key)
|
|
711
|
+
: false;
|
|
712
|
+
if (!hasUsableAuth) {
|
|
713
|
+
const fetchFn = options.fetch ?? globalThis.fetch;
|
|
714
|
+
const probe = await (0, mcp_client_js_1.probeRegistryAuth)(entry.url, fetchFn);
|
|
715
|
+
if (probe.ok === false) {
|
|
716
|
+
authRequirement = probe.requirement;
|
|
717
|
+
final = { ...final, authRequirement };
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
if (!entry.proxy && !authRequirement) {
|
|
522
721
|
try {
|
|
523
722
|
const probeConsumer = await (0, registry_consumer_js_1.createRegistryConsumer)({ registries: [entry], refs: [] }, { token: options.token, fetch: options.fetch });
|
|
524
723
|
const resolved = probeConsumer.registries()[0];
|
|
@@ -526,7 +725,7 @@ function createAdk(fs, options = {}) {
|
|
|
526
725
|
const discovered = await probeConsumer.discover(resolved.url);
|
|
527
726
|
if (discovered.proxy?.mode) {
|
|
528
727
|
final = {
|
|
529
|
-
...
|
|
728
|
+
...final,
|
|
530
729
|
proxy: {
|
|
531
730
|
mode: discovered.proxy.mode,
|
|
532
731
|
...(discovered.proxy.agent && { agent: discovered.proxy.agent }),
|
|
@@ -542,6 +741,7 @@ function createAdk(fs, options = {}) {
|
|
|
542
741
|
}
|
|
543
742
|
registries.push(final);
|
|
544
743
|
await writeConfig({ ...config, registries });
|
|
744
|
+
return authRequirement ? { authRequirement } : {};
|
|
545
745
|
},
|
|
546
746
|
async remove(nameOrUrl) {
|
|
547
747
|
const config = await readConfig();
|
|
@@ -594,18 +794,26 @@ function createAdk(fs, options = {}) {
|
|
|
594
794
|
return true;
|
|
595
795
|
},
|
|
596
796
|
async browse(name, query) {
|
|
597
|
-
const consumer = await buildConsumer(name);
|
|
598
797
|
const config = await readConfig();
|
|
599
798
|
const target = findRegistry(config.registries ?? [], name);
|
|
600
|
-
|
|
601
|
-
|
|
799
|
+
if (target && typeof target !== "string")
|
|
800
|
+
assertRegistryAuthorized(target);
|
|
801
|
+
return callWithRefresh(name, async () => {
|
|
802
|
+
const consumer = await buildConsumer(name);
|
|
803
|
+
const url = target ? registryUrl(target) : name;
|
|
804
|
+
return consumer.browse(url, query);
|
|
805
|
+
});
|
|
602
806
|
},
|
|
603
807
|
async inspect(name) {
|
|
604
|
-
const consumer = await buildConsumer(name);
|
|
605
808
|
const config = await readConfig();
|
|
606
809
|
const target = findRegistry(config.registries ?? [], name);
|
|
607
|
-
|
|
608
|
-
|
|
810
|
+
if (target && typeof target !== "string")
|
|
811
|
+
assertRegistryAuthorized(target);
|
|
812
|
+
return callWithRefresh(name, async () => {
|
|
813
|
+
const consumer = await buildConsumer(name);
|
|
814
|
+
const url = target ? registryUrl(target) : name;
|
|
815
|
+
return consumer.discover(url);
|
|
816
|
+
});
|
|
609
817
|
},
|
|
610
818
|
async test(name) {
|
|
611
819
|
const config = await readConfig();
|
|
@@ -616,9 +824,28 @@ function createAdk(fs, options = {}) {
|
|
|
616
824
|
const results = await Promise.allSettled(targets.map(async (r) => {
|
|
617
825
|
const url = registryUrl(r);
|
|
618
826
|
const rName = registryDisplayName(r);
|
|
827
|
+
if (typeof r !== "string" && r.authRequirement) {
|
|
828
|
+
const hasUsableAuth = r.auth && r.auth.type !== "none"
|
|
829
|
+
? (r.auth.type === "bearer" && !!r.auth.token) ||
|
|
830
|
+
(r.auth.type === "api-key" && !!r.auth.key)
|
|
831
|
+
: false;
|
|
832
|
+
if (!hasUsableAuth) {
|
|
833
|
+
return {
|
|
834
|
+
name: rName,
|
|
835
|
+
url,
|
|
836
|
+
status: "error",
|
|
837
|
+
error: `auth required — run: adk registry auth ${rName} --token <token>`,
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
}
|
|
619
841
|
try {
|
|
620
|
-
|
|
621
|
-
|
|
842
|
+
// Route through buildConsumer so encrypted auth/headers get
|
|
843
|
+
// decrypted, then use callWithRefresh so a 401 triggers the
|
|
844
|
+
// stored refresh token before giving up.
|
|
845
|
+
const disc = await callWithRefresh(rName, async () => {
|
|
846
|
+
const consumer = await buildConsumer(rName);
|
|
847
|
+
return consumer.discover(url);
|
|
848
|
+
});
|
|
622
849
|
return { name: rName, url, status: "active", issuer: disc.issuer };
|
|
623
850
|
}
|
|
624
851
|
catch (err) {
|
|
@@ -630,6 +857,259 @@ function createAdk(fs, options = {}) {
|
|
|
630
857
|
? r.value
|
|
631
858
|
: { name: "unknown", url: "unknown", status: "error", error: "unknown" });
|
|
632
859
|
},
|
|
860
|
+
async auth(nameOrUrl, credential) {
|
|
861
|
+
// Encrypt the secret value up-front so the write path is uniform;
|
|
862
|
+
// `buildConsumer` decrypts on the read side via `decryptConfigSecrets`.
|
|
863
|
+
const protectedValue = "token" in credential
|
|
864
|
+
? await protectSecret(credential.token)
|
|
865
|
+
: await protectSecret(credential.apiKey);
|
|
866
|
+
const updated = await updateRegistryEntry(nameOrUrl, (existing) => {
|
|
867
|
+
if ("token" in credential) {
|
|
868
|
+
existing.auth = {
|
|
869
|
+
type: "bearer",
|
|
870
|
+
token: protectedValue,
|
|
871
|
+
...(credential.tokenUrl && { tokenUrl: credential.tokenUrl }),
|
|
872
|
+
};
|
|
873
|
+
}
|
|
874
|
+
else {
|
|
875
|
+
existing.auth = {
|
|
876
|
+
type: "api-key",
|
|
877
|
+
key: protectedValue,
|
|
878
|
+
...(credential.header && { header: credential.header }),
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
delete existing.authRequirement;
|
|
882
|
+
});
|
|
883
|
+
if (updated)
|
|
884
|
+
await discoverProxyAfterAuth(nameOrUrl);
|
|
885
|
+
return updated;
|
|
886
|
+
},
|
|
887
|
+
async authLocal(nameOrUrl, opts) {
|
|
888
|
+
const config = await readConfig();
|
|
889
|
+
const target = findRegistry(config.registries ?? [], nameOrUrl);
|
|
890
|
+
if (!target || typeof target === "string") {
|
|
891
|
+
throw new adk_error_js_1.AdkError({
|
|
892
|
+
code: "registry_not_found",
|
|
893
|
+
message: `Registry not found: ${nameOrUrl}`,
|
|
894
|
+
hint: "Run `adk registry list` to see configured registries.",
|
|
895
|
+
details: { nameOrUrl },
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
// When the caller forces re-auth, wipe the existing credentials and
|
|
899
|
+
// re-probe so we know what scheme the registry wants now. Servers can
|
|
900
|
+
// rotate auth server metadata between runs.
|
|
901
|
+
if (opts?.force) {
|
|
902
|
+
await updateRegistryEntry(nameOrUrl, (existing) => {
|
|
903
|
+
delete existing.auth;
|
|
904
|
+
delete existing.oauth;
|
|
905
|
+
});
|
|
906
|
+
const fetchFn = options.fetch ?? globalThis.fetch;
|
|
907
|
+
const probe = await (0, mcp_client_js_1.probeRegistryAuth)(target.url, fetchFn);
|
|
908
|
+
if (probe.ok === false) {
|
|
909
|
+
await updateRegistryEntry(nameOrUrl, (existing) => {
|
|
910
|
+
existing.authRequirement = probe.requirement;
|
|
911
|
+
});
|
|
912
|
+
// Re-read so the flow below sees the fresh requirement.
|
|
913
|
+
const refreshed = await readConfig();
|
|
914
|
+
const refreshedTarget = findRegistry(refreshed.registries ?? [], nameOrUrl);
|
|
915
|
+
if (refreshedTarget && typeof refreshedTarget !== "string") {
|
|
916
|
+
Object.assign(target, refreshedTarget);
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
else if (probe.ok === true) {
|
|
920
|
+
// Registry no longer requires auth — nothing to do.
|
|
921
|
+
await updateRegistryEntry(nameOrUrl, (existing) => {
|
|
922
|
+
delete existing.authRequirement;
|
|
923
|
+
});
|
|
924
|
+
return { complete: true };
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
// Already authenticated — nothing to do (unless forced above).
|
|
928
|
+
const hasUsableAuth = target.auth && target.auth.type !== "none"
|
|
929
|
+
? (target.auth.type === "bearer" && !!target.auth.token) ||
|
|
930
|
+
(target.auth.type === "api-key" && !!target.auth.key)
|
|
931
|
+
: false;
|
|
932
|
+
if (hasUsableAuth && !target.authRequirement) {
|
|
933
|
+
return { complete: true };
|
|
934
|
+
}
|
|
935
|
+
const req = target.authRequirement;
|
|
936
|
+
const port = options.oauthCallbackPort ?? 8919;
|
|
937
|
+
const timeout = opts?.timeoutMs ?? 300_000;
|
|
938
|
+
const displayName = target.name ?? target.url;
|
|
939
|
+
const { createServer } = await Promise.resolve().then(() => __importStar(require("node:http")));
|
|
940
|
+
// OAuth path — the registry advertised authorization servers via
|
|
941
|
+
// RFC 9728 protected-resource metadata. Walk the full flow:
|
|
942
|
+
// AS metadata → dynamic client registration → PKCE authorize →
|
|
943
|
+
// local callback → token exchange → persist access token.
|
|
944
|
+
if (req?.authorizationServers?.length) {
|
|
945
|
+
const authServer = req.authorizationServers[0];
|
|
946
|
+
const metadata = (await (0, mcp_client_js_1.discoverOAuthMetadata)(authServer)) ??
|
|
947
|
+
(await tryFetchOAuthMetadata(authServer));
|
|
948
|
+
if (!metadata) {
|
|
949
|
+
throw new adk_error_js_1.AdkError({
|
|
950
|
+
code: "registry_oauth_discovery_failed",
|
|
951
|
+
message: `Could not discover OAuth metadata at ${authServer}.`,
|
|
952
|
+
hint: "The authorization server must expose /.well-known/oauth-authorization-server.",
|
|
953
|
+
details: { authServer, registry: displayName },
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
if (!metadata.registration_endpoint) {
|
|
957
|
+
throw new adk_error_js_1.AdkError({
|
|
958
|
+
code: "registry_oauth_no_registration",
|
|
959
|
+
message: `Authorization server ${authServer} does not support dynamic client registration.`,
|
|
960
|
+
hint: `Obtain a bearer token manually, then run: adk registry auth ${displayName} --token <token>`,
|
|
961
|
+
details: { authServer, registry: displayName },
|
|
962
|
+
});
|
|
963
|
+
}
|
|
964
|
+
const redirectUri = `http://localhost:${port}/callback`;
|
|
965
|
+
const registration = await (0, mcp_client_js_1.dynamicClientRegistration)(metadata.registration_endpoint, {
|
|
966
|
+
clientName: options.oauthClientName ?? "adk",
|
|
967
|
+
redirectUris: [redirectUri],
|
|
968
|
+
grantTypes: ["authorization_code"],
|
|
969
|
+
});
|
|
970
|
+
const state = crypto.randomUUID();
|
|
971
|
+
const { url: authorizeUrl, codeVerifier } = await (0, mcp_client_js_1.buildOAuthAuthorizeUrl)({
|
|
972
|
+
authorizationEndpoint: metadata.authorization_endpoint,
|
|
973
|
+
clientId: registration.clientId,
|
|
974
|
+
redirectUri,
|
|
975
|
+
scopes: req.scopes,
|
|
976
|
+
state,
|
|
977
|
+
});
|
|
978
|
+
return new Promise((resolve, reject) => {
|
|
979
|
+
const server = createServer(async (reqIn, resOut) => {
|
|
980
|
+
const reqUrl = new URL(reqIn.url ?? "/", `http://localhost:${port}`);
|
|
981
|
+
if (reqUrl.pathname !== "/callback") {
|
|
982
|
+
resOut.writeHead(404);
|
|
983
|
+
resOut.end();
|
|
984
|
+
return;
|
|
985
|
+
}
|
|
986
|
+
const code = reqUrl.searchParams.get("code");
|
|
987
|
+
const returnedState = reqUrl.searchParams.get("state");
|
|
988
|
+
if (!code || returnedState !== state) {
|
|
989
|
+
const error = reqUrl.searchParams.get("error") ?? "missing code/state";
|
|
990
|
+
resOut.writeHead(400, { "Content-Type": "text/html" });
|
|
991
|
+
resOut.end(`<h1>Error</h1><p>${esc(error)}</p>`);
|
|
992
|
+
server.close();
|
|
993
|
+
reject(new adk_error_js_1.AdkError({
|
|
994
|
+
code: "registry_oauth_denied",
|
|
995
|
+
message: `OAuth callback rejected: ${error}`,
|
|
996
|
+
hint: "Retry `adk registry auth` and complete the browser consent.",
|
|
997
|
+
details: { registry: displayName, error },
|
|
998
|
+
}));
|
|
999
|
+
return;
|
|
1000
|
+
}
|
|
1001
|
+
try {
|
|
1002
|
+
const tokens = await (0, mcp_client_js_1.exchangeCodeForTokens)(metadata.token_endpoint, {
|
|
1003
|
+
code,
|
|
1004
|
+
codeVerifier,
|
|
1005
|
+
clientId: registration.clientId,
|
|
1006
|
+
clientSecret: registration.clientSecret,
|
|
1007
|
+
redirectUri,
|
|
1008
|
+
});
|
|
1009
|
+
const expiresAt = tokens.expiresIn
|
|
1010
|
+
? new Date(Date.now() + tokens.expiresIn * 1000).toISOString()
|
|
1011
|
+
: undefined;
|
|
1012
|
+
const encToken = await protectSecret(tokens.accessToken);
|
|
1013
|
+
const encRefresh = tokens.refreshToken
|
|
1014
|
+
? await protectSecret(tokens.refreshToken)
|
|
1015
|
+
: undefined;
|
|
1016
|
+
const encClientSecret = registration.clientSecret
|
|
1017
|
+
? await protectSecret(registration.clientSecret)
|
|
1018
|
+
: undefined;
|
|
1019
|
+
await updateRegistryEntry(displayName, (existing) => {
|
|
1020
|
+
existing.auth = { type: "bearer", token: encToken };
|
|
1021
|
+
existing.oauth = {
|
|
1022
|
+
tokenEndpoint: metadata.token_endpoint,
|
|
1023
|
+
clientId: registration.clientId,
|
|
1024
|
+
...(encClientSecret && { clientSecret: encClientSecret }),
|
|
1025
|
+
...(encRefresh && { refreshToken: encRefresh }),
|
|
1026
|
+
...(expiresAt && { expiresAt }),
|
|
1027
|
+
...(req.scopes?.length && { scopes: req.scopes }),
|
|
1028
|
+
};
|
|
1029
|
+
delete existing.authRequirement;
|
|
1030
|
+
});
|
|
1031
|
+
await discoverProxyAfterAuth(displayName);
|
|
1032
|
+
resOut.writeHead(200, { "Content-Type": "text/html" });
|
|
1033
|
+
resOut.end(renderAuthSuccess(displayName));
|
|
1034
|
+
server.close();
|
|
1035
|
+
resolve({ complete: true });
|
|
1036
|
+
}
|
|
1037
|
+
catch (err) {
|
|
1038
|
+
resOut.writeHead(500, { "Content-Type": "text/html" });
|
|
1039
|
+
resOut.end(`<h1>Error</h1><p>${esc(err instanceof Error ? err.message : String(err))}</p>`);
|
|
1040
|
+
server.close();
|
|
1041
|
+
reject(err);
|
|
1042
|
+
}
|
|
1043
|
+
});
|
|
1044
|
+
server.listen(port, () => {
|
|
1045
|
+
opts?.onAuthorizeUrl?.(authorizeUrl);
|
|
1046
|
+
});
|
|
1047
|
+
const timer = setTimeout(() => {
|
|
1048
|
+
server.close();
|
|
1049
|
+
reject(new Error("OAuth callback timed out"));
|
|
1050
|
+
}, timeout);
|
|
1051
|
+
server.on("close", () => clearTimeout(timer));
|
|
1052
|
+
});
|
|
1053
|
+
}
|
|
1054
|
+
// No OAuth metadata — serve a local HTTPS form asking for a token.
|
|
1055
|
+
// Used when the registry returned 401 without pointing at an AS, or
|
|
1056
|
+
// when the caller simply wants to paste a pre-issued token.
|
|
1057
|
+
const fields = [
|
|
1058
|
+
{
|
|
1059
|
+
name: "token",
|
|
1060
|
+
label: "Bearer token",
|
|
1061
|
+
description: req?.realm
|
|
1062
|
+
? `Token for realm "${req.realm}"`
|
|
1063
|
+
: "Token sent as `Authorization: Bearer <token>`.",
|
|
1064
|
+
secret: true,
|
|
1065
|
+
},
|
|
1066
|
+
];
|
|
1067
|
+
return new Promise((resolve, reject) => {
|
|
1068
|
+
const server = createServer(async (reqIn, resOut) => {
|
|
1069
|
+
const reqUrl = new URL(reqIn.url ?? "/", `http://localhost:${port}`);
|
|
1070
|
+
if (reqIn.method === "GET" && reqUrl.pathname === "/auth") {
|
|
1071
|
+
resOut.writeHead(200, { "Content-Type": "text/html" });
|
|
1072
|
+
resOut.end(renderCredentialForm(displayName, fields));
|
|
1073
|
+
return;
|
|
1074
|
+
}
|
|
1075
|
+
if (reqIn.method === "POST" && reqUrl.pathname === "/auth") {
|
|
1076
|
+
const chunks = [];
|
|
1077
|
+
for await (const chunk of reqIn)
|
|
1078
|
+
chunks.push(chunk);
|
|
1079
|
+
const body = Buffer.concat(chunks).toString();
|
|
1080
|
+
const params = new URLSearchParams(body);
|
|
1081
|
+
const token = params.get("token");
|
|
1082
|
+
if (!token) {
|
|
1083
|
+
resOut.writeHead(200, { "Content-Type": "text/html" });
|
|
1084
|
+
resOut.end(renderCredentialForm(displayName, fields, "Token is required."));
|
|
1085
|
+
return;
|
|
1086
|
+
}
|
|
1087
|
+
try {
|
|
1088
|
+
await registry.auth(displayName, { token });
|
|
1089
|
+
resOut.writeHead(200, { "Content-Type": "text/html" });
|
|
1090
|
+
resOut.end(renderAuthSuccess(displayName));
|
|
1091
|
+
server.close();
|
|
1092
|
+
resolve({ complete: true });
|
|
1093
|
+
}
|
|
1094
|
+
catch (err) {
|
|
1095
|
+
resOut.writeHead(500, { "Content-Type": "text/html" });
|
|
1096
|
+
resOut.end(renderCredentialForm(displayName, fields, err instanceof Error ? err.message : String(err)));
|
|
1097
|
+
}
|
|
1098
|
+
return;
|
|
1099
|
+
}
|
|
1100
|
+
resOut.writeHead(404);
|
|
1101
|
+
resOut.end();
|
|
1102
|
+
});
|
|
1103
|
+
server.listen(port, () => {
|
|
1104
|
+
opts?.onAuthorizeUrl?.(`http://localhost:${port}/auth`);
|
|
1105
|
+
});
|
|
1106
|
+
const timer = setTimeout(() => {
|
|
1107
|
+
server.close();
|
|
1108
|
+
reject(new Error("Auth timed out"));
|
|
1109
|
+
}, timeout);
|
|
1110
|
+
server.on("close", () => clearTimeout(timer));
|
|
1111
|
+
});
|
|
1112
|
+
},
|
|
633
1113
|
};
|
|
634
1114
|
// ==========================================
|
|
635
1115
|
// Ref API
|