Skip to content

fix(mcp): preserve Markdown headings, lists, and table structure in context compressor - #298

Open
ABHISHEKUPAI wants to merge 2 commits into
kubeflow:mainfrom
ABHISHEKUPAI:fix-compressor-markdown-structure
Open

ABHISHEKUPAI wants to merge 2 commits into
kubeflow:mainfrom
ABHISHEKUPAI:fix-compressor-markdown-structure

Conversation

@ABHISHEKUPAI

Copy link
Copy Markdown

Closes #297

Overview

In docs-agent-mcp/mcp-server/compressor.py, the compressor previously joined selected blocks with " " whenever a chunk didn't contain a fenced code block (```).

For standard documentation chunks (such as API spec parameter dictionaries, prerequisites, or troubleshooting steps), this caused headings (### Heading) to swallow following bullet points and parameters onto a single line without newlines, turning the entire block into a single heading tag in Markdown.

Changes

  • Replaced the naive any(p.startswith("```")) ternary join with a structure-aware _join_blocks() helper in compressor.py.
  • Separated headings, code blocks, and tables with \n\n.
  • Separated consecutive list items (- , * , 1. ) with \n to preserve list formatting.
  • Kept \n\n boundaries when transitioning between list items and prose/headings.
  • Preserved space-joining for consecutive prose sentences within the same paragraph.
  • Added regression tests in tests/test_reranker_and_compressor.py.

Before vs After

before

Katib Configuration - Set parallelTrialCount: 3 in the experiment spec. - Enable metrics collector.

After

Katib Configuration

  • Set parallelTrialCount: 3 in the experiment spec.
  • Enable metrics collector.

Verification
pytest tests/test_reranker_and_compressor.py passed (13/13).
Full test suite passed (163 passed, 69 skipped).
ruff check and ruff format --check passed cleanly across docs-agent-mcp/mcp-server and tests.

…ontext compressor

Signed-off-by: ABHISHEKUPAI <abhishekupai6055@gmail.com>
@google-oss-prow

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign tarekabouzeid for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kmr-rohit kmr-rohit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks right to me: joining with a space turned a heading plus its bullets into one long heading line, and _join_blocks handles the cases I'd expect (heading, list, code fence, prose).

One question: are table rows separate blocks at this point, or is a whole table one block? If rows are separate, two consecutive rows are joined with \n\n, which would split the table into one-row tables. A test with a three-row table going through compress_hits would settle it.

It touches the same lines in compressor.py as #296 and sits next to #283, so whichever lands second will need a rebase.

…compressor

Signed-off-by: ABHISHEKUPAI <abhishekupai6055@gmail.com>
@ABHISHEKUPAI

Copy link
Copy Markdown
Author

This looks right to me: joining with a space turned a heading plus its bullets into one long heading line, and _join_blocks handles the cases I'd expect (heading, list, code fence, prose).

One question: are table rows separate blocks at this point, or is a whole table one block? If rows are separate, two consecutive rows are joined with \n\n, which would split the table into one-row tables. A test with a three-row table going through compress_hits would settle it.

It touches the same lines in compressor.py as #296 and sits next to #283, so whichever lands second will need a rebase.

In _split_into_blocks(), Markdown tables are recognized and appended as single atomic paragraph blocks (blocks.append(para)). Because of this, table rows are not split into individual blocks the whole table is treated as one block, preserving single newlines (\n) between rows.

I have added a test with a multi-row table going through compress_hits to verify that table rows remain intact and are not split into one-row tables with \n\n.

@kmr-rohit

Copy link
Copy Markdown
Contributor

Hi @tarekabouzeid
can you add /ok-to-test and /eval to run CI and evaluation for the changes. Have reviewed changes are okay to test.

@ABHISHEKUPAI

Copy link
Copy Markdown
Author

Hi @tarekabouzeid @kmr-rohit ,
Just a gentle check-in whenever you have a free moment. as mentioned above that the changes look good to test whenever it's convenient, could someone please help run /ok-to-test and /eval

if at all further changes are required please do let me know about the same as well happy

Really appreciate your time and help on this, thank you

@kmr-rohit

kmr-rohit commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

@tarekabouzeid @SanthoshToorpu this one is reviewed and ready: could one of you add /ok-to-test and then /eval? The change is in the MCP compressor, so no re-ingest is needed.

(The preview still carries #290's old eval label from 15 days ago; /eval clears labels older than 3 hours on its own, so nothing to do there.)

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(mcp): compressor space-joins non-code blocks, destroying headings, lists, and tables

2 participants