# In-Process Rack Dispatch in GitLab's MCP Server: Eliminating Local HTTP Loopback Without Corrupting env

> Source: <https://dev.to/aryan_rajput_81602becc3f6/in-process-rack-dispatch-in-gitlabs-mcp-server-eliminating-local-http-loopback-without-corrupting-4m9j>
> Published: 2026-11-16 12:43:26+00:00

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.
