diff --git a/src/strands/multiagent/a2a/server.py b/src/strands/multiagent/a2a/server.py index bbfbc824d..a9093742f 100644 --- a/src/strands/multiagent/a2a/server.py +++ b/src/strands/multiagent/a2a/server.py @@ -176,16 +176,21 @@ def agent_skills(self, skills: list[AgentSkill]) -> None: """ self._agent_skills = skills - def to_starlette_app(self) -> Starlette: + def to_starlette_app(self, *, app_kwargs: dict[str, Any] | None = None) -> Starlette: """Create a Starlette application for serving this agent via HTTP. Automatically handles path-based mounting if a mount path was derived from the http_url parameter. + Args: + app_kwargs: Additional keyword arguments to pass to the Starlette constructor. + Returns: Starlette: A Starlette application configured to serve this agent. """ - a2a_app = A2AStarletteApplication(agent_card=self.public_agent_card, http_handler=self.request_handler).build() + a2a_app = A2AStarletteApplication(agent_card=self.public_agent_card, http_handler=self.request_handler).build( + **app_kwargs or {} + ) if self.mount_path: # Create parent app and mount the A2A app at the specified path @@ -196,16 +201,21 @@ def to_starlette_app(self) -> Starlette: return a2a_app - def to_fastapi_app(self) -> FastAPI: + def to_fastapi_app(self, *, app_kwargs: dict[str, Any] | None = None) -> FastAPI: """Create a FastAPI application for serving this agent via HTTP. Automatically handles path-based mounting if a mount path was derived from the http_url parameter. + Args: + app_kwargs: Additional keyword arguments to pass to the FastAPI constructor. + Returns: FastAPI: A FastAPI application configured to serve this agent. """ - a2a_app = A2AFastAPIApplication(agent_card=self.public_agent_card, http_handler=self.request_handler).build() + a2a_app = A2AFastAPIApplication(agent_card=self.public_agent_card, http_handler=self.request_handler).build( + **app_kwargs or {} + ) if self.mount_path: # Create parent app and mount the A2A app at the specified path diff --git a/tests/strands/multiagent/a2a/test_server.py b/tests/strands/multiagent/a2a/test_server.py index 00dd164b5..647fce230 100644 --- a/tests/strands/multiagent/a2a/test_server.py +++ b/tests/strands/multiagent/a2a/test_server.py @@ -852,3 +852,27 @@ def test_serve_at_root_edge_cases(mock_strands_agent): ) assert server3.mount_path == "" assert server3.http_url == "http://api.example.com/v1/agents/team1/agent1/" + + +def test_to_starlette_app_with_app_kwargs(mock_strands_agent): + """Test that to_starlette_app passes app_kwargs to the Starlette constructor.""" + mock_strands_agent.tool_registry.get_all_tools_config.return_value = {} + + a2a_agent = A2AServer(mock_strands_agent, skills=[]) + + app = a2a_agent.to_starlette_app(app_kwargs={"debug": True}) + + assert isinstance(app, Starlette) + assert app.debug is True + + +def test_to_fastapi_app_with_app_kwargs(mock_strands_agent): + """Test that to_fastapi_app passes app_kwargs to the FastAPI constructor.""" + mock_strands_agent.tool_registry.get_all_tools_config.return_value = {} + + a2a_agent = A2AServer(mock_strands_agent, skills=[]) + + app = a2a_agent.to_fastapi_app(app_kwargs={"title": "Custom Agent Title"}) + + assert isinstance(app, FastAPI) + assert app.title == "Custom Agent Title"