From b4ecadeb9d4e86cdb7dd5758ebef22bbd142f7db Mon Sep 17 00:00:00 2001 From: Iwaniukooo11 Date: Tue, 17 Nov 2026 12:46:16 +0000 Subject: [PATCH 1/3] feat: add selected interface to client --- src/a2a/client/base_client.py | 10 ++++- src/a2a/client/client_factory.py | 1 + src/a2a/plugins/types.py | 6 ++- tests/client/test_base_client_plugins.py | 49 ++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/a2a/client/base_client.py b/src/a2a/client/base_client.py index fbcdb955e..dbc2e795f 100644 --- a/src/a2a/client/base_client.py +++ b/src/a2a/client/base_client.py @@ -17,6 +17,7 @@ from a2a.plugins.types import ClientRequest, ClientResponse from a2a.types.a2a_pb2 import ( AgentCard, + AgentInterface, CancelTaskRequest, DeleteTaskPushNotificationConfigRequest, GetExtendedAgentCardRequest, @@ -41,13 +42,14 @@ class BaseClient(Client): the interceptors, ``after_client_request`` after them. """ - def __init__( + def __init__( # noqa: PLR0913 self, card: AgentCard, config: ClientConfig, transport: ClientTransport, interceptors: list[ClientCallInterceptor], *, + interface: AgentInterface | None = None, plugins: PluginManager | None = None, ): """Initializes the client. @@ -57,6 +59,10 @@ def __init__( config: The client configuration. transport: The transport used for calls. interceptors: Interceptors applied to every call. + interface: The card interface this client talks to, as selected + by `ClientFactory`. A card can list several, so plugins + cannot tell which is in use from the card alone. Defaults to + None for clients built without the factory. plugins: Plugin manager whose client hooks run for every call. Defaults to the global manager at construction time. """ @@ -65,6 +71,7 @@ def __init__( self._config = config self._transport = transport self._interceptors = interceptors + self._interface = interface self._plugins = plugins if plugins is not None else get_global() async def send_message( @@ -411,6 +418,7 @@ def _client_request( payload=input_data, agent_card=self._card, context=context if context is not None else ClientCallContext(), + interface=self._interface, ) async def _execute_with_interceptors( diff --git a/src/a2a/client/client_factory.py b/src/a2a/client/client_factory.py index 14d2ab107..ef8ac446c 100644 --- a/src/a2a/client/client_factory.py +++ b/src/a2a/client/client_factory.py @@ -396,6 +396,7 @@ def create( self._config, transport, interceptors or [], + interface=selected_interface, ) diff --git a/src/a2a/plugins/types.py b/src/a2a/plugins/types.py index 922dcdf0e..bcb309cc1 100644 --- a/src/a2a/plugins/types.py +++ b/src/a2a/plugins/types.py @@ -18,7 +18,7 @@ from a2a.server.tasks.push_notification_sender import ( PushNotificationEvent, ) - from a2a.types.a2a_pb2 import AgentCard, Task, TaskState + from a2a.types.a2a_pb2 import AgentCard, AgentInterface, Task, TaskState @dataclass @@ -77,12 +77,16 @@ class ClientRequest: agent_card: The card of the remote agent. context: The call context. ``service_parameters`` holds the outgoing headers. + interface: The Agent Card interface the client selected, which gives + the URL, protocol binding and protocol version actually in use. + ``None`` for clients not built by `ClientFactory`. """ method: str payload: Any agent_card: AgentCard context: ClientCallContext + interface: AgentInterface | None = None @dataclass diff --git a/tests/client/test_base_client_plugins.py b/tests/client/test_base_client_plugins.py index dbb375e7e..dc300a90d 100644 --- a/tests/client/test_base_client_plugins.py +++ b/tests/client/test_base_client_plugins.py @@ -323,6 +323,55 @@ def test_factory_clients_use_the_global_manager(): assert c._plugins is manager +@pytest.mark.asyncio +async def test_factory_clients_report_the_selected_interface(): + """Plugins see the card interface the factory actually picked.""" + recorder = Recorder() + plugins.set_global(plugins.sequential(recorder)) + picked = AgentInterface( + url='http://remote:9000', + protocol_binding='JSONRPC', + protocol_version='1.0', + ) + multi = AgentCard( + name='Remote', + version='1.0', + capabilities=AgentCapabilities(streaming=True), + supported_interfaces=[ + AgentInterface( + url='http://other:1234', + protocol_binding='HTTP+JSON', + protocol_version='1.0', + ), + picked, + ], + ) + + c = ClientFactory( + ClientConfig(httpx_client=httpx_lib.AsyncClient()) + ).create(multi) + c._transport = AsyncMock(spec=ClientTransport) + await c.get_task(GetTaskRequest(id='t-1')) + + (request,) = recorder.hooks('before_client_request') + # The card lists two interfaces; the plugin gets the one in use. + assert request.interface == picked + assert request.interface.protocol_version == '1.0' + + +@pytest.mark.asyncio +async def test_clients_built_without_the_factory_report_no_interface(): + """A hand-built client leaves `interface` None rather than guessing.""" + recorder = Recorder() + transport = AsyncMock(spec=ClientTransport) + transport.get_task.return_value = Task(id='t-1') + + await client(recorder, transport).get_task(GetTaskRequest(id='t-1')) + + (request,) = recorder.hooks('before_client_request') + assert request.interface is None + + @pytest.mark.parametrize( ('call', 'method'), [ From d8f00e4ae11bdabe1c3837dfd9e0d41b2785108e Mon Sep 17 00:00:00 2001 From: Iwaniukooo11 Date: Wed, 18 Nov 2026 09:39:54 +0000 Subject: [PATCH 2/3] test: refactor test --- tests/client/test_base_client_plugins.py | 50 +----------------------- tests/client/test_client_factory.py | 1 + 2 files changed, 2 insertions(+), 49 deletions(-) diff --git a/tests/client/test_base_client_plugins.py b/tests/client/test_base_client_plugins.py index dc300a90d..420f2f5cc 100644 --- a/tests/client/test_base_client_plugins.py +++ b/tests/client/test_base_client_plugins.py @@ -120,6 +120,7 @@ async def test_unary_call_runs_hooks(): (request,) = recorder.hooks('before_client_request') assert request.method == 'get_task' assert request.agent_card.name == 'Remote' + assert request.interface is None (response,) = recorder.hooks('after_client_request') assert response.payload is result assert response.is_final @@ -323,55 +324,6 @@ def test_factory_clients_use_the_global_manager(): assert c._plugins is manager -@pytest.mark.asyncio -async def test_factory_clients_report_the_selected_interface(): - """Plugins see the card interface the factory actually picked.""" - recorder = Recorder() - plugins.set_global(plugins.sequential(recorder)) - picked = AgentInterface( - url='http://remote:9000', - protocol_binding='JSONRPC', - protocol_version='1.0', - ) - multi = AgentCard( - name='Remote', - version='1.0', - capabilities=AgentCapabilities(streaming=True), - supported_interfaces=[ - AgentInterface( - url='http://other:1234', - protocol_binding='HTTP+JSON', - protocol_version='1.0', - ), - picked, - ], - ) - - c = ClientFactory( - ClientConfig(httpx_client=httpx_lib.AsyncClient()) - ).create(multi) - c._transport = AsyncMock(spec=ClientTransport) - await c.get_task(GetTaskRequest(id='t-1')) - - (request,) = recorder.hooks('before_client_request') - # The card lists two interfaces; the plugin gets the one in use. - assert request.interface == picked - assert request.interface.protocol_version == '1.0' - - -@pytest.mark.asyncio -async def test_clients_built_without_the_factory_report_no_interface(): - """A hand-built client leaves `interface` None rather than guessing.""" - recorder = Recorder() - transport = AsyncMock(spec=ClientTransport) - transport.get_task.return_value = Task(id='t-1') - - await client(recorder, transport).get_task(GetTaskRequest(id='t-1')) - - (request,) = recorder.hooks('before_client_request') - assert request.interface is None - - @pytest.mark.parametrize( ('call', 'method'), [ diff --git a/tests/client/test_client_factory.py b/tests/client/test_client_factory.py index 269090c83..ecb4f65f0 100644 --- a/tests/client/test_client_factory.py +++ b/tests/client/test_client_factory.py @@ -88,6 +88,7 @@ def test_client_factory_selects_secondary_transport_url( assert isinstance(client._transport, RestTransport) # type: ignore[attr-defined] assert client._transport.url == 'http://secondary-url.com' # type: ignore[attr-defined] + assert client._interface.url == 'http://secondary-url.com' # type: ignore[attr-defined] def test_client_factory_server_preference(base_agent_card: AgentCard): From 7787c6eb33f4c3ba13f2848dab39242a69143daa Mon Sep 17 00:00:00 2001 From: Iwaniukooo11 Date: Wed, 18 Nov 2026 10:16:45 +0000 Subject: [PATCH 3/3] fix comments --- src/a2a/client/base_client.py | 5 +---- src/a2a/plugins/types.py | 4 +--- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/a2a/client/base_client.py b/src/a2a/client/base_client.py index dbc2e795f..9c5298653 100644 --- a/src/a2a/client/base_client.py +++ b/src/a2a/client/base_client.py @@ -59,10 +59,7 @@ def __init__( # noqa: PLR0913 config: The client configuration. transport: The transport used for calls. interceptors: Interceptors applied to every call. - interface: The card interface this client talks to, as selected - by `ClientFactory`. A card can list several, so plugins - cannot tell which is in use from the card alone. Defaults to - None for clients built without the factory. + interface: The card interface this client talks to. plugins: Plugin manager whose client hooks run for every call. Defaults to the global manager at construction time. """ diff --git a/src/a2a/plugins/types.py b/src/a2a/plugins/types.py index bcb309cc1..00acd51a0 100644 --- a/src/a2a/plugins/types.py +++ b/src/a2a/plugins/types.py @@ -77,9 +77,7 @@ class ClientRequest: agent_card: The card of the remote agent. context: The call context. ``service_parameters`` holds the outgoing headers. - interface: The Agent Card interface the client selected, which gives - the URL, protocol binding and protocol version actually in use. - ``None`` for clients not built by `ClientFactory`. + interface: The Agent Card interface the client selected. """ method: str