MCPfy SDK Widget React
mcpfy-sdk/widget provides React components and hooks for building interactive MCP widgets that can run across supported MCP hosts.
The React runtime abstracts host-specific communication so the same widget can work with protocols such as MCP Apps, ChatGPT Apps SDK, and MCP-UI where supported.
Installation
The React widget APIs are exported from themcpfy-sdk/widget entry point.
react and react-dom are peer dependencies of the SDK and must be available in the application using the React widget package.
When your widget code imports zod for schemas or related types, install zod explicitly as well; it is a peer dependency used by the SDK APIs.
Standard Widget Entry versus Manual Runtime
When using the standard convention-based entry point, create the widget atsrc/widgets/<name>/main.tsx. The mcpfy widget build/runtime pipeline automatically
provides the required ThemeProvider and HostRuntime wrapping for that entry point.
Do not add another ThemeProvider or HostRuntime in the standard entry file.
The examples below show advanced standalone/manual runtime usage. Use this pattern only
when you are mounting the React tree yourself outside the standard widget-entry pipeline.
Advanced standalone structure
HostRuntime establishes the connection between the React application and the MCP host and makes the runtime available to the widget hooks.
HostRuntime
Props
HostRuntime is required when using hooks that depend on the MCPfy widget runtime.
ThemeProvider
ThemeProvider manages the widget theme and initially detects the user’s preferred color scheme.
useHostContext
Returns information about the current MCP host.useHostProtocol
Returns the protocol currently being used by the widget."iframe" is an internal implementation detail and should not be treated as a value returned by useHostProtocol().
The hook can also detect the environment when it is used outside an active runtime context.
useToolPayload
Returns the input and output associated with the widget’s MCP tool.useCallTool
Calls another MCP tool from the widget. There are two supported forms.Function form
Named-tool form
Typed Tool Calls
Widgets can optionally provide compile-time types for tools through module augmentation.useCallTool can then use those types:
useLinkedTool
Returns the tool associated with the current mounted widget.useSendFollowUp
Sends a follow-up message to the host/model.useOpenExternal
Opens an external URL through the host.useLayoutMode
Provides the current widget display mode and allows the widget to request a different mode.useHostTheme
Returns the current widget theme.useWidgetState
Provides access to state that can be persisted by the host.useViewState
useViewState combines local React state with host-persisted widget state and model context.
- Updates the local React state.
- Persists the state through the host when supported.
- Publishes the state as model context when supported.
useViewState useful for interactive widgets whose state should remain available to both the UI and the model.
useModelContext
Provides access to model-context publishing.structuredContent can be used when structured application state needs to be made available to the model.
useViewTool
Registers a tool that can be called by the host/model while the widget is mounted.Host Capabilities
Widgets can inspect host capabilities throughuseHostContext():
HostImage
HostImage is a small image component that sets a safer default referrer policy for widget images.
referrerPolicy, HostImage defaults it to:
referrerPolicy is preserved.
Complete Example
API Summary
Best Practices
Use HostRuntime at the widget root
Hooks such as useToolPayload, useCallTool, useHostContext, and useViewState depend on the runtime.

