From 6fcb0f57eb89bdc792165af36b7e4aea8103fe0d Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Fri, 3 Apr 2026 20:18:10 -0400 Subject: [PATCH] fix: set reusedStoredClient before createFlow in joined-flow path When joining a PENDING flow, reusedStoredClient was only set on the success return but not before the await. If createFlow throws (e.g. invalid_client during token exchange), the outer catch returns the local variable which was still false, skipping stale-client cleanup. --- packages/api/src/mcp/MCPConnectionFactory.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/api/src/mcp/MCPConnectionFactory.ts b/packages/api/src/mcp/MCPConnectionFactory.ts index 1f24fe5ec4..3935ec61c8 100644 --- a/packages/api/src/mcp/MCPConnectionFactory.ts +++ b/packages/api/src/mcp/MCPConnectionFactory.ts @@ -596,6 +596,7 @@ export class MCPConnectionFactory { await this.oauthStart(storedAuthUrl); } + reusedStoredClient = flowMeta?.reusedStoredClient === true; const tokens = await this.flowManager.createFlow(flowId, 'mcp_oauth', {}, this.signal); if (typeof this.oauthEnd === 'function') { await this.oauthEnd();