Skip to content

Commit 32fecc7

Browse files
MadaraUchiha-314MadaraUchiha-314
andauthored
feat: add a2a routes to existing app (#188)
- Adds a method to add a2a routes to an existing app (Starlette or FastAPI) - There are some use-cases where an app already exists and we simply need to add a2a routes to that existing app - Refactors `build` method to use the above method --------- Co-authored-by: MadaraUchiha-314 <rohithr31@gmail.com>
1 parent 8e713b7 commit 32fecc7

File tree

2 files changed

+56
-18
lines changed

2 files changed

+56
-18
lines changed

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,21 @@ def __init__(
4949
context_builder=context_builder,
5050
)
5151

52-
def build(
52+
def add_routes_to_app(
5353
self,
54+
app: FastAPI,
5455
agent_card_url: str = '/.well-known/agent.json',
5556
rpc_url: str = '/',
5657
extended_agent_card_url: str = '/agent/authenticatedExtendedCard',
57-
**kwargs: Any,
58-
) -> FastAPI:
59-
"""Builds and returns the FastAPI application instance.
58+
) -> None:
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.
6364
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)
7167

7268
@app.post(rpc_url)
7369
async def handle_a2a_request(request: Request) -> Response:
@@ -85,4 +81,28 @@ async def get_extended_agent_card(request: Request) -> Response:
8581
request
8682
)
8783

84+
def build(
85+
self,
86+
agent_card_url: str = '/.well-known/agent.json',
87+
rpc_url: str = '/',
88+
extended_agent_card_url: str = '/agent/authenticatedExtendedCard',
89+
**kwargs: Any,
90+
) -> FastAPI:
91+
"""Builds and returns the FastAPI application instance.
92+
93+
Args:
94+
agent_card_url: The URL for the agent card endpoint.
95+
rpc_url: The URL for the A2A JSON-RPC endpoint.
96+
extended_agent_card_url: The URL for the authenticated extended agent card endpoint.
97+
**kwargs: Additional keyword arguments to pass to the FastAPI constructor.
98+
99+
Returns:
100+
A configured FastAPI application instance.
101+
"""
102+
app = FastAPI(**kwargs)
103+
104+
self.add_routes_to_app(
105+
app, agent_card_url, rpc_url, extended_agent_card_url
106+
)
107+
88108
return app

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,28 @@ def routes(
9292
)
9393
return app_routes
9494

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+
) -> None:
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)
116+
95117
def build(
96118
self,
97119
agent_card_url: str = '/.well-known/agent.json',
@@ -110,14 +132,10 @@ 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,
135+
app = Starlette(**kwargs)
136+
137+
self.add_routes_to_app(
138+
app, agent_card_url, rpc_url, extended_agent_card_url
117139
)
118-
if 'routes' in kwargs:
119-
kwargs['routes'].extend(app_routes)
120-
else:
121-
kwargs['routes'] = app_routes
122140

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

0 commit comments

Comments
 (0)