@ai-sdk/mcp 1.0.90 → 1.0.92

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/index.mjs CHANGED
@@ -835,8 +835,10 @@ function buildDiscoveryUrls(authorizationServerUrl) {
835
835
  type: "oidc",
836
836
  expectedIssuer: pathIssuer
837
837
  });
838
+ const oidcDiscoveryUrl = new URL(url.origin);
839
+ oidcDiscoveryUrl.pathname = `${pathname}/.well-known/openid-configuration`;
838
840
  urlsToTry.push({
839
- url: new URL(`${pathname}/.well-known/openid-configuration`, url.origin),
841
+ url: oidcDiscoveryUrl,
840
842
  type: "oidc",
841
843
  expectedIssuer: pathIssuer
842
844
  });
@@ -857,7 +859,13 @@ async function discoverAuthorizationServerMetadata(authorizationServerUrl, {
857
859
  } = {}) {
858
860
  var _a3;
859
861
  const headers = { "MCP-Protocol-Version": protocolVersion };
862
+ const authorizationServerOrigin = new URL(authorizationServerUrl).origin;
860
863
  const urlsToTry = buildDiscoveryUrls(authorizationServerUrl);
864
+ if (urlsToTry.some(({ url }) => url.origin !== authorizationServerOrigin)) {
865
+ throw new MCPClientOAuthError({
866
+ message: "OAuth discovery URL changed authorization-server origin"
867
+ });
868
+ }
861
869
  for (const { url: endpointUrl, type, expectedIssuer } of urlsToTry) {
862
870
  const response = await fetchWithCorsRetry(
863
871
  endpointUrl,
@@ -1190,8 +1198,9 @@ async function registerClient(authorizationServerUrl, {
1190
1198
  }
1191
1199
  async function auth(provider, options) {
1192
1200
  var _a3, _b3, _c;
1201
+ const refreshAttempt = {};
1193
1202
  try {
1194
- return await authInternal(provider, options);
1203
+ return await authInternal(provider, options, refreshAttempt);
1195
1204
  } catch (error) {
1196
1205
  if (error instanceof InvalidClientError || error instanceof UnauthorizedClientError) {
1197
1206
  if (options.authorizationCode !== void 0 || !await ((_a3 = provider.isClientInformationDynamicallyRegistered) == null ? void 0 : _a3.call(provider))) {
@@ -1199,8 +1208,10 @@ async function auth(provider, options) {
1199
1208
  }
1200
1209
  await ((_b3 = provider.invalidateCredentials) == null ? void 0 : _b3.call(provider, "all"));
1201
1210
  return await authInternal(provider, options);
1202
- } else if (error instanceof InvalidGrantError) {
1203
- await ((_c = provider.invalidateCredentials) == null ? void 0 : _c.call(provider, "tokens"));
1211
+ } else if (error instanceof InvalidGrantError && refreshAttempt.tokens) {
1212
+ await ((_c = provider.invalidateCredentials) == null ? void 0 : _c.call(provider, "tokens", {
1213
+ tokens: refreshAttempt.tokens
1214
+ }));
1204
1215
  return await authInternal(provider, options);
1205
1216
  }
1206
1217
  throw error;
@@ -1234,7 +1245,7 @@ async function authInternal(provider, {
1234
1245
  scope,
1235
1246
  resourceMetadataUrl,
1236
1247
  fetchFn
1237
- }) {
1248
+ }, refreshAttempt) {
1238
1249
  var _a3, _b3, _c;
1239
1250
  let resourceMetadata;
1240
1251
  let authorizationServerUrl;
@@ -1378,10 +1389,15 @@ async function authInternal(provider, {
1378
1389
  currentAuthorizationServerInformation
1379
1390
  });
1380
1391
  } else {
1381
- await ((_c = provider.invalidateCredentials) == null ? void 0 : _c.call(provider, "tokens"));
1392
+ await ((_c = provider.invalidateCredentials) == null ? void 0 : _c.call(provider, "tokens", {
1393
+ tokens: { ...tokens }
1394
+ }));
1382
1395
  }
1383
1396
  try {
1384
1397
  if (storedAuthorizationServerInformation) {
1398
+ if (refreshAttempt) {
1399
+ refreshAttempt.tokens = { ...tokens };
1400
+ }
1385
1401
  const newTokens = await refreshAuthorization(authorizationServerUrl, {
1386
1402
  metadata,
1387
1403
  clientInformation,