From 210e15d8d159b85235249111d8eacfa06cd33258 Mon Sep 17 00:00:00 2001 From: josedario87 Date: Sat, 14 Feb 2026 17:41:28 -0600 Subject: [PATCH] fix: Wait for connection to be established before returning - Add waitForConnection to ensure store is updated - Poll isConnected state with timeout - Prevents tool registration before connection is ready --- frontend/src/services/webmcp.ts | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/frontend/src/services/webmcp.ts b/frontend/src/services/webmcp.ts index 9c1d884..987f120 100644 --- a/frontend/src/services/webmcp.ts +++ b/frontend/src/services/webmcp.ts @@ -404,9 +404,39 @@ export async function connectWithToken(token: string): Promise { // Connect passing the token directly try { await webmcpInstance.connect(finalToken) - return true + + // Wait for connection to be fully established (event handler updates store) + const connected = await waitForConnection(3000) + if (!connected) { + console.warn('[WebMCP] Connection timeout - store not updated') + } + return connected } catch (e) { console.error('[WebMCP] Failed to connect:', e) return false } } + +/** + * Wait for isConnected to become true + */ +async function waitForConnection(timeoutMs: number = 3000): Promise { + const canvasStore = useCanvasStore() + + // Already connected + if (canvasStore.isConnected) { + return true + } + + // Wait for connection with polling + const startTime = Date.now() + while (Date.now() - startTime < timeoutMs) { + await new Promise(resolve => setTimeout(resolve, 100)) + if (canvasStore.isConnected) { + console.log('[WebMCP] Connection confirmed') + return true + } + } + + return false +}