Skip to content

Commit 63ddfd7

Browse files
cliffhallclaude
andcommitted
fix(fetch): reject unknown tool and prompt names
call_tool ran fetch for any tool name, and get_prompt served the fetch prompt for any prompt name. An unknown tool now returns an "Unknown tool: <name>" error result, phrased as git and time phrase it, and an unknown prompt now returns an INVALID_PARAMS JSON-RPC error, "Unknown prompt: <name>". Both checks run before argument validation, so nothing is fetched. The three tests that pinned the old behavior now assert the new one, and their KNOWN BUG #4988 markers are gone. Closes #4988 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 571e2b3 commit 63ddfd7

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ async def list_prompts() -> list[Prompt]:
234234

235235
@server.call_tool()
236236
async def call_tool(name, arguments: dict) -> list[TextContent]:
237+
if name != "fetch":
238+
raise ValueError(f"Unknown tool: {name}")
237239
try:
238240
args = Fetch(**arguments)
239241
except ValueError as e:
@@ -270,6 +272,10 @@ async def call_tool(name, arguments: dict) -> list[TextContent]:
270272

271273
@server.get_prompt()
272274
async def get_prompt(name: str, arguments: dict | None) -> GetPromptResult:
275+
if name != "fetch":
276+
raise McpError(
277+
ErrorData(code=INVALID_PARAMS, message=f"Unknown prompt: {name}")
278+
)
273279
if not arguments or "url" not in arguments:
274280
raise McpError(ErrorData(code=INVALID_PARAMS, message="URL is required"))
275281

‎src/fetch/tests/test_protocol.py‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -218,29 +218,27 @@ async def test_invalid_url_is_rejected_by_pydantic(web: FakeWeb, url: str) -> No
218218
assert web.requests == []
219219

220220

221-
# KNOWN BUG #4988: call_tool ignores the tool name and runs fetch for any name; the fix changes this assertion.
222-
async def test_call_tool_never_checks_the_tool_name(web: FakeWeb) -> None:
223-
# Characterizes call_tool ignoring `name`: an unknown tool name is not
224-
# rejected, the SDK skips schema validation for it (the tool is not
225-
# listed), and the URL is fetched as if `fetch` had been called.
221+
async def test_unknown_tool_name_is_rejected_without_fetching(web: FakeWeb) -> None:
222+
# The SDK skips schema validation for a tool it has not listed, so the
223+
# name check in call_tool is what stops an unknown name from fetching.
226224
web.add(ROBOTS, plain("", status=404))
227225
web.add(PAGE, plain("fetched anyway"))
228226
async with connect() as (session, _):
229227
result = await session.call_tool("nope", {"url": PAGE})
230-
assert wire(result)["isError"] is False
231-
assert text_of(result).endswith("fetched anyway")
232-
assert web.urls() == [ROBOTS, PAGE]
228+
assert wire(result)["isError"] is True
229+
assert text_of(result) == "Unknown tool: nope"
230+
assert web.requests == []
233231

234232

235-
# KNOWN BUG #4988: call_tool ignores the tool name and runs fetch for any name; the fix changes this assertion.
236-
async def test_unknown_tool_name_still_validates_through_pydantic(
233+
async def test_unknown_tool_name_is_rejected_before_argument_validation(
237234
web: FakeWeb,
238235
) -> None:
236+
# An unknown name is reported as such, not as a missing `url` argument.
239237
async with connect() as (session, _):
240238
result = await session.call_tool("nope", {})
241239
assert wire(result)["isError"] is True
242-
assert "url" in text_of(result)
243-
assert "Field required" in text_of(result)
240+
assert text_of(result) == "Unknown tool: nope"
241+
assert web.requests == []
244242

245243

246244
# --------------------------------------------------------------------------
@@ -785,13 +783,21 @@ async def test_get_prompt_without_url_is_a_jsonrpc_error(
785783
assert web.requests == []
786784

787785

788-
# KNOWN BUG #4988: get_prompt ignores the prompt name and serves fetch for any name; the fix changes this assertion.
789-
async def test_get_prompt_never_checks_the_prompt_name(web: FakeWeb) -> None:
790-
# Like call_tool, get_prompt ignores `name`.
786+
@pytest.mark.parametrize("arguments", [{"url": PAGE}, None])
787+
async def test_get_prompt_with_unknown_name_is_a_jsonrpc_error(
788+
web: FakeWeb, arguments: dict[str, str] | None
789+
) -> None:
790+
# The name is checked first, so an unknown prompt is reported as such
791+
# whether or not a URL was given, and nothing is fetched.
791792
web.add(PAGE, plain("ok"))
792793
async with connect() as (session, _):
793-
result = await session.get_prompt("nope", {"url": PAGE})
794-
assert wire(result)["description"] == f"Contents of {PAGE}"
794+
with pytest.raises(McpError) as excinfo:
795+
await session.get_prompt("nope", arguments)
796+
assert wire(excinfo.value.error) == {
797+
"code": INVALID_PARAMS,
798+
"message": "Unknown prompt: nope",
799+
}
800+
assert web.requests == []
795801

796802

797803
async def test_get_prompt_does_not_validate_the_url(

0 commit comments

Comments
 (0)