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. limit keeps its meaning: it is still the maximum number of lines (default 500). When a response covers only part of the file, system_instruction still gives the next offset. Existing callers keep working, but large files need more calls.
  • Adds an optional line_byte_offset argument. When a single line is longer than 32 KiB, the response ends at a UTF-8 character boundary. system_instruction then gives the exact offset and line_byte_offset to 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_offset is past the end of the line or inside a multi-byte character. The message points back to the value in system_instruction.
  • Adds "line_byte_offset": 0 to 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_file docs. It corrects the limit default, which the docs listed as 2000 instead of 500. It also adds the line_byte_offset parameter, 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

Screenshots or screen recordings

Not applicable. This MR has no UI change.

How to set up and validate locally

  1. 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')
  2. 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')
  3. 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 })
  4. Check that r[:structuredContent][:content].bytesize is 32768, and that r[:structuredContent][:system_instruction] asks for {"offset": 0, "line_byte_offset": 32768}.

  5. 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.

Merge request reports

Loading
Loading