Skip to content

Commit bcd2969

Browse files
author
MadaraUchiha-314
committed
feat: add a2a routes to existing app
1 parent d5e5f5f commit bcd2969

File tree

2 files changed

+56
-24
lines changed

2 files changed

+56
-24
lines changed

src/a2a/server/apps/jsonrpc/fastapi_app.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,22 @@ def __init__(
4848
extended_agent_card=extended_agent_card,
4949
context_builder=context_builder,
5050
)
51-
52-
def build(
53-
self,
51+
52+
def add_routes_to_app(
53+
self,
54+
app: FastAPI,
5455
agent_card_url: str = '/.well-known/agent.json',
5556
rpc_url: str = '/',
56-
extended_agent_card_url: str = '/agent/authenticatedExtendedCard',
57-
**kwargs: Any,
58-
) -> FastAPI:
59-
"""Builds and returns the FastAPI application instance.
57+
extended_agent_card_url: str = '/agent/authenticatedExtendedCard'
58+
):
59+
"""Adds the routes to the FastAPI application.
6060
6161
Args:
62+
app: The FastAPI application to add the routes to.
6263
agent_card_url: The URL for the agent card endpoint.
63-
rpc_url: The URL for the A2A JSON-RPC endpoint.
64+
rpc_url: The URL for the A2A JSON-RPC endpoint.
6465
extended_agent_card_url: The URL for the authenticated extended agent card endpoint.
65-
**kwargs: Additional keyword arguments to pass to the FastAPI constructor.
66-
67-
Returns:
68-
A configured FastAPI application instance.
6966
"""
70-
app = FastAPI(**kwargs)
71-
7267
@app.post(rpc_url)
7368
async def handle_a2a_request(request: Request) -> Response:
7469
return await self._handle_requests(request)
@@ -84,5 +79,26 @@ async def get_extended_agent_card(request: Request) -> Response:
8479
return await self._handle_get_authenticated_extended_agent_card(
8580
request
8681
)
82+
def build(
83+
self,
84+
agent_card_url: str = '/.well-known/agent.json',
85+
rpc_url: str = '/',
86+
extended_agent_card_url: str = '/agent/authenticatedExtendedCard',
87+
**kwargs: Any,
88+
) -> FastAPI:
89+
"""Builds and returns the FastAPI application instance.
90+
91+
Args:
92+
agent_card_url: The URL for the agent card endpoint.
93+
rpc_url: The URL for the A2A JSON-RPC endpoint.
94+
extended_agent_card_url: The URL for the authenticated extended agent card endpoint.
95+
**kwargs: Additional keyword arguments to pass to the FastAPI constructor.
96+
97+
Returns:
98+
A configured FastAPI application instance.
99+
"""
100+
app = FastAPI(**kwargs)
101+
102+
self.add_routes_to_app(app, agent_card_url, rpc_url, extended_agent_card_url)
87103

88104
return app

src/a2a/server/apps/jsonrpc/starlette_app.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,28 @@ def routes(
9191
)
9292
)
9393
return app_routes
94+
95+
def add_routes_to_app(
96+
self,
97+
app: Starlette,
98+
agent_card_url: str = '/.well-known/agent.json',
99+
rpc_url: str = '/',
100+
extended_agent_card_url: str = '/agent/authenticatedExtendedCard'
101+
):
102+
"""Adds the routes to the Starlette application.
103+
104+
Args:
105+
app: The Starlette application to add the routes to.
106+
agent_card_url: The URL path for the agent card endpoint.
107+
rpc_url: The URL path for the A2A JSON-RPC endpoint (POST requests).
108+
extended_agent_card_url: The URL for the authenticated extended agent card endpoint.
109+
"""
110+
routes = self.routes(
111+
agent_card_url=agent_card_url,
112+
rpc_url=rpc_url,
113+
extended_agent_card_url=extended_agent_card_url,
114+
)
115+
app.routes.extend(routes)
94116

95117
def build(
96118
self,
@@ -110,14 +132,8 @@ def build(
110132
Returns:
111133
A configured Starlette application instance.
112134
"""
113-
app_routes = self.routes(
114-
agent_card_url=agent_card_url,
115-
rpc_url=rpc_url,
116-
extended_agent_card_url=extended_agent_card_url,
117-
)
118-
if 'routes' in kwargs:
119-
kwargs['routes'].extend(app_routes)
120-
else:
121-
kwargs['routes'] = app_routes
135+
app = Starlette(**kwargs)
136+
137+
self.add_routes_to_app(app, agent_card_url, rpc_url, extended_agent_card_url)
122138

123-
return Starlette(**kwargs)
139+
return app

0 commit comments

Comments
 (0)