@mastra/mcp 0.14.5-alpha.0 → 0.14.5-alpha.1
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 +13 -0
- package/dist/client/client.d.ts +27 -0
- package/dist/client/client.d.ts.map +1 -1
- package/dist/index.cjs +70 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +71 -3
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -8,7 +8,7 @@ import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
|
8
8
|
import { StdioClientTransport, getDefaultEnvironment } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
9
9
|
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
10
10
|
import { DEFAULT_REQUEST_TIMEOUT_MSEC } from '@modelcontextprotocol/sdk/shared/protocol.js';
|
|
11
|
-
import { ListToolsRequestSchema, CallToolRequestSchema, SetLevelRequestSchema, ListResourcesRequestSchema, ReadResourceRequestSchema, ListResourceTemplatesRequestSchema, SubscribeRequestSchema, UnsubscribeRequestSchema, ListPromptsRequestSchema, PromptSchema, GetPromptRequestSchema, ListResourcesResultSchema, ReadResourceResultSchema, ListResourceTemplatesResultSchema, ListPromptsResultSchema, GetPromptResultSchema, PromptListChangedNotificationSchema, ResourceUpdatedNotificationSchema, ResourceListChangedNotificationSchema, ElicitRequestSchema,
|
|
11
|
+
import { ListToolsRequestSchema, CallToolRequestSchema, SetLevelRequestSchema, ListResourcesRequestSchema, ReadResourceRequestSchema, ListResourceTemplatesRequestSchema, SubscribeRequestSchema, UnsubscribeRequestSchema, ListPromptsRequestSchema, PromptSchema, GetPromptRequestSchema, ListResourcesResultSchema, ReadResourceResultSchema, ListResourceTemplatesResultSchema, ListPromptsResultSchema, GetPromptResultSchema, PromptListChangedNotificationSchema, ResourceUpdatedNotificationSchema, ResourceListChangedNotificationSchema, ElicitRequestSchema, JSONRPCMessageSchema, CallToolResultSchema, ErrorCode } from '@modelcontextprotocol/sdk/types.js';
|
|
12
12
|
import { asyncExitHook, gracefulExit } from 'exit-hook';
|
|
13
13
|
import { z } from 'zod';
|
|
14
14
|
import { convertJsonSchemaToZod } from 'zod-from-json-schema';
|
|
@@ -603,9 +603,57 @@ var InternalMastraMCPClient = class extends MastraBase {
|
|
|
603
603
|
throw e;
|
|
604
604
|
} finally {
|
|
605
605
|
this.transport = void 0;
|
|
606
|
-
this.isConnected =
|
|
606
|
+
this.isConnected = null;
|
|
607
607
|
}
|
|
608
608
|
}
|
|
609
|
+
/**
|
|
610
|
+
* Checks if an error indicates a session invalidation that requires reconnection.
|
|
611
|
+
*
|
|
612
|
+
* Common session-related errors include:
|
|
613
|
+
* - "No valid session ID provided" (HTTP 400)
|
|
614
|
+
* - "Server not initialized" (HTTP 400)
|
|
615
|
+
* - "Not connected" (protocol state error)
|
|
616
|
+
* - Connection refused errors
|
|
617
|
+
*
|
|
618
|
+
* @param error - The error to check
|
|
619
|
+
* @returns true if the error indicates a session problem requiring reconnection
|
|
620
|
+
*
|
|
621
|
+
* @internal
|
|
622
|
+
*/
|
|
623
|
+
isSessionError(error) {
|
|
624
|
+
if (!(error instanceof Error)) {
|
|
625
|
+
return false;
|
|
626
|
+
}
|
|
627
|
+
const errorMessage = error.message.toLowerCase();
|
|
628
|
+
return errorMessage.includes("no valid session") || errorMessage.includes("session") || errorMessage.includes("server not initialized") || errorMessage.includes("not connected") || errorMessage.includes("http 400") || errorMessage.includes("http 401") || errorMessage.includes("http 403") || errorMessage.includes("econnrefused") || errorMessage.includes("fetch failed") || errorMessage.includes("connection refused");
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Forces a reconnection to the MCP server by disconnecting and reconnecting.
|
|
632
|
+
*
|
|
633
|
+
* This is useful when the session becomes invalid (e.g., after server restart)
|
|
634
|
+
* and the client needs to establish a fresh connection.
|
|
635
|
+
*
|
|
636
|
+
* @returns Promise resolving when reconnection is complete
|
|
637
|
+
* @throws {Error} If reconnection fails
|
|
638
|
+
*
|
|
639
|
+
* @internal
|
|
640
|
+
*/
|
|
641
|
+
async forceReconnect() {
|
|
642
|
+
this.log("debug", "Forcing reconnection to MCP server...");
|
|
643
|
+
try {
|
|
644
|
+
if (this.transport) {
|
|
645
|
+
await this.transport.close();
|
|
646
|
+
}
|
|
647
|
+
} catch (e) {
|
|
648
|
+
this.log("debug", "Error during force disconnect (ignored)", {
|
|
649
|
+
error: e instanceof Error ? e.message : String(e)
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
this.transport = void 0;
|
|
653
|
+
this.isConnected = null;
|
|
654
|
+
await this.connect();
|
|
655
|
+
this.log("debug", "Successfully reconnected to MCP server");
|
|
656
|
+
}
|
|
609
657
|
async listResources() {
|
|
610
658
|
this.log("debug", `Requesting resources from MCP server`);
|
|
611
659
|
return await this.client.request({ method: "resources/list" }, ListResourcesResultSchema, {
|
|
@@ -778,7 +826,7 @@ var InternalMastraMCPClient = class extends MastraBase {
|
|
|
778
826
|
execute: async ({ context, runtimeContext }) => {
|
|
779
827
|
const previousContext = this.currentOperationContext;
|
|
780
828
|
this.currentOperationContext = runtimeContext || null;
|
|
781
|
-
|
|
829
|
+
const executeToolCall = async () => {
|
|
782
830
|
this.log("debug", `Executing tool: ${tool.name}`, { toolArgs: context });
|
|
783
831
|
const res = await this.client.callTool(
|
|
784
832
|
{
|
|
@@ -795,7 +843,27 @@ var InternalMastraMCPClient = class extends MastraBase {
|
|
|
795
843
|
return res.structuredContent;
|
|
796
844
|
}
|
|
797
845
|
return res;
|
|
846
|
+
};
|
|
847
|
+
try {
|
|
848
|
+
return await executeToolCall();
|
|
798
849
|
} catch (e) {
|
|
850
|
+
if (this.isSessionError(e)) {
|
|
851
|
+
this.log("debug", `Session error detected for tool ${tool.name}, attempting reconnection...`, {
|
|
852
|
+
error: e instanceof Error ? e.message : String(e)
|
|
853
|
+
});
|
|
854
|
+
try {
|
|
855
|
+
await this.forceReconnect();
|
|
856
|
+
this.log("debug", `Retrying tool ${tool.name} after reconnection...`);
|
|
857
|
+
return await executeToolCall();
|
|
858
|
+
} catch (reconnectError) {
|
|
859
|
+
this.log("error", `Reconnection or retry failed for tool ${tool.name}`, {
|
|
860
|
+
originalError: e instanceof Error ? e.message : String(e),
|
|
861
|
+
reconnectError: reconnectError instanceof Error ? reconnectError.stack : String(reconnectError),
|
|
862
|
+
toolArgs: context
|
|
863
|
+
});
|
|
864
|
+
throw e;
|
|
865
|
+
}
|
|
866
|
+
}
|
|
799
867
|
this.log("error", `Error calling tool: ${tool.name}`, {
|
|
800
868
|
error: e instanceof Error ? e.stack : JSON.stringify(e, null, 2),
|
|
801
869
|
toolArgs: context
|