[Refactor] Govern MCP merge request, commit and repository tools by their own lookup

What does this MR do and why?

This is the second of three stacked MRs that make MCP governance resolve a call's containers through each tool's own lookup. It's stacked on !261045 and targets its branch. That MR adds the plumbing and explains the change.

This MR converts the merge request, commit and repository tools:

  • get_merge_request (CE and EE) returns [resolve_target.first].
  • The merge request notes, list, accept and review tools, the commit tools, add_branch, add_commit, and the repository file and tree tools return the project or group their execute path resolves.
  • The custom services save_merge_request, get_merge_request_conflicts and list_tags use their own project lookup.
  • get_repository_file and list_repository_tree raise Gitlab::Access::AccessDeniedError for a user who can't read code. Governance treats that as no container, and the tool reports the denial itself.
  • The EE get_merge_request_tool_spec.rb conflict-file exclusion example now builds a fresh tool for its first call. resolve_target is memoized, so reusing one tool kept the project from before the exclusion rules were set. In production each call builds its own tool.

Tools that this MR doesn't touch keep the declared lookup through the fallback in GraphqlService.

How to reproduce the issue

There is no user-facing defect. On !261045's branch, governance for get_merge_request still goes through the declared lookup.

  1. Check out 633015-mcp-governance-tool-container-lookup.
  2. Build Mcp::Tools::MergeRequests::GetMergeRequestService and call governed_containers with a merge request URL.
  3. You can see that GetMergeRequestTool#resolve_target is never called.

How to test the fix

  1. On this branch, repeat the call. The result is the project resolve_target returns.
  2. Call get_repository_file as a guest of a private project. Governance resolves no container and the tool returns its own access error.

Run the specs:

bin/rspec spec/services/mcp ee/spec/services/mcp ee/spec/services/ee/mcp spec/lib/api/mcp/handlers ee/spec/lib/ee/api/mcp/handlers ee/spec/lib/ai/tool_rules spec/requests/api/mcp ee/spec/requests/api/mcp

References

Edited by Raounak Sharma

Merge request reports

Loading
Loading