Skip to content

ClientSideConnection

Base classes

Name Children Inherits
Agent
aclientp.agent.protocol
Base agent interface for ACP.

⋔ Inheritance diagram

graph TD
  95970860501488["connection.ClientSideConnection"]
  95970860128304["protocol.Agent"]
  95970836711072["typing.Protocol"]
  95970835676112["typing.Generic"]
  125067848591872["builtins.object"]
  95970860128304 --> 95970860501488
  95970836711072 --> 95970860128304
  95970835676112 --> 95970836711072
  125067848591872 --> 95970835676112

🛈 DocStrings

Bases: Agent

Client-side connection.

Use when you implement the Client and need to talk to an Agent.

Parameters:

Name Type Description Default
to_client Callable[[Agent], Client] | Client

factory that receives this connection and returns your Client

required
input

ByteSendStream (local -> peer)

required
output

ByteReceiveStream (peer -> local)

required
Source code in src/aclientp/client/connection.py
class ClientSideConnection(Agent):
    """Client-side connection.

    Use when you implement the Client and need to talk to an Agent.

    Args:
      to_client: factory that receives this connection and returns your Client
      input: ByteSendStream (local -> peer)
      output: ByteReceiveStream (peer -> local)
    """

    def __init__(
        self,
        to_client: Callable[[Agent], Client] | Client,
        input_stream: ByteSendStream,
        output_stream: ByteReceiveStream,
        observers: list[StreamObserver] | None = None,
    ) -> None:
        # Build client first so handler can delegate
        client = to_client if isinstance(to_client, Client) else to_client(self)
        handler = partial(_handle_client_method, client)
        self._conn = Connection(handler, input_stream, output_stream, observers=observers)

    async def _request[T: BaseModel](
        self, method: str, params: BaseModel, response_type: type[T]
    ) -> T:
        dct = params.model_dump(mode="json", by_alias=True, exclude_none=True)
        resp = await self._conn.request_object(method, dct)
        return response_type.model_validate(resp)

    # agent-bound methods (client -> agent)
    async def initialize(self, params: InitializeRequest) -> InitializeResponse:
        dct = params.model_dump(
            mode="json", by_alias=True, exclude_none=True, exclude_defaults=True
        )
        resp = await self._conn.request_object("initialize", dct)
        return InitializeResponse.model_validate(resp)

    async def new_session(self, params: NewSessionRequest) -> NewSessionResponse:
        # Don't exclude_defaults - MCP server 'type' field is required for discriminated unions
        dct = params.model_dump(mode="json", by_alias=True, exclude_none=True)
        resp = await self._conn.request_object("session/new", dct)
        return NewSessionResponse.model_validate(resp)

    async def load_session(self, params: LoadSessionRequest) -> LoadSessionResponse:
        dct = params.model_dump(
            mode="json", by_alias=True, exclude_none=True, exclude_defaults=True
        )
        resp = await self._conn.request_object("session/load", dct)
        return LoadSessionResponse.model_validate(resp)

    async def list_sessions(self, params: ListSessionsRequest) -> ListSessionsResponse:
        dct = params.model_dump(
            mode="json", by_alias=True, exclude_none=True, exclude_defaults=True
        )
        resp = await self._conn.request_object("session/list", dct)
        return ListSessionsResponse.model_validate(resp)

    async def fork_session(self, params: ForkSessionRequest) -> ForkSessionResponse:
        dct = params.model_dump(
            mode="json", by_alias=True, exclude_none=True, exclude_defaults=True
        )
        resp = await self._conn.request_object("session/fork", dct)
        return ForkSessionResponse.model_validate(resp)

    async def resume_session(self, params: ResumeSessionRequest) -> ResumeSessionResponse:
        dct = params.model_dump(
            mode="json", by_alias=True, exclude_none=True, exclude_defaults=True
        )
        resp = await self._conn.request_object("session/resume", dct)
        return ResumeSessionResponse.model_validate(resp)

    async def close_session(self, params: CloseSessionRequest) -> CloseSessionResponse:
        dct = params.model_dump(
            mode="json", by_alias=True, exclude_none=True, exclude_defaults=True
        )
        resp = await self._conn.request_object("session/close", dct)
        return CloseSessionResponse.model_validate(resp)

    async def set_session_mode(self, params: SetSessionModeRequest) -> SetSessionModeResponse:
        dct = params.model_dump(
            mode="json", by_alias=True, exclude_none=True, exclude_defaults=True
        )
        resp = await self._conn.request_object("session/set_mode", dct)
        return SetSessionModeResponse.model_validate(resp)

    async def delete_session(self, params: DeleteSessionRequest) -> DeleteSessionResponse:
        return await self._request("session/delete", params, DeleteSessionResponse)

    async def archive_session(self, params: ArchiveSessionRequest) -> ArchiveSessionResponse:
        return await self._request("session/archive", params, ArchiveSessionResponse)

    async def unarchive_session(self, params: UnarchiveSessionRequest) -> UnarchiveSessionResponse:
        return await self._request("session/unarchive", params, UnarchiveSessionResponse)

    async def session_status(self, params: SessionStatusRequest) -> SessionStatusResponse:
        return await self._request("session/status", params, SessionStatusResponse)

    async def session_options(self, params: SessionOptionsRequest) -> SessionOptionsResponse:
        return await self._request("session/options", params, SessionOptionsResponse)

    async def set_session_title(self, params: SetSessionTitleRequest) -> SetSessionTitleResponse:
        return await self._request("session/set_title", params, SetSessionTitleResponse)

    async def auth_status(self, params: AuthStatusRequest) -> AuthStatusResponse:
        return await self._request("auth/status", params, AuthStatusResponse)

    async def list_providers(self, params: ListProvidersRequest) -> ListProvidersResponse:
        return await self._request("providers/list", params, ListProvidersResponse)

    async def set_provider(self, params: SetProviderRequest) -> SetProviderResponse:
        return await self._request("providers/set", params, SetProviderResponse)

    async def disable_provider(self, params: DisableProviderRequest) -> DisableProviderResponse:
        return await self._request("providers/disable", params, DisableProviderResponse)

    async def mcp_message(self, params: MessageMcpRequest) -> MessageMcpResponse:
        return await self._request("mcp/message", params, MessageMcpResponse)

    async def mcp_notification(self, params: MessageMcpNotification) -> None:
        dct = params.model_dump(mode="json", by_alias=True, exclude_none=True)
        await self._conn.send_notification("mcp/message", dct)

    async def set_session_config_option(
        self, params: SetSessionConfigOptionRequest
    ) -> SetSessionConfigOptionResponse:
        dct = params.model_dump(
            mode="json", by_alias=True, exclude_none=True, exclude_defaults=True
        )
        resp = await self._conn.request_object("session/set_config_option", dct)
        return SetSessionConfigOptionResponse.model_validate(resp)

    async def authenticate(self, params: AuthenticateRequest) -> AuthenticateResponse:
        dct = params.model_dump(
            mode="json", by_alias=True, exclude_none=True, exclude_defaults=True
        )
        resp = await self._conn.request_object("authenticate", dct)
        return AuthenticateResponse.model_validate(resp)

    async def logout(self, params: LogoutRequest) -> LogoutResponse:
        dct = params.model_dump(
            mode="json", by_alias=True, exclude_none=True, exclude_defaults=True
        )
        resp = await self._conn.request_object("logout", dct)
        return LogoutResponse.model_validate(resp)

    async def prompt(self, params: PromptRequest) -> PromptResponse:
        # Don't exclude_defaults here - the 'type' field in content blocks has a default
        # value but is required for discriminated unions to work
        dct = params.model_dump(mode="json", by_alias=True, exclude_none=True)
        # The turn's updates, sent before the response, are handled when this returns
        resp = await self._conn.request_object("session/prompt", dct)
        return PromptResponse.model_validate(resp)

    async def cancel(self, params: CancelNotification) -> None:
        dct = params.model_dump(
            mode="json", by_alias=True, exclude_none=True, exclude_defaults=True
        )
        await self._conn.send_notification("session/cancel", dct)

    async def ext_method(self, method: str, params: dict[str, Any]) -> dict[str, Any]:
        return await self._conn.request_object(f"_{method}", params)

    async def ext_notification(self, method: str, params: dict[str, Any]) -> None:
        await self._conn.send_notification(f"_{method}", params)

    async def close(self) -> None:
        await self._conn.close()

    async def wait_closed(self) -> None:
        """Wait until the agent disconnected or the connection was closed."""
        await self._conn.wait_closed()

    async def __aenter__(self) -> Self:
        return self

    async def __aexit__(self, *args: object) -> None:
        await self.close()

wait_closed async

wait_closed() -> None

Wait until the agent disconnected or the connection was closed.

Source code in src/aclientp/client/connection.py
async def wait_closed(self) -> None:
    """Wait until the agent disconnected or the connection was closed."""
    await self._conn.wait_closed()

:fa-brands-github: Show source on GitHub