In-Process Rack Dispatch in GitLab's MCP Server: Eliminating Local HTTP Loopback Without Corrupting env 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. 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. In 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. Two 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. In 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. By 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 . When 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.