Skip to content

Commit b2fa435

Browse files
committed
waiting for openai mount
1 parent 2319254 commit b2fa435

File tree

3 files changed

+57
-19
lines changed

3 files changed

+57
-19
lines changed

src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ app.get("/", (req, res) => {
193193
`);
194194
});
195195

196-
app.listen(PORT, async () => {
196+
app.listen(PORT, '0.0.0.0', async () => {
197197
const TOOL_MODE = (process.env.TOOL_MODE || "basic").toLowerCase();
198198
console.log(`🚀 MCP server listening on http://localhost:${PORT}/mcp`);
199199
console.log(`📦 Tool mode: ${TOOL_MODE}`);

src/tools/ui-react.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,17 @@ export function registerTools(mcp: McpServer) {
3838
<meta charset="utf-8">
3939
<meta name="viewport" content="width=device-width, initial-scale=1">
4040
<title>Greet</title>
41-
</head>
42-
<body>
43-
<div id="root"></div>
4441
<script type="importmap">
4542
{
4643
"imports": {
4744
"react": "https://esm.sh/react@18",
48-
"react-dom": "https://esm.sh/react-dom@18",
4945
"react-dom/client": "https://esm.sh/react-dom@18/client"
5046
}
5147
}
5248
</script>
49+
</head>
50+
<body>
51+
<div id="root"></div>
5352
<script type="module">
5453
${greetBundle || 'console.error("React bundle not loaded. Run npm run build:web");'}
5554
</script>

web/src/components/greet.tsx

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,33 @@ export function GreetWidget() {
3535
const [greeting, setGreeting] = useState('');
3636
const [isLoading, setIsLoading] = useState(false);
3737

38-
// Hydrate from initial tool output
38+
// Hydrate from initial tool output and listen for updates
3939
useEffect(() => {
40-
const toolOutput = (window.openai?.toolOutput as ToolOutput) || {};
41-
if (toolOutput.greeting) {
42-
setGreeting(toolOutput.greeting);
43-
}
44-
if (toolOutput.name) {
45-
setName(toolOutput.name);
46-
}
40+
const updateFromToolOutput = () => {
41+
const toolOutput = (window.openai?.toolOutput as ToolOutput) || {};
42+
if (toolOutput.greeting) {
43+
setGreeting(toolOutput.greeting);
44+
}
45+
if (toolOutput.name) {
46+
setName(toolOutput.name);
47+
}
48+
};
49+
50+
// Initial load
51+
updateFromToolOutput();
52+
53+
// Listen for updates from the host
54+
const handleSetGlobals = (event: Event) => {
55+
const customEvent = event as CustomEvent<{ globals?: Partial<{ toolOutput?: ToolOutput }> }>;
56+
if (customEvent.detail?.globals?.toolOutput !== undefined) {
57+
updateFromToolOutput();
58+
}
59+
};
60+
61+
window.addEventListener('openai:set_globals', handleSetGlobals);
62+
return () => {
63+
window.removeEventListener('openai:set_globals', handleSetGlobals);
64+
};
4765
}, []);
4866

4967
const handleGreet = async () => {
@@ -150,11 +168,32 @@ export function GreetWidget() {
150168
);
151169
}
152170

153-
// Mount the component when used standalone (for MCP server build)
154-
if (typeof window !== 'undefined' && !document.getElementById('root')) {
155-
const root = document.createElement('div');
156-
document.body.appendChild(root);
157-
const reactRoot = createRoot(root);
158-
reactRoot.render(<GreetWidget />);
171+
// Wait for window.openai to be available before mounting
172+
function waitForOpenAI() {
173+
if (window.openai) {
174+
mountComponent();
175+
} else {
176+
// Listen for the openai:set_globals event
177+
window.addEventListener('openai:set_globals', mountComponent, { once: true });
178+
// Fallback: check periodically (in case event doesn't fire)
179+
const checkInterval = setInterval(() => {
180+
if (window.openai) {
181+
clearInterval(checkInterval);
182+
mountComponent();
183+
}
184+
}, 50);
185+
// Cleanup after 5 seconds if still not available
186+
setTimeout(() => clearInterval(checkInterval), 5000);
187+
}
159188
}
160189

190+
function mountComponent() {
191+
const root = document.getElementById('root');
192+
if (root) {
193+
const reactRoot = createRoot(root);
194+
reactRoot.render(<GreetWidget />);
195+
}
196+
}
197+
198+
waitForOpenAI();
199+

0 commit comments

Comments
 (0)