Echo the JSON-RPC request id in MCP errors
What does this MR do and why?
The /api/v4/mcp and /api/v4/orbit/mcp endpoints returned a null id in error responses, even when the request had a readable id. JSON-RPC 2.0 says the error id must match the request id. Without this, a client with several requests in flight cannot tell which call an error belongs to.
Both endpoints now use a small jsonrpc_request_id helper. The helper returns the id only when it is an Integer or a String. It returns null for any other id, as JSON-RPC requires.
Measured before and after, on the same requests:
| Request | Endpoint | Before | After |
|---|---|---|---|
Bad protocol version, id "abc-123", error -32602 |
/api/v4/mcp |
null |
"abc-123" |
Bad protocol version, id "abc-123", error -32602 |
/api/v4/orbit/mcp |
"abc-123" |
"abc-123" |
Invalid jsonrpc value, id 7, error -32600 |
/api/v4/mcp |
null |
7 |
Invalid jsonrpc value, id 7, error -32600 |
/api/v4/orbit/mcp |
null |
7 |
The Orbit -32602 path already worked, because that endpoint stores the id in env inside the route block. The other three cases are fixed by this MR.
References
- Related issue: #611372 (closed)
- JSON-RPC 2.0 response object: https://www.jsonrpc.org/specification#response_object
Screenshots or screen recordings
Not applicable. This changes a JSON-RPC API response.
How to set up and validate locally
-
Enable the MCP server. In the Rails console run
ApplicationSetting.current.update!(mcp_server_enabled: true). -
Send a request with a bad protocol version and a known id. Confirm the error code is
-32602and the response id is"abc-123".curl --request POST --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --data '{"jsonrpc":"2.0","id":"abc-123","method":"initialize","params":{"protocolVersion":"1999-01-01"}}' \ http://gdk.test:3000/api/v4/mcp -
Repeat with
"jsonrpc":"1.0"and id7. Confirm the error code is-32600and the response id is7. -
Run the specs.
bundle exec rspec spec/requests/api/mcp/base_spec.rb \ ee/spec/requests/api/orbit/mcp_spec.rb \ ee/spec/requests/api/mcp/base_spec.rb
Specs ran locally: 109 examples, 0 failures. Steps 2 and 3 ran against both endpoints through the full request stack.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.