diff --git a/src/mcbluetooth_esp32/tools/connection.py b/src/mcbluetooth_esp32/tools/connection.py index 993e88b..15fab26 100644 --- a/src/mcbluetooth_esp32/tools/connection.py +++ b/src/mcbluetooth_esp32/tools/connection.py @@ -8,7 +8,7 @@ from typing import Any from fastmcp import FastMCP from ..protocol import CMD_GET_INFO, CMD_GET_STATUS, CMD_PING, CMD_RESET, Status -from ..serial_client import NotConnected, get_client, init_client +from ..serial_client import CommandTimeout, NotConnected, get_client, init_client def register_tools(mcp: FastMCP) -> None: @@ -51,11 +51,25 @@ def register_tools(mcp: FastMCP) -> None: except TimeoutError: pass + # Ready probe: retry ping until the firmware's command handler is up. + # The boot event fires early in app_main, before the UART command + # task is fully initialised, so the first command can get lost. + ready = False + for attempt in range(5): + try: + resp = await client.send_command(CMD_PING, timeout=1.0) + if resp.status == Status.OK: + ready = True + break + except (CommandTimeout, NotConnected): + await asyncio.sleep(0.3) + return { "connected": True, "port": port, "baudrate": baudrate, "boot_event": boot_received, + "ready": ready, } @mcp.tool()