Loading
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.
- Two keys in the EE rate limit registry:
mcp_security_read(60 per minute) andmcp_security_write(20 per minute), both per user. Mcp::Tools::Base::GraphqlServicegets arate_limit :keydeclaration. The check runs before the GraphQL tool is built, and a throttled call returns the samerate_limitederror asMcp::Tools::Orbit::CommandTool. Services with no declaration, or with the flag off, behave as before.- A new
Mcp::Tools::Security::RateLimitedconcern declares the read key.ListVulnerabilitiesServiceandGetVulnerabilityServiceuse it.SaveVulnerabilityServiceandAttachScanProfileServiceoverride it with the write key. CallToolreports a throttled call asrate_limitedinstead oferror, so the existingfinish_mcp_tool_callevent andmcp.logshow it. This applies to every tool, not only the security tools, and is not behind the flag. No new events were added.- Docs: the MCP server and troubleshooting pages, the
tool_statustable formcp.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_queryandfeature_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_callfirst.list_vulnerabilitiesis paginated, so an agent paging a large project can use 60 calls quickly. - Not done: audit events (
ListAuditEventsServiceand its resolver are not onmaster), admin-configurable limits, and resolver-level limits on/api/graphqlfor vulnerabilities. The MR that re-adds theauditEventsfield 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_callevent definition. - No changelog entry: the change is behind a default-off flag.
References
- Closes https://gitlab.com/gitlab-org/gitlab/-/work_items/630441
- Feature flag rollout: #642212
- Project Bulkhead: https://gitlab.com/gitlab-org/gitlab/-/work_items/634979
Screenshots or screen recordings
Not applicable. This change has no UI.
How to set up and validate locally
-
Use a GDK with an Ultimate license and a project that has vulnerabilities.
-
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