|
| 1 | +"""The `fetch` prompt rejects a malformed `url` as a bad argument (#4914). |
| 2 | +
|
| 3 | +The tool's `url` is an AnyUrl, so pydantic rejects a malformed one before the |
| 4 | +tool runs. The prompt's `url` is a plain string (prompt arguments always are), |
| 5 | +and it used to go to httpx unchecked: `httpx.InvalidURL` is not an |
| 6 | +`HTTPError`, so it escaped the prompt's error handling and the SDK answered |
| 7 | +with JSON-RPC code 0, which a client cannot tell from a server fault. These |
| 8 | +tests pin the fix at the protocol level: the same AnyUrl check, answered with |
| 9 | +`-32602` and the validation message, in both protocol eras and on the raw |
| 10 | +wire, with nothing fetched; and a valid URL still fetched, normalized the way |
| 11 | +the tool normalizes it. |
| 12 | +""" |
| 13 | + |
| 14 | +import httpx |
| 15 | +import pytest |
| 16 | +from mcp.shared.exceptions import MCPError |
| 17 | +from mcp.types import INVALID_PARAMS, JSONRPCError, TextContent |
| 18 | + |
| 19 | +from .harness import FakeWeb, connect, raw_request |
| 20 | +from .test_eras import ERAS |
| 21 | +from .test_eras import connect as connect_era |
| 22 | + |
| 23 | +# (url, the pydantic message the error carries) |
| 24 | +INVALID = [ |
| 25 | + pytest.param("http://[::1", "invalid IPv6 address", id="unclosed-ipv6"), |
| 26 | + pytest.param("not a url", "relative URL without a base", id="no-scheme"), |
| 27 | + pytest.param("http://", "empty host", id="empty-host"), |
| 28 | + pytest.param("http://example.com:99999/", "invalid port number", id="bad-port"), |
| 29 | +] |
| 30 | + |
| 31 | + |
| 32 | +def expected_message(url: str, reason: str) -> str: |
| 33 | + return f"Invalid URL {url!r}: Input should be a valid URL, {reason}" |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize(("url", "reason"), INVALID) |
| 37 | +async def test_invalid_prompt_url_is_invalid_params_on_the_wire( |
| 38 | + web: FakeWeb, url: str, reason: str |
| 39 | +) -> None: |
| 40 | + # The issue's reproduction, as raw JSON-RPC: code -32602, never 0. |
| 41 | + response = await raw_request( |
| 42 | + "prompts/get", {"name": "fetch", "arguments": {"url": url}} |
| 43 | + ) |
| 44 | + assert isinstance(response, JSONRPCError), response |
| 45 | + assert response.error.code == INVALID_PARAMS |
| 46 | + assert response.error.message == expected_message(url, reason) |
| 47 | + assert web.requests == [] |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize(("url", "reason"), INVALID) |
| 51 | +async def test_invalid_prompt_url_is_invalid_params_through_a_client( |
| 52 | + web: FakeWeb, url: str, reason: str |
| 53 | +) -> None: |
| 54 | + async with connect() as (session, _): |
| 55 | + with pytest.raises(MCPError) as excinfo: |
| 56 | + await session.get_prompt("fetch", {"url": url}) |
| 57 | + assert excinfo.value.error.code == INVALID_PARAMS |
| 58 | + assert excinfo.value.error.message == expected_message(url, reason) |
| 59 | + assert web.requests == [] |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize(("mode", "negotiated"), ERAS) |
| 63 | +async def test_invalid_prompt_url_is_invalid_params_in_both_eras( |
| 64 | + web: FakeWeb, mode: str, negotiated: str |
| 65 | +) -> None: |
| 66 | + async with connect_era(mode) as client: |
| 67 | + assert client.session.protocol_version == negotiated |
| 68 | + with pytest.raises(MCPError) as excinfo: |
| 69 | + await client.get_prompt("fetch", {"url": "http://[::1"}) |
| 70 | + assert excinfo.value.error.code == INVALID_PARAMS |
| 71 | + assert excinfo.value.error.message == expected_message( |
| 72 | + "http://[::1", "invalid IPv6 address" |
| 73 | + ) |
| 74 | + assert web.requests == [] |
| 75 | + |
| 76 | + |
| 77 | +async def test_valid_prompt_url_is_fetched_as_the_tool_would_fetch_it( |
| 78 | + web: FakeWeb, |
| 79 | +) -> None: |
| 80 | + # A bare host is normalized with its trailing slash, as the tool's AnyUrl |
| 81 | + # normalizes it, and the page comes back as the prompt's text. |
| 82 | + web.add( |
| 83 | + "https://example.com/", |
| 84 | + httpx.Response(200, content=b"hello", headers={"content-type": "text/plain"}), |
| 85 | + ) |
| 86 | + async with connect() as (session, _): |
| 87 | + result = await session.get_prompt("fetch", {"url": "https://example.com"}) |
| 88 | + content = result.messages[0].content |
| 89 | + assert isinstance(content, TextContent) |
| 90 | + assert content.text.endswith("hello") |
| 91 | + assert web.urls() == ["https://example.com/"] |
0 commit comments