[Bug]: JSON-RPC server returns -32603 Internal error when `params` is omitted or the body is not a JSON object
What happened?
The JSON-RPC transport turns two kinds of structurally valid requests into -32603 Internal error, with an ERROR-level stack trace in the server log:
paramsomitted. JSON-RPC 2.0 (section 4) saysparamsMAY be omitted.JSONRPCUtils.parseRequestBody(String, String)readsjsonRpc.get("params"), which isnullwhen the member is absent.parseMethodRequestthen callsparseRequestBody(JsonElement, Builder, Object)for every method exceptGetExtendedAgentCard, and that call runsjsonRpc.toString()on the null node, which throws aNullPointerException. (TheGetExtendedAgentCardbranch already checksparamsNodefor null.)- Body is valid JSON but not an object (for example
[],"x"or1).parseRequestBodycallsjelement.getAsJsonObject()without checking the type first, which throwsIllegalStateException: Not a JSON Object.
parseRequestBody declares only JsonMappingException / JsonProcessingException. A2AServerRoutes.invokeJSONRPCHandler maps those exceptions and A2AError. Anything else falls through to catch (Throwable t), which logs Internal error while processing request and returns InternalError.
Reproduce (reference JSON-RPC server, current main ad9571c9, also in 1.4.0.Final):
POST / {"jsonrpc":"2.0","id":1,"method":"SendMessage"}
-> error.code = -32603, message "Internal error" (expected -32602)
POST / []
-> error.code = -32603, message "Internal error" (expected -32600)
Or call it directly: JSONRPCUtils.parseRequestBody("{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"GetTask\"}", null) throws an NPE, and JSONRPCUtils.parseRequestBody("[]", null) throws an IllegalStateException.
Expected
- An omitted
paramsshould be handled the same as"params": {}. Methods with only optional fields (ListTasks) should succeed. Methods with required fields should go through the existing validation, for exampleSendMessagereturns-32602 Invalid params("Parameter 'message' may not be null"), as it already does for"params": {}. - A non-object body should get
-32600 Invalid Request(JSON-RPC 2.0 section 5.1: "The JSON sent is not a valid Request object").
In both cases a client error should not show up as a server-side internal error. #512 set the same expectation for invalid SendMessage input that came back as -32603.
Suggested fix (small, in JSONRPCUtils only):
- In
parseRequestBody(String, String), checkjelement.isJsonObject()and throwJsonMappingExceptionif it is not an object. The route already maps that toInvalidRequestError. - In
parseMethodRequest, replace a missingparamswith an emptyJsonObjectbefore parsing. Leave theGetExtendedAgentCardbranch as it is.
I have a fix with regression tests in JSONRPCUtilsTest and A2AServerRoutesTest, and I'm happy to open a PR if someone assigns this to me.
Relevant log output
ERROR [org.a2aproject.sdk.server.apps.quarkus.A2AServerRoutes] Internal error while processing request
java.lang.NullPointerException: Cannot invoke "com.google.gson.JsonElement.toString()" because "jsonRpc" is null
at org.a2aproject.sdk.grpc.utils.JSONRPCUtils.parseRequestBody(JSONRPCUtils.java:437)
at org.a2aproject.sdk.grpc.utils.JSONRPCUtils.parseMethodRequest(JSONRPCUtils.java:211)
at org.a2aproject.sdk.grpc.utils.JSONRPCUtils.parseRequestBody(JSONRPCUtils.java:195)
java.lang.IllegalStateException: Not a JSON Object: []
at com.google.gson.JsonElement.getAsJsonObject(JsonElement.java)
at org.a2aproject.sdk.grpc.utils.JSONRPCUtils.parseRequestBody(JSONRPCUtils.java:184)
Environment: a2a-java main @ ad9571c9 (1.4.1.Final-SNAPSHOT), reference/jsonrpc (Quarkus), JDK 17.
Code of Conduct
- [ ] I agree to follow this project's Code of Conduct
AI assistance: drafted with an AI coding assistant (Claude). The reproduction above was run locally against the current default branch.
I'd like to take this one myself and will open a PR for it. The fix (returning -32602 Invalid params / -32600 Invalid Request instead of -32603 when params is omitted or the body is not a JSON object) and a regression test are ready locally. Could a maintainer assign this issue to me? I'll link the PR here with Fixes #1196 per CONTRIBUTING.md.
AI assistance: drafted with an AI coding assistant (Claude).
@Zhuoxi2000 feel free, thanks!
The diagnosis and proposed fix look sound. Please include regression tests covering omitted params for both ListTasks and methods with required fields, plus non-object request bodies.
A couple of details to preserve (from AI):
- Keep
GetExtendedAgentCard’s existing handling of absent parameters. - Normalize only omitted
params; explicit"params": nullshould remain a distinct case. - Verify response IDs: invalid-parameter responses should preserve the request ID, while non-object bodies should return
"id": null.
One small clarification to the issue wording: JSON-RPC supports nonempty arrays as batch requests, so not every non-object body is inherently invalid. The empty-array example ([]) is correctly expected to return -32600.