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 +}