View the source code for this module on GitHub: https://github.com/mcp-use/mcp-use/blob/main/libraries/typescript/packages/mcp-use/src/react/ErrorBoundary.tsx
ErrorBoundary is a React class component that catches JavaScript errors anywhere in its child component tree, logs those errors, and renders a fallback UI instead of crashing the entire widget. It extends React.Component and implements getDerivedStateFromError and componentDidCatch, so it stops error propagation at the boundary and keeps the rest of the host application alive.
ErrorBoundary
Catches React errors in the child component tree, logs them, and displays a fallback UI instead of crashing the entire widget.{ hasError: true, error }) and renders the fallback instead of the children.
Behavior on a caught error:
- It calls the static
getDerivedStateFromError(error)to flip its state tohasError: trueand store theerror. - In
componentDidCatch(error, errorInfo)it logs the error through the package logger as"Widget Error:"(logger name"ErrorBoundary"), then invokes the optionalonErrorcallback. Logging always happens whether or notonErroris provided. - If a
fallbackprop is provided (any value other thanundefined), it is rendered. A functionfallbackis called with the caughtErrorand its return value is rendered; a non-functionfallbackis rendered directly. - If no
fallbackis provided, it renders the built-in error card: a red-bordered, red-background container with a bold “Widget Error” heading and theerror.messageshown in apreblock withwhitespace-pre-wrap. The card includes dark mode classes (dark:bg-red-900/20 dark:text-red-100) so it adapts to the host theme.
setTimeout or promise callbacks), server-side rendering, or errors thrown in the boundary itself.
Props
SignatureThe widget content to wrap. Rendered as-is while no error has been caught.Custom fallback UI to render when an error is caught. Pass a static node, or a function that receives the caughtErrorand returns a node. When omitted (undefined), the built-in red error card is shown instead.Callback invoked when an error is caught, after the error is logged. Receives theErrorand React’serrorInfo(which includescomponentStack). Useful for reporting to an analytics or monitoring service.
Basic usage
Wrap any widget subtree to contain its render errors. While no error occurs, the children render normally.Custom fallback
Pass afallback prop to replace the default red error card. A static node renders directly.
Error and render details from it.
Error callback
UseonError to run a side effect (for example, reporting to analytics) when an error is caught. The callback receives the Error and React’s errorInfo, whose componentStack describes where the error originated. Logging to the console happens regardless of whether onError is provided.
fallback and onError can be combined: the fallback controls what the user sees, while the callback handles the side effect.
Automatic inclusion
ErrorBoundary is included automatically when you use McpUseProvider, so you usually do not need to mount it yourself. The provider wraps your widget content with the boundary as the innermost wrapper, ensuring all render errors in the subtree are caught.
Default error display
When nofallback is provided and an error is caught, the boundary renders a built-in card showing what went wrong:
- A red-bordered, red-background error container.
- A bold “Widget Error” heading.
- The
error.messagerendered in apreblock withwhitespace-pre-wrapso multi-line messages stay readable. - Dark mode classes so the card adapts to the host theme.
Related
McpUseProviderincludesErrorBoundaryautomatically.useWidgetis the widget hook for accessing props and actions.