Apply REST commit size limits to MCP add_commit
What does this MR do and why?
The MCP add_commit tool doesn't apply the commit size cap or the large-commit rate limit that the REST commits
endpoint enforces. This MR applies both, behind the mcp_add_commit_size_limits flag: commits over 300 MiB are
rejected, and commits over 20 MiB share the REST limit of three every 30 seconds per user. While the flag is off,
nothing is blocked and would-be violations are only logged.
How the limits are applied
- The size is the bytes of each action's
content,file_pathandprevious_path, pluscommit_message, measured after partial edits (old_str/new_str) expand. Base64 content counts at its encoded length. This approximates the REST request body. - Over
Repositories::CommitsUploader.max_request_size(300 MiB, overridable withGITLAB_COMMITS_MAX_REQUEST_SIZE_BYTES): abad_requesterror. This is checked first, so a rejected commit doesn't use rate-limit quota. - Over
Repositories::CommitsUploader::MAX_RATE_LIMITED_REQUEST_SIZE(20 MiB):Gitlab::ApplicationRateLimiter.throttled?(:user_large_commit_request, scope: { user: current_user }), the same rule and scope as REST, so MCP and REST share one quota. When throttled, the tool returns the standard MCPrate_limitederror. - Flag off (
gitlab_com_derisk, actorcurrent_user): commits that would hit a limit are logged toGitlab::AppLoggerwithrule,total_bytes,gl_user_idandclass_name. The limiter isn't called, so no quota is used. - The tool stays on version 0.1.0. Its schema, description and success output don't change, and a limit that only a new version enforced could be skipped by pinning 0.1.0.
Rollout plan
The steps are in the rollout issue, #636332.
- Read the log-only data on GitLab.com. The
user_large_commit_requestrows count commits over 20 MiB, not refusals, because the limiter isn't called while the flag is off; a user would have been refused on the fourth row within 30 seconds. - Enable on staging, then GitLab.com.
- Confirm a fourth large commit within 30 seconds returns the rate-limited error, and an oversize commit returns the cap error.
- Check the gprd value of
GITLAB_JSON_MAX_JSON_SIZE_BYTESand the edge body limit. In code nothing caps the body before Rails:/api/v4/mcphas noJsonValidationroute, and the variable defaults to 0. - To roll back, disable the flag.
References
- https://gitlab.com/gitlab-org/gitlab/-/work_items/635014
- Rollout: https://gitlab.com/gitlab-org/gitlab/-/work_items/636332
Screenshots or screen recordings
Not applicable; there is no UI change.
How to set up and validate locally
With the flag off, a commit over the cap goes through and is logged. With it on, the same commit is refused, and the fourth large commit in a row is rate limited. I ran every step below in GDK; the output is what each command printed.
Steps and output
I served the branch from a second Puma (bin/rails server -e development -b 127.0.0.1 -p 3999) on GDK's checkout
with this MR's commits cherry-picked; the size-limit code is identical to the branch.
-
Create an OAuth token with the
mcpscope and a private project with amainbranch:user = User.find_by!(username: 'root') org = Organizations::Organization.first app = Authn::OauthApplication.create!(name: 'mcp-264203-validation', redirect_uri: 'http://localhost', scopes: 'mcp', owner: user, organization: org) OauthAccessToken.create!(application: app, resource_owner: user, scopes: 'mcp', organization: org, expires_in: 3.hours).plaintext_token -
Set up the shell.
add_commitcreates one file of the given size through MCP:export GDK_URL=http://127.0.0.1:3999 PROJECT=root/mcp-size-limit-check-264203 TOKEN=<token> add_commit() { # $1 = file to create, $2 = content size in bytes python3 -c 'import json, sys; print(json.dumps({"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "add_commit", "arguments": {"project_id": sys.argv[1], "branch": "main", "commit_message": "Add " + sys.argv[2], "actions": [{"action": "create", "file_path": sys.argv[2], "content": "a" * int(sys.argv[3])}]}}}))' "$PROJECT" "$1" "$2" > payload.json curl -s "$GDK_URL/api/v4/mcp" -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \ -H 'Accept: application/json, text/event-stream' --data-binary @payload.json \ | jq -c '{isError: .result.isError, text: .result.content[0].text, error: .result.structuredContent.error}' } -
Start Rails with
GITLAB_COMMITS_MAX_REQUEST_SIZE_BYTES=1048576so the cap is 1 MiB. With the flag off, commit 1.5 MB and read the log:add_commit flag-off.txt 1500000 grep 'would exceed a commit size limit' log/application_json.log | tail -1 | jq -c '{message, rule, total_bytes, gl_user_id, class_name}'What I got:
{"isError":false,"text":"{\"commit\":{\"id\":\"gid://gitlab/Commit/0d05aaef695ffea36d4c87841e1b186224513f02\",\"sha\":\"0d05aaef695ffea36d4c87841e1b186224513f02\",\"shortId\":\"0d05aaef\",\"webUrl\":\"http://gdk.test:3000/root/mcp-size-limit-check-264203/-/commit/0d05aaef695ffea36d4c87841e1b186224513f02\"},\"errors\":[]}","error":null} {"message":"MCP add_commit would exceed a commit size limit","rule":"max_request_size","total_bytes":1500028,"gl_user_id":1,"class_name":"Mcp::Tools::Repositories::AddCommitService"} -
Enable the flag in the Rails console with
Feature.enable(:mcp_add_commit_size_limits). A running Puma picks it up within a minute. Then repeat the call:add_commit flag-on.txt 1500000What I got:
{"isError":true,"text":"The commit exceeds the 1 MiB request size limit","error":null} -
Restart Rails without
GITLAB_COMMITS_MAX_REQUEST_SIZE_BYTES, then make four 22 MB commits in a row:for i in 1 2 3 4; do add_commit "big-$i.txt" 22000000; doneWhat I got:
{"isError":false,"text":"{\"commit\":{\"id\":\"gid://gitlab/Commit/a521c0138ab4f993021177d49bb8b49ae2f53cdc\",\"sha\":\"a521c0138ab4f993021177d49bb8b49ae2f53cdc\",\"shortId\":\"a521c013\",\"webUrl\":\"http://gdk.test:3000/root/mcp-size-limit-check-264203/-/commit/a521c0138ab4f993021177d49bb8b49ae2f53cdc\"},\"errors\":[]}","error":null} {"isError":false,"text":"{\"commit\":{\"id\":\"gid://gitlab/Commit/aaefcb5f5505c7b33b0db244b3ad20acec7102d2\",\"sha\":\"aaefcb5f5505c7b33b0db244b3ad20acec7102d2\",\"shortId\":\"aaefcb5f\",\"webUrl\":\"http://gdk.test:3000/root/mcp-size-limit-check-264203/-/commit/aaefcb5f5505c7b33b0db244b3ad20acec7102d2\"},\"errors\":[]}","error":null} {"isError":false,"text":"{\"commit\":{\"id\":\"gid://gitlab/Commit/0e059f64000577ed4169563517be02c90c1b8f14\",\"sha\":\"0e059f64000577ed4169563517be02c90c1b8f14\",\"shortId\":\"0e059f64\",\"webUrl\":\"http://gdk.test:3000/root/mcp-size-limit-check-264203/-/commit/0e059f64000577ed4169563517be02c90c1b8f14\"},\"errors\":[]}","error":null} {"isError":true,"text":"Rate limited by user_large_commit_request. Retry after 30 seconds.","error":{"type":"rate_limited","retry_after_seconds":30,"limit":"user_large_commit_request","message":"This endpoint has been requested too many times. Try again later."}} -
In the Rails console, check that only the accepted commits landed:
repo = Project.find_by_full_path('root/mcp-size-limit-check-264203').repository %w[flag-off.txt flag-on.txt big-1.txt big-2.txt big-3.txt big-4.txt].to_h { |f| [f, repo.blob_at('main', f).present?] }What I got:
{"flag-off.txt"=>true, "flag-on.txt"=>false, "big-1.txt"=>true, "big-2.txt"=>true, "big-3.txt"=>true, "big-4.txt"=>false} -
Clean up with
Feature.remove(:mcp_add_commit_size_limits)and delete the OAuth application.
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.