diff --git a/src/a2a/server/apps/jsonrpc/fastapi_app.py b/src/a2a/server/apps/jsonrpc/fastapi_app.py index fa307be2..e961e868 100644 --- a/src/a2a/server/apps/jsonrpc/fastapi_app.py +++ b/src/a2a/server/apps/jsonrpc/fastapi_app.py @@ -49,25 +49,21 @@ def __init__( context_builder=context_builder, ) - def build( + 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. + ) -> 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. 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: @@ -85,4 +81,28 @@ async def get_extended_agent_card(request: Request) -> Response: 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..1826841b 100644 --- a/src/a2a/server/apps/jsonrpc/starlette_app.py +++ b/src/a2a/server/apps/jsonrpc/starlette_app.py @@ -92,6 +92,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', + ) -> None: + """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, agent_card_url: str = '/.well-known/agent.json', @@ -110,14 +132,10 @@ 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, + app = Starlette(**kwargs) + + self.add_routes_to_app( + app, agent_card_url, rpc_url, extended_agent_card_url ) - if 'routes' in kwargs: - kwargs['routes'].extend(app_routes) - else: - kwargs['routes'] = app_routes - return Starlette(**kwargs) + return app