Skip to content

fix(mcp): chunk documents before reranking in query tool#148

Open
kephail wants to merge 1 commit intotobi:mainfrom
kephail:fix/mcp-rerank-chunking
Open

fix(mcp): chunk documents before reranking in query tool#148
kephail wants to merge 1 commit intotobi:mainfrom
kephail:fix/mcp-rerank-chunking

Conversation

@kephail
Copy link

@kephail kephail commented Feb 9, 2026

Problem

The MCP query tool passes full document bodies to the reranker, which crashes on large documents (e.g. session transcripts >500KB) with:

The input lengths of some of the given documents exceed the context size.
Try to increase the context size to at least 509038

This also causes Bun to segfault under memory pressure (peak RSS ~7.8GB on a 16GB machine).

Root Cause

The CLI querySearch already handles this correctly — it calls chunkDocument() on each candidate and picks the best chunk per document via keyword overlap before passing to the reranker (lines 2200-2220 in qmd.ts).

The MCP query tool was missing this step, passing c.body directly to store.rerank().

Fix

Apply the same chunking strategy to the MCP query tool:

  1. Chunk each candidate document via chunkDocument()
  2. Select the best chunk per document using keyword overlap with the query
  3. Pass chunks (not full bodies) to the reranker

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.

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