Skip to content

fix: include root cause in MCPClientInitializationError message#2238

Open
aidandaly24 wants to merge 1 commit intostrands-agents:mainfrom
aidandaly24:fix/mcp-client-error-message
Open

fix: include root cause in MCPClientInitializationError message#2238
aidandaly24 wants to merge 1 commit intostrands-agents:mainfrom
aidandaly24:fix/mcp-client-error-message

Conversation

@aidandaly24
Copy link
Copy Markdown

@aidandaly24 aidandaly24 commented May 1, 2026

Description

When MCPClient.start() fails due to a transport or auth error (e.g., OAuth token exchange failure, connection refused), the MCPClientInitializationError thrown contains a generic message "the client initialization failed" that hides the root cause. The original exception is preserved in __cause__ (via from e) but not in str().

In managed environments like AgentCore Harness, the error message is surfaced through a streaming event and the full stack trace is not available to the user. This makes it impossible to diagnose the root cause.

This PR includes str(e) in the error message so the root cause is visible:

Before: MCPClientInitializationError: the client initialization failed
After: MCPClientInitializationError: the client initialization failed: Failed to get OAuth token for Gateway (provider=my-provider): Token has expired

The change is at src/strands/tools/mcp/mcp_client.py line 223.

Related Issues

Fixes #2237

Documentation PR

N/A — no documentation changes needed.

Type of Change

Bug fix

Testing

How have you tested the change?

  • Ran full MCP test suite: 142 passed, 0 failed

  • Updated test_enter_with_initialization_exception to verify the root cause message is included in the error

  • Verified no warnings introduced in the MCP module

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

When MCPClient.start() fails due to a transport or auth error, the
exception message was a generic "the client initialization failed"
that hid the actual root cause. The original exception was preserved
in the __cause__ chain but not in the string representation, making
it difficult to diagnose failures in managed environments where only
the error message is visible (e.g., streaming event responses).

Include str(e) in the error message so the root cause is visible
without needing a full stack trace.

Fixes strands-agents#2237
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] MCPClient.start() swallows root cause exception in MCPClientInitializationError

1 participant