Skip to content

Commit 6eb7e41

Browse files
feat: Add validation for the JSON-RPC version (#808)
# Description This PR introduces a check for the jsonrpc version, which for tck tests and [specifications](https://a2a-protocol.org/latest/specification/#91-protocol-requirements) must be 2.0.
1 parent bbd09f2 commit 6eb7e41

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

src/a2a/server/apps/jsonrpc/jsonrpc_app.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,13 @@ async def _handle_requests(self, request: Request) -> Response: # noqa: PLR0911
365365
message='Batch requests are not supported'
366366
),
367367
)
368+
if body.get('jsonrpc') != '2.0':
369+
return self._generate_error_response(
370+
request_id,
371+
InvalidRequestError(
372+
message="Invalid request: 'jsonrpc' must be exactly '2.0'"
373+
),
374+
)
368375
except Exception as e:
369376
logger.exception('Failed to validate base JSON-RPC request')
370377
return self._generate_error_response(

tests/server/test_integration.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,24 @@ def test_invalid_request_structure(client: TestClient):
703703
assert response.status_code == 200
704704
data = response.json()
705705
assert 'error' in data
706-
# The jsonrpc library returns MethodNotFoundError for unknown methods
706+
# The jsonrpc library returns InvalidRequestError for invalid requests format
707+
assert data['error']['code'] == InvalidRequestError().code
708+
709+
710+
def test_invalid_request_method(client: TestClient):
711+
"""Test handling an invalid request method."""
712+
response = client.post(
713+
'/',
714+
json={
715+
'jsonrpc': '2.0', # Missing or wrong required fields
716+
'id': '123',
717+
'method': 'foo/bar',
718+
},
719+
)
720+
assert response.status_code == 200
721+
data = response.json()
722+
assert 'error' in data
723+
# The jsonrpc library returns MethodNotFoundError for invalid request method
707724
assert data['error']['code'] == MethodNotFoundError().code
708725

709726

0 commit comments

Comments
 (0)