Skip to content

Commit e569516

Browse files
committed
feat(fetch): include page title in extracted content
The readabilipy library already extracts the HTML page title, but it was being discarded. Now the title is prepended as a markdown H1 heading when present, giving consumers useful context about the page. Handles missing, null, and whitespace-only titles gracefully. Fixes modelcontextprotocol#2472
1 parent f424458 commit e569516

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ def extract_content_from_html(html: str) -> str:
4242
ret["content"],
4343
heading_style=markdownify.ATX,
4444
)
45+
title = (ret.get("title") or "").strip()
46+
if title:
47+
content = f"# {title}\n\n{content}"
4548
return content
4649

4750

‎src/fetch/tests/test_server.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,39 @@ def test_simple_html(self):
6767
# readabilipy may extract different parts depending on the content
6868
assert "test paragraph" in result
6969

70+
@patch("mcp_server_fetch.server.readabilipy.simple_json.simple_json_from_html_string")
71+
def test_includes_page_title(self, mock_readability):
72+
"""Test that page title is included in the output."""
73+
mock_readability.return_value = {
74+
"title": "My Page Title",
75+
"content": "<p>Some content here.</p>",
76+
}
77+
result = extract_content_from_html("<html></html>")
78+
assert result.startswith("# My Page Title")
79+
assert "Some content here" in result
80+
81+
@patch("mcp_server_fetch.server.readabilipy.simple_json.simple_json_from_html_string")
82+
def test_no_title_still_works(self, mock_readability):
83+
"""Test that pages without a title still return content."""
84+
mock_readability.return_value = {
85+
"title": None,
86+
"content": "<p>Content without a title tag.</p>",
87+
}
88+
result = extract_content_from_html("<html></html>")
89+
assert "Content without a title" in result
90+
assert not result.startswith("# ")
91+
92+
@patch("mcp_server_fetch.server.readabilipy.simple_json.simple_json_from_html_string")
93+
def test_empty_title_not_prepended(self, mock_readability):
94+
"""Test that empty/whitespace-only titles are not prepended."""
95+
mock_readability.return_value = {
96+
"title": " ",
97+
"content": "<p>Body text.</p>",
98+
}
99+
result = extract_content_from_html("<html></html>")
100+
assert not result.startswith("# ")
101+
assert "Body text" in result
102+
70103
def test_html_with_links(self):
71104
"""Test that links are converted to markdown."""
72105
html = """

0 commit comments

Comments
 (0)