Skip to content

fix(cli): return every requested chunk from the MCP chunk_list tool - #6209

Open
columbia11 wants to merge 1 commit into
Tencent:mainfrom
columbia11:fix/cli-mcp-chunk-list-pages
Open

columbia11 wants to merge 1 commit into
Tencent:mainfrom
columbia11:fix/cli-mcp-chunk-list-pages

Conversation

@columbia11

Copy link
Copy Markdown
Contributor

Description

The MCP tool chunk_list (served by weknora mcp serve) accepts limit up to 1000 and says it returns at most limit chunks, with truncated_at_limit=true when the document has more. For any limit above 100 on a document with more than 100 chunks, it returns only the first 100 and still reports truncated_at_limit=false.

Example: a document with 300 chunks, chunk_list with limit: 500 → 100 chunks, total: 300, truncated_at_limit: false. An agent that reads the flag takes those 100 chunks for the whole document.

Cause

The tool makes one request with page=1, page_size=limit. The server caps page_size for GET /api/v1/chunks/:knowledge_id at 100 (internal/handler/chunk.go), so the response never holds more than 100 chunks. The flag is computed as total > limit, which compares against what was asked for, not against what came back.

Change

chunk_list now requests the next page until it has limit chunks, has all total chunks, or gets an empty page, and trims the result to limit. truncated_at_limit is computed from the number of chunks actually returned. The page size is the same on every request, so the offsets line up whatever cap the server applies, and a limit of 100 or less is still a single request.

Not changed: weknora chunk list --page-size also allows values above the server's cap. Without --all-pages it prints one page by design and reports total_count, so I left it as it is.

Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 💥 Breaking change
  • 📚 Documentation update
  • 🎨 Refactor
  • ⚡ Performance improvement
  • 🧪 Test
  • 🔧 Configuration / Build / CI

Related Issue

None found.

Testing

New test TestTool_ChunkList_WalksServerPagesUpToLimit in cli/internal/mcp/tools_test.go. The fake service now can serve chunks in pages of at most 100, as the server does. Two cases on a 300-chunk document: limit: 500 must return all 300 with truncated_at_limit=false; limit: 250 must return chunks 0..249 with truncated_at_limit=true.

With main's tools.go both cases fail (100 chunks returned); with the change both pass.

Commands, run in WSL (Ubuntu 24.04, Go 1.26.7), from cli/:

  • go test -count=1 ./... → every package ok (including acceptance/contract).
  • go vet ./... → clean. gofmt -l on the two files → no output.
  • golangci-lint run --new-from-patch=<git diff origin/main> on ./internal/mcp/ with golangci-lint v2.12.2 → 0 issues.
  • git diff --check origin/main...HEAD → clean.

Not run: the live-server e2e suite (-tags acceptance_e2e), and the tool was not called against a running server.

Checklist

  • git diff --check origin/main...HEAD passes
  • Changed source files are formatted
  • Targeted tests for the changed packages/components pass
  • Diff-scoped lint passes where applicable (for Go: golangci-lint run --new-from-rev=origin/main ./...) — run as --new-from-patch
  • Full-repository checks were run, or any unrelated/environment-dependent failures are documented above — not run; scoped to the cli module
  • Self-reviewed the code
  • Added/updated tests covering the change
  • Updated related documentation (README, website-docs/, Swagger annotations, etc.) — none needed
  • Breaking changes are clearly called out in the description above — none

Screenshots / Recordings

Not a UI change.

The server caps a page of chunks at 100, but chunk_list sent one request with page_size=limit (up to 1000) and reported truncated_at_limit from total > limit. A 300-chunk document with limit 500 came back as 100 chunks with truncated_at_limit=false.

Walk the server pages until limit or total is reached, and compute the flag from the number of chunks actually returned.

This branch has not been deployed

No deployments
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.

1 participant