Skip to content

Commit 9acf431

Browse files
authored
Merge pull request #5018 from modelcontextprotocol/v2/fix/4989-fetch-empty-page
fix(fetch): report an empty page without Node as a simplification failure
2 parents e0c54ef + 4721f93 commit 9acf431

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

‎src/fetch/src/mcp_server_fetch/server.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,19 @@ def extract_content_from_html(html: str) -> str:
4242
ret = readabilipy.simple_json.simple_json_from_html_string(
4343
html, use_readability=True
4444
)
45+
failed = "<error>Page failed to be simplified from HTML</error>"
4546
if not ret["content"]:
46-
return "<error>Page failed to be simplified from HTML</error>"
47+
return failed
4748
content = markdownify.markdownify(
4849
ret["content"],
4950
heading_style=markdownify.ATX,
5051
)
52+
# Without Node, readabilipy's pure-Python extractor never returns empty
53+
# content: an empty page comes back as "<div></div>", which converts to an
54+
# empty string. Report that as the same simplification failure the Node
55+
# path reports, rather than letting it reach pagination as an exhausted page.
56+
if not content.strip():
57+
return failed
5158
return content
5259

5360

‎src/fetch/tests/test_protocol.py‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -460,18 +460,23 @@ async def test_html_without_node_falls_back_to_pure_python(web: FakeWeb) -> None
460460
assert "https://example.com/x" not in text
461461

462462

463-
# KNOWN BUG #4989: an empty page without Node reports "No more content available" at start_index 0 instead of a simplification failure; the fix changes this assertion.
464463
@pytest.mark.usefixtures("python_readability")
465-
async def test_empty_html_without_node_reads_as_no_more_content(web: FakeWeb) -> None:
466-
# The pure-Python extractor never returns empty content, so the
467-
# "failed to be simplified" message is not reached; the page converts to
468-
# an empty string and the pagination check reports it as exhausted.
464+
async def test_empty_html_without_node_cannot_be_simplified(web: FakeWeb) -> None:
465+
# The pure-Python extractor returns "<div></div>" for an empty page, which
466+
# converts to an empty string. It reports the same simplification failure
467+
# as the Node path, not "No more content available" at start_index 0.
469468
web.add(PAGE, html("<html><body></body></html>"))
470469
async with connect(ignore_robots_txt=True) as (session, _):
471470
result = await call(session, {"url": PAGE})
472-
assert text_of(result) == (
473-
f"Contents of {PAGE}:\n<error>No more content available.</error>"
474-
)
471+
assert wire(result) == {
472+
"content": [
473+
{
474+
"type": "text",
475+
"text": f"Contents of {PAGE}:\n<error>Page failed to be simplified from HTML</error>",
476+
}
477+
],
478+
"isError": False,
479+
}
475480

476481

477482
async def test_raw_returns_html_unsimplified(web: FakeWeb) -> None:

0 commit comments

Comments
 (0)