|
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, Request, Response |
|
9 | 11 | JSONRPCApplication, |
10 | 12 | ) |
11 | 13 | from a2a.server.request_handlers.jsonrpc_handler import RequestHandler |
12 | | -from a2a.types import AgentCard |
| 14 | +from a2a.types import A2ARequest, AgentCard |
13 | 15 | from a2a.utils.constants import ( |
14 | 16 | AGENT_CARD_WELL_KNOWN_PATH, |
15 | 17 | DEFAULT_RPC_URL, |
@@ -70,7 +72,22 @@ def add_routes_to_app( |
70 | 72 | extended_agent_card_url: The URL for the authenticated extended agent card endpoint. |
71 | 73 | """ |
72 | 74 |
|
73 | | - @app.post(rpc_url) |
| 75 | + @app.post( |
| 76 | + rpc_url, |
| 77 | + openapi_extra={ |
| 78 | + 'requestBody': { |
| 79 | + 'content': { |
| 80 | + 'application/json': { |
| 81 | + 'schema': { |
| 82 | + '$ref': '#/components/schemas/A2ARequest' |
| 83 | + } |
| 84 | + } |
| 85 | + }, |
| 86 | + 'required': True, |
| 87 | + 'description': 'A2ARequest', |
| 88 | + } |
| 89 | + }, |
| 90 | + ) |
74 | 91 | async def handle_a2a_request(request: Request) -> Response: |
75 | 92 | return await self._handle_requests(request) |
76 | 93 |
|
@@ -104,7 +121,25 @@ def build( |
104 | 121 | Returns: |
105 | 122 | A configured FastAPI application instance. |
106 | 123 | """ |
107 | | - app = FastAPI(**kwargs) |
| 124 | + |
| 125 | + @asynccontextmanager |
| 126 | + async def lifespan(app: FastAPI) -> AsyncIterator[None]: |
| 127 | + a2a_request_schema = A2ARequest.model_json_schema( |
| 128 | + ref_template='#/components/schemas/{model}' |
| 129 | + ) |
| 130 | + defs = a2a_request_schema.pop('$defs', {}) |
| 131 | + openapi_schema = app.openapi() |
| 132 | + component_schemas = openapi_schema.setdefault( |
| 133 | + 'components', {} |
| 134 | + ).setdefault('schemas', {}) |
| 135 | + component_schemas.update(defs) |
| 136 | + component_schemas['A2ARequest'] = a2a_request_schema |
| 137 | + |
| 138 | + yield |
| 139 | + # Code to run on shutdown would go here |
| 140 | + |
| 141 | + # Create app with lifespan handler |
| 142 | + app = FastAPI(lifespan=lifespan, **kwargs) |
108 | 143 |
|
109 | 144 | self.add_routes_to_app( |
110 | 145 | app, agent_card_url, rpc_url, extended_agent_card_url |
|
0 commit comments