Skip to content

fix: old a2a url works #2626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions src/google/adk/cli/fast_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from typing import Mapping
from typing import Optional

from a2a.server.apps import A2AFastAPIApplication
import click
from fastapi import FastAPI
from fastapi import UploadFile
Expand Down Expand Up @@ -364,18 +365,12 @@ async def _get_a2a_runner_async() -> Runner:
data = json.load(f)
agent_card = AgentCard(**data)

a2a_app = A2AStarletteApplication(
a2a_app = A2AFastAPIApplication(
agent_card=agent_card,
http_handler=request_handler,
)

routes = a2a_app.routes(
rpc_url=f"/a2a/{app_name}",
agent_card_url=f"/a2a/{app_name}{AGENT_CARD_WELL_KNOWN_PATH}",
)
).build()

for new_route in routes:
app.router.routes.append(new_route)
app.mount(f"/a2a/{app_name}", a2a_app)

logger.info("Successfully configured A2A agent: %s", app_name)

Expand Down
48 changes: 38 additions & 10 deletions tests/unittests/cli/test_fast_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
from unittest.mock import MagicMock
from unittest.mock import patch

from a2a.utils import AGENT_CARD_WELL_KNOWN_PATH
from a2a.utils import PREV_AGENT_CARD_WELL_KNOWN_PATH
from fastapi.testclient import TestClient
from google.adk.agents.base_agent import BaseAgent
from google.adk.agents.run_config import RunConfig
Expand Down Expand Up @@ -502,11 +504,37 @@ def temp_agents_dir_with_a2a():

# Create agent.json file
agent_card = {
"capabilities": {
"pushNotifications": True,
"streaming": True
},
"defaultInputModes": [
"text",
"text/plain"
],
"defaultOutputModes": [
"text",
"text/plain"
],
"name": "test_a2a_agent",
"description": "Test A2A agent",
"version": "1.0.0",
"author": "test",
"capabilities": ["text"],
"protocolVersion": "0.2.6",
"skills": [
{
"description": "Makes the tests pass",
"examples": [
"Fix the tests."
],
"id": "test_a2a_agent",
"name": "Test A2A agent",
"tags": [
"testing"
]
}
],
"url": "",
}

with open(agent_dir / "agent.json", "w") as f:
Expand Down Expand Up @@ -580,20 +608,12 @@ def test_app_with_a2a(
patch(
"a2a.server.request_handlers.DefaultRequestHandler"
) as mock_handler,
patch("a2a.server.apps.A2AStarletteApplication") as mock_a2a_app,
):
# Configure mocks
mock_task_store.return_value = MagicMock()
mock_executor.return_value = MagicMock()
mock_handler.return_value = MagicMock()

# Mock A2AStarletteApplication
mock_app_instance = MagicMock()
mock_app_instance.routes.return_value = (
[]
) # Return empty routes for testing
mock_a2a_app.return_value = mock_app_instance

# Change to temp directory
original_cwd = os.getcwd()
os.chdir(temp_agents_dir_with_a2a)
Expand Down Expand Up @@ -879,9 +899,17 @@ def test_debug_trace(test_app):
)
def test_a2a_agent_discovery(test_app_with_a2a):
"""Test that A2A agents are properly discovered and configured."""
# This test mainly verifies that the A2A setup doesn't break the app
# This test verifies that the A2A setup doesn't break the app
# and that the well known card works
response = test_app_with_a2a.get("/list-apps")
assert response.status_code == 200
response2 = test_app_with_a2a.get(f"/a2a/test_a2a_agent{AGENT_CARD_WELL_KNOWN_PATH}")
assert response2.status_code == 200
# testing backward compatibility
response3 = test_app_with_a2a.get(f"/a2a/test_a2a_agent{PREV_AGENT_CARD_WELL_KNOWN_PATH}")
assert response3.status_code == 200
response4 = test_app_with_a2a.get(f"/a2a/test_a2a_agent/openapi.json")
assert response4.status_code == 200
Comment on lines 904 to +912

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better readability and maintainability of this test, consider using more descriptive variable names for the different responses being checked. Names like response, response2, response3, and response4 don't convey what is being tested at a glance.

Suggested change
response = test_app_with_a2a.get("/list-apps")
assert response.status_code == 200
response2 = test_app_with_a2a.get(f"/a2a/test_a2a_agent{AGENT_CARD_WELL_KNOWN_PATH}")
assert response2.status_code == 200
# testing backward compatibility
response3 = test_app_with_a2a.get(f"/a2a/test_a2a_agent{PREV_AGENT_CARD_WELL_KNOWN_PATH}")
assert response3.status_code == 200
response4 = test_app_with_a2a.get(f"/a2a/test_a2a_agent/openapi.json")
assert response4.status_code == 200
list_apps_response = test_app_with_a2a.get("/list-apps")
assert list_apps_response.status_code == 200
response2 = test_app_with_a2a.get(f"/a2a/test_a2a_agent{AGENT_CARD_WELL_KNOWN_PATH}")
assert response2.status_code == 200
# testing backward compatibility
response3 = test_app_with_a2a.get(f"/a2a/test_a2a_agent{PREV_AGENT_CARD_WELL_KNOWN_PATH}")
assert response3.status_code == 200
response4 = test_app_with_a2a.get(f"/a2a/test_a2a_agent/openapi.json")
assert response4.status_code == 200

logger.info("A2A agent discovery test passed")


Expand Down