Skip to main content

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 the mcpfy-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 at src/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.
The provider exposes the current theme to the widget runtime. Supported themes:
The theme is also written to:
or:

useHostContext

Returns information about the current MCP host.
The returned value has the following shape:
This is useful when a widget needs to adapt its UI or behavior according to the host.

useHostProtocol

Returns the protocol currently being used by the widget.
The hook returns the protocol exposed by the widget runtime. The supported returned values are:
"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.
The returned structure is:
Example:

useCallTool

Calls another MCP tool from the widget. There are two supported forms.

Function form

Named-tool form

The named form returns a handle with the following shape:
The generic parameters allow the tool’s argument and result types to be represented when typed tool mappings are available. The actual communication is delegated to the active host. Depending on the environment, mcpfy uses the available Apps SDK, MCP Apps, or host messaging mechanism.

Typed Tool Calls

Widgets can optionally provide compile-time types for tools through module augmentation.
The named form of useCallTool can then use those types:
This allows widget code to remain strongly typed without changing the runtime protocol.

useLinkedTool

Returns the tool associated with the current mounted widget.
The returned value is:
This is useful when the widget needs to call its own associated tool again.

useSendFollowUp

Sends a follow-up message to the host/model.
Depending on the host, mcpfy delegates the operation to the corresponding host API or messaging mechanism.

useOpenExternal

Opens an external URL through the host.
When supported, the host controls how the external link is opened.

useLayoutMode

Provides the current widget display mode and allows the widget to request a different mode.
Request a new layout:
The returned object is:
The requested mode depends on the capabilities provided by the current host.

useHostTheme

Returns the current widget theme.
Possible values:
The hook considers both the active host runtime and the local theme provider.

useWidgetState

Provides access to state that can be persisted by the host.
The returned value is:
When supported by the host, the state can survive widget updates or remounts.

useViewState

useViewState combines local React state with host-persisted widget state and model context.
Update the state:
When the state changes, mcpfy:
  1. Updates the local React state.
  2. Persists the state through the host when supported.
  3. Publishes the state as model context when supported.
This makes useViewState useful for interactive widgets whose state should remain available to both the UI and the model.

useModelContext

Provides access to model-context publishing.
Publish context:
The returned value is:
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.
The definition supports:
The handler receives the tool arguments:
View tools are supported where the host exposes the required capability. On unsupported environments, the registration becomes a no-op.

Host Capabilities

Widgets can inspect host capabilities through useHostContext():
Capabilities are derived from the active protocol and host. This allows widgets to progressively enhance their behavior rather than assuming that every MCP host supports every feature.

HostImage

HostImage is a small image component that sets a safer default referrer policy for widget images.
If the caller does not provide a referrerPolicy, HostImage defaults it to:
A caller-provided 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.

Check capabilities before using optional features

Do not assume every host supports every API

mcpfy provides host adapters so widgets can work across multiple environments. Optional capabilities should therefore be detected rather than assumed.

Keep widget state serializable

Widget state is represented as:
Prefer simple serializable values such as strings, numbers, booleans, arrays, and objects.

Keep host-specific logic out of UI components

Prefer:
over directly checking browser globals or implementing protocol-specific messaging inside individual components. This keeps the widget portable across supported MCP hosts.