"""Session state for the Birdcage MCP server."""

from dataclasses import dataclass, field


@dataclass
class BirdcageState:
    """Mutable session state shared across all MCP tools.

    Populated by the server lifespan and mutated by the ``connect``
    and ``disconnect`` tools. Device and craft_client are typed as
    ``object`` to accept both real (SerialBridge/CraftClient) and
    demo (DemoDevice/DemoCraftClient) implementations without
    importing them at definition time.
    """

    device: object = None
    craft_client: object = None
    demo_mode: bool = False
    serial_port: str = ""
    firmware_name: str = "g2"
    connected: bool = False
    _errors: list[str] = field(default_factory=list)

    def record_error(self, msg: str) -> None:
        """Append an error message (capped at 50 entries)."""
        self._errors.append(msg)
        if len(self._errors) > 50:
            self._errors = self._errors[-50:]
