@mastra/mcp 1.15.1-alpha.0 → 1.15.1

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.js CHANGED
@@ -9890,7 +9890,7 @@ function toStandardSchema$4(schema) {
9890
9890
  return new AiSdkSchemaWrapper(schema);
9891
9891
  }
9892
9892
  //#endregion
9893
- //#region ../schema-compat/dist/json-schema-DwHzF7au.js
9893
+ //#region ../schema-compat/dist/json-schema-BstKYbJi.js
9894
9894
  var require_code$1 = /* @__PURE__ */ __commonJSMin$1(((exports) => {
9895
9895
  Object.defineProperty(exports, "__esModule", { value: true });
9896
9896
  exports.regexpCode = exports.getEsmExportName = exports.getProperty = exports.safeStringify = exports.stringify = exports.strConcat = exports.addCodeArg = exports.str = exports._ = exports.nil = exports._Code = exports.Name = exports.IDENTIFIER = exports._CodeOrName = void 0;
@@ -13063,7 +13063,10 @@ var require_fast_uri = /* @__PURE__ */ __commonJSMin$1(((exports, module) => {
13063
13063
  */
13064
13064
  function resolve(baseURI, relativeURI, options) {
13065
13065
  const schemelessOptions = options ? Object.assign({ scheme: "null" }, options) : { scheme: "null" };
13066
- const resolved = resolveComponent(parse(baseURI, schemelessOptions), parse(relativeURI, schemelessOptions), schemelessOptions, true);
13066
+ const { parsed: baseParsed, malformedAuthorityOrPort: baseMalformed } = parseWithStatus(baseURI, schemelessOptions);
13067
+ const { parsed: relativeParsed, malformedAuthorityOrPort: relativeMalformed } = parseWithStatus(relativeURI, schemelessOptions);
13068
+ if (baseMalformed || relativeMalformed) throw new Error(baseParsed.error || relativeParsed.error || "URI is malformed.");
13069
+ const resolved = resolveComponent(baseParsed, relativeParsed, schemelessOptions, true);
13067
13070
  schemelessOptions.skipEscape = true;
13068
13071
  return serialize(resolved, schemelessOptions);
13069
13072
  }
@@ -13180,6 +13183,7 @@ var require_fast_uri = /* @__PURE__ */ __commonJSMin$1(((exports, module) => {
13180
13183
  }
13181
13184
  const URI_PARSE = /^(?:([^#/:?]+):)?(?:\/\/((?:([^#/?@]*)@)?(\[[^#/?\]]+\]|[^#/:?]*)(?::(\d*))?))?([^#?]*)(?:\?([^#]*))?(?:#((?:.|[\n\r])*))?/u;
13182
13185
  const AUTHORITY_PREFIX = /^(?:[^#/:?]+:)?\/\/([^/?#]*)/;
13186
+ const AUTHORITY_INTRODUCER_REGION = /^(?:[^#/:?]+:)?([/\\\t\n\r]*)/;
13183
13187
  /**
13184
13188
  * @param {import('./types/index').URIComponent} parsed
13185
13189
  * @param {RegExpMatchArray} matches
@@ -13215,6 +13219,20 @@ var require_fast_uri = /* @__PURE__ */ __commonJSMin$1(((exports, module) => {
13215
13219
  parsed.error = "URI authority must not contain a literal backslash.";
13216
13220
  malformedAuthorityOrPort = true;
13217
13221
  }
13222
+ const introducerMatch = uri.match(AUTHORITY_INTRODUCER_REGION);
13223
+ if (introducerMatch !== null) {
13224
+ const region = introducerMatch[1];
13225
+ const normalizedRegion = region.replace(/[\t\n\r]/g, "");
13226
+ if (normalizedRegion.length >= 2) {
13227
+ if (normalizedRegion.slice(0, 2) !== "//") {
13228
+ parsed.error = parsed.error || "URI authority must not contain a literal backslash.";
13229
+ malformedAuthorityOrPort = true;
13230
+ } else if (region.length !== normalizedRegion.length) {
13231
+ parsed.error = parsed.error || "URI authority introducer must not contain whitespace.";
13232
+ malformedAuthorityOrPort = true;
13233
+ }
13234
+ }
13235
+ }
13218
13236
  const matches = uri.match(URI_PARSE);
13219
13237
  if (matches) {
13220
13238
  parsed.scheme = matches[1];
@@ -16580,7 +16598,7 @@ function toStandardSchema$2(zodSchema) {
16580
16598
  return wrapper;
16581
16599
  }
16582
16600
  //#endregion
16583
- //#region ../schema-compat/dist/schema-Ba6HLBWs.js
16601
+ //#region ../schema-compat/dist/schema-Bv9OdW1f.js
16584
16602
  /**
16585
16603
  * Supported JSON Schema targets for z.toJSONSchema().
16586
16604
  * Works with both real Zod v4 and Zod 3.25's v4 compat layer.
@@ -21451,6 +21469,8 @@ var InternalMastraMCPClient = class extends MastraBase {
21451
21469
  }
21452
21470
  }
21453
21471
  isConnected = null;
21472
+ reconnectPromise = null;
21473
+ lifecycleGeneration = 0;
21454
21474
  /**
21455
21475
  * Connects to the MCP server using the configured transport.
21456
21476
  *
@@ -21548,6 +21568,9 @@ var InternalMastraMCPClient = class extends MastraBase {
21548
21568
  this.serverInstructions = this.client.getInstructions();
21549
21569
  }
21550
21570
  async disconnect() {
21571
+ this.lifecycleGeneration++;
21572
+ const reconnectPromise = this.reconnectPromise;
21573
+ if (reconnectPromise) await reconnectPromise.catch(() => {});
21551
21574
  this.closePendingAuthTransport();
21552
21575
  if (!this.transport) {
21553
21576
  await this.detachStaleClientTransport();
@@ -21593,21 +21616,52 @@ var InternalMastraMCPClient = class extends MastraBase {
21593
21616
  * @internal
21594
21617
  */
21595
21618
  async forceReconnect() {
21596
- this.log("debug", "Forcing reconnection to MCP server...");
21597
- this.closePendingAuthTransport();
21598
- const disconnectedTransport = this.transport;
21619
+ if (this.reconnectPromise) {
21620
+ this.log("debug", "Reconnection already in progress; waiting for it to complete");
21621
+ return await this.reconnectPromise;
21622
+ }
21623
+ const reconnectPromise = (async () => {
21624
+ this.log("debug", "Forcing reconnection to MCP server...");
21625
+ this.closePendingAuthTransport();
21626
+ const disconnectedTransport = this.transport;
21627
+ try {
21628
+ if (disconnectedTransport) await disconnectedTransport.close();
21629
+ } catch (e) {
21630
+ this.log("debug", "Error during force disconnect (ignored)", { error: e instanceof Error ? e.message : String(e) });
21631
+ } finally {
21632
+ if (disconnectedTransport) this.severClientTransportLink(disconnectedTransport);
21633
+ }
21634
+ if (!disconnectedTransport || this.transport === disconnectedTransport) {
21635
+ this.transport = void 0;
21636
+ this.isConnected = null;
21637
+ this.serverInstructions = void 0;
21638
+ }
21639
+ await this.connect();
21640
+ this.log("debug", "Successfully reconnected to MCP server");
21641
+ })();
21642
+ this.reconnectPromise = reconnectPromise;
21599
21643
  try {
21600
- if (disconnectedTransport) await disconnectedTransport.close();
21601
- } catch (e) {
21602
- this.log("debug", "Error during force disconnect (ignored)", { error: e instanceof Error ? e.message : String(e) });
21644
+ await reconnectPromise;
21603
21645
  } finally {
21604
- if (disconnectedTransport) this.severClientTransportLink(disconnectedTransport);
21646
+ if (this.reconnectPromise === reconnectPromise) this.reconnectPromise = null;
21647
+ }
21648
+ }
21649
+ async reconnectAfterTransportFailure(failedTransport, lifecycleGeneration) {
21650
+ while (true) {
21651
+ if (this.lifecycleGeneration !== lifecycleGeneration) throw new Error("MCP client was disconnected while recovering the failed transport");
21652
+ const reconnectPromise = this.reconnectPromise;
21653
+ if (reconnectPromise) {
21654
+ await reconnectPromise;
21655
+ continue;
21656
+ }
21657
+ if (failedTransport && this.transport && this.transport !== failedTransport) {
21658
+ this.log("debug", "Connection was already replaced after the failed operation; skipping reconnect");
21659
+ return;
21660
+ }
21661
+ await this.forceReconnect();
21662
+ if (this.lifecycleGeneration !== lifecycleGeneration) throw new Error("MCP client was disconnected while recovering the failed transport");
21663
+ return;
21605
21664
  }
21606
- this.transport = void 0;
21607
- this.isConnected = null;
21608
- this.serverInstructions = void 0;
21609
- await this.connect();
21610
- this.log("debug", "Successfully reconnected to MCP server");
21611
21665
  }
21612
21666
  async listResources() {
21613
21667
  this.log("debug", `Requesting resources from MCP server`);
@@ -21809,13 +21863,15 @@ var InternalMastraMCPClient = class extends MastraBase {
21809
21863
  if (res.structuredContent !== void 0) return res.structuredContent;
21810
21864
  return res;
21811
21865
  };
21866
+ const failedTransport = this.transport;
21867
+ const lifecycleGeneration = this.lifecycleGeneration;
21812
21868
  try {
21813
21869
  return await executeToolCall();
21814
21870
  } catch (e) {
21815
21871
  if (isReconnectableMCPError(e)) {
21816
21872
  this.log("debug", `Session error detected for tool ${tool.name}, attempting reconnection...`, { error: e instanceof Error ? e.message : String(e) });
21817
21873
  try {
21818
- await this.forceReconnect();
21874
+ await this.reconnectAfterTransportFailure(failedTransport, lifecycleGeneration);
21819
21875
  this.log("debug", `Retrying tool ${tool.name} after reconnection...`);
21820
21876
  return await executeToolCall();
21821
21877
  } catch (reconnectError) {
@@ -23727,7 +23783,7 @@ var streamSSE = (c, cb, onError) => {
23727
23783
  return c.newResponse(stream.responseReadable);
23728
23784
  };
23729
23785
  //#endregion
23730
- //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@/hono-mcp-server-sse-transport/0.0.7/cca448b28e692a7c0ee32da4a941ecaa73adf28248dca4347dd108a429e51b1e/node_modules/hono-mcp-server-sse-transport/build/sse.js
23786
+ //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@/hono-mcp-server-sse-transport/0.0.7/518df2f53e0bbb46e48def5118fc7f6b5bba3a268854c8a5c7e83260299d376e/node_modules/hono-mcp-server-sse-transport/build/sse.js
23731
23787
  const MAXIMUM_MESSAGE_SIZE = 4 * 1024 * 1024;
23732
23788
  var SSETransport = class {
23733
23789
  messageUrl;