Add rate limits to MCP security tools

What does this MR do and why?

MCP security tools call GitlabSchema.execute directly, so no controller-level throttle applies and one agent can call them as often as it likes. This MR adds a per-user rate limit to these tools, behind the mcp_security_rate_limits feature flag (gitlab_com_derisk, default off). It follows the implementation plan.

  1. Two keys in the EE rate limit registry: mcp_security_read (60 per minute) and mcp_security_write (20 per minute), both per user.
  2. Mcp::Tools::Base::GraphqlService gets a rate_limit :key declaration. The check runs before the GraphQL tool is built, and a throttled call returns the same rate_limited error as Mcp::Tools::Orbit::CommandTool. Services with no declaration, or with the flag off, behave as before.
  3. A new Mcp::Tools::Security::RateLimited concern declares the read key. ListVulnerabilitiesService and GetVulnerabilityService use it. SaveVulnerabilityService and AttachScanProfileService override it with the write key.
  4. CallTool reports a throttled call as rate_limited instead of error, so the existing finish_mcp_tool_call event and mcp.log show it. This applies to every tool, not only the security tools, and is not behind the flag. No new events were added.
  5. Docs: the MCP server and troubleshooting pages, the tool_status table for mcp.log, the MCP developer docs, and the non-configurable rate limits page.

Review notes

  • Limits: fixed per user, the same for every plan, with no admin setting and no separate hard limit. Reads are data plane and writes are control plane. 60 per minute matches orbit_query and feature_library_search. This is in line with the other Project Bulkhead limits (see https://gitlab.com/gitlab-org/gitlab/-/work_items/634979). REST vulnerability endpoints have no rate limit today, so these are new limits, not parity with REST.
  • Flag: it is the rollback. Per-user call rates for these tools are not measured yet, so turn it on gradually and check finish_mcp_tool_call first. list_vulnerabilities is paginated, so an agent paging a large project can use 60 calls quickly.
  • Not done: audit events (ListAuditEventsService and its resolver are not on master), admin-configurable limits, and resolver-level limits on /api/graphql for vulnerabilities. The MR that re-adds the auditEvents field must enforce the REST audit event limits in the resolver.
  • Approvals: a backend maintainer, a technical writer for the docs, a product manager for the new application limits, and an Analytics Instrumentation engineer for the finish_mcp_tool_call event definition.
  • No changelog entry: the change is behind a default-off flag.

References

Screenshots or screen recordings

Not applicable. This change has no UI.

How to set up and validate locally

  1. Use a GDK with an Ultimate license and a project that has vulnerabilities.

  2. Enable the flag in the Rails console and call a tool more than 60 times:

    Feature.enable(:mcp_security_rate_limits)
    user = User.find_by_username('root')
    service = Mcp::Tools::Security::ListVulnerabilitiesService.new(name: 'list_vulnerabilities')
    service.set_cred(current_user: user)
    args = { arguments: { project_full_path: 'gitlab-org/gitlab-test' } }
    61.times.map { service.execute(params: args).dig(:structuredContent, :error, :type) }.last
    # => "rate_limited"

    With the flag off, the same loop never returns rate_limited.

Verification status

  • RSpec, run locally: every spec file this MR touches, plus the base and aggregated MCP service specs. 442 examples, 0 failures. The CE specs also pass with FOSS_ONLY=1.
  • RuboCop on the changed Ruby files, and markdownlint and Vale on the changed docs.

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 Nate Rosandich

Merge request reports

Loading
Loading