Skip to content

fix(mcp): include exception type in error messages when str(exc) is empty#19425

Closed
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/mcp-empty-error-message
Closed

fix(mcp): include exception type in error messages when str(exc) is empty#19425
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/mcp-empty-error-message

Conversation

@liuhao1024
Copy link
Copy Markdown
Contributor

Summary

When MCP exceptions are raised without a message argument (e.g. anyio.ClosedResourceError), str(exc) returns "". The existing error format f"MCP call failed: {type(exc).__name__}: {exc}" produces messages like MCP call failed: ClosedResourceError: with nothing after the colon, making debugging impossible.

Root Cause

The ClosedResourceError class from anyio is initialized with no message argument:

class ClosedResourceError(Exception):
    """Raised when trying to use a resource that has been closed."""

When raised without arguments, str(e) returns "". The error handler in _make_tool_handler (and 4 other MCP handler factories) uses f"{exc}" which calls str(exc), producing an empty string in the error output.

Fix

Add _exc_str() helper that falls back to repr(exc) when str(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 use str(exc)
  • test_exc_str_falls_back_to_repr_when_str_empty — empty-message exceptions fall back to repr(exc)
  • test_exc_str_falls_back_to_repr_for_whitespace_only — whitespace-only messages trigger fallback
  • test_exc_str_handles_closedresource_like_exception — simulates anyio.ClosedResourceError behavior
  • test_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 message

Testing

All 23 MCP-related tests pass (6 new + 17 existing).

Fixes #19417

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P3 Low — cosmetic, nice to have tool/mcp MCP client and OAuth type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: MCP tool calls fail with ClosedResourceError and empty error message

2 participants