Skip to content

Commit d5cf1c8

Browse files
committed
Merge v2/fix/4914-fetch-prompt-invalid-url into the #5271 rollup
Signed-off-by: cliffhall <[email protected]>
2 parents f59c6cd + e8138f9 commit d5cf1c8

4 files changed

Lines changed: 109 additions & 22 deletions

File tree

‎src/fetch/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ The markdown of an HTML page opens with the page's title as a `#` heading (its `
3737
### Errors
3838

3939
- A failed fetch (a robots.txt refusal, an unreachable page, an HTTP error status, a refused private address, an unusable proxy setting) and arguments that do not match the tool's schema are returned as a tool result with `isError: true`, whose text starts `Error executing tool fetch:` and says what went wrong, so the model can read it and decide what to do.
40-
- Calling a tool the server does not have is a JSON-RPC protocol error, `-32602` (`Unknown tool: <name>`), as the specification requires. So is getting a prompt it does not have (`Unknown prompt: <name>`), or the `fetch` prompt without its `url` (`URL is required`).
40+
- Calling a tool the server does not have is a JSON-RPC protocol error, `-32602` (`Unknown tool: <name>`), as the specification requires. So is getting a prompt it does not have (`Unknown prompt: <name>`), or the `fetch` prompt without its `url` (`URL is required`) or with a `url` that is not a valid URL (`Invalid URL '<url>': <reason>`).
4141
- When the `fetch` prompt's fetch fails, the prompt still returns: its message holds the reason the fetch failed.
4242

4343
The server is built on the MCP Python SDK's high-level `MCPServer`: the tool and the prompt are decorated functions, and their type hints and field descriptions are the input schema and the prompt's arguments.

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# prompt the server does not have, or a prompt call without its `url`, is a
1515
# malformed request, answered with a JSON-RPC `-32602` protocol error by
1616
# `reject_unknown_names` before `MCPServer` sees it (see there for why).
17+
# A prompt `url` that is not a valid URL gets the same `-32602` from the
18+
# prompt handler (#4914).
1719
# `serve()` serves both protocol eras (#4853): a 2025-11-25 client through the
1820
# initialize handshake, and a 2026-07-28 client sessionlessly. No handler reads
1921
# the session or calls back to the client (roots, sampling, elicitation,
@@ -29,6 +31,7 @@
2931
from urllib.parse import urlparse, urlunparse
3032

3133
import httpx
34+
from pydantic import TypeAdapter, ValidationError
3235
import markdownify
3336
import readabilipy.simple_json
3437
from .readability_js import extract_article
@@ -657,6 +660,10 @@ async def _check_redirect_robots_txt(request: httpx.Request) -> None:
657660
Although originally you did not have internet access, and were advised to refuse and tell the user this, this tool now grants you internet access. Now you can fetch the most up-to-date information and let the user know that."""
658661

659662

663+
# Validates the prompt's `url` the way the tool's AnyUrl parameter does.
664+
_PROMPT_URL = TypeAdapter(AnyUrl)
665+
666+
660667
def reject_unknown_names(server: MCPServer) -> ServerMiddleware[Any]:
661668
"""Middleware answering a malformed tool or prompt request with `-32602`.
662669
@@ -816,6 +823,16 @@ async def fetch(
816823
async def fetch_prompt(
817824
url: Annotated[str, Field(description="URL to fetch")],
818825
) -> str:
826+
# The URL is checked as the tool's AnyUrl parameter checks it, so a
827+
# malformed one is a bad argument (-32602) rather than an exception
828+
# out of httpx, which the SDK would answer with code 0 (#4914).
829+
try:
830+
url = str(_PROMPT_URL.validate_python(url))
831+
except ValidationError as e:
832+
raise MCPError(
833+
code=INVALID_PARAMS,
834+
message=f"Invalid URL {url!r}: {e.errors()[0]['msg']}",
835+
)
819836
# The user asked for this page, so robots.txt is not consulted, and a
820837
# failure is shown to them as the prompt's text rather than raised.
821838
try:

‎src/fetch/tests/test_prompt_url.py‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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/"]

‎src/fetch/tests/test_protocol.py‎

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
from __future__ import annotations
1717

18-
import os
1918
from collections.abc import AsyncIterator
2019
from contextlib import asynccontextmanager
2120
from importlib.metadata import version
@@ -1193,26 +1192,6 @@ async def test_get_prompt_with_unknown_name_is_a_jsonrpc_error(
11931192
assert web.requests == []
11941193

11951194

1196-
async def test_get_prompt_does_not_validate_the_url(
1197-
monkeypatch: pytest.MonkeyPatch,
1198-
) -> None:
1199-
# The prompt passes the raw string to httpx; an unusable URL comes back as
1200-
# a "Failed to fetch" prompt message rather than a validation error.
1201-
# No `web` fixture: the real transport rejects the URL before any I/O.
1202-
# Proxy variables are cleared so the real client cannot pick up one from
1203-
# the environment and send the request through it.
1204-
for name in list(os.environ):
1205-
if name.lower() in ("http_proxy", "https_proxy", "all_proxy"):
1206-
monkeypatch.delenv(name)
1207-
async with connect() as (session, _):
1208-
result = await session.get_prompt("fetch", {"url": "not a url"})
1209-
data = wire(result)
1210-
assert data["description"] == PROMPT_DESCRIPTION
1211-
assert data["messages"][0]["content"]["text"].startswith(
1212-
"Failed to fetch not a url: UnsupportedProtocol("
1213-
)
1214-
1215-
12161195
# ---------------------------------------------------------------------------
12171196
# Protocol era
12181197
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)