Govern MCP calls from Duo sessions by their own access type
What does this MR do and why?
/api/v4/mcp checks the Local access column for every caller, because it assumes every caller is an external agent. Agentic Chat on the web and background flows also call it, through Workhorse. Say a tool is set to Deny for Local and Allow for Cloud. Web chat checks Cloud, so it offers the tool. The call then reaches /api/v4/mcp, which checks Local and refuses it.
This MR makes the server use the access type of the Duo session that made the call. It builds on !266251 (merged), now merged, which binds each session token to its workflow.
A new class, Ai::ToolRules::McpCallerSurface, picks the column from the caller's token:
- A token with no
duo_workflow:<id>scope keeps Local. That covers Claude Code, Cursor, other third-party clients, the Duo CLI, and external agents started by flow triggers. - A token bound to a workflow the caller can read uses that workflow's access type. It returns Background when background governance applies to the workflow, the same check the session uses for its own tools. Otherwise it returns the workflow's environment as a Symbol, which
ToolRule#access_formaps to the Cloud (web_access) or Local column. Background applies to a target only when the target's own top-level group also hasduo_workflow_background_tool_governanceon. Where it's off,McpGovernancereads that group's Cloud column, which is what a background flow got before background governance. - A token bound to a workflow that can't be resolved is refused. That covers a deleted workflow, a workflow the caller can no longer read, a malformed scope (also when it sits beside a valid one), and two workflow scopes.
McpGovernancethen denies every tool for governed groups and projects, sotools/callreturns 403 andtools/listhides them. The refusal raisesSessionRefusedError, which names no denying group and has its ownerror_typeinmcp.log. Each refusal is also logged tomcp.logwith the workflow ID and the reason (invalid_workflow_scope,workflow_not_foundorworkflow_not_readable). Where the feature flag is off, nothing changes. tools/callandtools/listboth use it through agovernance_surfacehelper. CEListToolsnow keeps the requestcontext, so the EE override can read the token.tools/listresolves the session only when the caller's namespace is governed, astools/callalready does.REFUSEDlives inGovernanceSurfacenext toUNGOVERNED.ResolutionServicewon't resolve rules for it, becauseToolRule#access_forwould read the Local column. Its permission lookups return deny, and it raises in development and test.
WorkflowTokenScope changes in two ways:
workflow_id_fromreads a workflow only when the token has exactly one workflow scope and it is well formed. Before,duo_workflow:42next toduo_workflow:abcread as workflow 42, whichallows_workflow?rejects. Now that token is refused.
The permission check is the real read_duo_workflow policy. For a composite token, the API authenticates as the linked human (resolve_composite_identity_user), so a background flow's call is checked as the person who started it. A request spec covers a composite service account token bound to a background Developer Flow.
The X-Gitlab-Duo-Workflow-Id header is not trusted for this, because any client can send it. A request spec shows that a forged header keeps Local.
The docs now say which access type the server checks for each caller, and that a deleted workflow or lost access means the call is refused.
How to reproduce the issue
- Enable
duo_mcp_server_governance_rule_enforcementfor a group. - In AI > Governance > Tool management, set a GitLab MCP tool such as
get_projectto Deny for Local access and Allow for Cloud access. - In a project in that group, ask Agentic Chat in the GitLab UI to get the project details.
- The agent sees the tool, but the call fails with a forbidden error.
How to test the fix
- Repeat the steps on this branch.
- Agentic Chat runs
get_project. - Call
get_projecton the same project from Claude Code. It's still refused with JSON-RPC error-32002. - Send the same Claude Code request with
X-Gitlab-Duo-Workflow-Idset to your chat's workflow ID. It's still refused. - In the Rails console, create an OAuth token for your user with the scopes
mcp duo_workflow:<id>, where<id>is a workflow you then delete. Callget_projecton the group's project throughPOST /api/v4/mcpwith that token. The call is refused with JSON-RPC error-32002.
bin/rspec ee/spec/lib/ai/duo_workflows/workflow_token_scope_spec.rb ee/spec/lib/ai/tool_rules/mcp_caller_surface_spec.rb \
ee/spec/lib/ai/tool_rules/mcp_governance_spec.rb ee/spec/lib/ee/api/mcp/handlers/call_tool_spec.rb \
ee/spec/lib/ee/api/mcp/handlers/list_tools_spec.rb ee/spec/services/ai/tool_rules/resolution_service_spec.rb \
ee/spec/requests/api/mcp/base_spec.rb ee/spec/requests/api/mcp/handlers/list_tools_spec.rbReferences
- Builds on (merged): !266251 (merged)
- Asked for in: https://gitlab.com/groups/gitlab-org/-/work_items/21113#note_4103360101
- Analysis: https://gitlab.com/groups/gitlab-org/-/work_items/21113#note_4104983475
- Rollout issue: https://gitlab.com/gitlab-org/gitlab/-/work_items/632906
- Related: !266221 (merged) touches the same docs section, so whichever merges second needs a rebase.
Screenshots or screen recordings
Not applicable. There is no UI change.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.