Skip to content

SdkMcpBridge

🛈 DocStrings

Routes raw JSON-RPC for one in-process MCP server through mcp's memory transport.

Source code in src/clawd_code_sdk/_internal/sdk_mcp_bridge.py
class SdkMcpBridge:
    """Routes raw JSON-RPC for one in-process MCP server through mcp's memory transport."""

    def __init__(self, name: str, server: McpServer) -> None:
        self.name = name
        self._server = server
        self._session: _Session | None = None
        self._closed = False

    async def handle(self, message: dict[str, Any]) -> dict[str, Any] | None:
        """Handle one message from the CLI.

        Returns the JSON-RPC response dict for requests and ``None`` for
        notifications and responses, which expect no reply.
        """
        if self._closed:
            raise SdkMcpBridgeError(f"SDK MCP server {self.name!r} is closed")
        session = self._session
        if session is None:
            session = self._session = _Session(self.name, self._server)
        elif session.finished:
            # The server stopped underneath the CLI. It stays stopped until
            # the CLI starts it again with a new handshake.
            if message.get("method") != "initialize":
                cause = session.failure or "its run() returned"
                raise SdkMcpBridgeError(f"SDK MCP server {self.name!r} stopped: {cause}")
            stopped, session = session, _Session(self.name, self._server)
            self._session = session
            await stopped.aclose()
        pending = await session.submit(message)
        if pending is None:
            return None
        return await pending.wait()

    async def aclose(self) -> None:
        """Stop the session, if any. Safe to call more than once."""
        self._closed = True
        if self._session is not None:
            session, self._session = self._session, None
            await session.aclose()

aclose async

aclose() -> None

Stop the session, if any. Safe to call more than once.

Source code in src/clawd_code_sdk/_internal/sdk_mcp_bridge.py
async def aclose(self) -> None:
    """Stop the session, if any. Safe to call more than once."""
    self._closed = True
    if self._session is not None:
        session, self._session = self._session, None
        await session.aclose()

handle async

handle(message: dict[str, Any]) -> dict[str, Any] | None

Handle one message from the CLI.

Returns the JSON-RPC response dict for requests and None for notifications and responses, which expect no reply.

Source code in src/clawd_code_sdk/_internal/sdk_mcp_bridge.py
async def handle(self, message: dict[str, Any]) -> dict[str, Any] | None:
    """Handle one message from the CLI.

    Returns the JSON-RPC response dict for requests and ``None`` for
    notifications and responses, which expect no reply.
    """
    if self._closed:
        raise SdkMcpBridgeError(f"SDK MCP server {self.name!r} is closed")
    session = self._session
    if session is None:
        session = self._session = _Session(self.name, self._server)
    elif session.finished:
        # The server stopped underneath the CLI. It stays stopped until
        # the CLI starts it again with a new handshake.
        if message.get("method") != "initialize":
            cause = session.failure or "its run() returned"
            raise SdkMcpBridgeError(f"SDK MCP server {self.name!r} stopped: {cause}")
        stopped, session = session, _Session(self.name, self._server)
        self._session = session
        await stopped.aclose()
    pending = await session.submit(message)
    if pending is None:
        return None
    return await pending.wait()

:fa-brands-github: Show source on GitHub