# Unifying Request Context Across Polymorphic MCP Tool Hierarchies in Ruby

> Source: <https://dev.to/aryan_rajput_81602becc3f6/unifying-request-context-across-polymorphic-mcp-tool-hierarchies-in-ruby-5dp9>
> Published: 2026-11-16 12:43:11+00:00

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`)](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/268791), 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](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/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](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/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.
