a2a-java icon indicating copy to clipboard operation
a2a-java copied to clipboard

[Bug]: JSON-RPC server returns -32603 Internal error when `params` is omitted or the body is not a JSON object

Open Zhuoxi2000 opened this issue 2 months ago • 2 comments

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:

  1. params omitted. JSON-RPC 2.0 (section 4) says params MAY be omitted. JSONRPCUtils.parseRequestBody(String, String) reads jsonRpc.get("params"), which is null when the member is absent. parseMethodRequest then calls parseRequestBody(JsonElement, Builder, Object) for every method except GetExtendedAgentCard, and that call runs jsonRpc.toString() on the null node, which throws a NullPointerException. (The GetExtendedAgentCard branch already checks paramsNode for null.)
  2. Body is valid JSON but not an object (for example [], "x" or 1). parseRequestBody calls jelement.getAsJsonObject() without checking the type first, which throws IllegalStateException: 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 params should 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 example SendMessage returns -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), check jelement.isJsonObject() and throw JsonMappingException if it is not an object. The route already maps that to InvalidRequestError.
  • In parseMethodRequest, replace a missing params with an empty JsonObject before parsing. Leave the GetExtendedAgentCard branch 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.

Zhuoxi2000 avatar Oct 01 '26 13:10 Zhuoxi2000

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 avatar Oct 01 '26 23:10 Zhuoxi2000

@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": null should 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.

kabir avatar Oct 02 '26 09:10 kabir