@mastra/mcp 0.14.4 → 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 +22 -0
- package/dist/client/client.d.ts +27 -0
- package/dist/client/client.d.ts.map +1 -1
- package/dist/index.cjs +164 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +165 -3
- package/dist/index.js.map +1 -1
- package/dist/server/server.d.ts +37 -1
- package/dist/server/server.d.ts.map +1 -1
- package/package.json +6 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# @mastra/mcp
|
|
2
2
|
|
|
3
|
+
## 0.14.5-alpha.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Fix MCPClient automatic reconnection when session becomes invalid ([#10993](https://github.com/mastra-ai/mastra/pull/10993))
|
|
8
|
+
|
|
9
|
+
When an MCP server restarts, the session ID becomes invalid causing "Bad Request: No valid session ID provided" errors. The MCPClient now automatically detects session-related errors, reconnects to the server, and retries the tool call.
|
|
10
|
+
|
|
11
|
+
This fix addresses issue #7675 where MCPClient would fail to reconnect after an MCP server went offline and came back online.
|
|
12
|
+
|
|
13
|
+
- Updated dependencies []:
|
|
14
|
+
- @mastra/core@0.24.7-alpha.3
|
|
15
|
+
|
|
16
|
+
## 0.14.5-alpha.0
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- add flag to skip sessions and streaming in serverless mcp ([#10927](https://github.com/mastra-ai/mastra/pull/10927))
|
|
21
|
+
|
|
22
|
+
- Updated dependencies [[`b685c9c`](https://github.com/mastra-ai/mastra/commit/b685c9c0b89f49e0d4542c4ac72569682db69794)]:
|
|
23
|
+
- @mastra/core@0.24.7-alpha.2
|
|
24
|
+
|
|
3
25
|
## 0.14.4
|
|
4
26
|
|
|
5
27
|
### Patch Changes
|
package/dist/client/client.d.ts
CHANGED
|
@@ -200,6 +200,33 @@ export declare class InternalMastraMCPClient extends MastraBase {
|
|
|
200
200
|
*/
|
|
201
201
|
get sessionId(): string | undefined;
|
|
202
202
|
disconnect(): Promise<void>;
|
|
203
|
+
/**
|
|
204
|
+
* Checks if an error indicates a session invalidation that requires reconnection.
|
|
205
|
+
*
|
|
206
|
+
* Common session-related errors include:
|
|
207
|
+
* - "No valid session ID provided" (HTTP 400)
|
|
208
|
+
* - "Server not initialized" (HTTP 400)
|
|
209
|
+
* - "Not connected" (protocol state error)
|
|
210
|
+
* - Connection refused errors
|
|
211
|
+
*
|
|
212
|
+
* @param error - The error to check
|
|
213
|
+
* @returns true if the error indicates a session problem requiring reconnection
|
|
214
|
+
*
|
|
215
|
+
* @internal
|
|
216
|
+
*/
|
|
217
|
+
private isSessionError;
|
|
218
|
+
/**
|
|
219
|
+
* Forces a reconnection to the MCP server by disconnecting and reconnecting.
|
|
220
|
+
*
|
|
221
|
+
* This is useful when the session becomes invalid (e.g., after server restart)
|
|
222
|
+
* and the client needs to establish a fresh connection.
|
|
223
|
+
*
|
|
224
|
+
* @returns Promise resolving when reconnection is complete
|
|
225
|
+
* @throws {Error} If reconnection fails
|
|
226
|
+
*
|
|
227
|
+
* @internal
|
|
228
|
+
*/
|
|
229
|
+
forceReconnect(): Promise<void>;
|
|
203
230
|
listResources(): Promise<z.objectOutputType<{
|
|
204
231
|
_meta: z.ZodOptional<z.ZodObject<{}, "passthrough", z.ZodTypeAny, z.objectOutputType<{}, z.ZodTypeAny, "passthrough">, z.objectInputType<{}, z.ZodTypeAny, "passthrough">>>;
|
|
205
232
|
} & {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client/client.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAMtD,OAAO,KAAK,EAAE,yBAAyB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M,EAAE,CAAC,CAAC,KAAK,CAAC,OAAO,iCAAiC,CAAC,CAAC,QAAQ,CAAC,KAAK,IAAI,GACrF,IAAI;IAOP,yCAAyC,CAAC,OAAO,EAAE,MAAM,IAAI,GAAG,IAAI;IAOpE,4BAA4B,CAAC,OAAO,EAAE,kBAAkB,GAAG,IAAI;YAQjD,kBAAkB;YA0ClB,mBAAmB;IA2C3B,KAAK;CA6FZ;AAED;;GAEG;AAEH,qBAAa,eAAgB,SAAQ,uBAAuB;gBAC9C,IAAI,EAAE,8BAA8B;CAWjD"}
|
package/dist/index.cjs
CHANGED
|
@@ -610,9 +610,57 @@ var InternalMastraMCPClient = class extends base.MastraBase {
|
|
|
610
610
|
throw e;
|
|
611
611
|
} finally {
|
|
612
612
|
this.transport = void 0;
|
|
613
|
-
this.isConnected =
|
|
613
|
+
this.isConnected = null;
|
|
614
614
|
}
|
|
615
615
|
}
|
|
616
|
+
/**
|
|
617
|
+
* Checks if an error indicates a session invalidation that requires reconnection.
|
|
618
|
+
*
|
|
619
|
+
* Common session-related errors include:
|
|
620
|
+
* - "No valid session ID provided" (HTTP 400)
|
|
621
|
+
* - "Server not initialized" (HTTP 400)
|
|
622
|
+
* - "Not connected" (protocol state error)
|
|
623
|
+
* - Connection refused errors
|
|
624
|
+
*
|
|
625
|
+
* @param error - The error to check
|
|
626
|
+
* @returns true if the error indicates a session problem requiring reconnection
|
|
627
|
+
*
|
|
628
|
+
* @internal
|
|
629
|
+
*/
|
|
630
|
+
isSessionError(error) {
|
|
631
|
+
if (!(error instanceof Error)) {
|
|
632
|
+
return false;
|
|
633
|
+
}
|
|
634
|
+
const errorMessage = error.message.toLowerCase();
|
|
635
|
+
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");
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Forces a reconnection to the MCP server by disconnecting and reconnecting.
|
|
639
|
+
*
|
|
640
|
+
* This is useful when the session becomes invalid (e.g., after server restart)
|
|
641
|
+
* and the client needs to establish a fresh connection.
|
|
642
|
+
*
|
|
643
|
+
* @returns Promise resolving when reconnection is complete
|
|
644
|
+
* @throws {Error} If reconnection fails
|
|
645
|
+
*
|
|
646
|
+
* @internal
|
|
647
|
+
*/
|
|
648
|
+
async forceReconnect() {
|
|
649
|
+
this.log("debug", "Forcing reconnection to MCP server...");
|
|
650
|
+
try {
|
|
651
|
+
if (this.transport) {
|
|
652
|
+
await this.transport.close();
|
|
653
|
+
}
|
|
654
|
+
} catch (e) {
|
|
655
|
+
this.log("debug", "Error during force disconnect (ignored)", {
|
|
656
|
+
error: e instanceof Error ? e.message : String(e)
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
this.transport = void 0;
|
|
660
|
+
this.isConnected = null;
|
|
661
|
+
await this.connect();
|
|
662
|
+
this.log("debug", "Successfully reconnected to MCP server");
|
|
663
|
+
}
|
|
616
664
|
async listResources() {
|
|
617
665
|
this.log("debug", `Requesting resources from MCP server`);
|
|
618
666
|
return await this.client.request({ method: "resources/list" }, types_js.ListResourcesResultSchema, {
|
|
@@ -785,7 +833,7 @@ var InternalMastraMCPClient = class extends base.MastraBase {
|
|
|
785
833
|
execute: async ({ context, runtimeContext }) => {
|
|
786
834
|
const previousContext = this.currentOperationContext;
|
|
787
835
|
this.currentOperationContext = runtimeContext || null;
|
|
788
|
-
|
|
836
|
+
const executeToolCall = async () => {
|
|
789
837
|
this.log("debug", `Executing tool: ${tool.name}`, { toolArgs: context });
|
|
790
838
|
const res = await this.client.callTool(
|
|
791
839
|
{
|
|
@@ -802,7 +850,27 @@ var InternalMastraMCPClient = class extends base.MastraBase {
|
|
|
802
850
|
return res.structuredContent;
|
|
803
851
|
}
|
|
804
852
|
return res;
|
|
853
|
+
};
|
|
854
|
+
try {
|
|
855
|
+
return await executeToolCall();
|
|
805
856
|
} catch (e) {
|
|
857
|
+
if (this.isSessionError(e)) {
|
|
858
|
+
this.log("debug", `Session error detected for tool ${tool.name}, attempting reconnection...`, {
|
|
859
|
+
error: e instanceof Error ? e.message : String(e)
|
|
860
|
+
});
|
|
861
|
+
try {
|
|
862
|
+
await this.forceReconnect();
|
|
863
|
+
this.log("debug", `Retrying tool ${tool.name} after reconnection...`);
|
|
864
|
+
return await executeToolCall();
|
|
865
|
+
} catch (reconnectError) {
|
|
866
|
+
this.log("error", `Reconnection or retry failed for tool ${tool.name}`, {
|
|
867
|
+
originalError: e instanceof Error ? e.message : String(e),
|
|
868
|
+
reconnectError: reconnectError instanceof Error ? reconnectError.stack : String(reconnectError),
|
|
869
|
+
toolArgs: context
|
|
870
|
+
});
|
|
871
|
+
throw e;
|
|
872
|
+
}
|
|
873
|
+
}
|
|
806
874
|
this.log("error", `Error calling tool: ${tool.name}`, {
|
|
807
875
|
error: e instanceof Error ? e.stack : JSON.stringify(e, null, 2),
|
|
808
876
|
toolArgs: context
|
|
@@ -2970,6 +3038,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
|
|
|
2970
3038
|
* @param options.options.onsessioninitialized - Callback when a new session is initialized
|
|
2971
3039
|
* @param options.options.enableJsonResponse - If true, return JSON instead of SSE streaming
|
|
2972
3040
|
* @param options.options.eventStore - Event store for message resumability
|
|
3041
|
+
* @param options.options.serverless - If true, run in stateless mode without session management (ideal for serverless environments)
|
|
2973
3042
|
*
|
|
2974
3043
|
* @throws {MastraError} If HTTP connection setup fails
|
|
2975
3044
|
*
|
|
@@ -2995,6 +3064,25 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
|
|
|
2995
3064
|
*
|
|
2996
3065
|
* httpServer.listen(1234);
|
|
2997
3066
|
* ```
|
|
3067
|
+
*
|
|
3068
|
+
* @example Serverless mode (Cloudflare Workers, Vercel Edge, etc.)
|
|
3069
|
+
* ```typescript
|
|
3070
|
+
* export default {
|
|
3071
|
+
* async fetch(request: Request) {
|
|
3072
|
+
* const url = new URL(request.url);
|
|
3073
|
+
* if (url.pathname === '/mcp') {
|
|
3074
|
+
* await server.startHTTP({
|
|
3075
|
+
* url,
|
|
3076
|
+
* httpPath: '/mcp',
|
|
3077
|
+
* req: request,
|
|
3078
|
+
* res: response,
|
|
3079
|
+
* options: { serverless: true },
|
|
3080
|
+
* });
|
|
3081
|
+
* }
|
|
3082
|
+
* return new Response('Not found', { status: 404 });
|
|
3083
|
+
* },
|
|
3084
|
+
* };
|
|
3085
|
+
* ```
|
|
2998
3086
|
*/
|
|
2999
3087
|
async startHTTP({
|
|
3000
3088
|
url,
|
|
@@ -3010,6 +3098,12 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
|
|
|
3010
3098
|
res.end();
|
|
3011
3099
|
return;
|
|
3012
3100
|
}
|
|
3101
|
+
const isStatelessMode = options?.serverless || options && "sessionIdGenerator" in options && options.sessionIdGenerator === void 0;
|
|
3102
|
+
if (isStatelessMode) {
|
|
3103
|
+
this.logger.debug("startHTTP: Running in stateless mode (serverless or sessionIdGenerator: undefined)");
|
|
3104
|
+
await this.handleServerlessRequest(req, res);
|
|
3105
|
+
return;
|
|
3106
|
+
}
|
|
3013
3107
|
const mergedOptions = {
|
|
3014
3108
|
sessionIdGenerator: () => crypto$1.randomUUID(),
|
|
3015
3109
|
// default: enabled
|
|
@@ -3151,6 +3245,74 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
|
|
|
3151
3245
|
}
|
|
3152
3246
|
}
|
|
3153
3247
|
}
|
|
3248
|
+
/**
|
|
3249
|
+
* Handles a stateless, serverless HTTP request without session management.
|
|
3250
|
+
*
|
|
3251
|
+
* This method bypasses all session/transport state and handles each request independently.
|
|
3252
|
+
* For serverless environments (Cloudflare Workers, Vercel Edge, etc.) where
|
|
3253
|
+
* persistent connections and session state cannot be maintained across requests.
|
|
3254
|
+
*
|
|
3255
|
+
* Each request gets a fresh transport and server instance that are discarded after the response.
|
|
3256
|
+
*
|
|
3257
|
+
* @param req - Incoming HTTP request
|
|
3258
|
+
* @param res - HTTP response object
|
|
3259
|
+
* @private
|
|
3260
|
+
*/
|
|
3261
|
+
async handleServerlessRequest(req, res) {
|
|
3262
|
+
try {
|
|
3263
|
+
this.logger.debug(`handleServerlessRequest: Received ${req.method} request`);
|
|
3264
|
+
const body = req.method === "POST" ? await new Promise((resolve, reject) => {
|
|
3265
|
+
let data = "";
|
|
3266
|
+
req.on("data", (chunk) => data += chunk);
|
|
3267
|
+
req.on("end", () => {
|
|
3268
|
+
try {
|
|
3269
|
+
resolve(JSON.parse(data));
|
|
3270
|
+
} catch (e) {
|
|
3271
|
+
reject(new Error(`Invalid JSON in request body: ${e instanceof Error ? e.message : String(e)}`));
|
|
3272
|
+
}
|
|
3273
|
+
});
|
|
3274
|
+
req.on("error", reject);
|
|
3275
|
+
}) : void 0;
|
|
3276
|
+
this.logger.debug(`handleServerlessRequest: Processing ${req.method} request`, {
|
|
3277
|
+
method: body?.method,
|
|
3278
|
+
id: body?.id
|
|
3279
|
+
});
|
|
3280
|
+
const transientServer = this.createServerInstance();
|
|
3281
|
+
const tempTransport = new streamableHttp_js.StreamableHTTPServerTransport({
|
|
3282
|
+
sessionIdGenerator: void 0,
|
|
3283
|
+
enableJsonResponse: true
|
|
3284
|
+
});
|
|
3285
|
+
await transientServer.connect(tempTransport);
|
|
3286
|
+
await tempTransport.handleRequest(req, res, body);
|
|
3287
|
+
this.logger.debug(`handleServerlessRequest: Completed ${body?.method} request`, { id: body?.id });
|
|
3288
|
+
} catch (error$1) {
|
|
3289
|
+
const mastraError = new error.MastraError(
|
|
3290
|
+
{
|
|
3291
|
+
id: "MCP_SERVER_SERVERLESS_REQUEST_FAILED",
|
|
3292
|
+
domain: error.ErrorDomain.MCP,
|
|
3293
|
+
category: error.ErrorCategory.USER,
|
|
3294
|
+
text: "Failed to handle serverless MCP request"
|
|
3295
|
+
},
|
|
3296
|
+
error$1
|
|
3297
|
+
);
|
|
3298
|
+
this.logger.trackException(mastraError);
|
|
3299
|
+
this.logger.error("handleServerlessRequest: Error handling request:", { error: mastraError });
|
|
3300
|
+
if (!res.headersSent) {
|
|
3301
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
3302
|
+
res.end(
|
|
3303
|
+
JSON.stringify({
|
|
3304
|
+
jsonrpc: "2.0",
|
|
3305
|
+
error: {
|
|
3306
|
+
code: -32603,
|
|
3307
|
+
message: "Internal server error",
|
|
3308
|
+
data: error$1 instanceof Error ? error$1.message : String(error$1)
|
|
3309
|
+
},
|
|
3310
|
+
id: null
|
|
3311
|
+
})
|
|
3312
|
+
);
|
|
3313
|
+
}
|
|
3314
|
+
}
|
|
3315
|
+
}
|
|
3154
3316
|
/**
|
|
3155
3317
|
* Establishes the SSE connection for the MCP server.
|
|
3156
3318
|
*
|