Skip to content

Commit 4343be9

Browse files
authored
fix: add proper a2a request body documentation to Swagger UI (#276)
When using `A2AFastAPIApplication`, the A2A request endpoint showed no space to fill in a request body in Swagger UI. This made it impossible to use Swagger UI to test the endpoint directly, since you couldn't input the necessary data. **Changes** - Added proper request body documentation using `A2ARequest` model - Now shows a text box to enter JSON requests and an example of the expected request format **Before** <img width="1705" alt="image" src="https://github.com/user-attachments/assets/8450d6f3-08b9-4b32-9fb0-4af3c1e66208" /> **After** <img width="1484" alt="image" src="https://github.com/user-attachments/assets/86911b91-6a66-470c-836e-73158a613380" /> Fixes #274
1 parent fda4223 commit 4343be9

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
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
@@ -9,7 +11,7 @@
911
JSONRPCApplication,
1012
)
1113
from a2a.server.request_handlers.jsonrpc_handler import RequestHandler
12-
from a2a.types import AgentCard
14+
from a2a.types import A2ARequest, AgentCard
1315
from a2a.utils.constants import (
1416
AGENT_CARD_WELL_KNOWN_PATH,
1517
DEFAULT_RPC_URL,
@@ -69,7 +71,22 @@ def add_routes_to_app(
6971
rpc_url: The URL for the A2A JSON-RPC endpoint.
7072
extended_agent_card_url: The URL for the authenticated extended agent card endpoint.
7173
"""
72-
app.post(rpc_url)(self._handle_requests)
74+
app.post(
75+
rpc_url,
76+
openapi_extra={
77+
'requestBody': {
78+
'content': {
79+
'application/json': {
80+
'schema': {
81+
'$ref': '#/components/schemas/A2ARequest'
82+
}
83+
}
84+
},
85+
'required': True,
86+
'description': 'A2ARequest',
87+
}
88+
},
89+
)(self._handle_requests)
7390
app.get(agent_card_url)(self._handle_get_agent_card)
7491

7592
if self.agent_card.supportsAuthenticatedExtendedCard:
@@ -95,7 +112,23 @@ def build(
95112
Returns:
96113
A configured FastAPI application instance.
97114
"""
98-
app = FastAPI(**kwargs)
115+
116+
@asynccontextmanager
117+
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
118+
a2a_request_schema = A2ARequest.model_json_schema(
119+
ref_template='#/components/schemas/{model}'
120+
)
121+
defs = a2a_request_schema.pop('$defs', {})
122+
openapi_schema = app.openapi()
123+
component_schemas = openapi_schema.setdefault(
124+
'components', {}
125+
).setdefault('schemas', {})
126+
component_schemas.update(defs)
127+
component_schemas['A2ARequest'] = a2a_request_schema
128+
129+
yield
130+
131+
app = FastAPI(lifespan=lifespan, **kwargs)
99132

100133
self.add_routes_to_app(
101134
app, agent_card_url, rpc_url, extended_agent_card_url

0 commit comments

Comments
 (0)