{"slug": "unifying-request-context-across-polymorphic-mcp-tool-hierarchies-in-ruby", "title": "Unifying Request Context Across Polymorphic MCP Tool Hierarchies in Ruby", "summary": "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:`.", "body_md": "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`.\n\nWithin 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.\n\nWhile 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.\n\nIn [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:`.\n\nGitLab'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.\n\nCentralize 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.", "url": "https://wpnews.pro/news/unifying-request-context-across-polymorphic-mcp-tool-hierarchies-in-ruby", "canonical_source": "https://dev.to/aryan_rajput_81602becc3f6/unifying-request-context-across-polymorphic-mcp-tool-hierarchies-in-ruby-5dp9", "published_at": "2026-11-16 12:43:11+00:00", "updated_at": "2026-11-16 12:51:16.832849+00:00", "lang": "en", "topics": ["ai-agents", "agent-protocols", "developer-tools", "ai-tools"], "entities": ["GitLab", "GitLab Duo", "Mcp::Tools::Base::BaseService", "GraphqlService", "AggregatedService", "CustomService", "Model Context Protocol"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/unifying-request-context-across-polymorphic-mcp-tool-hierarchies-in-ruby", "markdown": "https://wpnews.pro/news/unifying-request-context-across-polymorphic-mcp-tool-hierarchies-in-ruby.md", "text": "https://wpnews.pro/news/unifying-request-context-across-polymorphic-mcp-tool-hierarchies-in-ruby.txt", "jsonld": "https://wpnews.pro/news/unifying-request-context-across-polymorphic-mcp-tool-hierarchies-in-ruby.jsonld"}}