Skip to content

Commit fee5d5e

Browse files
committed
fix suggestions
1 parent 71a9285 commit fee5d5e

5 files changed

Lines changed: 9 additions & 52 deletions

File tree

src/a2a/server/routes/agent_card_routes.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,21 @@ def __init__(
6363
'The `starlette` package is required to use `AgentCardRoutes`. '
6464
'It can be installed as part of `a2a-sdk` optional dependencies, `a2a-sdk[http-server]`.'
6565
)
66-
)
6766

6867
self.agent_card = agent_card
6968
self.card_modifier = card_modifier
7069

71-
async def get_agent_card(request: Request) -> Response:
72-
card_to_serve = self.agent_card
73-
if self.card_modifier:
74-
card_to_serve = await maybe_await(
75-
self.card_modifier(card_to_serve)
76-
)
77-
return JSONResponse(agent_card_to_dict(card_to_serve))
78-
7970
self.routes = [
8071
Route(
8172
path=card_url,
82-
endpoint=get_agent_card,
73+
endpoint=self._get_agent_card,
8374
methods=['GET'],
8475
middleware=middleware,
8576
)
8677
]
78+
79+
async def _get_agent_card(self, request: Request) -> Response:
80+
card_to_serve = self.agent_card
81+
if self.card_modifier:
82+
card_to_serve = await maybe_await(self.card_modifier(card_to_serve))
83+
return JSONResponse(agent_card_to_dict(card_to_serve))

src/a2a/server/routes/jsonrpc_dispatcher.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@
4747
SubscribeToTaskRequest,
4848
TaskPushNotificationConfig,
4949
)
50-
from a2a.utils.constants import (
51-
DEFAULT_MAX_CONTENT_LENGTH,
52-
)
5350
from a2a.utils.errors import (
5451
A2AError,
5552
UnsupportedOperationError,
@@ -202,7 +199,6 @@ def __init__( # noqa: PLR0913
202199
]
203200
| None = None,
204201
enable_v0_3_compat: bool = False,
205-
max_content_length: int | None = DEFAULT_MAX_CONTENT_LENGTH,
206202
) -> None:
207203
"""Initializes the JsonRpcDispatcher.
208204
@@ -220,8 +216,6 @@ def __init__( # noqa: PLR0913
220216
extended_card_modifier: An optional callback to dynamically modify
221217
the extended agent card before it is served. It receives the
222218
call context.
223-
max_content_length: The maximum allowed content length for incoming
224-
requests. Defaults to 10MB. Set to None for unbounded maximum.
225219
enable_v0_3_compat: Whether to enable v0.3 backward compatibility on the same endpoint.
226220
"""
227221
if not _package_starlette_installed:
@@ -242,7 +236,6 @@ def __init__( # noqa: PLR0913
242236
extended_card_modifier=extended_card_modifier,
243237
)
244238
self._context_builder = context_builder or DefaultCallContextBuilder()
245-
self._max_content_length = max_content_length
246239
self.enable_v0_3_compat = enable_v0_3_compat
247240
self._v03_adapter: JSONRPC03Adapter | None = None
248241

@@ -298,22 +291,6 @@ def _generate_error_response(
298291
status_code=200,
299292
)
300293

301-
def _allowed_content_length(self, request: Request) -> bool:
302-
"""Checks if the request content length is within the allowed maximum.
303-
304-
Args:
305-
request: The incoming Starlette Request object.
306-
307-
Returns:
308-
False if the content length is larger than the allowed maximum, True otherwise.
309-
"""
310-
if self._max_content_length is not None:
311-
with contextlib.suppress(ValueError):
312-
content_length = int(request.headers.get('content-length', '0'))
313-
if content_length and content_length > self._max_content_length:
314-
return False
315-
return True
316-
317294
async def _handle_requests(self, request: Request) -> Response: # noqa: PLR0911, PLR0912
318295
"""Handles incoming POST requests to the main A2A endpoint.
319296
@@ -344,12 +321,6 @@ async def _handle_requests(self, request: Request) -> Response: # noqa: PLR0911
344321
request_id, str | int
345322
):
346323
request_id = None
347-
# Treat payloads lager than allowed as invalid request (-32600) before routing
348-
if not self._allowed_content_length(request):
349-
return self._generate_error_response(
350-
request_id,
351-
InvalidRequestError(message='Payload too large'),
352-
)
353324
logger.debug('Request body: %s', body)
354325
# 1) Validate base JSON-RPC structure only (-32600 on failure)
355326
try:

src/a2a/utils/constants.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class TransportProtocol(str, Enum):
2020
GRPC = 'GRPC'
2121

2222

23-
DEFAULT_MAX_CONTENT_LENGTH = 10 * 1024 * 1024 # 10MB
2423
JSONRPC_PARSE_ERROR_CODE = -32700
2524
VERSION_HEADER = 'A2A-Version'
2625

tests/server/routes/test_agent_card_routes.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pytest
77
from starlette.testclient import TestClient
88
from starlette.middleware import Middleware
9+
from starlette.applications import Starlette
910

1011
from a2a.server.routes.agent_card_routes import AgentCardRoutes
1112
from a2a.types.a2a_pb2 import AgentCard
@@ -20,8 +21,6 @@ def test_get_agent_card_success(agent_card):
2021
"""Tests that the agent card route returns the card correctly."""
2122
routes = AgentCardRoutes(agent_card=agent_card).routes
2223

23-
from starlette.applications import Starlette
24-
2524
app = Starlette(routes=routes)
2625
client = TestClient(app)
2726

@@ -52,8 +51,6 @@ async def modifier(card: AgentCard) -> AgentCard:
5251
agent_card=agent_card, card_modifier=mock_modifier
5352
).routes
5453

55-
from starlette.applications import Starlette
56-
5754
app = Starlette(routes=routes)
5855
client = TestClient(app)
5956

@@ -67,8 +64,6 @@ def test_agent_card_custom_url(agent_card):
6764
custom_url = '/custom/path/agent.json'
6865
routes = AgentCardRoutes(agent_card=agent_card, card_url=custom_url).routes
6966

70-
from starlette.applications import Starlette
71-
7267
app = Starlette(routes=routes)
7368
client = TestClient(app)
7469

@@ -95,8 +90,6 @@ async def __call__(self, scope, receive, send):
9590
agent_card=agent_card, middleware=[Middleware(MyMiddleware)]
9691
).routes
9792

98-
from starlette.applications import Starlette
99-
10093
app = Starlette(routes=routes)
10194
client = TestClient(app)
10295

tests/server/routes/test_jsonrpc_routes.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66
from starlette.testclient import TestClient
77
from starlette.middleware import Middleware
8+
from starlette.applications import Starlette
89

910
from a2a.server.routes.jsonrpc_routes import JsonRpcRoutes
1011
from a2a.server.request_handlers.request_handler import RequestHandler
@@ -44,8 +45,6 @@ def test_jsonrpc_custom_url(agent_card, mock_handler):
4445
agent_card=agent_card, request_handler=mock_handler, rpc_url=custom_url
4546
)
4647

47-
from starlette.applications import Starlette
48-
4948
app = Starlette(routes=jsonrpc_routes.routes)
5049
client = TestClient(app)
5150

@@ -83,8 +82,6 @@ async def __call__(self, scope, receive, send):
8382
rpc_url='/',
8483
)
8584

86-
from starlette.applications import Starlette
87-
8885
app = Starlette(routes=jsonrpc_routes.routes)
8986
client = TestClient(app)
9087

0 commit comments

Comments
 (0)