{"slug": "in-process-rack-dispatch-in-gitlab-s-mcp-server-eliminating-local-http-loopback", "title": "In-Process Rack Dispatch in GitLab's MCP Server: Eliminating Local HTTP Loopback Without Corrupting env", "summary": "A GitLab engineer hardened the company's MCP server so REST-backed AI tools dispatch directly to Grape API endpoints in-process instead of over loopback HTTP, eliminating the extra hop's latency and thread contention. The fix, merged in MR !268703, snapshots and restores the Rack env method and input stream inside an ensure block, swaps in an empty StringIO for the input, and subclasses the request logger to suppress duplicate api_json.log entries while propagating the inner route's status into mcp.log. The engineer notes that in-process sub-dispatch should treat the request environment hash as a stack frame, snapshotting and restoring state so inner calls cannot corrupt outer telemetry.", "body_md": "When an enterprise MCP server exposes dozens of existing REST API endpoints as AI tools, proxying each `tools/call` over a loopback HTTP request back into the same monolith doubles Puma thread contention, serializes JSON twice, and adds network tail latency—yet invoking the router in-process mutates shared Rack state under your feet.\n\nIn the GitLab monolith, route-backed MCP tools (`Mcp::Tools::Base::ApiTool`) avoid loopback HTTP overhead by dispatching directly to the underlying Grape API endpoint in-process via `route.app.call(env)` inside the outer `POST /api/:version/mcp` request. While investigating Issue `#638385` and authoring [MR !268703 (`fix(mcp): prevent duplicate API request logging`)](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/268703), I uncovered what happens when an in-process sub-dispatch shares a mutable Rack `env` hash with its parent request. Because every MCP wire call arrives as an HTTP `POST`, `ApiTool` overrode `env[Rack::REQUEST_METHOD] = route.request_method` (for example, mutating `POST` to `GET` when executing `list_branches`) and passed the raw `env` down the Grape middleware stack.\n\nTwo systemic side effects followed immediately. First, `GrapeLogging::Middleware::RequestLogger` wrapped both the inner route and the outer `/api/v4/mcp` endpoint: the inner invocation logged a synthetic `api_json.log` entry and called `GrapeLogging::Timings.reset_db_runtime`, wiping accumulated database timing metrics mid-request, and then the outer endpoint logged a second `api_json.log` line still bearing the mutated `GET` method because `env[Rack::REQUEST_METHOD]` was never restored. Second, `env[Rack::RACK_INPUT]` still held the outer JSON-RPC 2.0 request stream, causing Grape's parameter parser to leak top-level JSON-RPC envelope keys like `id` into the inner REST route's parameters.\n\nIn [MR !268703](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/268703), I hardened `Mcp::Tools::Base::ApiTool#execute_route` to snapshot `original_method` and `original_input`, replace `Rack::RACK_INPUT` with an empty `StringIO.new` (since validated tool arguments are already injected into `Grape::Env::GRAPE_ROUTING_ARGS`), and restore the original Rack environment inside an `ensure` block. I then subclassed `Gitlab::GrapeLogging::Middleware::RequestLogger` to skip nested `api_json.log` emission when `SKIP_REQUEST_LOGGING_ENV_KEY` is set, while propagating the inner route's HTTP status code via `:route_status` into `API::Mcp::Handlers::CallTool` so `mcp.log` records the exact downstream HTTP status without leaking internal metadata keys into the JSON-RPC client response.\n\nBy executing REST-backed MCP tools through an isolated in-process Rack dispatch pipeline ([MR !268703](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/268703)), GitLab's MCP server delivers zero-hop tool execution latency while maintaining strict separation between outer HTTP access logs (`api_json.log`) and granular AI tool telemetry (` mcp.log`).\n\nWhen short-circuiting HTTP with in-process ASGI/Rack/WSGI sub-dispatch, treat the request environment hash as a stack frame—snapshot and restore method/input streams in an `ensure` block and suppress re-entrant logging middleware so inner calls cannot corrupt outer telemetry.", "url": "https://wpnews.pro/news/in-process-rack-dispatch-in-gitlab-s-mcp-server-eliminating-local-http-loopback", "canonical_source": "https://dev.to/aryan_rajput_81602becc3f6/in-process-rack-dispatch-in-gitlabs-mcp-server-eliminating-local-http-loopback-without-corrupting-4m9j", "published_at": "2026-11-16 12:43:26+00:00", "updated_at": "2026-11-16 12:51:04.893806+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "ai-infrastructure"], "entities": ["GitLab", "Grape", "Rack", "Puma", "Mcp::Tools::Base::ApiTool", "GrapeLogging::Middleware::RequestLogger"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/in-process-rack-dispatch-in-gitlab-s-mcp-server-eliminating-local-http-loopback", "markdown": "https://wpnews.pro/news/in-process-rack-dispatch-in-gitlab-s-mcp-server-eliminating-local-http-loopback.md", "text": "https://wpnews.pro/news/in-process-rack-dispatch-in-gitlab-s-mcp-server-eliminating-local-http-loopback.txt", "jsonld": "https://wpnews.pro/news/in-process-rack-dispatch-in-gitlab-s-mcp-server-eliminating-local-http-loopback.jsonld"}}