fix(mcp): include exception type in error messages when str(exc) is empty#19425
Closed
liuhao1024 wants to merge 1 commit into
Closed
fix(mcp): include exception type in error messages when str(exc) is empty#19425liuhao1024 wants to merge 1 commit into
liuhao1024 wants to merge 1 commit into
Conversation
…mpty
Some exception classes (e.g. anyio.ClosedResourceError) are raised without
a message argument, so str(exc) returns an empty string. The existing error
format f'{type(exc).__name__}: {exc}' would produce messages like
'MCP call failed: ClosedResourceError: ' with nothing after the colon.
Add _exc_str() helper that falls back to repr(exc) when str(exc) is empty,
and apply it to all 6 MCP error formatting sites (5 tool/prompt/resource
handlers + 1 sampling handler).
Fixes NousResearch#19417
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.
Summary
When MCP exceptions are raised without a message argument (e.g.
anyio.ClosedResourceError),str(exc)returns"". The existing error formatf"MCP call failed: {type(exc).__name__}: {exc}"produces messages likeMCP call failed: ClosedResourceError:with nothing after the colon, making debugging impossible.Root Cause
The
ClosedResourceErrorclass fromanyiois initialized with no message argument:When raised without arguments,
str(e)returns"". The error handler in_make_tool_handler(and 4 other MCP handler factories) usesf"{exc}"which callsstr(exc), producing an empty string in the error output.Fix
Add
_exc_str()helper that falls back torepr(exc)whenstr(exc)is empty or whitespace-only, and apply it to all 6 MCP error formatting sites:_make_tool_handler(line 2103)_make_list_resources_handler(line 2161)_make_read_resource_handler(line 2221)_make_list_prompts_handler(line 2284)_make_get_prompt_handler(line 2355)SamplingHandler._call(line 835)Regression Coverage
test_exc_str_returns_str_when_nonempty— normal exceptions usestr(exc)test_exc_str_falls_back_to_repr_when_str_empty— empty-message exceptions fall back torepr(exc)test_exc_str_falls_back_to_repr_for_whitespace_only— whitespace-only messages trigger fallbacktest_exc_str_handles_closedresource_like_exception— simulatesanyio.ClosedResourceErrorbehaviortest_error_message_not_empty_when_exc_has_no_message— formatted error never ends with": "test_error_message_preserves_normal_exception_text— normal exceptions still show their messageTesting
All 23 MCP-related tests pass (6 new + 17 existing).
Fixes #19417