Skip to content

Commit cd7f67a

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

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
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:
@@ -121,6 +122,7 @@ def add_routes_to_app(
121122
agent_card_url: str = AGENT_CARD_WELL_KNOWN_PATH,
122123
rpc_url: str = DEFAULT_RPC_URL,
123124
extended_agent_card_url: str = EXTENDED_AGENT_CARD_PATH,
125+
dependencies: Sequence[Depends] | None = None,
124126
) -> None:
125127
"""Adds the routes to the FastAPI application.
126128
@@ -129,7 +131,16 @@ def add_routes_to_app(
129131
agent_card_url: The URL for the agent card endpoint.
130132
rpc_url: The URL for the A2A JSON-RPC endpoint.
131133
extended_agent_card_url: The URL for the authenticated extended agent card endpoint.
134+
dependencies: Optional sequence of FastAPI dependencies (e.g.
135+
`[Security(get_current_active_user, scopes=["a2a"])]`)
136+
applied to the RPC endpoint and the authenticated extended
137+
agent card endpoint. The public agent card endpoint is left
138+
unprotected.
132139
"""
140+
route_deps: dict[str, Any] = {}
141+
if dependencies:
142+
route_deps['dependencies'] = list(dependencies)
143+
133144
app.post(
134145
rpc_url,
135146
openapi_extra={
@@ -145,6 +156,7 @@ def add_routes_to_app(
145156
'description': 'A2ARequest',
146157
}
147158
},
159+
**route_deps,
148160
)(self._handle_requests)
149161
app.get(agent_card_url)(self._handle_get_agent_card)
150162

@@ -156,7 +168,7 @@ def add_routes_to_app(
156168
)
157169

158170
if self.agent_card.supports_authenticated_extended_card:
159-
app.get(extended_agent_card_url)(
171+
app.get(extended_agent_card_url, **route_deps)(
160172
self._handle_get_authenticated_extended_agent_card
161173
)
162174

@@ -165,6 +177,7 @@ def build(
165177
agent_card_url: str = AGENT_CARD_WELL_KNOWN_PATH,
166178
rpc_url: str = DEFAULT_RPC_URL,
167179
extended_agent_card_url: str = EXTENDED_AGENT_CARD_PATH,
180+
dependencies: Sequence[Depends] | None = None,
168181
**kwargs: Any,
169182
) -> FastAPI:
170183
"""Builds and returns the FastAPI application instance.
@@ -173,6 +186,10 @@ def build(
173186
agent_card_url: The URL for the agent card endpoint.
174187
rpc_url: The URL for the A2A JSON-RPC endpoint.
175188
extended_agent_card_url: The URL for the authenticated extended agent card endpoint.
189+
dependencies: Optional sequence of FastAPI dependencies (e.g.
190+
`[Security(get_current_active_user, scopes=["a2a"])]`)
191+
applied to authenticated routes. See
192+
:meth:`add_routes_to_app`.
176193
**kwargs: Additional keyword arguments to pass to the FastAPI constructor.
177194
178195
Returns:
@@ -181,7 +198,7 @@ def build(
181198
app = A2AFastAPI(**kwargs)
182199

183200
self.add_routes_to_app(
184-
app, agent_card_url, rpc_url, extended_agent_card_url
201+
app, agent_card_url, rpc_url, extended_agent_card_url, dependencies
185202
)
186203

187204
return app

0 commit comments

Comments
 (0)