diff --git a/agentkit/apps/a2a_app/a2a_app.py b/agentkit/apps/a2a_app/a2a_app.py index 7f57882..e6a4b92 100644 --- a/agentkit/apps/a2a_app/a2a_app.py +++ b/agentkit/apps/a2a_app/a2a_app.py @@ -12,10 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. +import hashlib +import inspect +import json import logging import os import uvicorn -import inspect from typing import Callable from typing_extensions import override @@ -47,6 +49,26 @@ _A2A_1X = False +def _agent_card_1x_payload(agent_card: AgentCard) -> dict: + """Return a 1.x-shaped AgentCard payload accepted by AgentKit A2A Center. + + a2a-sdk's compatibility layer injects a top-level ``protocolVersion`` next + to ``supportedInterfaces``. AgentKit A2A Center rejects that combination + with ``protocolVersion and supportedInterfaces cannot be used together``. + Keep the legacy shape on ``/.well-known/agent.json`` and serve this 1.x + shape on the standard card path. + """ + from a2a.server.request_handlers.response_helpers import agent_card_to_dict + + payload = agent_card_to_dict(agent_card) + payload.pop("protocolVersion", None) + # Keep the legacy ``url`` for clients that do not understand + # ``supportedInterfaces`` yet (for example a 1.0-only card). + if not payload.get("url") and payload.get("supportedInterfaces"): + payload["url"] = payload["supportedInterfaces"][0]["url"] + return payload + + def _wrap_agent_executor_execute_func(execute_func: Callable) -> Callable: async def wrapper(*args, **kwargs): executor_instance: AgentExecutor = args[0] @@ -242,9 +264,26 @@ def _build_a2a_app(self, agent_card: AgentCard) -> Starlette: task_store=task_store, agent_card=agent_card, ) + + async def _serve_1x_card(request: Request) -> Response: + payload = _agent_card_1x_payload(agent_card) + body = json.dumps(payload, ensure_ascii=False, separators=(",", ":")) + etag = f'"{hashlib.sha256(body.encode("utf-8")).hexdigest()[:32]}"' + if request.headers.get("if-none-match") == etag: + return Response(status_code=304, headers={"ETag": etag}) + return Response( + content=body, + media_type="application/json", + headers={"ETag": etag}, + ) + return Starlette( routes=[ - *create_agent_card_routes(agent_card), + Route( + "/.well-known/agent-card.json", + endpoint=_serve_1x_card, + methods=["GET"], + ), *create_agent_card_routes( agent_card, card_url="/.well-known/agent.json" ), diff --git a/tests/apps/test_a2a_app.py b/tests/apps/test_a2a_app.py index 847e39c..775bdaf 100644 --- a/tests/apps/test_a2a_app.py +++ b/tests/apps/test_a2a_app.py @@ -323,8 +323,9 @@ def test_build_a2a_app_serves_both_card_paths_and_rpc_route(): a2a_app = app._build_a2a_app(_card()) client = TestClient(a2a_app) - for path in ("/.well-known/agent-card.json", "/.well-known/agent.json"): - response = client.get(path) + standard = client.get("/.well-known/agent-card.json") + legacy = client.get("/.well-known/agent.json") + for response in (standard, legacy): assert response.status_code == 200 card = response.json() assert card["url"] == "http://127.0.0.1:8000/" @@ -335,6 +336,12 @@ def test_build_a2a_app_serves_both_card_paths_and_rpc_route(): else: assert card["protocolVersion"] == "0.3" + if A2A_1X: + # AgentKit A2A Center rejects a card that carries both the legacy + # top-level protocolVersion and the 1.x supportedInterfaces. + assert "protocolVersion" not in standard.json() + assert legacy.json()["protocolVersion"] == "0.3" + assert any(getattr(route, "path", None) == "/" for route in a2a_app.routes)