fix(fetch): 3-stage SSR fallback in extract_content_from_html (#3878)#4511
Open
ZhangJing-gugugaga wants to merge 1 commit into
Open
fix(fetch): 3-stage SSR fallback in extract_content_from_html (#3878)#4511ZhangJing-gugugaga wants to merge 1 commit into
ZhangJing-gugugaga wants to merge 1 commit into
Conversation
…extprotocol#3878) Port of PR modelcontextprotocol#3922 design. When Readability extracts text that is short relative to the input HTML size (a loading-shell signature common to progressive-SSR pages like Next.js streaming), fall back through: Stage 1 readabilipy + Readability (unchanged behaviour) Stage 2 readabilipy without Readability (keeps hidden markup) Stage 3 raw markdownify (last resort) Fallback only fires on the short-output path, so normal readable pages see zero behaviour change (probed by the existing TestExtractContentFromHtml suite, 3 tests, all green). Co-Authored-By: Claude Opus 4.6 <<EMAIL>>
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
Pages that use progressive SSR (Next.js streaming, Remix deferred, custom
Lambda SSR) deliver content in two phases: a small visible loading shell,
then the real content in a hidden container
(
visibility:hidden; position:absolute; top:-9999px) that becomesvisible only after client-side hydration.
Mozilla Readability treats those hidden elements as non-content and
strips them, so
extract_content_from_htmlsilently returns just theloading shell (e.g.
"Unified Serverless Framework …") with no indicationthat content was lost.
Upstream PR #3922 proposes a fix; this PR ports the same design.
Fix
src/fetch/src/mcp_server_fetch/server.py— 3-stage fallback inextract_content_from_html:for normal readable pages).
< 1% of the HTML size as text — keeps
visibility:hiddenmarkup.Normal readable pages always hit Stage 1 only and are byte-for-byte
unchanged.
Verification
cd src/fetch && uv sync --frozen --all-extras --dev && uv run pytest tests/test_server.py -qThe existing
TestExtractContentFromHtmlsuite (3 tests, zero-behaviour-changecontract) still passes. Added
TestExtractContentFromHtmlFallback(3 tests):empty input errors, Stage 2/3 don't crash on an empty Stage 1, long readable
Stage 1 is unaffected.
Notes
tests/conftest.pyto injectsrc/ontoPYTHONPATHso pytestcollects
mcp_server_fetch. The repo's other Python servers appear toconfigure this same way via
[tool.uv]or equivalent; I used aconftest.pyto stay minimally invasive.Fixes #3878.