Skip to content

fix: make read_page advance when max_bytes is smaller than the rune at offset - #27

Merged
Sriram-PR merged 4 commits into
Sriram-PR:mainfrom
one-pr-a-day:fix/19-max-bytes-progress
Oct 7, 2026
Merged

Sriram-PR merged 4 commits into
Sriram-PR:mainfrom
one-pr-a-day:fix/19-max-bytes-progress

Conversation

@lesbass

@lesbass lesbass commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Fixes #19.

Problem

read_page paginates stored content by max_bytes/offset. When max_bytes is smaller than the multi-byte rune at offset, sliceAtRuneBoundary snapped the cut down to 0, returning content: "" with next_offset == offset and truncated: true. A client that follows next_offset until truncated is false loops forever.

Change

pkg/mcp/handlers.go: when the rune-boundary snap reaches 0 because the rune at offset does not fit in max_bytes, return that whole rune (via utf8.DecodeRuneInString). A truncated response now always returns at least one character and next_offset strictly advances, while every chunk stays valid UTF-8.

Test

pkg/mcp/handlers_test.go: TestHandleReadPage_SmallMaxBytesMakesProgress pages a string of 20 U+2019 (3-byte) runes with max_bytes 1 and 2, asserting each chunk is non-empty and valid UTF-8, next_offset strictly advances, paging terminates, and the chunks reassemble the page exactly. It fails against the old helper and passes with the fix.

Verification

  • go test ./... — passes (all packages)
  • go vet ./... — clean
  • gofmt -l pkg/mcp/handlers.go pkg/mcp/handlers_test.go — no output

lesbass and others added 4 commits October 7, 2026 10:36
read_page paginates stored content by max_bytes and offset. When the rune
at offset is longer than max_bytes, sliceAtRuneBoundary snapped the cut
down to zero, returning an empty chunk with next_offset equal to the
requested offset. A client that follows next_offset until truncated is
false looped forever.

Return the whole rune at offset when it does not fit in max_bytes, so a
truncated response always makes progress. Add a regression test that pages
a string of 3-byte runes with max_bytes 1 and 2 and asserts each chunk is
non-empty and valid UTF-8, next_offset strictly advances, and following it
reassembles the page.
@Sriram-PR

Copy link
Copy Markdown
Owner

Thanks for this! I checked it end to end: I crawled a page with multi-byte characters and paged it through read_page over MCP with max_bytes 1 and 2. On main it gets stuck; with your change it makes progress every step and the chunks reassemble the page exactly. Returning one whole character even when it exceeds max_bytes matches what #19 asked for. The earlier macOS failure was a flaky test on our side, now fixed on main. Merging, thanks!

@Sriram-PR
Sriram-PR merged commit 4ff5a1c into Sriram-PR:main Oct 7, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP: read_page with max_bytes smaller than a multi-byte character never advances

2 participants