Cap get_repository_file responses at 32 KiB and continue cut lines
What does this MR do and why?
get_repository_file currently returns up to 1 MiB of file content in one response. That is far above the response sizes that MCP clients pass inline, as described in #633042.
This MR:
- Lowers the content limit for one response from 1 MiB to 32 KiB.
limitkeeps its meaning: it is still the maximum number of lines (default 500). When a response covers only part of the file,system_instructionstill gives the nextoffset. Existing callers keep working, but large files need more calls. - Adds an optional
line_byte_offsetargument. When a single line is longer than 32 KiB, the response ends at a UTF-8 character boundary.system_instructionthen gives the exactoffsetandline_byte_offsetto read the rest of that line. Before this change, the rest of such a line could not be read, and the remaining-lines hint skipped past it. - Returns a validation error when
line_byte_offsetis past the end of the line or inside a multi-byte character. The message points back to the value insystem_instruction. - Adds
"line_byte_offset": 0to the remaining-lines hint when the request started partway through a line. A client that merges the hint into its previous arguments then reads the next line from its start. For other requests, the hint text is unchanged. - Updates the
get_repository_filedocs. It corrects thelimitdefault, which the docs listed as 2000 instead of 500. It also adds theline_byte_offsetparameter, the 32 KiB limit, and history entries.
Why the new argument ships with the lower limit: the longest line of vendor/gems/sidekiq/web/assets/javascripts/chart.min.js in this repository is 191,564 bytes. Today it fits in one response. With a 32 KiB limit and no way to continue a line, most of that line would be unreachable.
No new tool version is registered. Every existing argument keeps its meaning, and the new argument is optional, as agreed in #633042 (comment 4106359751). The argument name is open to change if you prefer another one (#633042 (comment 4127149486)).
Open question for @fdegier: the 32 KiB limit counts file content only. Each response carries that content twice: as JSON in the text item and again in structuredContent. For 32 KiB windows of a minified file, text was 34,952 to 36,111 characters, and the complete result was 70,925 to 74,150 bytes of JSON. Is a content-based limit enough here, or should the limit account for the full response?
The service code is owned by the MCP tool review board.
References
- Related to #633042 (
get_artifact_filestays in that issue) - Scope agreed in #633042 (comment 4106359751)
- Same opt-in continuation pattern for job logs: !264446 (merged)
- Byte-based reading of single-line content: #597417
- Merge request and commit diffs are tracked in #636668
Screenshots or screen recordings
Not applicable. This MR has no UI change.
How to set up and validate locally
-
In a Rails console on a GDK with
gitlab-org/gitlab-test, run:user = User.find_by_username('root') project = Project.find_by_full_path('gitlab-org/gitlab-test') -
Create a file with one long line:
project.repository.create_file(user, 'long.min.js', "#{'a' * 40_000}\n", message: 'Add long line', branch_name: 'master') -
Call the tool:
svc = Mcp::Tools::Repositories::GetRepositoryFileService.new(name: 'get_repository_file', version: '0.1.0') svc.set_cred(current_user: user) args = { 'project_id' => project.full_path, 'file_path' => 'long.min.js', 'ref' => 'master' } r = svc.execute(params: { arguments: args }) -
Check that
r[:structuredContent][:content].bytesizeis32768, and thatr[:structuredContent][:system_instruction]asks for{"offset": 0, "line_byte_offset": 32768}. -
Call again with the values from the hint:
r2 = svc.execute(params: { arguments: args.merge('offset' => 0, 'line_byte_offset' => 32768) })The content is the remaining 7,233 bytes, and there is no
system_instruction.
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.