From 4f27da93f1dca41e0d1115bafe4f42a058b9dacc Mon Sep 17 00:00:00 2001 From: 0z5a Date: Mon, 16 Nov 2026 14:18:17 +0100 Subject: [PATCH] fix(client): resolve v0.3 agent cards that list several OAuth flows In v0.3 every OAuth flow is an optional field, so a card may list several. In 1.0 OAuthFlows is a oneof, and parse_agent_card copied the legacy flows object across unchanged, so ParseDict rejected such cards and the client could not be created. Keep one flow for a legacy oauth2 scheme, the same one the v0.3 compat conversion keeps, and log a warning naming the flows. --- src/a2a/client/card_resolver.py | 36 ++++++++++++++++++++++++ tests/client/test_card_resolver.py | 45 ++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/src/a2a/client/card_resolver.py b/src/a2a/client/card_resolver.py index 447913572..16129f7da 100644 --- a/src/a2a/client/card_resolver.py +++ b/src/a2a/client/card_resolver.py @@ -88,6 +88,40 @@ def _map_legacy_security( ] +# v0.3 OAuthFlows fields in the order `to_core_oauth_flows` applies them. +_LEGACY_OAUTH_FLOWS = ( + 'authorizationCode', + 'clientCredentials', + 'implicit', + 'password', +) + + +def _keep_single_legacy_oauth_flow(scheme: dict[str, Any]) -> None: + """Reduce a v0.3 OAuthFlows object to the single flow 1.0.0 allows. + + In v0.3 every flow is an optional field, so a card may list several. In + 1.0.0 `OAuthFlows` is a oneof. Keep the flow the v0.3 compat conversion + keeps (the last one in `_LEGACY_OAUTH_FLOWS`) so both paths agree. + """ + flows = scheme.get('flows') + if not isinstance(flows, dict): + return + present = [ + name for name in _LEGACY_OAUTH_FLOWS if flows.get(name) is not None + ] + if len(present) <= 1: + return + kept = present[-1] + logger.warning( + 'Legacy OAuth2 security scheme lists flows %s; keeping only %r.', + present, + kept, + ) + for name in present[:-1]: + del flows[name] + + def _handle_security_compatibility(agent_card_data: dict[str, Any]) -> None: """Map legacy security requirements and schemas to their 1.0.0 Protobuf equivalents.""" legacy_security = agent_card_data.pop('security', None) @@ -121,6 +155,8 @@ def _handle_security_compatibility(agent_card_data: dict[str, Any]) -> None: # Map legacy 'in' to modern 'location' if scheme_type == 'apiKey' and 'in' in scheme: scheme['location'] = scheme.pop('in') + if scheme_type == 'oauth2': + _keep_single_legacy_oauth_flow(scheme) mapped_name = type_mapping[scheme_type] new_scheme_wrapper = {mapped_name: scheme.copy()} diff --git a/tests/client/test_card_resolver.py b/tests/client/test_card_resolver.py index a96402542..aa361a4ee 100644 --- a/tests/client/test_card_resolver.py +++ b/tests/client/test_card_resolver.py @@ -727,6 +727,51 @@ def test_parse_agent_card_security_scheme_unknown_type(self) -> None: 'scheme' ) + def test_parse_030_agent_card_oauth2_with_several_flows(self) -> None: + flows = { + 'authorizationCode': { + 'authorizationUrl': 'https://auth.example.com/authorize', + 'tokenUrl': 'https://auth.example.com/token', + 'scopes': {'read': 'Read access'}, + }, + 'clientCredentials': { + 'tokenUrl': 'https://auth.example.com/token', + 'scopes': {'read': 'Read access'}, + }, + } + data = { + 'protocolVersion': '0.3.0', + 'name': 'OAuth Agent', + 'description': 'Lists two OAuth flows', + 'url': 'http://agent.example.com/api', + 'version': '1.0', + 'securitySchemes': { + 'oauth': {'type': 'oauth2', 'flows': copy.deepcopy(flows)} + }, + } + + card = parse_agent_card(data) + + scheme = card.security_schemes['oauth'].oauth2_security_scheme + assert scheme.flows.WhichOneof('flow') == 'client_credentials' + assert ( + scheme.flows.client_credentials.token_url + == 'https://auth.example.com/token' + ) + + # A 1.0 card must still carry a single flow. + with pytest.raises(ParseError): + parse_agent_card( + { + 'name': 'OAuth Agent', + 'description': 'Lists two OAuth flows', + 'version': '1.0', + 'securitySchemes': { + 'oauth': {'oauth2SecurityScheme': {'flows': flows}} + }, + } + ) + def test_parse_030_agent_card_route_planner(self) -> None: data = { 'protocolVersion': '0.3',