#5329·fastmcp

ErrorHandlingMiddleware replaces a re-raised error's cause with the error itself

Author: asasemahmedCreated Sep 28, 2026Updated Sep 28, 2026
Labelsbugserver

What happened?

When ErrorHandlingMiddleware re-raises an error without transforming it (with transform_errors=False, or for any MCPError), the error's __cause__ becomes the error itself, and the original exception is lost to everything outside the middleware. For example, a RetryMiddleware added before it stops retrying a tool that fails with ConnectionError.

Example Code

python
import asyncio

from fastmcp import FastMCP
from fastmcp.server.middleware import Middleware
from fastmcp.server.middleware.error_handling import ErrorHandlingMiddleware


class CaptureError(Middleware):
    async def on_call_tool(self, context, call_next):
        try:
            return await call_next(context)
        except Exception as error:
            print(f"{type(error).__name__}: {error}")
            print(f"__cause__: {error.__cause__!r}")
            print(f"cause is error itself: {error.__cause__ is error}")
            raise


mcp = FastMCP("demo")
mcp.add_middleware(CaptureError())
mcp.add_middleware(ErrorHandlingMiddleware(transform_errors=False))


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


async def main():
    try:
        await mcp.call_tool("fetch", {})
    except Exception:
        pass


asyncio.run(main())

Output:

ToolError: Error calling tool 'fetch': upstream unreachable
__cause__: ToolError("Error calling tool 'fetch': upstream unreachable")
cause is error itself: True

Version Information

FastMCP version:                                          4.0.11.dev11+89b00b7f
MCP version:                                                              2.0.0
Python version:                                                         3.12.14
Platform:                                             Windows-11-10.0.26200-SP0