@mastra/mcp 1.0.0-beta.1 → 1.0.0-beta.3

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
@@ -366,6 +366,7 @@ var ResourceClientActions = class {
366
366
  };
367
367
 
368
368
  // src/client/client.ts
369
+ var DEFAULT_SERVER_CONNECT_TIMEOUT_MSEC = 3e3;
369
370
  function convertLogLevelToLoggerMethod(level) {
370
371
  switch (level) {
371
372
  case "debug":
@@ -393,6 +394,8 @@ var InternalMastraMCPClient = class extends MastraBase {
393
394
  serverConfig;
394
395
  transport;
395
396
  currentOperationContext = null;
397
+ exitHookUnsubscribe;
398
+ sigTermHandler;
396
399
  /** Provides access to resource operations (list, read, subscribe, etc.) */
397
400
  resources;
398
401
  /** Provides access to prompt operations (list, get, notifications) */
@@ -483,7 +486,7 @@ var InternalMastraMCPClient = class extends MastraBase {
483
486
  }
484
487
  }
485
488
  async connectHttp(url) {
486
- const { requestInit, eventSourceInit, authProvider } = this.serverConfig;
489
+ const { requestInit, eventSourceInit, authProvider, connectTimeout } = this.serverConfig;
487
490
  this.log("debug", `Attempting to connect to URL: ${url}`);
488
491
  let shouldTrySSE = url.pathname.endsWith(`/sse`);
489
492
  if (!shouldTrySSE) {
@@ -495,10 +498,7 @@ var InternalMastraMCPClient = class extends MastraBase {
495
498
  authProvider
496
499
  });
497
500
  await this.client.connect(streamableTransport, {
498
- timeout: (
499
- // this is hardcoded to 3s because the long default timeout would be extremely slow for sse backwards compat (60s)
500
- 3e3
501
- )
501
+ timeout: connectTimeout ?? DEFAULT_SERVER_CONNECT_TIMEOUT_MSEC
502
502
  });
503
503
  this.transport = streamableTransport;
504
504
  this.log("debug", "Successfully connected using Streamable HTTP transport.");
@@ -563,14 +563,19 @@ var InternalMastraMCPClient = class extends MastraBase {
563
563
  reject(e);
564
564
  }
565
565
  });
566
- asyncExitHook(
567
- async () => {
568
- this.log("debug", `Disconnecting MCP server during exit`);
569
- await this.disconnect();
570
- },
571
- { wait: 5e3 }
572
- );
573
- process.on("SIGTERM", () => gracefulExit());
566
+ if (!this.exitHookUnsubscribe) {
567
+ this.exitHookUnsubscribe = asyncExitHook(
568
+ async () => {
569
+ this.log("debug", `Disconnecting MCP server during exit`);
570
+ await this.disconnect();
571
+ },
572
+ { wait: 5e3 }
573
+ );
574
+ }
575
+ if (!this.sigTermHandler) {
576
+ this.sigTermHandler = () => gracefulExit();
577
+ process.on("SIGTERM", this.sigTermHandler);
578
+ }
574
579
  this.log("debug", `Successfully connected to MCP server`);
575
580
  return this.isConnected;
576
581
  }
@@ -606,6 +611,14 @@ var InternalMastraMCPClient = class extends MastraBase {
606
611
  } finally {
607
612
  this.transport = void 0;
608
613
  this.isConnected = Promise.resolve(false);
614
+ if (this.exitHookUnsubscribe) {
615
+ this.exitHookUnsubscribe();
616
+ this.exitHookUnsubscribe = void 0;
617
+ }
618
+ if (this.sigTermHandler) {
619
+ process.off("SIGTERM", this.sigTermHandler);
620
+ this.sigTermHandler = void 0;
621
+ }
609
622
  }
610
623
  }
611
624
  async listResources() {
@@ -793,6 +806,9 @@ var InternalMastraMCPClient = class extends MastraBase {
793
806
  }
794
807
  );
795
808
  this.log("debug", `Tool executed successfully: ${tool.name}`);
809
+ if (res.structuredContent !== void 0) {
810
+ return res.structuredContent;
811
+ }
796
812
  return res;
797
813
  } catch (e) {
798
814
  this.log("error", `Error calling tool: ${tool.name}`, {
@@ -1580,7 +1596,7 @@ To fix this you have three different options:
1580
1596
  }
1581
1597
  };
1582
1598
 
1583
- // ../../node_modules/.pnpm/hono@4.10.3/node_modules/hono/dist/utils/stream.js
1599
+ // ../../node_modules/.pnpm/hono@4.10.6/node_modules/hono/dist/utils/stream.js
1584
1600
  var StreamingApi = class {
1585
1601
  writer;
1586
1602
  encoder;
@@ -1647,7 +1663,7 @@ var StreamingApi = class {
1647
1663
  }
1648
1664
  };
1649
1665
 
1650
- // ../../node_modules/.pnpm/hono@4.10.3/node_modules/hono/dist/helper/streaming/utils.js
1666
+ // ../../node_modules/.pnpm/hono@4.10.6/node_modules/hono/dist/helper/streaming/utils.js
1651
1667
  var isOldBunVersion = () => {
1652
1668
  const version = typeof Bun !== "undefined" ? Bun.version : void 0;
1653
1669
  if (version === void 0) {
@@ -1658,7 +1674,7 @@ var isOldBunVersion = () => {
1658
1674
  return result;
1659
1675
  };
1660
1676
 
1661
- // ../../node_modules/.pnpm/hono@4.10.3/node_modules/hono/dist/utils/html.js
1677
+ // ../../node_modules/.pnpm/hono@4.10.6/node_modules/hono/dist/utils/html.js
1662
1678
  var HtmlEscapedCallbackPhase = {
1663
1679
  Stringify: 1};
1664
1680
  var resolveCallback = async (str, phase, preserveCallbacks, context, buffer) => {
@@ -1689,7 +1705,7 @@ var resolveCallback = async (str, phase, preserveCallbacks, context, buffer) =>
1689
1705
  }
1690
1706
  };
1691
1707
 
1692
- // ../../node_modules/.pnpm/hono@4.10.3/node_modules/hono/dist/helper/streaming/sse.js
1708
+ // ../../node_modules/.pnpm/hono@4.10.6/node_modules/hono/dist/helper/streaming/sse.js
1693
1709
  var SSEStreamingApi = class extends StreamingApi {
1694
1710
  constructor(writable, readable) {
1695
1711
  super(writable, readable);
@@ -3001,7 +3017,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3001
3017
  httpPath,
3002
3018
  req,
3003
3019
  res,
3004
- options = { sessionIdGenerator: () => randomUUID() }
3020
+ options
3005
3021
  }) {
3006
3022
  this.logger.debug(`startHTTP: Received ${req.method} request to ${url.pathname}`);
3007
3023
  if (url.pathname !== httpPath) {
@@ -3010,11 +3026,18 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3010
3026
  res.end();
3011
3027
  return;
3012
3028
  }
3013
- if (options?.serverless) {
3014
- this.logger.debug("startHTTP: Running in serverless (stateless) mode");
3029
+ const isStatelessMode = options?.serverless || options && "sessionIdGenerator" in options && options.sessionIdGenerator === void 0;
3030
+ if (isStatelessMode) {
3031
+ this.logger.debug("startHTTP: Running in stateless mode (serverless or sessionIdGenerator: undefined)");
3015
3032
  await this.handleServerlessRequest(req, res);
3016
3033
  return;
3017
3034
  }
3035
+ const mergedOptions = {
3036
+ sessionIdGenerator: () => randomUUID(),
3037
+ // default: enabled
3038
+ ...options
3039
+ // user-provided overrides default
3040
+ };
3018
3041
  const sessionId = req.headers["mcp-session-id"];
3019
3042
  let transport;
3020
3043
  this.logger.debug(
@@ -3061,8 +3084,8 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3061
3084
  if (isInitializeRequest(body)) {
3062
3085
  this.logger.debug("startHTTP: Received Streamable HTTP initialize request, creating new transport.");
3063
3086
  transport = new StreamableHTTPServerTransport({
3064
- ...options,
3065
- sessionIdGenerator: () => randomUUID(),
3087
+ ...mergedOptions,
3088
+ sessionIdGenerator: mergedOptions.sessionIdGenerator,
3066
3089
  onsessioninitialized: (id) => {
3067
3090
  this.streamableHTTPTransports.set(id, transport);
3068
3091
  }