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:

  1. 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.
  2. 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_for maps to the Cloud (web_access) or Local column. Background applies to a target only when the target's own top-level group also has duo_workflow_background_tool_governance on. Where it's off, McpGovernance reads that group's Cloud column, which is what a background flow got before background governance.
  3. 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. McpGovernance then denies every tool for governed groups and projects, so tools/call returns 403 and tools/list hides them. The refusal raises SessionRefusedError, which names no denying group and has its own error_type in mcp.log. Each refusal is also logged to mcp.log with the workflow ID and the reason (invalid_workflow_scope, workflow_not_found or workflow_not_readable). Where the feature flag is off, nothing changes.
  4. tools/call and tools/list both use it through a governance_surface helper. CE ListTools now keeps the request context, so the EE override can read the token. tools/list resolves the session only when the caller's namespace is governed, as tools/call already does.
  5. REFUSED lives in GovernanceSurface next to UNGOVERNED. ResolutionService won't resolve rules for it, because ToolRule#access_for would read the Local column. Its permission lookups return deny, and it raises in development and test.

WorkflowTokenScope changes in two ways:

  • workflow_id_from reads a workflow only when the token has exactly one workflow scope and it is well formed. Before, duo_workflow:42 next to duo_workflow:abc read as workflow 42, which allows_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

  1. Enable duo_mcp_server_governance_rule_enforcement for a group.
  2. In AI > Governance > Tool management, set a GitLab MCP tool such as get_project to Deny for Local access and Allow for Cloud access.
  3. In a project in that group, ask Agentic Chat in the GitLab UI to get the project details.
  4. The agent sees the tool, but the call fails with a forbidden error.

How to test the fix

  1. Repeat the steps on this branch.
  2. Agentic Chat runs get_project.
  3. Call get_project on the same project from Claude Code. It's still refused with JSON-RPC error -32002.
  4. Send the same Claude Code request with X-Gitlab-Duo-Workflow-Id set to your chat's workflow ID. It's still refused.
  5. 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. Call get_project on the group's project through POST /api/v4/mcp with 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.rb

References

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.

Edited by Raounak Sharma

Merge request reports

Loading
Loading