11import logging
22
3+ from collections .abc import AsyncIterator
4+ from contextlib import asynccontextmanager
35from typing import Any
46
57from fastapi import FastAPI
911 JSONRPCApplication ,
1012)
1113from a2a .server .request_handlers .jsonrpc_handler import RequestHandler
12- from a2a .types import AgentCard
14+ from a2a .types import A2ARequest , AgentCard
1315from 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