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
- Catch this error around operations that may span a disconnect and treat it as cancellation, not a fault
- Avoid calling disconnect() while reconnect recovery is pending, or accept in-flight operations failing
- 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
- Avoid disconnect() while operations are awaiting transport recovery
- Serialize connect/disconnect calls; treat generation mismatch as cancellation
- Use the fresh client instance after reconfiguration instead of a stale one
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
- MCPClient was initialized multiple times with the same confi
- MastraAuthBetterAuth is not initialized — init() must run fi
- Shared browser not launched. Call createSharedSession() firs
- Browser not launched
- App deletion failed: ${data.error}
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/397474a79454fd62.
Report an issue: GitHub.