Skip to content

Commit 085ab57

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

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
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

77
if TYPE_CHECKING:
88
from fastapi import FastAPI
9+
from fastapi.params import Depends
910

1011
_package_fastapi_installed = True
1112
else:
1213
try:
1314
from fastapi import FastAPI
15+
from fastapi.params import Depends
1416

1517
_package_fastapi_installed = True
1618
except ImportError:
@@ -121,6 +123,7 @@ def add_routes_to_app(
121123
agent_card_url: str = AGENT_CARD_WELL_KNOWN_PATH,
122124
rpc_url: str = DEFAULT_RPC_URL,
123125
extended_agent_card_url: str = EXTENDED_AGENT_CARD_PATH,
126+
dependencies: Sequence[Depends] | None = None,
124127
) -> None:
125128
"""Adds the routes to the FastAPI application.
126129
@@ -129,7 +132,16 @@ def add_routes_to_app(
129132
agent_card_url: The URL for the agent card endpoint.
130133
rpc_url: The URL for the A2A JSON-RPC endpoint.
131134
extended_agent_card_url: The URL for the authenticated extended agent card endpoint.
135+
dependencies: Optional sequence of FastAPI dependencies (e.g.
136+
`[Security(get_current_active_user, scopes=["a2a"])]`)
137+
applied to the RPC endpoint and the authenticated extended
138+
agent card endpoint. The public agent card endpoint is left
139+
unprotected.
132140
"""
141+
route_deps: dict[str, Any] = {}
142+
if dependencies:
143+
route_deps['dependencies'] = list(dependencies)
144+
133145
app.post(
134146
rpc_url,
135147
openapi_extra={
@@ -145,6 +157,7 @@ def add_routes_to_app(
145157
'description': 'A2ARequest',
146158
}
147159
},
160+
**route_deps,
148161
)(self._handle_requests)
149162
app.get(agent_card_url)(self._handle_get_agent_card)
150163

@@ -156,7 +169,7 @@ def add_routes_to_app(
156169
)
157170

158171
if self.agent_card.supports_authenticated_extended_card:
159-
app.get(extended_agent_card_url)(
172+
app.get(extended_agent_card_url, **route_deps)(
160173
self._handle_get_authenticated_extended_agent_card
161174
)
162175

@@ -165,6 +178,7 @@ def build(
165178
agent_card_url: str = AGENT_CARD_WELL_KNOWN_PATH,
166179
rpc_url: str = DEFAULT_RPC_URL,
167180
extended_agent_card_url: str = EXTENDED_AGENT_CARD_PATH,
181+
dependencies: Sequence[Depends] | None = None,
168182
**kwargs: Any,
169183
) -> FastAPI:
170184
"""Builds and returns the FastAPI application instance.
@@ -173,6 +187,10 @@ def build(
173187
agent_card_url: The URL for the agent card endpoint.
174188
rpc_url: The URL for the A2A JSON-RPC endpoint.
175189
extended_agent_card_url: The URL for the authenticated extended agent card endpoint.
190+
dependencies: Optional sequence of FastAPI dependencies (e.g.
191+
`[Security(get_current_active_user, scopes=["a2a"])]`)
192+
applied to authenticated routes. See
193+
:meth:`add_routes_to_app`.
176194
**kwargs: Additional keyword arguments to pass to the FastAPI constructor.
177195
178196
Returns:
@@ -181,7 +199,7 @@ def build(
181199
app = A2AFastAPI(**kwargs)
182200

183201
self.add_routes_to_app(
184-
app, agent_card_url, rpc_url, extended_agent_card_url
202+
app, agent_card_url, rpc_url, extended_agent_card_url, dependencies
185203
)
186204

187205
return app

0 commit comments

Comments
 (0)