Skip to content

Commit dc91ecf

Browse files
committed
Merge branch 'refactor-apps2' of https://github.com/google-a2a/a2a-python into refactor-apps2
2 parents 35731c7 + ae6d3b6 commit dc91ecf

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import logging
22

3+
from collections.abc import AsyncIterator
4+
from contextlib import asynccontextmanager
35
from typing import Any
46

57
from fastapi import FastAPI
68

79
from a2a.server.apps.jsonrpc.jsonrpc_app import (
810
JSONRPCApplication,
911
)
12+
from a2a.server.request_handlers.jsonrpc_handler import RequestHandler
13+
from a2a.types import A2ARequest
1014
from a2a.utils.constants import (
1115
AGENT_CARD_WELL_KNOWN_PATH,
1216
DEFAULT_RPC_URL,
@@ -40,7 +44,22 @@ def add_routes_to_app(
4044
rpc_url: The URL for the A2A JSON-RPC endpoint.
4145
extended_agent_card_url: The URL for the authenticated extended agent card endpoint.
4246
"""
43-
app.post(rpc_url)(self._handle_requests)
47+
app.post(
48+
rpc_url,
49+
openapi_extra={
50+
'requestBody': {
51+
'content': {
52+
'application/json': {
53+
'schema': {
54+
'$ref': '#/components/schemas/A2ARequest'
55+
}
56+
}
57+
},
58+
'required': True,
59+
'description': 'A2ARequest',
60+
}
61+
},
62+
)(self._handle_requests)
4463
app.get(agent_card_url)(self._handle_get_agent_card)
4564

4665
if self.agent_card.supportsAuthenticatedExtendedCard:
@@ -66,7 +85,23 @@ def build(
6685
Returns:
6786
A configured FastAPI application instance.
6887
"""
69-
app = FastAPI(**kwargs)
88+
89+
@asynccontextmanager
90+
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
91+
a2a_request_schema = A2ARequest.model_json_schema(
92+
ref_template='#/components/schemas/{model}'
93+
)
94+
defs = a2a_request_schema.pop('$defs', {})
95+
openapi_schema = app.openapi()
96+
component_schemas = openapi_schema.setdefault(
97+
'components', {}
98+
).setdefault('schemas', {})
99+
component_schemas.update(defs)
100+
component_schemas['A2ARequest'] = a2a_request_schema
101+
102+
yield
103+
104+
app = FastAPI(lifespan=lifespan, **kwargs)
70105

71106
self.add_routes_to_app(
72107
app, agent_card_url, rpc_url, extended_agent_card_url

0 commit comments

Comments
 (0)