{"slug": "pre-dispatch-wire-validation-in-json-rpc-2-0-closing-the-phantom-202-accepted", "title": "Pre-Dispatch Wire Validation in JSON-RPC 2.0: Closing the Phantom 202 Accepted Mutation Hole", "summary": "A GitLab engineer merged MR !268112, adding a reject_invalid_jsonrpc_request_id! helper to lib/api/helpers/mcp/json_rpc.rb that runs at the top of the post block in both the core MCP endpoint (lib/api/mcp/base.rb) and the Orbit MCP endpoint (ee/lib/api/orbit/mcp.rb). The fix closes a hole where a tools/call request missing its JSON-RPC id was treated as a notification and returned 202 Accepted after executing side effects and consuming rate-limit budget; such requests now fail fast with HTTP 400 and JSON-RPC error -32600, while legitimate notifications/* methods still return 202. The change enforces strict wire invariants on both /api/v4/mcp and the Orbit knowledge-graph MCP interface before rate limiting, gRPC initialization, or tool dispatch.", "body_md": "In JSON-RPC 2.0, omitting the `id` member transforms a request into a fire-and-forget notification—meaning a naive MCP server that checks `id` presence after routing will happily execute a state-mutating `tools/call` and return `202 Accepted` with an empty body, leaving the AI agent blind to the mutation it just triggered.\n\nUnlike REST, where HTTP verbs and status codes carry transport-level semantics, Model Context Protocol (MCP) multiplexes all requests and notifications over a single JSON-RPC 2.0 message stream. Per the MCP specification, notifications are strictly methods prefixed with `notifications/` (such as `notifications/initialized`) and must omit `id`, whereas every standard procedure call (` initialize`, `tools/list`, `tools/call`) must include a non-null `String` or `Integer` `id`. When resolving Issue `#638380` in [MR !268112 (`fix(mcp): reject invalid JSON-RPC request IDs`)](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/268112), I traced how malformed JSON-RPC envelopes interacted with both GitLab's core MCP endpoint (`lib/api/mcp/base.rb`) and the GitLab Orbit MCP endpoint (` ee/lib/api/orbit/mcp.rb`).\n\nTwo subtle traps existed at the Grape/Ruby boundary. First, in Ruby, checking `params[:id].nil?` cannot distinguish between an omitted `\"id\"` key (a JSON-RPC notification) and an explicit `\"id\": null` (which the MCP spec forbids for requests). Distinguishing the two requires inspecting key presence on the parsed parameter hash via `params.key?(:id)` before type validation. Second, if `id` validation is deferred until response serialization or after rate-limiting and gRPC/tool dispatch, a request with `\"method\": \"tools/call\"` and no `\"id\"` (or boolean, float, or array `id`) either consumes rate-limit budgets and executes side effects before failing, or gets misclassified as a notification and returns `202 Accepted` with no payload.\n\nIn [MR !268112](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/268112), I implemented `reject_invalid_jsonrpc_request_id!` inside `lib/api/helpers/mcp/json_rpc.rb` and placed it at the top of the `post` block in both `lib/api/mcp/base.rb` and `ee/lib/api/orbit/mcp.rb`—executing before `status :ok`, `rate_limit_mcp_command!`, Orbit gRPC client initialization, or tool handler invocation. Legitimate `notifications/*` methods without an `id` short-circuit cleanly with `202 Accepted`, while any non-notification missing `:id` or carrying a non-`String`/non-` Integer` `:id` is rejected immediately with HTTP `400 Bad Request` and JSON-RPC error code `-32600` (`Invalid Request`).\n\nAcross both `/api/v4/mcp` and GitLab Orbit's knowledge-graph MCP interface, GitLab enforces strict, pre-dispatch JSON-RPC 2.0 wire invariants ([MR !268112](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/268112)) so malformed AI client frames never trigger unacknowledged side effects or consume enterprise rate-limit quotas.\n\nValidate JSON-RPC 2.0 envelope invariants (`params.key?(:id)`, strict integer/string type guards, and `notifications/` method namespace rules) at the outermost ingress gate before touching rate limiters, gRPC channels, or tool dispatchers.", "url": "https://wpnews.pro/news/pre-dispatch-wire-validation-in-json-rpc-2-0-closing-the-phantom-202-accepted", "canonical_source": "https://dev.to/aryan_rajput_81602becc3f6/pre-dispatch-wire-validation-in-json-rpc-20-closing-the-phantom-202-accepted-mutation-hole-ede", "published_at": "2026-11-16 12:43:21+00:00", "updated_at": "2026-11-16 12:51:08.749935+00:00", "lang": "en", "topics": ["ai-agents", "agent-protocols", "ai-tools", "developer-tools"], "entities": ["GitLab", "Model Context Protocol", "JSON-RPC 2.0", "GitLab Orbit", "Grape"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/pre-dispatch-wire-validation-in-json-rpc-2-0-closing-the-phantom-202-accepted", "markdown": "https://wpnews.pro/news/pre-dispatch-wire-validation-in-json-rpc-2-0-closing-the-phantom-202-accepted.md", "text": "https://wpnews.pro/news/pre-dispatch-wire-validation-in-json-rpc-2-0-closing-the-phantom-202-accepted.txt", "jsonld": "https://wpnews.pro/news/pre-dispatch-wire-validation-in-json-rpc-2-0-closing-the-phantom-202-accepted.jsonld"}}