Loading
[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_conflictsandlist_tagsuse their own project lookup. get_repository_fileandlist_repository_treeraiseGitlab::Access::AccessDeniedErrorfor 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.rbconflict-file exclusion example now builds a fresh tool for its first call.resolve_targetis 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.
- Check out
633015-mcp-governance-tool-container-lookup. - Build
Mcp::Tools::MergeRequests::GetMergeRequestServiceand callgoverned_containerswith a merge request URL. - You can see that
GetMergeRequestTool#resolve_targetis never called.
How to test the fix
- On this branch, repeat the call. The result is the project
resolve_targetreturns. - Call
get_repository_fileas 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/mcpReferences
- Issue: https://gitlab.com/gitlab-org/gitlab/-/work_items/633015
- Split proposal: !261045 (comment 4162846677)
- Previous in the stack: !261045
- Next in the stack: !268284
Edited by Raounak Sharma