@mastra/mcp 1.0.3 → 1.1.0-alpha.0

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
@@ -1,3 +1,4 @@
1
+ import { AsyncLocalStorage } from 'async_hooks';
1
2
  import $RefParser from '@apidevtools/json-schema-ref-parser';
2
3
  import { MastraBase } from '@mastra/core/base';
3
4
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
@@ -413,7 +414,7 @@ var InternalMastraMCPClient = class extends MastraBase {
413
414
  enableProgressTracking;
414
415
  serverConfig;
415
416
  transport;
416
- currentOperationContext = null;
417
+ operationContextStore = new AsyncLocalStorage();
417
418
  exitHookUnsubscribe;
418
419
  sigTermHandler;
419
420
  _roots;
@@ -485,7 +486,7 @@ var InternalMastraMCPClient = class extends MastraBase {
485
486
  timestamp: /* @__PURE__ */ new Date(),
486
487
  serverName: this.name,
487
488
  details,
488
- requestContext: this.currentOperationContext
489
+ requestContext: this.operationContextStore.getStore() ?? null
489
490
  });
490
491
  }
491
492
  }
@@ -578,7 +579,8 @@ var InternalMastraMCPClient = class extends MastraBase {
578
579
  }
579
580
  }
580
581
  async connectHttp(url) {
581
- const { requestInit, eventSourceInit, authProvider, connectTimeout, fetch: fetch2 } = this.serverConfig;
582
+ const { requestInit, eventSourceInit, authProvider, connectTimeout, fetch: userFetch } = this.serverConfig;
583
+ const fetch2 = userFetch ? (url2, init) => userFetch(url2, init, this.operationContextStore.getStore() ?? null) : void 0;
582
584
  this.log("debug", `Attempting to connect to URL: ${url}`);
583
585
  let shouldTrySSE = url.pathname.endsWith(`/sse`);
584
586
  if (!shouldTrySSE) {
@@ -975,66 +977,65 @@ var InternalMastraMCPClient = class extends MastraBase {
975
977
  inputSchema: await this.convertInputSchema(tool.inputSchema),
976
978
  outputSchema: await this.convertOutputSchema(tool.outputSchema),
977
979
  execute: async (input, context) => {
978
- const previousContext = this.currentOperationContext;
979
- this.currentOperationContext = context?.requestContext || null;
980
- const executeToolCall = async () => {
981
- this.log("debug", `Executing tool: ${tool.name}`, { toolArgs: input, runId: context?.runId });
982
- const res = await this.client.callTool(
983
- {
984
- name: tool.name,
985
- arguments: input,
986
- // Use runId as progress token if available, otherwise generate a random UUID
987
- ...this.enableProgressTracking ? { _meta: { progressToken: context?.runId || crypto.randomUUID() } } : {}
988
- },
989
- CallToolResultSchema,
990
- {
991
- timeout: this.timeout
980
+ const operationContext = context?.requestContext ?? null;
981
+ return this.operationContextStore.run(operationContext, async () => {
982
+ const executeToolCall = async () => {
983
+ this.log("debug", `Executing tool: ${tool.name}`, { toolArgs: input, runId: context?.runId });
984
+ const res = await this.client.callTool(
985
+ {
986
+ name: tool.name,
987
+ arguments: input,
988
+ // Use runId as progress token if available, otherwise generate a random UUID
989
+ ...this.enableProgressTracking ? { _meta: { progressToken: context?.runId || crypto.randomUUID() } } : {}
990
+ },
991
+ CallToolResultSchema,
992
+ {
993
+ timeout: this.timeout
994
+ }
995
+ );
996
+ this.log("debug", `Tool executed successfully: ${tool.name}`);
997
+ if (res.structuredContent !== void 0) {
998
+ return res.structuredContent;
992
999
  }
993
- );
994
- this.log("debug", `Tool executed successfully: ${tool.name}`);
995
- if (res.structuredContent !== void 0) {
996
- return res.structuredContent;
997
- }
998
- if (tool.outputSchema && !res.isError) {
999
- const content = res.content;
1000
- if (content && content.length === 1 && content[0].type === "text" && content[0].text !== void 0) {
1001
- try {
1002
- return JSON.parse(content[0].text);
1003
- } catch {
1004
- return content[0].text;
1000
+ if (tool.outputSchema && !res.isError) {
1001
+ const content = res.content;
1002
+ if (content && content.length === 1 && content[0].type === "text" && content[0].text !== void 0) {
1003
+ try {
1004
+ return JSON.parse(content[0].text);
1005
+ } catch {
1006
+ return content[0].text;
1007
+ }
1005
1008
  }
1006
1009
  }
1007
- }
1008
- return res;
1009
- };
1010
- try {
1011
- return await executeToolCall();
1012
- } catch (e) {
1013
- if (this.isSessionError(e)) {
1014
- this.log("debug", `Session error detected for tool ${tool.name}, attempting reconnection...`, {
1015
- error: e instanceof Error ? e.message : String(e)
1016
- });
1017
- try {
1018
- await this.forceReconnect();
1019
- this.log("debug", `Retrying tool ${tool.name} after reconnection...`);
1020
- return await executeToolCall();
1021
- } catch (reconnectError) {
1022
- this.log("error", `Reconnection or retry failed for tool ${tool.name}`, {
1023
- originalError: e instanceof Error ? e.message : String(e),
1024
- reconnectError: reconnectError instanceof Error ? reconnectError.stack : String(reconnectError),
1025
- toolArgs: input
1010
+ return res;
1011
+ };
1012
+ try {
1013
+ return await executeToolCall();
1014
+ } catch (e) {
1015
+ if (this.isSessionError(e)) {
1016
+ this.log("debug", `Session error detected for tool ${tool.name}, attempting reconnection...`, {
1017
+ error: e instanceof Error ? e.message : String(e)
1026
1018
  });
1027
- throw e;
1019
+ try {
1020
+ await this.forceReconnect();
1021
+ this.log("debug", `Retrying tool ${tool.name} after reconnection...`);
1022
+ return await executeToolCall();
1023
+ } catch (reconnectError) {
1024
+ this.log("error", `Reconnection or retry failed for tool ${tool.name}`, {
1025
+ originalError: e instanceof Error ? e.message : String(e),
1026
+ reconnectError: reconnectError instanceof Error ? reconnectError.stack : String(reconnectError),
1027
+ toolArgs: input
1028
+ });
1029
+ throw e;
1030
+ }
1028
1031
  }
1032
+ this.log("error", `Error calling tool: ${tool.name}`, {
1033
+ error: e instanceof Error ? e.stack : JSON.stringify(e, null, 2),
1034
+ toolArgs: input
1035
+ });
1036
+ throw e;
1029
1037
  }
1030
- this.log("error", `Error calling tool: ${tool.name}`, {
1031
- error: e instanceof Error ? e.stack : JSON.stringify(e, null, 2),
1032
- toolArgs: input
1033
- });
1034
- throw e;
1035
- } finally {
1036
- this.currentOperationContext = previousContext;
1037
- }
1038
+ });
1038
1039
  }
1039
1040
  });
1040
1041
  if (tool.name) {