From f658d249ae56741769905b22c3cb5fcd009347e1 Mon Sep 17 00:00:00 2001 From: Aleksandr Shcherbakov <241861944+miinimal@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:08:49 -0800 Subject: [PATCH] feat(server): match media ranges such as image/* in validate_input_modes Fixes #1468 --- src/a2a/utils/input_mode_validator.py | 16 +++++++++-- tests/utils/test_input_mode_validator.py | 34 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/a2a/utils/input_mode_validator.py b/src/a2a/utils/input_mode_validator.py index 8901409d1..9c0a06ecd 100644 --- a/src/a2a/utils/input_mode_validator.py +++ b/src/a2a/utils/input_mode_validator.py @@ -23,7 +23,9 @@ def validate_input_modes(message: Message, agent_card: AgentCard) -> None: Media types are compared on type and subtype alone, ignoring case (RFC 2045 section 5.1) and parameters, so `Text/Plain; charset=utf-8` - matches a card that declares `text/plain`. + matches a card that declares `text/plain`. A declared media range + (RFC 9110 section 12.5.1) matches by type: `image/*` accepts `image/png` + but not `video/mp4`, and `*/*` accepts any media type. """ modes = set(agent_card.default_input_modes) for skill in agent_card.skills: @@ -33,7 +35,9 @@ def validate_input_modes(message: Message, agent_card: AgentCard) -> None: return for part in message.parts: - if part.media_type and _essence(part.media_type) not in declared: + if part.media_type and not _is_declared( + _essence(part.media_type), declared + ): raise ContentTypeNotSupportedError( message=( f'Media type {part.media_type} is not supported. ' @@ -42,6 +46,14 @@ def validate_input_modes(message: Message, agent_card: AgentCard) -> None: ) +def _is_declared(essence: str, declared: set[str]) -> bool: + """Returns whether `essence` is declared exactly or by a media range.""" + if essence in declared or '*/*' in declared: + return True + media_type = essence.split('/', 1)[0] + return f'{media_type}/*' in declared + + def _essence(media_type: str) -> str: """Returns `type/subtype` in lower case, without parameters.""" return media_type.split(';', 1)[0].strip().lower() diff --git a/tests/utils/test_input_mode_validator.py b/tests/utils/test_input_mode_validator.py index 86a302453..17d524473 100644 --- a/tests/utils/test_input_mode_validator.py +++ b/tests/utils/test_input_mode_validator.py @@ -149,3 +149,37 @@ def test_parameters_do_not_widen_the_declared_type(): ) assert 'text/html; charset=utf-8' in exc_info.value.message + + +@pytest.mark.parametrize( + ('part_type', 'card_type'), + [ + ('image/png', 'image/*'), + ('image/png; q=1', 'IMAGE/*'), + ('Image/PNG', 'image/*;q=0.8'), + ('video/mp4', '*/*'), + ], +) +def test_declared_media_range_matches_its_type(part_type, card_type): + """RFC 9110 12.5.1: `type/*` covers every subtype, `*/*` every type.""" + validate_input_modes( + make_message(Part(raw=b'x', media_type=part_type)), + make_card('text/plain', card_type), + ) + + +def test_media_range_does_not_widen_to_other_types(): + with pytest.raises(ContentTypeNotSupportedError) as exc_info: + validate_input_modes( + make_message(Part(raw=b'x', media_type='video/mp4')), + make_card('text/plain', 'image/*'), + ) + + assert 'video/mp4' in exc_info.value.message + + +def test_media_range_declared_by_a_skill_widens_the_union(): + validate_input_modes( + make_message(Part(raw=b'x', media_type='audio/wav')), + make_card('text/plain', skill_modes=['audio/*']), + )