cd /news/ai-agents/in-process-rack-dispatch-in-gitlab-s… · home › topics › ai-agents › article
[ARTICLE · art-170566] src=dev.to ↗ pub= topic=ai-agents verified=true sentiment=↑ positive

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.

by read2 min views4 publishedNov 16, 2026

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), 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, 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), 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.

── more in #ai-agents 4 stories · sorted by recency
── more on @gitlab 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/in-process-rack-disp…] indexed:0 read:2min 2026-11-16 · —