Skip to content

Fix self-referential connection error causes - #4720

Merged
jakekaplan merged 1 commit into
mainfrom
fix/client-connection-self-cause
Jul 30, 2026
Merged

jakekaplan merged 1 commit into
mainfrom
fix/client-connection-self-cause

Conversation

@jakekaplan

Copy link
Copy Markdown
Collaborator

Client._connect() unconditionally chained normalized connection failures, causing pass-through MCPError and HTTPStatusError instances to become their own __cause__. Cause-chain traversal could therefore loop indefinitely after an upstream HTTP failure.

Preserved exceptions are now raised directly, while newly created RuntimeError wrappers retain the original failure as their cause:

failure = _connection_failure(exception)
if failure is exception:
    raise exception
raise failure from exception

🤖 Generated with OpenAI Codex

🤖 Generated with OpenAI Codex
@marvin-context-protocol marvin-context-protocol Bot added bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. client Related to the FastMCP client SDK or client-side functionality. labels Jul 30, 2026
@jakekaplan
jakekaplan marked this pull request as ready for review July 30, 2026 18:46
@jakekaplan
jakekaplan requested review from jlowin and zzstoatzz July 30, 2026 18:48
@jakekaplan
jakekaplan merged commit bc07264 into main Jul 30, 2026
23 checks passed
@jakekaplan
jakekaplan deleted the fix/client-connection-self-cause branch July 30, 2026 20:23
jlowin pushed a commit that referenced this pull request Oct 5, 2026
…or unchanged (#5344)

`ErrorHandlingMiddleware.on_message` always re-raised with `raise transformed_error from error`. `_transform_error` returns the same exception for every `MCPError`, and for every error when `transform_errors=False`. In those cases the error became its own `__cause__`, and the original cause was lost to everything further out. A `RetryMiddleware` added before it could no longer see the `ConnectionError` behind a `ToolError`, so it stopped retrying. Anything walking the exception chain found a loop instead of the real exception.

An untransformed error is now re-raised as-is and keeps its original cause. Transformed errors still chain from the error they replace. #4720 fixed the same pattern for the client's connection errors.

```python
mcp.add_middleware(RetryMiddleware(retry_exceptions=(ConnectionError,)))
mcp.add_middleware(ErrorHandlingMiddleware(transform_errors=False))

@mcp.tool
def fetch() -> str:
    raise ConnectionError("upstream unreachable")

# before: the ToolError's __cause__ is the ToolError itself, so the call is never retried
# after:  its __cause__ is the ConnectionError, and RetryMiddleware retries the call
```

Fixes #5329.

Co-authored-by: asemabdallah <asasem547@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. client Related to the FastMCP client SDK or client-side functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants