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:`. 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.