Skip to content

http_transport

Class info

Classes

Name Children Inherits
HttpTransportError
aclientp.http_transport
The ACP server rejected the connection or became unreachable.

    🛈 DocStrings

    Client side of ACP's Streamable HTTP transport (experimental).

    Implements the client of the "Streamable HTTP & WebSocket Transport" RFD (docs/rfds/streamable-http-websocket-transport.mdx in the ACP repository), behaving like the TypeScript SDK's createHttpStream (@agentclientprotocol/sdk/experimental):

    • POST /acp sends each client message. initialize is answered directly (200, with the Acp-Connection-Id header); everything else is answered 202 and the response arrives on an SSE stream.
    • GET /acp opens the SSE streams: one for the connection (responses to session/new and session/load, connection-level messages), and one per session (Acp-Session-Id), opened as soon as a response names the session.
    • DELETE /acp ends the connection on close. Cookies are kept for the connection.

    The transport is exposed as a pair of byte streams carrying newline-delimited JSON-RPC, like stdio, so any connection (or a stdio relay) can use it unchanged.

    Failure handling follows the TypeScript SDK: HTTP 401, 403, 404 and 410, network errors and failed POSTs of notifications or responses close the transport. Any other HTTP error fails only the request, with a JSON-RPC error whose data is {status, body}. HTTP 409 marks a single session as lost: its requests fail until session/load or session/resume reattaches it. ACP v1 does not replay messages missed while a stream was down.

    CONNECTION_FAILURE_STATUSES module-attribute

    CONNECTION_FAILURE_STATUSES = frozenset({401, 403, 404, 410})
    

    POST statuses meaning the whole connection is unusable or gone.

    SESSION_CONFLICT_STATUS module-attribute

    SESSION_CONFLICT_STATUS = 409
    

    Status meaning a session is no longer attached to this connection.

    HttpTransportError

    Bases: ConnectionError

    The ACP server rejected the connection or became unreachable.

    Source code in src/aclientp/http_transport.py
    class HttpTransportError(ConnectionError):
        """The ACP server rejected the connection or became unreachable."""
    

    connect_http_transport async

    connect_http_transport(
        url: str,
        *,
        headers: Mapping[str, str] | None = None,
        client: AsyncClient | None = None,
        timeout: float = 30.0,
    ) -> AsyncGenerator[tuple[ByteReceiveStream, ByteSendStream]]
    

    Connect to an ACP server's Streamable HTTP endpoint.

    The streams carry newline-delimited JSON-RPC, like a stdio agent's. The first message written must be initialize.

    Parameters:

    Name Type Description Default
    url str

    The /acp endpoint.

    required
    headers Mapping[str, str] | None

    Extra headers for every request (e.g. authentication).

    None
    client AsyncClient | None

    HTTP client to use (its cookies and settings apply). By default a client is created, using HTTP/2 when the h2 package is installed.

    None
    timeout float

    Timeout for connecting and for POSTs (SSE streams have no read timeout).

    30.0

    Yields:

    Type Description
    AsyncGenerator[tuple[ByteReceiveStream, ByteSendStream]]

    (reader, writer) byte streams.

    Source code in src/aclientp/http_transport.py
    @asynccontextmanager
    async def connect_http_transport(
        url: str,
        *,
        headers: Mapping[str, str] | None = None,
        client: httpx2.AsyncClient | None = None,
        timeout: float = 30.0,
    ) -> AsyncGenerator[tuple[ByteReceiveStream, ByteSendStream]]:
        """Connect to an ACP server's Streamable HTTP endpoint.
    
        The streams carry newline-delimited JSON-RPC, like a stdio agent's. The first message
        written must be ``initialize``.
    
        Args:
            url: The ``/acp`` endpoint.
            headers: Extra headers for every request (e.g. authentication).
            client: HTTP client to use (its cookies and settings apply). By default a client
                is created, using HTTP/2 when the ``h2`` package is installed.
            timeout: Timeout for connecting and for POSTs (SSE streams have no read timeout).
    
        Yields:
            (reader, writer) byte streams.
        """
        owned = client is None
        if client is None:
            client = httpx2.AsyncClient(
                http2=_http2_available(),
                headers=dict(headers or {}),
                timeout=httpx2.Timeout(timeout, read=None),
            )
        elif headers:
            client.headers.update(headers)
        send_inbox, receive_inbox = anyio.create_memory_object_stream[bytes](max_buffer_size=1024)
        try:
            async with anyio.create_task_group() as tg:
                connection = _HttpConnection(url, client, tg, send_inbox)
                try:
                    yield _InboxReader(receive_inbox), _PostWriter(connection)
                finally:
                    await connection.close_inbox()
                    await connection.terminate()
                    tg.cancel_scope.cancel()
        finally:
            if owned:
                await client.aclose()