@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/CHANGELOG.md +33 -0
- package/dist/client/client.d.ts +2 -2
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/types.d.ts +44 -11
- package/dist/client/types.d.ts.map +1 -1
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/reference-tools-mcp-client.md +38 -27
- package/dist/docs/references/reference-tools-mcp-server.md +45 -45
- package/dist/index.cjs +58 -57
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +58 -57
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
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
|
-
|
|
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.
|
|
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:
|
|
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
|
|
986
|
-
this.
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
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
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
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
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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) {
|