Skip to content

Commit e0c54ef

Browse files
authored
Merge pull request #5016 from modelcontextprotocol/v2/fix/4988-fetch-unknown-names
fix(fetch): reject unknown tool and prompt names
2 parents 11bd9c8 + 63ddfd7 commit e0c54ef

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
@@ -243,6 +243,8 @@ async def list_prompts() -> list[Prompt]:
243243

244244
@server.call_tool()
245245
async def call_tool(name, arguments: dict) -> list[TextContent]:
246+
if name != "fetch":
247+
raise ValueError(f"Unknown tool: {name}")
246248
try:
247249
args = Fetch(**arguments)
248250
except ValueError as e:
@@ -279,6 +281,10 @@ async def call_tool(name, arguments: dict) -> list[TextContent]:
279281

280282
@server.get_prompt()
281283
async def get_prompt(name: str, arguments: dict | None) -> GetPromptResult:
284+
if name != "fetch":
285+
raise McpError(
286+
ErrorData(code=INVALID_PARAMS, message=f"Unknown prompt: {name}")
287+
)
282288
if not arguments or "url" not in arguments:
283289
raise McpError(ErrorData(code=INVALID_PARAMS, message="URL is required"))
284290

‎src/fetch/tests/test_protocol.py‎

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

231231

232-
# KNOWN BUG #4988: call_tool ignores the tool name and runs fetch for any name; the fix changes this assertion.
233-
async def test_call_tool_never_checks_the_tool_name(web: FakeWeb) -> None:
234-
# Characterizes call_tool ignoring `name`: an unknown tool name is not
235-
# rejected, the SDK skips schema validation for it (the tool is not
236-
# listed), and the URL is fetched as if `fetch` had been called.
232+
async def test_unknown_tool_name_is_rejected_without_fetching(web: FakeWeb) -> None:
233+
# The SDK skips schema validation for a tool it has not listed, so the
234+
# name check in call_tool is what stops an unknown name from fetching.
237235
web.add(ROBOTS, plain("", status=404))
238236
web.add(PAGE, plain("fetched anyway"))
239237
async with connect() as (session, _):
240238
result = await session.call_tool("nope", {"url": PAGE})
241-
assert wire(result)["isError"] is False
242-
assert text_of(result).endswith("fetched anyway")
243-
assert web.urls() == [ROBOTS, PAGE]
239+
assert wire(result)["isError"] is True
240+
assert text_of(result) == "Unknown tool: nope"
241+
assert web.requests == []
244242

245243

246-
# KNOWN BUG #4988: call_tool ignores the tool name and runs fetch for any name; the fix changes this assertion.
247-
async def test_unknown_tool_name_still_validates_through_pydantic(
244+
async def test_unknown_tool_name_is_rejected_before_argument_validation(
248245
web: FakeWeb,
249246
) -> None:
247+
# An unknown name is reported as such, not as a missing `url` argument.
250248
async with connect() as (session, _):
251249
result = await session.call_tool("nope", {})
252250
assert wire(result)["isError"] is True
253-
assert "url" in text_of(result)
254-
assert "Field required" in text_of(result)
251+
assert text_of(result) == "Unknown tool: nope"
252+
assert web.requests == []
255253

256254

257255
# --------------------------------------------------------------------------
@@ -796,13 +794,21 @@ async def test_get_prompt_without_url_is_a_jsonrpc_error(
796794
assert web.requests == []
797795

798796

799-
# KNOWN BUG #4988: get_prompt ignores the prompt name and serves fetch for any name; the fix changes this assertion.
800-
async def test_get_prompt_never_checks_the_prompt_name(web: FakeWeb) -> None:
801-
# Like call_tool, get_prompt ignores `name`.
797+
@pytest.mark.parametrize("arguments", [{"url": PAGE}, None])
798+
async def test_get_prompt_with_unknown_name_is_a_jsonrpc_error(
799+
web: FakeWeb, arguments: dict[str, str] | None
800+
) -> None:
801+
# The name is checked first, so an unknown prompt is reported as such
802+
# whether or not a URL was given, and nothing is fetched.
802803
web.add(PAGE, plain("ok"))
803804
async with connect() as (session, _):
804-
result = await session.get_prompt("nope", {"url": PAGE})
805-
assert wire(result)["description"] == f"Contents of {PAGE}"
805+
with pytest.raises(McpError) as excinfo:
806+
await session.get_prompt("nope", arguments)
807+
assert wire(excinfo.value.error) == {
808+
"code": INVALID_PARAMS,
809+
"message": "Unknown prompt: nope",
810+
}
811+
assert web.requests == []
806812

807813

808814
async def test_get_prompt_does_not_validate_the_url(

0 commit comments

Comments
 (0)