From bcd2969e73978746eade2b4a4bf15bb73f85d258 Mon Sep 17 00:00:00 2001 From: MadaraUchiha-314 Date: Thu, 12 Jun 2025 09:17:23 -0700 Subject: [PATCH 1/3] feat: add a2a routes to existing app --- src/a2a/server/apps/jsonrpc/fastapi_app.py | 44 +++++++++++++------- src/a2a/server/apps/jsonrpc/starlette_app.py | 36 +++++++++++----- 2 files changed, 56 insertions(+), 24 deletions(-) diff --git a/src/a2a/server/apps/jsonrpc/fastapi_app.py b/src/a2a/server/apps/jsonrpc/fastapi_app.py index fa307be2..ed22ae69 100644 --- a/src/a2a/server/apps/jsonrpc/fastapi_app.py +++ b/src/a2a/server/apps/jsonrpc/fastapi_app.py @@ -48,27 +48,22 @@ def __init__( extended_agent_card=extended_agent_card, context_builder=context_builder, ) - - def build( - self, + + def add_routes_to_app( + self, + app: FastAPI, agent_card_url: str = '/.well-known/agent.json', rpc_url: str = '/', - extended_agent_card_url: str = '/agent/authenticatedExtendedCard', - **kwargs: Any, - ) -> FastAPI: - """Builds and returns the FastAPI application instance. + extended_agent_card_url: str = '/agent/authenticatedExtendedCard' + ): + """Adds the routes to the FastAPI application. Args: + app: The FastAPI application to add the routes to. agent_card_url: The URL for the agent card endpoint. - rpc_url: The URL for the A2A JSON-RPC endpoint. + rpc_url: The URL for the A2A JSON-RPC endpoint. extended_agent_card_url: The URL for the authenticated extended agent card endpoint. - **kwargs: Additional keyword arguments to pass to the FastAPI constructor. - - Returns: - A configured FastAPI application instance. """ - app = FastAPI(**kwargs) - @app.post(rpc_url) async def handle_a2a_request(request: Request) -> Response: return await self._handle_requests(request) @@ -84,5 +79,26 @@ async def get_extended_agent_card(request: Request) -> Response: return await self._handle_get_authenticated_extended_agent_card( request ) + def build( + self, + agent_card_url: str = '/.well-known/agent.json', + rpc_url: str = '/', + extended_agent_card_url: str = '/agent/authenticatedExtendedCard', + **kwargs: Any, + ) -> FastAPI: + """Builds and returns the FastAPI application instance. + + Args: + agent_card_url: The URL for the agent card endpoint. + rpc_url: The URL for the A2A JSON-RPC endpoint. + extended_agent_card_url: The URL for the authenticated extended agent card endpoint. + **kwargs: Additional keyword arguments to pass to the FastAPI constructor. + + Returns: + A configured FastAPI application instance. + """ + app = FastAPI(**kwargs) + + self.add_routes_to_app(app, agent_card_url, rpc_url, extended_agent_card_url) return app diff --git a/src/a2a/server/apps/jsonrpc/starlette_app.py b/src/a2a/server/apps/jsonrpc/starlette_app.py index 4aaff37e..be9b943c 100644 --- a/src/a2a/server/apps/jsonrpc/starlette_app.py +++ b/src/a2a/server/apps/jsonrpc/starlette_app.py @@ -91,6 +91,28 @@ def routes( ) ) return app_routes + + def add_routes_to_app( + self, + app: Starlette, + agent_card_url: str = '/.well-known/agent.json', + rpc_url: str = '/', + extended_agent_card_url: str = '/agent/authenticatedExtendedCard' + ): + """Adds the routes to the Starlette application. + + Args: + app: The Starlette application to add the routes to. + agent_card_url: The URL path for the agent card endpoint. + rpc_url: The URL path for the A2A JSON-RPC endpoint (POST requests). + extended_agent_card_url: The URL for the authenticated extended agent card endpoint. + """ + routes = self.routes( + agent_card_url=agent_card_url, + rpc_url=rpc_url, + extended_agent_card_url=extended_agent_card_url, + ) + app.routes.extend(routes) def build( self, @@ -110,14 +132,8 @@ def build( Returns: A configured Starlette application instance. """ - app_routes = self.routes( - agent_card_url=agent_card_url, - rpc_url=rpc_url, - extended_agent_card_url=extended_agent_card_url, - ) - if 'routes' in kwargs: - kwargs['routes'].extend(app_routes) - else: - kwargs['routes'] = app_routes + app = Starlette(**kwargs) + + self.add_routes_to_app(app, agent_card_url, rpc_url, extended_agent_card_url) - return Starlette(**kwargs) + return app From bae177493db913c5658463f807446b204a8f5b0b Mon Sep 17 00:00:00 2001 From: MadaraUchiha-314 Date: Thu, 12 Jun 2025 09:29:45 -0700 Subject: [PATCH 2/3] fix: lint errors --- src/a2a/server/apps/jsonrpc/fastapi_app.py | 11 ++++++----- src/a2a/server/apps/jsonrpc/starlette_app.py | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/a2a/server/apps/jsonrpc/fastapi_app.py b/src/a2a/server/apps/jsonrpc/fastapi_app.py index ed22ae69..0b8cf912 100644 --- a/src/a2a/server/apps/jsonrpc/fastapi_app.py +++ b/src/a2a/server/apps/jsonrpc/fastapi_app.py @@ -48,20 +48,20 @@ def __init__( extended_agent_card=extended_agent_card, context_builder=context_builder, ) - + def add_routes_to_app( - self, - app: FastAPI, + self, + app: FastAPI, agent_card_url: str = '/.well-known/agent.json', rpc_url: str = '/', extended_agent_card_url: str = '/agent/authenticatedExtendedCard' - ): + ) -> None: """Adds the routes to the FastAPI application. Args: app: The FastAPI application to add the routes to. agent_card_url: The URL for the agent card endpoint. - rpc_url: The URL for the A2A JSON-RPC endpoint. + rpc_url: The URL for the A2A JSON-RPC endpoint. extended_agent_card_url: The URL for the authenticated extended agent card endpoint. """ @app.post(rpc_url) @@ -79,6 +79,7 @@ async def get_extended_agent_card(request: Request) -> Response: return await self._handle_get_authenticated_extended_agent_card( request ) + def build( self, agent_card_url: str = '/.well-known/agent.json', diff --git a/src/a2a/server/apps/jsonrpc/starlette_app.py b/src/a2a/server/apps/jsonrpc/starlette_app.py index be9b943c..22e9266e 100644 --- a/src/a2a/server/apps/jsonrpc/starlette_app.py +++ b/src/a2a/server/apps/jsonrpc/starlette_app.py @@ -91,14 +91,14 @@ def routes( ) ) return app_routes - + def add_routes_to_app( self, app: Starlette, agent_card_url: str = '/.well-known/agent.json', rpc_url: str = '/', extended_agent_card_url: str = '/agent/authenticatedExtendedCard' - ): + ) -> None: """Adds the routes to the Starlette application. Args: From 2cb2a714ac2f816484e0824e21cc4da9a3e4fce4 Mon Sep 17 00:00:00 2001 From: MadaraUchiha-314 Date: Thu, 12 Jun 2025 09:31:26 -0700 Subject: [PATCH 3/3] fix: format using nox --- src/a2a/server/apps/jsonrpc/fastapi_app.py | 9 ++++++--- src/a2a/server/apps/jsonrpc/starlette_app.py | 6 ++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/a2a/server/apps/jsonrpc/fastapi_app.py b/src/a2a/server/apps/jsonrpc/fastapi_app.py index 0b8cf912..e961e868 100644 --- a/src/a2a/server/apps/jsonrpc/fastapi_app.py +++ b/src/a2a/server/apps/jsonrpc/fastapi_app.py @@ -54,7 +54,7 @@ def add_routes_to_app( app: FastAPI, agent_card_url: str = '/.well-known/agent.json', rpc_url: str = '/', - extended_agent_card_url: str = '/agent/authenticatedExtendedCard' + extended_agent_card_url: str = '/agent/authenticatedExtendedCard', ) -> None: """Adds the routes to the FastAPI application. @@ -64,6 +64,7 @@ def add_routes_to_app( rpc_url: The URL for the A2A JSON-RPC endpoint. extended_agent_card_url: The URL for the authenticated extended agent card endpoint. """ + @app.post(rpc_url) async def handle_a2a_request(request: Request) -> Response: return await self._handle_requests(request) @@ -79,7 +80,7 @@ async def get_extended_agent_card(request: Request) -> Response: return await self._handle_get_authenticated_extended_agent_card( request ) - + def build( self, agent_card_url: str = '/.well-known/agent.json', @@ -100,6 +101,8 @@ def build( """ app = FastAPI(**kwargs) - self.add_routes_to_app(app, agent_card_url, rpc_url, extended_agent_card_url) + self.add_routes_to_app( + app, agent_card_url, rpc_url, extended_agent_card_url + ) return app diff --git a/src/a2a/server/apps/jsonrpc/starlette_app.py b/src/a2a/server/apps/jsonrpc/starlette_app.py index 22e9266e..1826841b 100644 --- a/src/a2a/server/apps/jsonrpc/starlette_app.py +++ b/src/a2a/server/apps/jsonrpc/starlette_app.py @@ -97,7 +97,7 @@ def add_routes_to_app( app: Starlette, agent_card_url: str = '/.well-known/agent.json', rpc_url: str = '/', - extended_agent_card_url: str = '/agent/authenticatedExtendedCard' + extended_agent_card_url: str = '/agent/authenticatedExtendedCard', ) -> None: """Adds the routes to the Starlette application. @@ -134,6 +134,8 @@ def build( """ app = Starlette(**kwargs) - self.add_routes_to_app(app, agent_card_url, rpc_url, extended_agent_card_url) + self.add_routes_to_app( + app, agent_card_url, rpc_url, extended_agent_card_url + ) return app