@@ -171,6 +171,7 @@ export function useMCPServerManager() {
171171 message : localize ( 'com_ui_mcp_oauth_timeout' , { 0 : serverName } ) ,
172172 status : 'error' ,
173173 } ) ;
174+ clearInterval ( pollInterval ) ;
174175 cleanupServerState ( serverName ) ;
175176 return ;
176177 }
@@ -180,10 +181,15 @@ export function useMCPServerManager() {
180181 message : localize ( 'com_ui_mcp_init_failed' ) ,
181182 status : 'error' ,
182183 } ) ;
184+ clearInterval ( pollInterval ) ;
183185 cleanupServerState ( serverName ) ;
186+ return ;
184187 }
185188 } catch ( error ) {
186189 console . error ( `[MCP Manager] Error polling server ${ serverName } :` , error ) ;
190+ clearInterval ( pollInterval ) ;
191+ cleanupServerState ( serverName ) ;
192+ return ;
187193 }
188194 } , 3500 ) ;
189195
@@ -201,7 +207,7 @@ export function useMCPServerManager() {
201207 ) ;
202208
203209 const initializeServer = useCallback (
204- async ( serverName : string ) => {
210+ async ( serverName : string , autoOpenOAuth : boolean = true ) => {
205211 updateServerState ( serverName , { isInitializing : true } ) ;
206212
207213 try {
@@ -216,7 +222,9 @@ export function useMCPServerManager() {
216222 isInitializing : true ,
217223 } ) ;
218224
219- window . open ( response . oauthUrl , '_blank' , 'noopener,noreferrer' ) ;
225+ if ( autoOpenOAuth ) {
226+ window . open ( response . oauthUrl , '_blank' , 'noopener,noreferrer' ) ;
227+ }
220228
221229 startServerPolling ( serverName ) ;
222230 } else {
@@ -265,13 +273,25 @@ export function useMCPServerManager() {
265273
266274 const cancelOAuthFlow = useCallback (
267275 ( serverName : string ) => {
268- queryClient . invalidateQueries ( [ QueryKeys . mcpConnectionStatus ] ) ;
269- cleanupServerState ( serverName ) ;
270- cancelOAuthMutation . mutate ( serverName ) ;
276+ // Call backend cancellation first, then clean up frontend state on success
277+ cancelOAuthMutation . mutate ( serverName , {
278+ onSuccess : ( ) => {
279+ // Only clean up frontend state after backend confirms cancellation
280+ cleanupServerState ( serverName ) ;
281+ queryClient . invalidateQueries ( [ QueryKeys . mcpConnectionStatus ] ) ;
271282
272- showToast ( {
273- message : localize ( 'com_ui_mcp_oauth_cancelled' , { 0 : serverName } ) ,
274- status : 'warning' ,
283+ showToast ( {
284+ message : localize ( 'com_ui_mcp_oauth_cancelled' , { 0 : serverName } ) ,
285+ status : 'warning' ,
286+ } ) ;
287+ } ,
288+ onError : ( error ) => {
289+ console . error ( `[MCP Manager] Failed to cancel OAuth for ${ serverName } :` , error ) ;
290+ showToast ( {
291+ message : localize ( 'com_ui_mcp_init_failed' , { 0 : serverName } ) ,
292+ status : 'error' ,
293+ } ) ;
294+ } ,
275295 } ) ;
276296 } ,
277297 [ queryClient , cleanupServerState , showToast , localize , cancelOAuthMutation ] ,
@@ -309,6 +329,10 @@ export function useMCPServerManager() {
309329 const disconnectedServers : string [ ] = [ ] ;
310330
311331 serverNames . forEach ( ( serverName ) => {
332+ if ( isInitializing ( serverName ) ) {
333+ return ;
334+ }
335+
312336 const serverStatus = connectionStatus [ serverName ] ;
313337 if ( serverStatus ?. connectionState === 'connected' ) {
314338 connectedServers . push ( serverName ) ;
@@ -323,11 +347,15 @@ export function useMCPServerManager() {
323347 initializeServer ( serverName ) ;
324348 } ) ;
325349 } ,
326- [ connectionStatus , setMCPValues , initializeServer ] ,
350+ [ connectionStatus , setMCPValues , initializeServer , isInitializing ] ,
327351 ) ;
328352
329353 const toggleServerSelection = useCallback (
330354 ( serverName : string ) => {
355+ if ( isInitializing ( serverName ) ) {
356+ return ;
357+ }
358+
331359 const currentValues = mcpValues ?? [ ] ;
332360 const isCurrentlySelected = currentValues . includes ( serverName ) ;
333361
@@ -343,7 +371,7 @@ export function useMCPServerManager() {
343371 }
344372 }
345373 } ,
346- [ mcpValues , setMCPValues , connectionStatus , initializeServer ] ,
374+ [ mcpValues , setMCPValues , connectionStatus , initializeServer , isInitializing ] ,
347375 ) ;
348376
349377 const handleConfigSave = useCallback (
0 commit comments