Skip to content

datarobot_genai.dragent.a2a.cards

cards

Reading A2A agent cards of either protocol version into a2a-sdk 1.x's AgentCard.

Used wherever dragent consumes other agents' cards: the A2A clients, the agent card registry and its cache, and the Okta cross-app provider.

card_rpc_url

card_rpc_url(card: AgentCard) -> str

Return the URL a client sends JSON-RPC calls to.

v0.3 cards carry it as the top-level url, which parses into the card's first interface; v1.0 cards list it in supportedInterfaces. When a card offers JSON-RPC at several protocol versions, this picks the interface the SDK's ClientFactory picks (v1.0 first, then the newest supported), so it is the endpoint the client actually calls. Falls back to the first interface of any binding, else "".

Source code in datarobot_genai/dragent/a2a/cards.py
def card_rpc_url(card: AgentCard) -> str:
    """Return the URL a client sends JSON-RPC calls to.

    v0.3 cards carry it as the top-level ``url``, which parses into the card's first
    interface; v1.0 cards list it in ``supportedInterfaces``. When a card offers JSON-RPC at
    several protocol versions, this picks the interface the SDK's ``ClientFactory`` picks
    (v1.0 first, then the newest supported), so it is the endpoint the client actually
    calls. Falls back to the first interface of any binding, else ``""``.
    """
    interface = best_interface(card.supported_interfaces, JSONRPC)
    if interface is not None:
        return interface.url
    return card.supported_interfaces[0].url if card.supported_interfaces else ""

best_interface

best_interface(interfaces: Sequence[AgentInterface], protocol_binding: str) -> AgentInterface | None

Return the interface the SDK's client calls for protocol_binding, if any.

The first v1.0 interface wins, then the first newer than v1.0, then the first from v0.3 on, then the first without a version. This follows ClientFactory._find_best_interface of a2a-sdk 1.x, which is private and so not called here; test_cards checks they agree.

Source code in datarobot_genai/dragent/a2a/cards.py
def best_interface(
    interfaces: Sequence[AgentInterface], protocol_binding: str
) -> AgentInterface | None:
    """Return the interface the SDK's client calls for ``protocol_binding``, if any.

    The first v1.0 interface wins, then the first newer than v1.0, then the first from v0.3 on,
    then the first without a version. This follows ``ClientFactory._find_best_interface`` of
    a2a-sdk 1.x, which is private and so not called here; ``test_cards`` checks they agree.
    """
    newer_than_v1_0: AgentInterface | None = None
    from_v0_3: AgentInterface | None = None
    without_version: AgentInterface | None = None
    for interface in interfaces:
        if interface.protocol_binding != protocol_binding:
            continue
        if not interface.protocol_version:
            if without_version is None:
                without_version = interface
            continue
        version = _major_minor(interface.protocol_version)
        if version is None:
            continue
        if version == _V1_0:
            return interface
        if newer_than_v1_0 is None and version > _V1_0:
            newer_than_v1_0 = interface
        if from_v0_3 is None and version >= _V0_3:
            from_v0_3 = interface
    return newer_than_v1_0 or from_v0_3 or without_version

parse_agent_card

parse_agent_card(card_data: Mapping[str, Any]) -> AgentCard

Parse agent card JSON of either protocol version into a protobuf AgentCard.

The input is not modified.

Raises

ValueError The data is not a valid agent card, or lacks a name or an interface URL.

Source code in datarobot_genai/dragent/a2a/cards.py
def parse_agent_card(card_data: Mapping[str, Any]) -> AgentCard:
    """Parse agent card JSON of either protocol version into a protobuf ``AgentCard``.

    The input is not modified.

    Raises
    ------
    ValueError
        The data is not a valid agent card, or lacks a name or an interface URL.
    """
    if not isinstance(card_data, Mapping):
        raise ValueError("Invalid agent card: not a JSON object")
    data = copy.deepcopy(dict(card_data))
    _keep_one_oauth_flow(data)
    try:
        card = _sdk_parse_agent_card(data)
    except ParseError as exc:
        raise ValueError(f"Invalid agent card: {exc}") from exc
    # Protobuf parsing ignores unknown keys, so any JSON object would otherwise parse into
    # an empty card. Require what a client needs to call the agent at all.
    if not card.name:
        raise ValueError("Invalid agent card: missing 'name'")
    if not card_rpc_url(card):
        raise ValueError("Invalid agent card: missing 'url' / 'supportedInterfaces'")
    return card

fetch_agent_card async

fetch_agent_card(httpx_client: AsyncClient, base_url: str, agent_card_path: str = AGENT_CARD_WELL_KNOWN_PATH) -> AgentCard

GET the agent card at agent_card_path under base_url and parse it.

Replaces a2a.client.A2ACardResolver, which rejects v0.3 cards whose OAuth2 scheme lists several flows (see :func:parse_agent_card).

Raises

httpx.HTTPError The request failed or returned an error status. ValueError The response is not a valid agent card.

Source code in datarobot_genai/dragent/a2a/cards.py
async def fetch_agent_card(
    httpx_client: httpx.AsyncClient,
    base_url: str,
    agent_card_path: str = AGENT_CARD_WELL_KNOWN_PATH,
) -> AgentCard:
    """GET the agent card at *agent_card_path* under *base_url* and parse it.

    Replaces ``a2a.client.A2ACardResolver``, which rejects v0.3 cards whose OAuth2 scheme
    lists several flows (see :func:`parse_agent_card`).

    Raises
    ------
    httpx.HTTPError
        The request failed or returned an error status.
    ValueError
        The response is not a valid agent card.
    """
    response = await httpx_client.get(f"{base_url.rstrip('/')}/{agent_card_path.lstrip('/')}")
    response.raise_for_status()
    return parse_agent_card(response.json())