mastra-ai/mastra · warning · Error

MCP client was disconnected while recovering the failed tran

Error message

MCP client was disconnected while recovering the failed transport

What it means

reconnectAfterTransportFailure is a private recovery loop that retries reconnecting after a transport failure, but only while the client is still connected for the same lifecycle generation. If disconnect() runs during recovery (lifecycleGeneration changed), the loop aborts by throwing this error — recovery is meaningless on a client that was intentionally disconnected.

Source

Thrown at packages/mcp/src/client/client.ts:1070

      await this.connect();
      this.log('debug', 'Successfully reconnected to MCP server');
    })();
    this.reconnectPromise = reconnectPromise;

    try {
      await reconnectPromise;
    } finally {
      if (this.reconnectPromise === reconnectPromise) {
        this.reconnectPromise = null;
      }
    }
  }

  private async reconnectAfterTransportFailure(failedTransport: Transport | undefined, lifecycleGeneration: number) {
    while (true) {
      if (this.lifecycleGeneration !== lifecycleGeneration) {
        throw new Error('MCP client was disconnected while recovering the failed transport');
      }

      const reconnectPromise = this.reconnectPromise;
      if (reconnectPromise) {
        await reconnectPromise;
        // Re-evaluate after the owner clears the settled promise. The transport
        // that failed may itself be the replacement created by that reconnect.
        continue;
      }

      if (failedTransport && this.transport && this.transport !== failedTransport) {
        this.log('debug', 'Connection was already replaced after the failed operation; skipping reconnect');
        return;
      }

      await this.forceReconnect();

      if (this.lifecycleGeneration !== lifecycleGeneration) {

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Catch this error around operations that may span a disconnect and treat it as cancellation, not a fault
  2. Avoid calling disconnect() while reconnect recovery is pending, or accept in-flight operations failing
  3. Use the latest client instance after reconfiguring instead of the old one

Example fix

// before
const tools = await client.tools(); // may reject with disconnect-during-recovery
// after
try {
  const tools = await client.tools();
} catch (e) {
  if (e instanceof Error && e.message.includes('disconnected while recovering')) return; // cancelled
  throw e;
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await client.listResources();
} catch (e) {
  if (e instanceof Error && e.message.includes('disconnected while recovering')) {
    return; // operation cancelled by disconnect(); not a fault
  }
  throw e;
}

Prevention

When it happens

Trigger: The transport fails and reconnectAfterTransportFailure begins, then the user calls disconnect() (or a new connect() starts a new generation) before recovery completes; the next loop iteration detects the generation mismatch and throws.

Common situations: Application shutting down or swapping server configs while a background reconnect is in flight; a request awaiting recovery is cancelled by disconnect; concurrent connect/disconnect calls racing.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/397474a79454fd62. Report an issue: GitHub.