cd /news/ai-agents/unifying-request-context-across-poly… · home › topics › ai-agents › article
[ARTICLE · art-170569] src=dev.to ↗ pub= topic=ai-agents verified=true sentiment=↑ positive

Unifying Request Context Across Polymorphic MCP Tool Hierarchies in Ruby

A GitLab engineer consolidated request-context handling in the company's MCP server tool hierarchy, moving the `@request = request` assignment to the first line of `Mcp::Tools::Base::BaseService#execute` and defining a single canonical private `attr_reader :request` on the base class. The change, merged in MR !268791 to resolve issue #638725, removes duplicate shadowing readers and assignments from `GraphqlService` and `AggregatedService`, and adds lifecycle specs across all four service specs confirming the request is available in every subclass and resets to nil when a later `#execute` call omits `request:`.

by read2 min views1 publishedNov 16, 2026

In a multi-layered service hierarchy where AI tools inherit from BaseService, CustomService, GraphqlService, or AggregatedService, scattering @request = request assignments across leaf subclasses guarantees that sooner or later a security check or audit logger in a custom tool will dereference a nil or stale ActionDispatch::Request.

Within GitLab's MCP server architecture (app/services/mcp/tools/), tools are implemented across four distinct execution strategies under Mcp::Tools::Base: ApiTool (for Grape REST routes), GraphqlService (for GraphQL queries/mutations), AggregatedService (for orchestrating composite tool calls), and CustomService (for direct domain service invocations). When the MCP CallTool handler dispatches a tool, it passes the current Rack/Rails request object into #execute(request: request, params: ...) so downstream logic can inspect request headers, client IP metadata, or authentication state.

While resolving Issue #638725 in MR !268791 (Store request in MCP tools BaseService), I audited how request was stored across the class hierarchy. Both GraphqlService and AggregatedService had independently declared private attr_reader :request methods and manually assigned @request = request inside their own #execute overrides, whereas BaseService and CustomService never stored request at all. Worse, if any subclass called super before or after validation inconsistently, @request was either unavailable during argument validation hooks or risked retaining state across multiple #execute invocations on the same service instance when request: nil was omitted. In MR !268791, I hoisted @request = request to the very first line of Mcp::Tools::Base::BaseService#execute—prior to schema validation and tool execution—defined a single canonical private attr_reader :request on BaseService, and removed the duplicate shadowing readers and assignments from GraphqlService and AggregatedService. I then added lifecycle specs across base_service_spec.rb, custom_service_spec.rb, graphql_service_spec.rb, and aggregated_service_spec.rb verifying that @request is immediately accessible in every subclass and deterministically resets to nil if a subsequent #execute call omits request:.

GitLab's object-oriented MCP service layer (MR !268791) gives every GitLab Duo and MCP tool—whether GraphQL-backed, REST-aggregated, or custom Ruby domain logic—a uniform, leak-free request context contract.

Centralize request-context binding at the root #execute template method of your tool hierarchy before validation runs, and explicitly test that re-invoking a service instance with request: nil clears prior request state.

── 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/unifying-request-con…] indexed:0 read:2min 2026-11-16 · —