|
39 | 39 | from a2a.utils.constants import ( |
40 | 40 | AGENT_CARD_WELL_KNOWN_PATH, |
41 | 41 | DEFAULT_RPC_URL, |
| 42 | + EXTENDED_AGENT_CARD_PATH, |
42 | 43 | ) |
43 | 44 | from a2a.utils.errors import MethodNotImplementedError |
44 | 45 |
|
|
50 | 51 | from sse_starlette.sse import EventSourceResponse |
51 | 52 | from starlette.applications import Starlette |
52 | 53 | from starlette.authentication import BaseUser |
| 54 | + from starlette.exceptions import HTTPException |
53 | 55 | from starlette.requests import Request |
54 | 56 | from starlette.responses import JSONResponse, Response |
| 57 | + from starlette.status import HTTP_413_REQUEST_ENTITY_TOO_LARGE |
55 | 58 |
|
56 | 59 | _package_starlette_installed = True |
57 | 60 | else: |
|
60 | 63 | from sse_starlette.sse import EventSourceResponse |
61 | 64 | from starlette.applications import Starlette |
62 | 65 | from starlette.authentication import BaseUser |
| 66 | + from starlette.exceptions import HTTPException |
63 | 67 | from starlette.requests import Request |
64 | 68 | from starlette.responses import JSONResponse, Response |
| 69 | + from starlette.status import HTTP_413_REQUEST_ENTITY_TOO_LARGE |
65 | 70 |
|
66 | 71 | _package_starlette_installed = True |
67 | 72 | except ImportError: |
|
71 | 76 | EventSourceResponse = Any |
72 | 77 | Starlette = Any |
73 | 78 | BaseUser = Any |
| 79 | + HTTPException = Any |
74 | 80 | Request = Any |
75 | 81 | JSONResponse = Any |
76 | 82 | Response = Any |
| 83 | + HTTP_413_REQUEST_ENTITY_TOO_LARGE = Any |
77 | 84 |
|
78 | 85 |
|
79 | 86 | class StarletteUserProxy(A2AUser): |
@@ -206,7 +213,7 @@ def _generate_error_response( |
206 | 213 | status_code=200, |
207 | 214 | ) |
208 | 215 |
|
209 | | - async def _handle_requests(self, request: Request) -> Response: |
| 216 | + async def _handle_requests(self, request: Request) -> Response: # noqa: PLR0911 |
210 | 217 | """Handles incoming POST requests to the main A2A endpoint. |
211 | 218 |
|
212 | 219 | Parses the request body as JSON, validates it against A2A request types, |
@@ -262,6 +269,15 @@ async def _handle_requests(self, request: Request) -> Response: |
262 | 269 | request_id, |
263 | 270 | A2AError(root=InvalidRequestError(data=json.loads(e.json()))), |
264 | 271 | ) |
| 272 | + except HTTPException as e: |
| 273 | + if e.status_code == HTTP_413_REQUEST_ENTITY_TOO_LARGE: |
| 274 | + return self._generate_error_response( |
| 275 | + request_id, |
| 276 | + A2AError( |
| 277 | + root=InvalidRequestError(message='Payload too large') |
| 278 | + ), |
| 279 | + ) |
| 280 | + raise e |
265 | 281 | except Exception as e: |
266 | 282 | logger.error(f'Unhandled exception: {e}') |
267 | 283 | traceback.print_exc() |
@@ -468,13 +484,16 @@ def build( |
468 | 484 | self, |
469 | 485 | agent_card_url: str = AGENT_CARD_WELL_KNOWN_PATH, |
470 | 486 | rpc_url: str = DEFAULT_RPC_URL, |
| 487 | + extended_agent_card_url: str = EXTENDED_AGENT_CARD_PATH, |
471 | 488 | **kwargs: Any, |
472 | 489 | ) -> FastAPI | Starlette: |
473 | 490 | """Builds and returns the JSONRPC application instance. |
474 | 491 |
|
475 | 492 | Args: |
476 | 493 | agent_card_url: The URL for the agent card endpoint. |
477 | | - rpc_url: The URL for the A2A JSON-RPC endpoint |
| 494 | + rpc_url: The URL for the A2A JSON-RPC endpoint. |
| 495 | + extended_agent_card_url: The URL for the authenticated extended |
| 496 | + agent card endpoint. |
478 | 497 | **kwargs: Additional keyword arguments to pass to the FastAPI constructor. |
479 | 498 |
|
480 | 499 | Returns: |
|
0 commit comments