fix(mcp): chunk documents before reranking in query tool#148
Open
fix(mcp): chunk documents before reranking in query tool#148
Conversation
The MCP query tool was passing full document bodies to the reranker, which crashes on large documents (e.g. session transcripts >500KB) with 'input lengths exceed the context size' errors, and can cause Bun to segfault under memory pressure. The CLI querySearch already chunks documents and picks the best chunk per document before reranking. This applies the same approach to the MCP query tool: chunk each candidate, select the best chunk via keyword overlap, and rerank chunks instead of full bodies.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The MCP
querytool passes full document bodies to the reranker, which crashes on large documents (e.g. session transcripts >500KB) with:This also causes Bun to segfault under memory pressure (peak RSS ~7.8GB on a 16GB machine).
Root Cause
The CLI
querySearchalready handles this correctly — it callschunkDocument()on each candidate and picks the best chunk per document via keyword overlap before passing to the reranker (lines 2200-2220 inqmd.ts).The MCP
querytool was missing this step, passingc.bodydirectly tostore.rerank().Fix
Apply the same chunking strategy to the MCP query tool:
chunkDocument()This matches the existing CLI behaviour and keeps documents within the reranker's context window.
Testing
Reproduced with OpenClaw's QMD session transcript indexing — a 1.6MB session transcript was causing the crash. After this fix, the MCP server stays stable and reranking works correctly on large documents.