@ai-sdk/mcp 1.0.41 → 1.0.43

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
@@ -1085,10 +1085,11 @@ var SseMCPTransport = class {
1085
1085
  this.fetchFn = fetchFn != null ? fetchFn : globalThis.fetch;
1086
1086
  }
1087
1087
  async commonHeaders(base) {
1088
+ var _a3;
1088
1089
  const headers = {
1089
1090
  ...this.headers,
1090
1091
  ...base,
1091
- "mcp-protocol-version": LATEST_PROTOCOL_VERSION
1092
+ "mcp-protocol-version": (_a3 = this.protocolVersion) != null ? _a3 : LATEST_PROTOCOL_VERSION
1092
1093
  };
1093
1094
  if (this.authProvider) {
1094
1095
  const tokens = await this.authProvider.tokens();
@@ -1303,10 +1304,11 @@ var HttpMCPTransport = class {
1303
1304
  this.fetchFn = fetchFn != null ? fetchFn : globalThis.fetch;
1304
1305
  }
1305
1306
  async commonHeaders(base) {
1307
+ var _a3;
1306
1308
  const headers = {
1307
1309
  ...this.headers,
1308
1310
  ...base,
1309
- "mcp-protocol-version": LATEST_PROTOCOL_VERSION
1311
+ "mcp-protocol-version": (_a3 = this.protocolVersion) != null ? _a3 : LATEST_PROTOCOL_VERSION
1310
1312
  };
1311
1313
  if (this.sessionId) {
1312
1314
  headers["mcp-session-id"] = this.sessionId;
@@ -1323,6 +1325,24 @@ var HttpMCPTransport = class {
1323
1325
  getRuntimeEnvironmentUserAgent2()
1324
1326
  );
1325
1327
  }
1328
+ /**
1329
+ * Runs a single OAuth recovery flow for concurrent 401 responses.
1330
+ */
1331
+ authorizeOnce(resourceMetadataUrl) {
1332
+ if (!this.authProvider) {
1333
+ return Promise.resolve("REDIRECT");
1334
+ }
1335
+ if (!this.authPromise) {
1336
+ this.authPromise = auth(this.authProvider, {
1337
+ serverUrl: this.url,
1338
+ resourceMetadataUrl,
1339
+ fetchFn: this.fetchFn
1340
+ }).finally(() => {
1341
+ this.authPromise = void 0;
1342
+ });
1343
+ }
1344
+ return this.authPromise;
1345
+ }
1326
1346
  async start() {
1327
1347
  if (this.abortController) {
1328
1348
  throw new MCPClientError({
@@ -1373,11 +1393,7 @@ var HttpMCPTransport = class {
1373
1393
  if (response.status === 401 && this.authProvider && !triedAuth) {
1374
1394
  this.resourceMetadataUrl = extractResourceMetadataUrl(response);
1375
1395
  try {
1376
- const result = await auth(this.authProvider, {
1377
- serverUrl: this.url,
1378
- resourceMetadataUrl: this.resourceMetadataUrl,
1379
- fetchFn: this.fetchFn
1380
- });
1396
+ const result = await this.authorizeOnce(this.resourceMetadataUrl);
1381
1397
  if (result !== "AUTHORIZED") {
1382
1398
  const error2 = new UnauthorizedError();
1383
1399
  throw error2;
@@ -1523,11 +1539,7 @@ var HttpMCPTransport = class {
1523
1539
  if (response.status === 401 && this.authProvider && !triedAuth) {
1524
1540
  this.resourceMetadataUrl = extractResourceMetadataUrl(response);
1525
1541
  try {
1526
- const result = await auth(this.authProvider, {
1527
- serverUrl: this.url,
1528
- resourceMetadataUrl: this.resourceMetadataUrl,
1529
- fetchFn: this.fetchFn
1530
- });
1542
+ const result = await this.authorizeOnce(this.resourceMetadataUrl);
1531
1543
  if (result !== "AUTHORIZED") {
1532
1544
  const error = new UnauthorizedError();
1533
1545
  (_b3 = this.onerror) == null ? void 0 : _b3.call(this, error);
@@ -1695,6 +1707,9 @@ var DefaultMCPClient = class {
1695
1707
  get serverInfo() {
1696
1708
  return this._serverInfo;
1697
1709
  }
1710
+ get instructions() {
1711
+ return this._serverInstructions;
1712
+ }
1698
1713
  async init() {
1699
1714
  try {
1700
1715
  await this.transport.start();
@@ -1722,6 +1737,8 @@ var DefaultMCPClient = class {
1722
1737
  }
1723
1738
  this.serverCapabilities = result.capabilities;
1724
1739
  this._serverInfo = result.serverInfo;
1740
+ this._serverInstructions = result.instructions;
1741
+ this.transport.protocolVersion = result.protocolVersion;
1725
1742
  await this.notification({
1726
1743
  method: "notifications/initialized"
1727
1744
  });