1
1
import logging
2
2
3
+ from collections .abc import AsyncIterator
4
+ from contextlib import asynccontextmanager
3
5
from typing import Any
4
6
5
7
from fastapi import FastAPI
6
8
7
9
from a2a .server .apps .jsonrpc .jsonrpc_app import (
8
10
JSONRPCApplication ,
9
11
)
12
+ from a2a .server .request_handlers .jsonrpc_handler import RequestHandler
13
+ from a2a .types import A2ARequest
10
14
from a2a .utils .constants import (
11
15
AGENT_CARD_WELL_KNOWN_PATH ,
12
16
DEFAULT_RPC_URL ,
@@ -40,7 +44,22 @@ def add_routes_to_app(
40
44
rpc_url: The URL for the A2A JSON-RPC endpoint.
41
45
extended_agent_card_url: The URL for the authenticated extended agent card endpoint.
42
46
"""
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 )
44
63
app .get (agent_card_url )(self ._handle_get_agent_card )
45
64
46
65
if self .agent_card .supportsAuthenticatedExtendedCard :
@@ -66,7 +85,23 @@ def build(
66
85
Returns:
67
86
A configured FastAPI application instance.
68
87
"""
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 )
70
105
71
106
self .add_routes_to_app (
72
107
app , agent_card_url , rpc_url , extended_agent_card_url
0 commit comments