Skip to content

Commit 9017460

Browse files
committed
feat(server): allow standard FastAPI Security in routes
1 parent fa14dbf commit 9017460

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22

3-
from collections.abc import Awaitable, Callable
3+
from collections.abc import Awaitable, Callable, Sequence
44
from typing import TYPE_CHECKING, Any
55

66

@@ -121,6 +121,7 @@ def add_routes_to_app(
121121
agent_card_url: str = AGENT_CARD_WELL_KNOWN_PATH,
122122
rpc_url: str = DEFAULT_RPC_URL,
123123
extended_agent_card_url: str = EXTENDED_AGENT_CARD_PATH,
124+
dependencies: Sequence[Any] | None = None,
124125
) -> None:
125126
"""Adds the routes to the FastAPI application.
126127
@@ -129,7 +130,16 @@ def add_routes_to_app(
129130
agent_card_url: The URL for the agent card endpoint.
130131
rpc_url: The URL for the A2A JSON-RPC endpoint.
131132
extended_agent_card_url: The URL for the authenticated extended agent card endpoint.
133+
dependencies: Optional sequence of FastAPI dependencies (e.g.
134+
`[Security(get_current_active_user, scopes=["a2a"])]`)
135+
applied to the RPC endpoint and the authenticated extended
136+
agent card endpoint. The public agent card endpoint is left
137+
unprotected.
132138
"""
139+
route_deps: dict[str, Any] = {}
140+
if dependencies:
141+
route_deps['dependencies'] = list(dependencies)
142+
133143
app.post(
134144
rpc_url,
135145
openapi_extra={
@@ -145,6 +155,7 @@ def add_routes_to_app(
145155
'description': 'A2ARequest',
146156
}
147157
},
158+
**route_deps,
148159
)(self._handle_requests)
149160
app.get(agent_card_url)(self._handle_get_agent_card)
150161

@@ -156,7 +167,7 @@ def add_routes_to_app(
156167
)
157168

158169
if self.agent_card.supports_authenticated_extended_card:
159-
app.get(extended_agent_card_url)(
170+
app.get(extended_agent_card_url, **route_deps)(
160171
self._handle_get_authenticated_extended_agent_card
161172
)
162173

@@ -165,6 +176,7 @@ def build(
165176
agent_card_url: str = AGENT_CARD_WELL_KNOWN_PATH,
166177
rpc_url: str = DEFAULT_RPC_URL,
167178
extended_agent_card_url: str = EXTENDED_AGENT_CARD_PATH,
179+
dependencies: Sequence[Any] | None = None,
168180
**kwargs: Any,
169181
) -> FastAPI:
170182
"""Builds and returns the FastAPI application instance.
@@ -173,6 +185,10 @@ def build(
173185
agent_card_url: The URL for the agent card endpoint.
174186
rpc_url: The URL for the A2A JSON-RPC endpoint.
175187
extended_agent_card_url: The URL for the authenticated extended agent card endpoint.
188+
dependencies: Optional sequence of FastAPI dependencies (e.g.
189+
`[Security(get_current_active_user, scopes=["items"])]`)
190+
applied to authenticated routes. See
191+
:meth:`add_routes_to_app`.
176192
**kwargs: Additional keyword arguments to pass to the FastAPI constructor.
177193
178194
Returns:
@@ -181,7 +197,7 @@ def build(
181197
app = A2AFastAPI(**kwargs)
182198

183199
self.add_routes_to_app(
184-
app, agent_card_url, rpc_url, extended_agent_card_url
200+
app, agent_card_url, rpc_url, extended_agent_card_url, dependencies
185201
)
186202

187203
return app

0 commit comments

Comments
 (0)