Repository navigation
fix(fetch): validate prompt URL and raise INVALID_PARAMS (#4914) - #4918
Closed
DawnofGenX wants to merge 2 commits into
Closed
DawnofGenX wants to merge 2 commits into
DawnofGenX wants to merge 2 commits into
Conversation
httpx.InvalidURL is not an httpx.HTTPError, so it escaped the prompt handler's 'except McpError' block and the SDK answered with a generic JSON-RPC code 0 instead of a proper error. Validate the url argument through the Fetch model before fetching (the same way the fetch tool already does), so invalid URLs raise McpError with INVALID_PARAMS (-32602). Fixes modelcontextprotocol#4914
Author
|
Closing due to inactivity — 4d idle, no review. Feel free to reopen if still wanted. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes #4914
The
fetchprompt passes itsurlargument straight tofetch_url, which converts onlyhttpx.HTTPError.httpx.InvalidURLis not anHTTPError, so it escapes the prompt'sexcept McpErrorhandler; withraise_exceptions=Falsethe SDK answers with a generic JSON-RPC error, code 0, carrying the raw exception text. A client cannot distinguish a bad argument from a server fault.The
fetchtool already handles this correctly by validating through theFetchmodel and raisingINVALID_PARAMS.Before
{"jsonrpc": "2.0", "id": 3, "error": {"code": 0, "message": "Invalid port: ':1'"}}After
{"jsonrpc": "2.0", "id": 3, "error": {"code": -32602, "message": "1 validation error for Fetch\nurl\n Input should be a valid URL, invalid IPv6 address ..."}}Fix
Validate the prompt's
urlthrough the sameFetchmodel the tool uses, before fetching:model_validateis used rather thanFetch(url=...)because a direct constructor call makes pyright complain about the model's required-with-default fields; behaviour is identical.Test plan
TestGetPrompt::test_invalid_url_raises_invalid_params— drivesget_promptthrough a realClientSessionover in-memory transports and assertserror.code == INVALID_PARAMSTestGetPrompt::test_valid_url_returns_fetched_content— regression coverage for the happy pathErrorData(code=0, message="Invalid port: ':1'"), i.e. the reported buguv run --no-sync pytestinsrc/fetch: 22 passeduv run --no-sync pyright: 0 errors, 0 warningsuv run --no-sync ruff check .: all checks passed