Skip to content

Commit 076e0ec

Browse files
fix(compat): parse the v0.3 REST extended agent card as JSON (a2aproject#1420)
## Problem The compat REST client parsed the extended agent card as proto JSON, so security schemes and requirements were lost. ## Fix Parse it with `parse_agent_card`, the same path the card resolver uses, and log missing required fields with `warn_on_missing_required_fields` (a2aproject#1279). A card missing required fields still parses, as before. ## Tests `test_compat_rest_transport_get_extended_agent_card_keeps_security` fails on main. It uses the payload `REST03Handler` returns. The existing minimal-card test is unchanged. 2520 passed. Fixes a2aproject#1418 --------- Co-authored-by: Mateusz Iwaniuk <45974414+Iwaniukooo11@users.noreply.github.com>
1 parent 28f8a62 commit 076e0ec

2 files changed

Lines changed: 58 additions & 6 deletions

File tree

‎src/a2a/compat/v0_3/rest_transport.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from google.protobuf.json_format import MessageToDict, Parse, ParseDict
1111

12+
from a2a.client.card_resolver import parse_agent_card
1213
from a2a.client.client import ClientCallContext
1314
from a2a.client.errors import A2AClientError
1415
from a2a.client.transports.base import ClientTransport
@@ -46,6 +47,7 @@
4647
)
4748
from a2a.utils.constants import PROTOCOL_VERSION_0_3, VERSION_HEADER
4849
from a2a.utils.errors import JSON_RPC_ERROR_CODE_MAP, MethodNotFoundError
50+
from a2a.utils.proto_utils import warn_on_missing_required_fields
4951
from a2a.utils.telemetry import SpanKind, trace_class
5052

5153

@@ -343,12 +345,11 @@ async def get_extended_agent_card(
343345
response_data = await self._execute_request(
344346
'GET', '/v1/card', context=context
345347
)
346-
resp_proto = ParseDict(
347-
response_data, a2a_v0_3_pb2.AgentCard(), ignore_unknown_fields=True
348-
)
349-
card = conversions.to_core_agent_card(
350-
proto_utils.FromProto.agent_card(resp_proto)
351-
)
348+
# v0.3 servers send the card as JSON. parse_agent_card reads it like
349+
# the card resolver does, and still accepts a card missing required
350+
# fields, which only gets a warning.
351+
card = parse_agent_card(response_data)
352+
warn_on_missing_required_fields(card, 'v0.3 extended agent card')
352353
self.agent_card = card
353354
return card
354355

‎tests/compat/v0_3/test_rest_transport.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pytest
77

88
from a2a.client.errors import A2AClientError
9+
from a2a.compat.v0_3 import types as types_v03
910
from a2a.compat.v0_3.rest_transport import CompatRestTransport
1011
from a2a.types.a2a_pb2 import (
1112
AgentCapabilities,
@@ -191,6 +192,56 @@ async def mock_send_request(*args, **kwargs):
191192
assert response.capabilities.extended_agent_card is True
192193

193194

195+
@pytest.mark.asyncio
196+
async def test_compat_rest_transport_get_extended_agent_card_keeps_security(
197+
transport,
198+
):
199+
"""v0.3 servers send the card as JSON, not as the v0.3 proto."""
200+
card = types_v03.AgentCard(
201+
name='ExtendedAgent',
202+
description='Extended',
203+
version='1.0.0',
204+
url='http://example.com',
205+
capabilities=types_v03.AgentCapabilities(),
206+
default_input_modes=['text/plain'],
207+
default_output_modes=['text/plain'],
208+
skills=[
209+
types_v03.AgentSkill(
210+
id='skill-1',
211+
name='Skill',
212+
description='Skill',
213+
tags=['tag'],
214+
security=[{'bearer': []}],
215+
)
216+
],
217+
security_schemes={
218+
'bearer': types_v03.SecurityScheme(
219+
root=types_v03.HTTPAuthSecurityScheme(scheme='bearer')
220+
)
221+
},
222+
security=[{'bearer': []}],
223+
supports_authenticated_extended_card=True,
224+
)
225+
226+
async def mock_send_request(*args, **kwargs):
227+
# Same serialization as REST03Handler.on_get_extended_agent_card.
228+
return card.model_dump(mode='json', exclude_none=True)
229+
230+
transport._send_request = mock_send_request
231+
232+
response = await transport.get_extended_agent_card(
233+
GetExtendedAgentCardRequest()
234+
)
235+
236+
scheme = response.security_schemes['bearer']
237+
assert scheme.WhichOneof('scheme') == 'http_auth_security_scheme'
238+
assert scheme.http_auth_security_scheme.scheme == 'bearer'
239+
assert list(response.security_requirements[0].schemes) == ['bearer']
240+
assert list(response.skills[0].security_requirements[0].schemes) == [
241+
'bearer'
242+
]
243+
244+
194245
@pytest.mark.asyncio
195246
async def test_compat_rest_transport_get_extended_agent_card_not_supported(
196247
transport,

0 commit comments

Comments
 (0)