resonance.mcp.connection

resonance.mcp.connection

Connection management for Beamline in MCP context.

ConnectionManager

Singleton connection manager for Beamline.

is_connected property

Check if beamline is connected.

get_server() async

Get or create Beamline instance.

Returns:

Type Description
Beamline

Connected beamline instance

Raises:

Type Description
ConnectionError

If connection fails

Source code in src/resonance/mcp/connection.py
async def get_server(self) -> Beamline:
    """
    Get or create Beamline instance.

    Returns
    -------
    Beamline
        Connected beamline instance

    Raises
    ------
    ConnectionError
        If connection fails
    """
    async with self._lock:
        if self._server is None or not self._connected:
            self._server = await Beamline.create()
            self._connected = True
        return self._server

ensure_connected() async

Ensure beamline is connected, reconnect if needed.

Raises:

Type Description
ConnectionError

If reconnection fails

RuntimeError

If connection state is inconsistent

Source code in src/resonance/mcp/connection.py
async def ensure_connected(self) -> None:
    """
    Ensure beamline is connected, reconnect if needed.

    Raises
    ------
    ConnectionError
        If reconnection fails
    RuntimeError
        If connection state is inconsistent
    """
    async with self._lock:
        if self._server is None or not self._connected:
            try:
                self._server = await Beamline.create()
                self._connected = True
            except ConnectionError as e:
                self._connected = False
                self._server = None
                raise ConnectionError(
                    f"Failed to connect to beamline server: {e}"
                ) from e
            except Exception as e:
                self._connected = False
                self._server = None
                raise RuntimeError(
                    f"Unexpected error connecting to beamline server: {e}"
                ) from e

disconnect() async

Disconnect from beamline.

Source code in src/resonance/mcp/connection.py
async def disconnect(self) -> None:
    """Disconnect from beamline."""
    async with self._lock:
        self._connected = False
        self._server = None