@mastra/mcp 1.0.0-beta.2 → 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
@@ -394,6 +394,8 @@ var InternalMastraMCPClient = class extends MastraBase {
394
394
  serverConfig;
395
395
  transport;
396
396
  currentOperationContext = null;
397
+ exitHookUnsubscribe;
398
+ sigTermHandler;
397
399
  /** Provides access to resource operations (list, read, subscribe, etc.) */
398
400
  resources;
399
401
  /** Provides access to prompt operations (list, get, notifications) */
@@ -561,14 +563,19 @@ var InternalMastraMCPClient = class extends MastraBase {
561
563
  reject(e);
562
564
  }
563
565
  });
564
- asyncExitHook(
565
- async () => {
566
- this.log("debug", `Disconnecting MCP server during exit`);
567
- await this.disconnect();
568
- },
569
- { wait: 5e3 }
570
- );
571
- 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
+ }
572
579
  this.log("debug", `Successfully connected to MCP server`);
573
580
  return this.isConnected;
574
581
  }
@@ -604,6 +611,14 @@ var InternalMastraMCPClient = class extends MastraBase {
604
611
  } finally {
605
612
  this.transport = void 0;
606
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
+ }
607
622
  }
608
623
  }
609
624
  async listResources() {
@@ -791,6 +806,9 @@ var InternalMastraMCPClient = class extends MastraBase {
791
806
  }
792
807
  );
793
808
  this.log("debug", `Tool executed successfully: ${tool.name}`);
809
+ if (res.structuredContent !== void 0) {
810
+ return res.structuredContent;
811
+ }
794
812
  return res;
795
813
  } catch (e) {
796
814
  this.log("error", `Error calling tool: ${tool.name}`, {
@@ -2999,7 +3017,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
2999
3017
  httpPath,
3000
3018
  req,
3001
3019
  res,
3002
- options = { sessionIdGenerator: () => randomUUID() }
3020
+ options
3003
3021
  }) {
3004
3022
  this.logger.debug(`startHTTP: Received ${req.method} request to ${url.pathname}`);
3005
3023
  if (url.pathname !== httpPath) {
@@ -3008,11 +3026,18 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3008
3026
  res.end();
3009
3027
  return;
3010
3028
  }
3011
- if (options?.serverless) {
3012
- 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)");
3013
3032
  await this.handleServerlessRequest(req, res);
3014
3033
  return;
3015
3034
  }
3035
+ const mergedOptions = {
3036
+ sessionIdGenerator: () => randomUUID(),
3037
+ // default: enabled
3038
+ ...options
3039
+ // user-provided overrides default
3040
+ };
3016
3041
  const sessionId = req.headers["mcp-session-id"];
3017
3042
  let transport;
3018
3043
  this.logger.debug(
@@ -3059,8 +3084,8 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3059
3084
  if (isInitializeRequest(body)) {
3060
3085
  this.logger.debug("startHTTP: Received Streamable HTTP initialize request, creating new transport.");
3061
3086
  transport = new StreamableHTTPServerTransport({
3062
- ...options,
3063
- sessionIdGenerator: () => randomUUID(),
3087
+ ...mergedOptions,
3088
+ sessionIdGenerator: mergedOptions.sessionIdGenerator,
3064
3089
  onsessioninitialized: (id) => {
3065
3090
  this.streamableHTTPTransports.set(id, transport);
3066
3091
  }