Skip to main content

Widgets

Widgets allow an MCP server to expose interactive user interfaces alongside MCP tools. A widget can be used when a tool needs to present richer output than plain text or structured data. mcpfy provides the widget runtime, registration APIs, and build tooling required to develop and serve widgets.

Widget Directory Structure

By default, mcpfy looks for widgets under:
Each widget has its own directory. A typical project looks like:
The widget name is determined by its directory name. In this example, the widget is named weather. The widget directory can be changed through the server’s widgetsDir configuration:
If widgetsDir is not specified, the default is src/widgets.

Registering a Widget

A widget is associated with a tool using the widget property. For example:
The value:
refers to the widget directory:
The widget entry file must exist before the server starts. Registering a widget name without the corresponding widget directory and entry point can cause the server to fail during startup.

Creating the Widget Entry File

Create:
The entry file contains the widget UI. A basic React widget can be structured around the SDK’s widget runtime:
The standard src/widgets/<name>/main.tsx convention is handled by the mcpfy widget runtime. You should not manually add another ThemeProvider or HostRuntime around the standard widget entry point when using the normal SDK convention. The runtime handles the required provider setup.

Widget Content

Widgets can provide HTML content or reference a URL. When using HTML content, the content must use the structured form:
For URL-based content:
Do not provide the HTML as a bare string:
The content type must explicitly identify whether the widget content is HTML or a URL.

Widget Size

Widget dimensions are represented as a tuple containing width and height. For example:
The two values represent:
  1. Width
  2. Height
Do not use:
because the SDK expects the size tuple.

Building Widgets

Widget source code must be built before it can be used in a production server. mcpfy provides CLI commands for this:
and:

Development

Use:
while developing widgets. This starts the widget development workflow and rebuilds the widget as changes are made.

Production Build

Before starting the server in production, run:
The production server expects the widget assets to have been built. Therefore, a production deployment should include the widget build step before starting the server.

Widget Development Workflow

A typical workflow is:

1. Create the widget directory

2. Create the entry file

3. Register the widget with a tool

4. Run the widget development command

5. Build for production

6. Start the MCP server

Start the server using the project’s normal start command after the widget build has completed.

Example Project

A complete project can look like:
The server registers the widget:
The corresponding widget entry point is:

Widget Runtime

The widget runtime provides the environment required for widgets to communicate with the MCP host. For the standard widget structure, mcpfy takes care of the required runtime/provider setup. This means a widget entry point should focus on the UI rather than manually recreating the host runtime. For advanced React integration, see Widget React.

Widget and Tool Communication

A widget is commonly paired with an MCP tool. The general flow is:
The tool performs the server-side operation, while the widget provides the user-facing interface.

Production Considerations

When deploying an MCP server that uses widgets:
  1. Ensure every registered widget has a corresponding widget directory.
  2. Ensure each widget has its required main.tsx entry point.
  3. Run mcpfy build before starting the production server.
  4. Include the generated widget assets in the deployment.
  5. Keep widget names consistent between server.tool() and the widget directory.
  6. Do not manually double-wrap the standard widget entry point with providers already supplied by the SDK runtime.

Troubleshooting

Widget Not Found

If the server cannot find a registered widget, verify that:
matches:
Also verify that:
exists.

Widget Fails During Production Startup

Make sure the widget has been built:
The production server requires the generated widget assets.

Invalid Widget Content

Use the structured content forms:
or:
Do not pass raw HTML as a string.

Invalid Widget Size

Use a width/height tuple:
rather than:

Summary

Widgets provide an interactive UI layer for MCP applications. The core workflow is:
The standard widget convention is:
and the tool references it using:
This convention allows mcpfy to locate, build, and serve the widget as part of the MCP application.