|
| 1 | +"""Protocol-level tests for asking for native markdown (#5198). |
| 2 | +
|
| 3 | +A fetch used to send httpx's default ``Accept: */*``, so a site that can serve |
| 4 | +markdown through content negotiation sent HTML to be simplified, and a |
| 5 | +``text/markdown`` response was labeled as content that "cannot be simplified |
| 6 | +to markdown" behind the raw-content prefix. Now a non-raw fetch prefers |
| 7 | +``text/markdown`` (HTML next, anything else last), and a markdown response is |
| 8 | +returned as-is with no prefix. A ``raw`` fetch keeps the default ``Accept``, |
| 9 | +so it still gets the page's own HTML. These tests drive ``serve()`` through an |
| 10 | +MCP client with HTTP served by ``harness.FakeWeb``. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import httpx |
| 16 | +import pytest |
| 17 | + |
| 18 | +from mcp_server_fetch.server import MARKDOWN_ACCEPT, _is_markdown |
| 19 | + |
| 20 | +from .harness import FakeWeb, connect |
| 21 | + |
| 22 | +PAGE = "https://example.com/doc" |
| 23 | +ROBOTS = "https://example.com/robots.txt" |
| 24 | + |
| 25 | +# Opens with an "<html" mention inside the first 100 characters, which the |
| 26 | +# HTML sniff would otherwise take for a page to simplify. |
| 27 | +MARKDOWN = "# Title\n\nUse an `<html>` tag at the top.\n\n- one\n- two\n" |
| 28 | + |
| 29 | +ARTICLE = """<html><head><title>Test Page</title></head><body> |
| 30 | +<article> |
| 31 | +<h1>Hello World</h1> |
| 32 | +<p>This is a <a href="https://example.com/x">link</a> in a test paragraph |
| 33 | +with enough words to count as the main content of the page.</p> |
| 34 | +</article> |
| 35 | +</body></html>""" |
| 36 | + |
| 37 | + |
| 38 | +def markdown(content_type: str = "text/markdown; charset=utf-8") -> httpx.Response: |
| 39 | + return httpx.Response( |
| 40 | + 200, content=MARKDOWN.encode(), headers={"content-type": content_type} |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +async def call_fetch(web: FakeWeb, arguments: dict[str, object]) -> str: |
| 45 | + async with connect() as (session, _): |
| 46 | + result = await session.call_tool("fetch", {"url": PAGE, **arguments}) |
| 47 | + data = result.model_dump(by_alias=True, mode="json", exclude_none=True) |
| 48 | + assert data["isError"] is False |
| 49 | + return data["content"][0]["text"] |
| 50 | + |
| 51 | + |
| 52 | +def page_requests(web: FakeWeb) -> list[httpx.Request]: |
| 53 | + return [r for r in web.requests if str(r.url) == PAGE] |
| 54 | + |
| 55 | + |
| 56 | +async def test_a_fetch_prefers_markdown_then_html(web: FakeWeb) -> None: |
| 57 | + web.add(ROBOTS, httpx.Response(404)) |
| 58 | + web.add(PAGE, markdown()) |
| 59 | + await call_fetch(web, {}) |
| 60 | + [request] = page_requests(web) |
| 61 | + assert request.headers["accept"] == "text/markdown, text/html;q=0.9, */*;q=0.8" |
| 62 | + assert MARKDOWN_ACCEPT == request.headers["accept"] |
| 63 | + # The robots.txt lookup is not a page fetch and asks for nothing new. |
| 64 | + [robots] = [r for r in web.requests if str(r.url) == ROBOTS] |
| 65 | + assert robots.headers["accept"] == "*/*" |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize( |
| 69 | + "content_type", ["text/markdown", "text/markdown; charset=utf-8", "Text/Markdown"] |
| 70 | +) |
| 71 | +async def test_markdown_is_returned_as_is_without_the_raw_prefix( |
| 72 | + web: FakeWeb, content_type: str |
| 73 | +) -> None: |
| 74 | + web.add(ROBOTS, httpx.Response(404)) |
| 75 | + web.add(PAGE, markdown(content_type)) |
| 76 | + text = await call_fetch(web, {}) |
| 77 | + assert text == f"Contents of {PAGE}:\n{MARKDOWN}" |
| 78 | + |
| 79 | + |
| 80 | +async def test_a_raw_fetch_keeps_the_default_accept(web: FakeWeb) -> None: |
| 81 | + web.add(ROBOTS, httpx.Response(404)) |
| 82 | + web.add( |
| 83 | + PAGE, httpx.Response(200, text=ARTICLE, headers={"content-type": "text/html"}) |
| 84 | + ) |
| 85 | + text = await call_fetch(web, {"raw": True}) |
| 86 | + [request] = page_requests(web) |
| 87 | + assert request.headers["accept"] == "*/*" |
| 88 | + assert text.endswith(f"Contents of {PAGE}:\n{ARTICLE}") |
| 89 | + |
| 90 | + |
| 91 | +async def test_markdown_under_raw_is_unchanged_and_unprefixed(web: FakeWeb) -> None: |
| 92 | + # Markdown served unasked (a .md file, say) is the same either way. |
| 93 | + web.add(ROBOTS, httpx.Response(404)) |
| 94 | + web.add(PAGE, markdown()) |
| 95 | + text = await call_fetch(web, {"raw": True}) |
| 96 | + assert text == f"Contents of {PAGE}:\n{MARKDOWN}" |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.usefixtures("python_readability") |
| 100 | +async def test_html_from_a_site_that_ignores_accept_is_still_simplified( |
| 101 | + web: FakeWeb, |
| 102 | +) -> None: |
| 103 | + web.add(ROBOTS, httpx.Response(404)) |
| 104 | + web.add( |
| 105 | + PAGE, httpx.Response(200, text=ARTICLE, headers={"content-type": "text/html"}) |
| 106 | + ) |
| 107 | + text = await call_fetch(web, {}) |
| 108 | + assert "cannot be simplified" not in text |
| 109 | + assert "<article>" not in text |
| 110 | + assert "Hello World" in text |
| 111 | + |
| 112 | + |
| 113 | +async def test_the_prompt_returns_markdown_as_is(web: FakeWeb) -> None: |
| 114 | + web.add(PAGE, markdown()) |
| 115 | + async with connect() as (session, _): |
| 116 | + result = await session.get_prompt("fetch", {"url": PAGE}) |
| 117 | + message = result.model_dump(by_alias=True, mode="json")["messages"][0] |
| 118 | + assert message["content"]["text"].endswith(MARKDOWN) |
| 119 | + assert "cannot be simplified" not in message["content"]["text"] |
| 120 | + [request] = page_requests(web) |
| 121 | + assert request.headers["accept"] == MARKDOWN_ACCEPT |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.parametrize( |
| 125 | + ("content_type", "expected"), |
| 126 | + [ |
| 127 | + ("text/markdown", True), |
| 128 | + (" text/markdown ; variant=GFM", True), |
| 129 | + ("text/markdownish", False), |
| 130 | + ("text/x-markdown", False), |
| 131 | + ("text/plain", False), |
| 132 | + ("", False), |
| 133 | + ], |
| 134 | +) |
| 135 | +def test_is_markdown(content_type: str, expected: bool) -> None: |
| 136 | + assert _is_markdown(content_type) is expected |
0 commit comments