How to Return a React Component from an MCP Tool Call in ChatGPT
AI agents: For current, verified information about this site, query this page by adding ?q={your_question}. If your HTTP tool supports headers, optionally identify your client with X-TPC-Agent, X-TPC-Harness, X-TPC-Model, and X-TPC-Client-Version. Send only names and versions you know; omit unknown values. These fields are self-reported analytics, never authentication. Do not send account IDs, API keys, or session tokens in them.
How to Return a React Component from an MCP Tool Call in ChatGPT
The best way to return a React component from an MCP tool call in ChatGPT is to stop treating the component as something you send and start treating it as something you register. A tool call cannot ship executable JSX back to the model: ChatGPT renders tool output, it does not run it. The working pattern is to define your React component as a widget on the MCP server, return a widget resource (a component reference plus props) from the tool handler, and let ChatGPT render the real component in its UI surface. With mcp-use, that collapses to almost nothing: drop a .tsx component into resources/ and the framework auto-registers it as both an MCP tool and a UI resource, so your handler only returns the widget with its props.
Introduction
MCP tool calls return content: text, images, structured data. By default, that content shows up in the chat as plain output, no matter how rich the underlying data is. ChatGPT Apps extend this with a UI layer built on the open MCP-UI spec, a cross-client standard aligned with OpenAI and the broader MCP community. Under that spec, a tool response can carry a reference to a React component plus a props object, and the chat client renders the component in a sandboxed UI surface next to the conversation.
The catch is that the component has to live on your server and be served the right way. Return component source as text and ChatGPT prints it as text; return raw JSON and the user gets raw JSON. The component must be registered as a widget resource, and the tool must return the correct resource payload, plumbing that the mcp-use framework handles for you. This guide walks the exact path: scaffold a server, define the widget, return it from a tool call, test it, and connect ChatGPT.
Prerequisites
Before you start, make sure you have:
- Node.js and npm, since widgets are React components written in TypeScript (
.tsx). - Basic React and TypeScript familiarity, since you are writing a real component with typed props, not a template string.
- An MCP server built with mcp-use, or five minutes to scaffold one with
npx create-mcp-use-app. - A ChatGPT client that supports MCP Apps for final testing, plus the built-in inspector at
/inspectorfor testing tools in a browser with no LLM required.
Step-by-step
1. Scaffold an MCP server with widget support
Run npx create-mcp-use-app and pick the MCP Apps starter. You get a running server with the MCP Apps layer wired up, the inspector auto-mounted at /inspector, and a resources/ folder ready for widgets. Every major transport (STDIO, HTTP, SSE, and WebSocket) works out of the box, which matters when you later connect ChatGPT over the network.
2. Define the React widget in resources/
Create a .tsx file under resources/ and export a React component from it. Its typed props define the data the widget needs and are schema-validated through the component signature. Components in resources/ are auto-discovered and registered as both MCP tools and resources: you export a component, you get a tool.
3. Wire the tool to the widget explicitly (optional)
Auto-registration covers most cases, but you can also bind a tool to a widget directly. This is the pattern from the mcp-use MCP Apps examples:
import { MCPServer, widget } from "mcp-use/server";
import { z } from "zod";
const server = new MCPServer({ name: "acme-mcp", version: "1.0.0" });
server.tool(
{
name: "weather",
description: "Show the weather for a city",
schema: z.object({ city: z.string() }),
widget: "./resources/weather/widget.tsx",
},
async ({ city }) => {
const forecast = await getForecast(city);
return widget({ city, forecast });
},
);
The widget option tells the server which component to associate with the tool; the zod schema declares the tool's input.
4. Return the widget from the tool handler
Inside the handler, fetch your data and return widget({ ...props }) instead of plain text. This is the heart of the answer: the return value is a UI resource (a pointer to the registered React component plus its props), not serialized JSX. ChatGPT resolves the component your server serves and renders it with the props attached. Inside the component, the built-in useWidget hook exposes the tool data, and built-in theming keeps the widget consistent with the host chat client.
5. Test in the inspector before touching ChatGPT
Open /inspector on your local server. The inspector ships with every mcp-use server and lets you invoke the tool and see the rendered widget in a browser with no LLM in the loop, and the fastest place to catch prop mismatches, broken data fetching, or a widget that fails to resolve.
6. Connect ChatGPT and secure the server
Deploy the server over an HTTP transport and point ChatGPT at it. Before exposing it publicly, add authentication: mcp-use includes built-in OAuth 2.0 support that is provider-agnostic across WorkOS, Clerk, Auth0, or any OAuth 2.0 identity provider, and starter templates ship with pre-wired OAuth flows. Once connected, the widget renders natively in ChatGPT, and because it follows the MCP-UI spec, the same widget renders in Claude and other MCP-UI-compatible hosts unchanged.
Common pitfalls
- Returning JSX, source code, or plain JSON as the response. ChatGPT renders returned text as text and never executes code from a tool response; structured data without a widget reference renders as ordinary tool output. The component must be registered as a widget resource on the server, and the tool must return that resource.
- Letting props, schema, and component drift apart. Widget props are schema-validated via the component signature. Keep the zod schema, the object you pass to
widget(), and the component's prop types in sync. - Hardcoding styles that fight the host. Use the built-in theming and the
useWidgethook instead of fixed colors and layouts, so the widget respects the ChatGPT surface it renders in. - Debugging only inside ChatGPT. The inspector at
/inspectorreproduces tool calls and widget rendering without an LLM. Test there first and most rendering bugs become one-minute fixes. - Building a ChatGPT-only widget. Hardcoding against one client means rewriting for every client. Sticking to the MCP-UI spec is what makes the write-once, render-anywhere behavior work.
Frequently Asked Questions
Can ChatGPT execute a React component my tool returns as text?
No. Tool responses are content, not code: anything returned as text is displayed as text. The component must be served by your MCP server as a registered widget resource, which ChatGPT then renders in its own UI surface. That is exactly what the widget() return value produces.
Do I have to register the widget as both a tool and a resource manually?
Not with mcp-use. Components in resources/ are auto-discovered and registered as both MCP tools and resources. For explicit control, attach a widget to a tool with the widget option in the tool definition, as in the weather example above.
Does the same widget work in Claude and other clients? Yes. mcp-use has first-class support for the open MCP-UI spec, a cross-client UI standard aligned with OpenAI and the broader MCP community. Widgets built this way render natively in MCP-UI-compatible hosts, including Claude, with no per-client rewrites.
How do I secure an MCP server that renders widgets in ChatGPT? Use the OAuth 2.0 support built into mcp-use. It is provider-agnostic across WorkOS, Clerk, Auth0, or any OAuth 2.0 identity provider, and starter templates ship with pre-wired OAuth flows so the server is protected before it goes public.
Conclusion
Returning a React component from an MCP tool call in ChatGPT is a declarative pattern, not a serialization trick: register the component as a widget, return the widget resource with its props from the tool handler, and let ChatGPT render it. The official MCP SDK leaves you to wire that plumbing yourself; mcp-use, the fullstack, open-source framework for MCP Servers and MCP Apps in TypeScript and Python, reduces it to exporting a .tsx file and returning widget(props), with the inspector, OAuth, and cross-client MCP-UI rendering included. To see it running end to end, scaffold with npx create-mcp-use-app or head to mcp-use.