fix(mcp): reject invalid JSON-RPC request IDs

What does this MR do and why?

This MR fixes invalid JSON-RPC request ID handling in both GitLab MCP endpoints:

  • /api/v4/mcp
  • /api/v4/orbit/mcp

Currently, non-notification MCP requests without a JSON-RPC id can execute tools and return HTTP 202 with an empty response. This causes clients to lose tool results.

Additionally, the Orbit endpoint accepts invalid JSON-RPC ID types, including booleans, floats, arrays, and objects.

This MR:

  • Rejects non-notification requests with a missing JSON-RPC id.
  • Returns HTTP 400 with JSON-RPC error -32600 (Invalid Request).
  • Preserves existing notification handling for notifications/* methods.
  • Restricts Orbit JSON-RPC IDs to String and Integer values.
  • Performs validation before tool dispatch to prevent unintended execution.
  • Adds request specs covering both MCP endpoints and relevant edge cases.

References

Screenshots or screen recordings

Not applicable. This MR changes backend API request validation and does not introduce UI changes.

Before

MCP requests without an ID could execute tools and return HTTP 202 with an empty response. Orbit also accepted unsupported JSON-RPC ID types.

After

Invalid non-notification requests are rejected with HTTP 400 and JSON-RPC error -32600 before tool execution. Notifications retain their existing handling.

How to set up and validate locally

  1. Check out the MR branch.
  2. Run the relevant MCP request specs:

bundle exec rspec spec/requests/api/mcp/base_spec.rb bundle exec rspec ee/spec/requests/api/orbit/mcp_spec.rb

  1. Run RuboCop on the modified Ruby files:

bundle exec rubocop \ lib/api/helpers/mcp/json_rpc.rb \ lib/api/mcp/base.rb \ ee/lib/api/orbit/mcp.rb \ spec/requests/api/mcp/base_spec.rb \ ee/spec/requests/api/orbit/mcp_spec.rb

  1. Verify the expected behaviour:
    • Missing IDs on non-notification requests return HTTP 400.
    • The JSON-RPC error code is -32600.
    • Invalid requests do not execute tools.
    • Notifications continue returning HTTP 202.
    • Orbit accepts only String and Integer IDs.

Validation status: Ruby syntax checks passed for all five modified files, and git diff --check passed. RSpec and RuboCop have not yet been executed.

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.

  • Changes are scoped to the reported MCP request validation issue.
  • Validation happens before tool dispatch.
  • Existing notification handling is preserved.
  • Both the main MCP and Orbit endpoints are covered.
  • Request specs have been added for the relevant cases.
  • Ruby syntax checks passed for all modified files.
  • RSpec request specs executed successfully.
  • RuboCop checks executed successfully.
  • CI pipeline passes.
  • Final maintainer review completed.

Merge request reports

Loading
Loading