Skip to content

Commit b2f8adc

Browse files
add fastapi jsonrpc app
1 parent 04a3626 commit b2f8adc

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,31 @@ class A2AFastAPIApplication(JSONRPCApplication):
1717
handler methods, and manages response generation including Server-Sent Events
1818
(SSE)."""
1919

20-
def __init__(self, agent_card: AgentCard, http_handler: RequestHandler):
21-
"""Initializes the A2A FastAPI application.
20+
def __init__(
21+
self,
22+
agent_card: AgentCard,
23+
http_handler: RequestHandler,
24+
extended_agent_card: AgentCard | None = None,
25+
context_builder: CallContextBuilder | None = None,
26+
):
27+
"""Initializes the A2AStarletteApplication.
2228
2329
Args:
2430
agent_card: The AgentCard describing the agent's capabilities.
25-
http_handler: The handler instance responsible for processing A2A requests via http.
31+
http_handler: The handler instance responsible for processing A2A
32+
requests via http.
33+
extended_agent_card: An optional, distinct AgentCard to be served
34+
at the authenticated extended card endpoint.
35+
context_builder: The CallContextBuilder used to construct the
36+
ServerCallContext passed to the http_handler. If None, no
37+
ServerCallContext is passed.
2638
"""
27-
super().__init__(agent_card, http_handler)
39+
super().__init__(
40+
agent_card=agent_card,
41+
http_handler=http_handler,
42+
extended_agent_card=extended_agent_card,
43+
context_builder=context_builder
44+
)
2845

2946
def build(
3047
self,

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ def build(self, request: Request) -> ServerCallContext:
5151

5252

5353
class JSONRPCApplication(ABC):
54-
"""Base class for A2A applications.
54+
"""Base class for A2A JSONRPC applications.
5555
56-
Args:
57-
agent_card: The AgentCard describing the agent's capabilities.
58-
http_handler: The handler instance responsible for processing A2A
59-
requests via http.
56+
Handles incoming JSON-RPC requests, routes them to the appropriate
57+
handler methods, and manages response generation including Server-Sent Events
58+
(SSE).
6059
"""
6160

6261
def __init__(
6362
self,
6463
agent_card: AgentCard,
6564
http_handler: RequestHandler,
65+
extended_agent_card: AgentCard | None = None,
6666
context_builder: CallContextBuilder | None = None,
6767
):
6868
"""Initializes the A2AStarletteApplication.
@@ -71,14 +71,24 @@ def __init__(
7171
agent_card: The AgentCard describing the agent's capabilities.
7272
http_handler: The handler instance responsible for processing A2A
7373
requests via http.
74+
extended_agent_card: An optional, distinct AgentCard to be served
75+
at the authenticated extended card endpoint.
7476
context_builder: The CallContextBuilder used to construct the
7577
ServerCallContext passed to the http_handler. If None, no
7678
ServerCallContext is passed.
7779
"""
7880
self.agent_card = agent_card
81+
self.extended_agent_card = extended_agent_card
7982
self.handler = JSONRPCHandler(
8083
agent_card=agent_card, request_handler=http_handler
8184
)
85+
if (
86+
self.agent_card.supportsAuthenticatedExtendedCard
87+
and self.extended_agent_card is None
88+
):
89+
logger.error(
90+
'AgentCard.supportsAuthenticatedExtendedCard is True, but no extended_agent_card was provided. The /agent/authenticatedExtendedCard endpoint will return 404.'
91+
)
8292
self._context_builder = context_builder
8393

8494
def _generate_error_response(
@@ -321,10 +331,37 @@ async def _handle_get_agent_card(self, request: Request) -> JSONResponse:
321331
Returns:
322332
A JSONResponse containing the agent card data.
323333
"""
334+
# The public agent card is a direct serialization of the agent_card
335+
# provided at initialization.
324336
return JSONResponse(
325337
self.agent_card.model_dump(mode='json', exclude_none=True)
326338
)
327339

340+
async def _handle_get_authenticated_extended_agent_card(
341+
self, request: Request
342+
) -> JSONResponse:
343+
"""Handles GET requests for the authenticated extended agent card."""
344+
if not self.agent_card.supportsAuthenticatedExtendedCard:
345+
return JSONResponse(
346+
{'error': 'Extended agent card not supported or not enabled.'},
347+
status_code=404,
348+
)
349+
350+
# If an explicit extended_agent_card is provided, serve that.
351+
if self.extended_agent_card:
352+
return JSONResponse(
353+
self.extended_agent_card.model_dump(
354+
mode='json', exclude_none=True
355+
)
356+
)
357+
# If supportsAuthenticatedExtendedCard is true, but no specific
358+
# extended_agent_card was provided during server initialization,
359+
# return a 404
360+
return JSONResponse(
361+
{'error': 'Authenticated extended agent card is supported but not configured on the server.'},
362+
status_code=404,
363+
)
364+
328365
@abstractmethod
329366
def build(
330367
self,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def __init__(
2626
self,
2727
agent_card: AgentCard,
2828
http_handler: RequestHandler,
29+
extended_agent_card: AgentCard | None = None,
2930
context_builder: CallContextBuilder | None = None,
3031
):
3132
"""Initializes the A2AStarletteApplication.
@@ -34,13 +35,16 @@ def __init__(
3435
agent_card: The AgentCard describing the agent's capabilities.
3536
http_handler: The handler instance responsible for processing A2A
3637
requests via http.
38+
extended_agent_card: An optional, distinct AgentCard to be served
39+
at the authenticated extended card endpoint.
3740
context_builder: The CallContextBuilder used to construct the
3841
ServerCallContext passed to the http_handler. If None, no
3942
ServerCallContext is passed.
4043
"""
4144
super().__init__(
4245
agent_card=agent_card,
4346
http_handler=http_handler,
47+
extended_agent_card=extended_agent_card,
4448
context_builder=context_builder
4549
)
4650

0 commit comments

Comments
 (0)