@ai-sdk/mcp 1.0.80 → 1.0.82

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
@@ -1172,9 +1172,11 @@ async function authInternal(provider, {
1172
1172
  resourceMetadataUrl,
1173
1173
  fetchFn
1174
1174
  }) {
1175
- var _a3, _b3;
1175
+ var _a3, _b3, _c;
1176
1176
  let resourceMetadata;
1177
1177
  let authorizationServerUrl;
1178
+ let clientInformation;
1179
+ let callbackAuthorizationServerInformation;
1178
1180
  assertResourceMetadataUrlSameOrigin(serverUrl, resourceMetadataUrl);
1179
1181
  try {
1180
1182
  resourceMetadata = await discoverOAuthProtectedResourceMetadata(
@@ -1187,15 +1189,24 @@ async function authInternal(provider, {
1187
1189
  }
1188
1190
  } catch (e) {
1189
1191
  }
1192
+ if (authorizationCode !== void 0) {
1193
+ clientInformation = await Promise.resolve(provider.clientInformation());
1194
+ if (clientInformation) {
1195
+ callbackAuthorizationServerInformation = await getStoredAuthorizationServerInformation({
1196
+ provider,
1197
+ clientInformation
1198
+ });
1199
+ }
1200
+ }
1190
1201
  if (!authorizationServerUrl) {
1191
- authorizationServerUrl = serverUrl;
1202
+ authorizationServerUrl = (_a3 = callbackAuthorizationServerInformation == null ? void 0 : callbackAuthorizationServerInformation.authorizationServerUrl) != null ? _a3 : serverUrl;
1192
1203
  }
1193
1204
  const resource = await selectResourceURL(
1194
1205
  serverUrl,
1195
1206
  provider,
1196
1207
  resourceMetadata
1197
1208
  );
1198
- await ((_a3 = provider.validateAuthorizationServerURL) == null ? void 0 : _a3.call(
1209
+ await ((_b3 = provider.validateAuthorizationServerURL) == null ? void 0 : _b3.call(
1199
1210
  provider,
1200
1211
  serverUrl,
1201
1212
  authorizationServerUrl
@@ -1213,7 +1224,9 @@ async function authInternal(provider, {
1213
1224
  resourceMetadata,
1214
1225
  clientMetadata
1215
1226
  });
1216
- let clientInformation = await Promise.resolve(provider.clientInformation());
1227
+ if (authorizationCode === void 0) {
1228
+ clientInformation = await Promise.resolve(provider.clientInformation());
1229
+ }
1217
1230
  if (!clientInformation) {
1218
1231
  if (authorizationCode !== void 0) {
1219
1232
  throw new Error(
@@ -1248,7 +1261,7 @@ async function authInternal(provider, {
1248
1261
  );
1249
1262
  }
1250
1263
  }
1251
- const storedAuthorizationServerInformation = await getStoredAuthorizationServerInformation({
1264
+ const storedAuthorizationServerInformation = callbackAuthorizationServerInformation != null ? callbackAuthorizationServerInformation : await getStoredAuthorizationServerInformation({
1252
1265
  provider,
1253
1266
  clientInformation
1254
1267
  });
@@ -1293,7 +1306,7 @@ async function authInternal(provider, {
1293
1306
  currentAuthorizationServerInformation
1294
1307
  });
1295
1308
  } else {
1296
- await ((_b3 = provider.invalidateCredentials) == null ? void 0 : _b3.call(provider, "tokens"));
1309
+ await ((_c = provider.invalidateCredentials) == null ? void 0 : _c.call(provider, "tokens"));
1297
1310
  }
1298
1311
  try {
1299
1312
  if (storedAuthorizationServerInformation) {
@@ -1394,6 +1407,30 @@ var SseMCPTransport = class {
1394
1407
  getRuntimeEnvironmentUserAgent()
1395
1408
  );
1396
1409
  }
1410
+ /**
1411
+ * Runs a single OAuth recovery flow for concurrent 401 responses.
1412
+ */
1413
+ authorizeOnce(resourceMetadataUrl, scope) {
1414
+ if (!this.authProvider) {
1415
+ return Promise.resolve("REDIRECT");
1416
+ }
1417
+ if (!this.authPromise) {
1418
+ this.authPromise = auth(this.authProvider, {
1419
+ serverUrl: this.url,
1420
+ resourceMetadataUrl,
1421
+ scope,
1422
+ fetchFn: this.fetchFn
1423
+ }).finally(() => {
1424
+ this.authPromise = void 0;
1425
+ });
1426
+ }
1427
+ return this.authPromise;
1428
+ }
1429
+ async accessTokenChanged(requestAuthorization) {
1430
+ var _a3, _b3;
1431
+ const accessToken = (_b3 = await ((_a3 = this.authProvider) == null ? void 0 : _a3.tokens())) == null ? void 0 : _b3.access_token;
1432
+ return accessToken != null && requestAuthorization !== `Bearer ${accessToken}`;
1433
+ }
1397
1434
  async start() {
1398
1435
  return new Promise((resolve, reject) => {
1399
1436
  if (this.connected) {
@@ -1415,12 +1452,10 @@ var SseMCPTransport = class {
1415
1452
  const { resourceMetadataUrl, scope } = extractWWWAuthenticateParams(response);
1416
1453
  this.resourceMetadataUrl = resourceMetadataUrl;
1417
1454
  try {
1418
- const result = await auth(this.authProvider, {
1419
- serverUrl: this.url,
1420
- resourceMetadataUrl: this.resourceMetadataUrl,
1421
- scope,
1422
- fetchFn: this.fetchFn
1423
- });
1455
+ const result = await this.authorizeOnce(
1456
+ this.resourceMetadataUrl,
1457
+ scope
1458
+ );
1424
1459
  if (result !== "AUTHORIZED") {
1425
1460
  const error = new UnauthorizedError();
1426
1461
  (_b3 = this.onerror) == null ? void 0 : _b3.call(this, error);
@@ -1533,7 +1568,7 @@ var SseMCPTransport = class {
1533
1568
  const transportSignal = (_b3 = this.abortController) == null ? void 0 : _b3.signal;
1534
1569
  const requestSignal = (options == null ? void 0 : options.signal) == null ? transportSignal : transportSignal == null ? options.signal : AbortSignal.any([transportSignal, options.signal]);
1535
1570
  const attempt = async (triedAuth = false) => {
1536
- var _a4, _b4;
1571
+ var _a4, _b4, _c;
1537
1572
  try {
1538
1573
  const headers = await this.commonHeaders({
1539
1574
  "Content-Type": "application/json"
@@ -1547,14 +1582,17 @@ var SseMCPTransport = class {
1547
1582
  };
1548
1583
  const response = await this.fetchFn(endpoint.href, init);
1549
1584
  if (response.status === 401 && this.authProvider && !triedAuth) {
1585
+ if (await this.accessTokenChanged(
1586
+ (_a4 = new Headers(headers).get("authorization")) != null ? _a4 : void 0
1587
+ )) {
1588
+ return attempt(true);
1589
+ }
1550
1590
  const { resourceMetadataUrl, scope } = extractWWWAuthenticateParams(response);
1551
1591
  this.resourceMetadataUrl = resourceMetadataUrl;
1552
- const result = await auth(this.authProvider, {
1553
- serverUrl: this.url,
1554
- resourceMetadataUrl: this.resourceMetadataUrl,
1555
- scope,
1556
- fetchFn: this.fetchFn
1557
- });
1592
+ const result = await this.authorizeOnce(
1593
+ this.resourceMetadataUrl,
1594
+ scope
1595
+ );
1558
1596
  if (result !== "AUTHORIZED") {
1559
1597
  throw new UnauthorizedError();
1560
1598
  }
@@ -1571,10 +1609,10 @@ var SseMCPTransport = class {
1571
1609
  throw error;
1572
1610
  }
1573
1611
  } catch (error) {
1574
- if ((_a4 = options == null ? void 0 : options.signal) == null ? void 0 : _a4.aborted) {
1612
+ if ((_b4 = options == null ? void 0 : options.signal) == null ? void 0 : _b4.aborted) {
1575
1613
  throw error;
1576
1614
  }
1577
- (_b4 = this.onerror) == null ? void 0 : _b4.call(this, error);
1615
+ (_c = this.onerror) == null ? void 0 : _c.call(this, error);
1578
1616
  throw error;
1579
1617
  }
1580
1618
  };