9191 Response = Any
9292 HTTP_413_REQUEST_ENTITY_TOO_LARGE = Any
9393
94- MAX_CONTENT_LENGTH = 10_000_000
95-
9694
9795class StarletteUserProxy (A2AUser ):
9896 """Adapts the Starlette User class to the A2A user representation."""
@@ -134,7 +132,7 @@ def build(self, request: Request) -> ServerCallContext:
134132 """
135133 user : A2AUser = UnauthenticatedUser ()
136134 state = {}
137- with contextlib .suppress (Exception ):
135+ with contextlib .suppress (AttributeError ):
138136 user = StarletteUserProxy (request .user )
139137 state ['auth' ] = request .auth
140138 state ['headers' ] = dict (request .headers )
@@ -185,7 +183,7 @@ def __init__( # noqa: PLR0913
185183 [AgentCard , ServerCallContext ], AgentCard
186184 ]
187185 | None = None ,
188- disable_content_length_check : bool = False ,
186+ max_content_length : int | None = 10 * 1024 * 1024 , # 10MB
189187 ) -> None :
190188 """Initializes the JSONRPCApplication.
191189
@@ -203,8 +201,8 @@ def __init__( # noqa: PLR0913
203201 extended_card_modifier: An optional callback to dynamically modify
204202 the extended agent card before it is served. It receives the
205203 call context.
206- disable_content_length_check: An optional, if True disables the check
207- for oversized payloads .
204+ max_content_length: The maximum allowed content length for incoming
205+ requests. Defaults to 10MB. Set to None for unbounded maximum .
208206 """
209207 if not _package_starlette_installed :
210208 raise ImportError (
@@ -223,7 +221,7 @@ def __init__( # noqa: PLR0913
223221 extended_card_modifier = extended_card_modifier ,
224222 )
225223 self ._context_builder = context_builder or DefaultCallContextBuilder ()
226- self ._disable_content_length_check = disable_content_length_check
224+ self ._max_content_length = max_content_length
227225
228226 def _generate_error_response (
229227 self , request_id : str | int | None , error : JSONRPCError | A2AError
@@ -265,19 +263,19 @@ def _generate_error_response(
265263 status_code = 200 ,
266264 )
267265
268- def _check_content_length (self , request : Request ) -> bool :
269- """Checks if the request content length exceeds the maximum allowed size .
266+ def _allowed_content_length (self , request : Request ) -> bool :
267+ """Checks if the request content length is within the allowed maximum .
270268
271269 Args:
272270 request: The incoming Starlette Request object.
273271
274272 Returns:
275- True if the content length is within the allowed limit, False otherwise.
273+ False if the content length is larger than the allowed maximum, True otherwise.
276274 """
277- if not self ._disable_content_length_check :
278- with contextlib .suppress (Exception ):
275+ if self ._max_content_length is not None :
276+ with contextlib .suppress (ValueError ):
279277 content_length = int (request .headers .get ('content-length' , '0' ))
280- if content_length and content_length > MAX_CONTENT_LENGTH :
278+ if content_length and content_length > self . _max_content_length :
281279 return False
282280 return True
283281
@@ -311,9 +309,8 @@ async def _handle_requests(self, request: Request) -> Response: # noqa: PLR0911
311309 request_id , str | int
312310 ):
313311 request_id = None
314- # If content length check is not disabled,
315- # treat very large payloads as invalid request (-32600) before routing
316- if not self ._check_content_length (request ):
312+ # Treat payloads lager than allowed as invalid request (-32600) before routing
313+ if not self ._allowed_content_length (request ):
317314 return self ._generate_error_response (
318315 request_id ,
319316 A2AError (
0 commit comments