@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.cjs CHANGED
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ var async_hooks = require('async_hooks');
3
4
  var $RefParser = require('@apidevtools/json-schema-ref-parser');
4
5
  var base = require('@mastra/core/base');
5
6
  var error = require('@mastra/core/error');
@@ -420,7 +421,7 @@ var InternalMastraMCPClient = class extends base.MastraBase {
420
421
  enableProgressTracking;
421
422
  serverConfig;
422
423
  transport;
423
- currentOperationContext = null;
424
+ operationContextStore = new async_hooks.AsyncLocalStorage();
424
425
  exitHookUnsubscribe;
425
426
  sigTermHandler;
426
427
  _roots;
@@ -492,7 +493,7 @@ var InternalMastraMCPClient = class extends base.MastraBase {
492
493
  timestamp: /* @__PURE__ */ new Date(),
493
494
  serverName: this.name,
494
495
  details,
495
- requestContext: this.currentOperationContext
496
+ requestContext: this.operationContextStore.getStore() ?? null
496
497
  });
497
498
  }
498
499
  }
@@ -585,7 +586,8 @@ var InternalMastraMCPClient = class extends base.MastraBase {
585
586
  }
586
587
  }
587
588
  async connectHttp(url) {
588
- const { requestInit, eventSourceInit, authProvider, connectTimeout, fetch: fetch2 } = this.serverConfig;
589
+ const { requestInit, eventSourceInit, authProvider, connectTimeout, fetch: userFetch } = this.serverConfig;
590
+ const fetch2 = userFetch ? (url2, init) => userFetch(url2, init, this.operationContextStore.getStore() ?? null) : void 0;
589
591
  this.log("debug", `Attempting to connect to URL: ${url}`);
590
592
  let shouldTrySSE = url.pathname.endsWith(`/sse`);
591
593
  if (!shouldTrySSE) {
@@ -982,66 +984,65 @@ var InternalMastraMCPClient = class extends base.MastraBase {
982
984
  inputSchema: await this.convertInputSchema(tool.inputSchema),
983
985
  outputSchema: await this.convertOutputSchema(tool.outputSchema),
984
986
  execute: async (input, context) => {
985
- const previousContext = this.currentOperationContext;
986
- this.currentOperationContext = context?.requestContext || null;
987
- const executeToolCall = async () => {
988
- this.log("debug", `Executing tool: ${tool.name}`, { toolArgs: input, runId: context?.runId });
989
- const res = await this.client.callTool(
990
- {
991
- name: tool.name,
992
- arguments: input,
993
- // Use runId as progress token if available, otherwise generate a random UUID
994
- ...this.enableProgressTracking ? { _meta: { progressToken: context?.runId || crypto.randomUUID() } } : {}
995
- },
996
- types_js.CallToolResultSchema,
997
- {
998
- timeout: this.timeout
987
+ const operationContext = context?.requestContext ?? null;
988
+ return this.operationContextStore.run(operationContext, async () => {
989
+ const executeToolCall = async () => {
990
+ this.log("debug", `Executing tool: ${tool.name}`, { toolArgs: input, runId: context?.runId });
991
+ const res = await this.client.callTool(
992
+ {
993
+ name: tool.name,
994
+ arguments: input,
995
+ // Use runId as progress token if available, otherwise generate a random UUID
996
+ ...this.enableProgressTracking ? { _meta: { progressToken: context?.runId || crypto.randomUUID() } } : {}
997
+ },
998
+ types_js.CallToolResultSchema,
999
+ {
1000
+ timeout: this.timeout
1001
+ }
1002
+ );
1003
+ this.log("debug", `Tool executed successfully: ${tool.name}`);
1004
+ if (res.structuredContent !== void 0) {
1005
+ return res.structuredContent;
999
1006
  }
1000
- );
1001
- this.log("debug", `Tool executed successfully: ${tool.name}`);
1002
- if (res.structuredContent !== void 0) {
1003
- return res.structuredContent;
1004
- }
1005
- if (tool.outputSchema && !res.isError) {
1006
- const content = res.content;
1007
- if (content && content.length === 1 && content[0].type === "text" && content[0].text !== void 0) {
1008
- try {
1009
- return JSON.parse(content[0].text);
1010
- } catch {
1011
- return content[0].text;
1007
+ if (tool.outputSchema && !res.isError) {
1008
+ const content = res.content;
1009
+ if (content && content.length === 1 && content[0].type === "text" && content[0].text !== void 0) {
1010
+ try {
1011
+ return JSON.parse(content[0].text);
1012
+ } catch {
1013
+ return content[0].text;
1014
+ }
1012
1015
  }
1013
1016
  }
1014
- }
1015
- return res;
1016
- };
1017
- try {
1018
- return await executeToolCall();
1019
- } catch (e) {
1020
- if (this.isSessionError(e)) {
1021
- this.log("debug", `Session error detected for tool ${tool.name}, attempting reconnection...`, {
1022
- error: e instanceof Error ? e.message : String(e)
1023
- });
1024
- try {
1025
- await this.forceReconnect();
1026
- this.log("debug", `Retrying tool ${tool.name} after reconnection...`);
1027
- return await executeToolCall();
1028
- } catch (reconnectError) {
1029
- this.log("error", `Reconnection or retry failed for tool ${tool.name}`, {
1030
- originalError: e instanceof Error ? e.message : String(e),
1031
- reconnectError: reconnectError instanceof Error ? reconnectError.stack : String(reconnectError),
1032
- toolArgs: input
1017
+ return res;
1018
+ };
1019
+ try {
1020
+ return await executeToolCall();
1021
+ } catch (e) {
1022
+ if (this.isSessionError(e)) {
1023
+ this.log("debug", `Session error detected for tool ${tool.name}, attempting reconnection...`, {
1024
+ error: e instanceof Error ? e.message : String(e)
1033
1025
  });
1034
- throw e;
1026
+ try {
1027
+ await this.forceReconnect();
1028
+ this.log("debug", `Retrying tool ${tool.name} after reconnection...`);
1029
+ return await executeToolCall();
1030
+ } catch (reconnectError) {
1031
+ this.log("error", `Reconnection or retry failed for tool ${tool.name}`, {
1032
+ originalError: e instanceof Error ? e.message : String(e),
1033
+ reconnectError: reconnectError instanceof Error ? reconnectError.stack : String(reconnectError),
1034
+ toolArgs: input
1035
+ });
1036
+ throw e;
1037
+ }
1035
1038
  }
1039
+ this.log("error", `Error calling tool: ${tool.name}`, {
1040
+ error: e instanceof Error ? e.stack : JSON.stringify(e, null, 2),
1041
+ toolArgs: input
1042
+ });
1043
+ throw e;
1036
1044
  }
1037
- this.log("error", `Error calling tool: ${tool.name}`, {
1038
- error: e instanceof Error ? e.stack : JSON.stringify(e, null, 2),
1039
- toolArgs: input
1040
- });
1041
- throw e;
1042
- } finally {
1043
- this.currentOperationContext = previousContext;
1044
- }
1045
+ });
1045
1046
  }
1046
1047
  });
1047
1048
  if (tool.name) {