@ai-sdk/mcp 1.0.49 → 1.0.50
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/CHANGELOG.md +6 -0
- package/dist/index.js +26 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +26 -8
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/tool/oauth.ts +30 -4
package/dist/index.mjs
CHANGED
|
@@ -692,15 +692,18 @@ async function discoverOAuthProtectedResourceMetadata(serverUrl, opts, fetchFn =
|
|
|
692
692
|
function buildDiscoveryUrls(authorizationServerUrl) {
|
|
693
693
|
const url = typeof authorizationServerUrl === "string" ? new URL(authorizationServerUrl) : authorizationServerUrl;
|
|
694
694
|
const hasPath = url.pathname !== "/";
|
|
695
|
+
const rootIssuer = url.origin;
|
|
695
696
|
const urlsToTry = [];
|
|
696
697
|
if (!hasPath) {
|
|
697
698
|
urlsToTry.push({
|
|
698
699
|
url: new URL("/.well-known/oauth-authorization-server", url.origin),
|
|
699
|
-
type: "oauth"
|
|
700
|
+
type: "oauth",
|
|
701
|
+
expectedIssuer: rootIssuer
|
|
700
702
|
});
|
|
701
703
|
urlsToTry.push({
|
|
702
704
|
url: new URL("/.well-known/openid-configuration", url.origin),
|
|
703
|
-
type: "oidc"
|
|
705
|
+
type: "oidc",
|
|
706
|
+
expectedIssuer: rootIssuer
|
|
704
707
|
});
|
|
705
708
|
return urlsToTry;
|
|
706
709
|
}
|
|
@@ -708,27 +711,39 @@ function buildDiscoveryUrls(authorizationServerUrl) {
|
|
|
708
711
|
if (pathname.endsWith("/")) {
|
|
709
712
|
pathname = pathname.slice(0, -1);
|
|
710
713
|
}
|
|
714
|
+
const pathIssuer = `${url.origin}${pathname}`;
|
|
711
715
|
urlsToTry.push({
|
|
712
716
|
url: new URL(
|
|
713
717
|
`/.well-known/oauth-authorization-server${pathname}`,
|
|
714
718
|
url.origin
|
|
715
719
|
),
|
|
716
|
-
type: "oauth"
|
|
720
|
+
type: "oauth",
|
|
721
|
+
expectedIssuer: pathIssuer
|
|
717
722
|
});
|
|
718
723
|
urlsToTry.push({
|
|
719
724
|
url: new URL("/.well-known/oauth-authorization-server", url.origin),
|
|
720
|
-
type: "oauth"
|
|
725
|
+
type: "oauth",
|
|
726
|
+
expectedIssuer: rootIssuer
|
|
721
727
|
});
|
|
722
728
|
urlsToTry.push({
|
|
723
729
|
url: new URL(`/.well-known/openid-configuration${pathname}`, url.origin),
|
|
724
|
-
type: "oidc"
|
|
730
|
+
type: "oidc",
|
|
731
|
+
expectedIssuer: pathIssuer
|
|
725
732
|
});
|
|
726
733
|
urlsToTry.push({
|
|
727
734
|
url: new URL(`${pathname}/.well-known/openid-configuration`, url.origin),
|
|
728
|
-
type: "oidc"
|
|
735
|
+
type: "oidc",
|
|
736
|
+
expectedIssuer: pathIssuer
|
|
729
737
|
});
|
|
730
738
|
return urlsToTry;
|
|
731
739
|
}
|
|
740
|
+
function assertMetadataIssuerMatches(metadata, expectedIssuer) {
|
|
741
|
+
if (metadata.issuer !== expectedIssuer) {
|
|
742
|
+
throw new MCPClientOAuthError({
|
|
743
|
+
message: `OAuth authorization server metadata issuer ${metadata.issuer} does not match expected issuer ${expectedIssuer}`
|
|
744
|
+
});
|
|
745
|
+
}
|
|
746
|
+
}
|
|
732
747
|
async function discoverAuthorizationServerMetadata(authorizationServerUrl, {
|
|
733
748
|
fetchFn = fetch,
|
|
734
749
|
protocolVersion = LATEST_PROTOCOL_VERSION
|
|
@@ -736,7 +751,7 @@ async function discoverAuthorizationServerMetadata(authorizationServerUrl, {
|
|
|
736
751
|
var _a3;
|
|
737
752
|
const headers = { "MCP-Protocol-Version": protocolVersion };
|
|
738
753
|
const urlsToTry = buildDiscoveryUrls(authorizationServerUrl);
|
|
739
|
-
for (const { url: endpointUrl, type } of urlsToTry) {
|
|
754
|
+
for (const { url: endpointUrl, type, expectedIssuer } of urlsToTry) {
|
|
740
755
|
const response = await fetchWithCorsRetry(endpointUrl, headers, fetchFn);
|
|
741
756
|
if (!response) {
|
|
742
757
|
continue;
|
|
@@ -750,11 +765,14 @@ async function discoverAuthorizationServerMetadata(authorizationServerUrl, {
|
|
|
750
765
|
);
|
|
751
766
|
}
|
|
752
767
|
if (type === "oauth") {
|
|
753
|
-
|
|
768
|
+
const metadata = OAuthMetadataSchema.parse(await response.json());
|
|
769
|
+
assertMetadataIssuerMatches(metadata, expectedIssuer);
|
|
770
|
+
return metadata;
|
|
754
771
|
} else {
|
|
755
772
|
const metadata = OpenIdProviderDiscoveryMetadataSchema.parse(
|
|
756
773
|
await response.json()
|
|
757
774
|
);
|
|
775
|
+
assertMetadataIssuerMatches(metadata, expectedIssuer);
|
|
758
776
|
if (!((_a3 = metadata.code_challenge_methods_supported) == null ? void 0 : _a3.includes("S256"))) {
|
|
759
777
|
throw new Error(
|
|
760
778
|
`Incompatible OIDC provider at ${endpointUrl}: does not support S256 code challenge method required by MCP specification`
|