@mastra/mcp 1.0.0-beta.4 → 1.0.0-beta.6

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
@@ -384,6 +384,7 @@ var ResourceClientActions = class {
384
384
 
385
385
  // src/client/client.ts
386
386
  var DEFAULT_SERVER_CONNECT_TIMEOUT_MSEC = 3e3;
387
+ var SSE_FALLBACK_STATUS_CODES = [400, 404, 405];
387
388
  function convertLogLevelToLoggerMethod(level) {
388
389
  switch (level) {
389
390
  case "debug":
@@ -576,7 +577,7 @@ var InternalMastraMCPClient = class extends MastraBase {
576
577
  }
577
578
  }
578
579
  async connectHttp(url) {
579
- const { requestInit, eventSourceInit, authProvider, connectTimeout } = this.serverConfig;
580
+ const { requestInit, eventSourceInit, authProvider, connectTimeout, fetch } = this.serverConfig;
580
581
  this.log("debug", `Attempting to connect to URL: ${url}`);
581
582
  let shouldTrySSE = url.pathname.endsWith(`/sse`);
582
583
  if (!shouldTrySSE) {
@@ -585,7 +586,8 @@ var InternalMastraMCPClient = class extends MastraBase {
585
586
  const streamableTransport = new StreamableHTTPClientTransport(url, {
586
587
  requestInit,
587
588
  reconnectionOptions: this.serverConfig.reconnectionOptions,
588
- authProvider
589
+ authProvider,
590
+ fetch
589
591
  });
590
592
  await this.client.connect(streamableTransport, {
591
593
  timeout: connectTimeout ?? DEFAULT_SERVER_CONNECT_TIMEOUT_MSEC
@@ -594,13 +596,23 @@ var InternalMastraMCPClient = class extends MastraBase {
594
596
  this.log("debug", "Successfully connected using Streamable HTTP transport.");
595
597
  } catch (error) {
596
598
  this.log("debug", `Streamable HTTP transport failed: ${error}`);
599
+ const status = error?.code;
600
+ if (status !== void 0 && !SSE_FALLBACK_STATUS_CODES.includes(status)) {
601
+ throw error;
602
+ }
597
603
  shouldTrySSE = true;
598
604
  }
599
605
  }
600
606
  if (shouldTrySSE) {
601
607
  this.log("debug", "Falling back to deprecated HTTP+SSE transport...");
602
608
  try {
603
- const sseTransport = new SSEClientTransport(url, { requestInit, eventSourceInit, authProvider });
609
+ const sseEventSourceInit = fetch ? { ...eventSourceInit, fetch } : eventSourceInit;
610
+ const sseTransport = new SSEClientTransport(url, {
611
+ requestInit,
612
+ eventSourceInit: sseEventSourceInit,
613
+ authProvider,
614
+ fetch
615
+ });
604
616
  await this.client.connect(sseTransport, { timeout: this.serverConfig.timeout ?? this.timeout });
605
617
  this.transport = sseTransport;
606
618
  this.log("debug", "Successfully connected using deprecated HTTP+SSE transport.");
@@ -717,6 +729,7 @@ var InternalMastraMCPClient = class extends MastraBase {
717
729
  * Common session-related errors include:
718
730
  * - "No valid session ID provided" (HTTP 400)
719
731
  * - "Server not initialized" (HTTP 400)
732
+ * - "Not connected" (protocol state error)
720
733
  * - Connection refused errors
721
734
  *
722
735
  * @param error - The error to check
@@ -729,7 +742,7 @@ var InternalMastraMCPClient = class extends MastraBase {
729
742
  return false;
730
743
  }
731
744
  const errorMessage = error.message.toLowerCase();
732
- return errorMessage.includes("no valid session") || errorMessage.includes("session") || errorMessage.includes("server not initialized") || errorMessage.includes("http 400") || errorMessage.includes("http 401") || errorMessage.includes("http 403") || errorMessage.includes("econnrefused") || errorMessage.includes("fetch failed") || errorMessage.includes("connection refused");
745
+ 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");
733
746
  }
734
747
  /**
735
748
  * Forces a reconnection to the MCP server by disconnecting and reconnecting.
@@ -2383,8 +2396,8 @@ var MCPServer = class extends MCPServerBase {
2383
2396
  }
2384
2397
  });
2385
2398
  this.elicitation = {
2386
- sendRequest: async (request) => {
2387
- return this.handleElicitationRequest(request);
2399
+ sendRequest: async (request, options) => {
2400
+ return this.handleElicitationRequest(request, void 0, options);
2388
2401
  }
2389
2402
  };
2390
2403
  }
@@ -2394,12 +2407,13 @@ var MCPServer = class extends MCPServerBase {
2394
2407
  *
2395
2408
  * @param request - The elicitation request containing message and schema
2396
2409
  * @param serverInstance - Optional server instance to use; defaults to main server for backward compatibility
2410
+ * @param options - Optional request options (timeout, signal, etc.)
2397
2411
  * @returns Promise that resolves to the client's response
2398
2412
  */
2399
- async handleElicitationRequest(request, serverInstance) {
2413
+ async handleElicitationRequest(request, serverInstance, options) {
2400
2414
  this.logger.debug(`Sending elicitation request: ${request.message}`);
2401
2415
  const server = serverInstance || this.server;
2402
- const response = await server.elicitInput(request);
2416
+ const response = await server.elicitInput(request, options);
2403
2417
  this.logger.debug(`Received elicitation response: ${JSON.stringify(response)}`);
2404
2418
  return response;
2405
2419
  }
@@ -2523,8 +2537,8 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
2523
2537
  };
2524
2538
  }
2525
2539
  const sessionElicitation = {
2526
- sendRequest: async (request2) => {
2527
- return this.handleElicitationRequest(request2, serverInstance);
2540
+ sendRequest: async (request2, options) => {
2541
+ return this.handleElicitationRequest(request2, serverInstance, options);
2528
2542
  }
2529
2543
  };
2530
2544
  const mcpOptions = {