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
This commit is contained in:
2026-02-14 17:41:28 -06:00
parent c280e974c0
commit 210e15d8d1

View File

@@ -404,9 +404,39 @@ export async function connectWithToken(token: string): Promise<boolean> {
// 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<boolean> {
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
}