Skip to content

Commit b16880d

Browse files
ochafikclaude
andcommitted
feat(example): wire tool-cancelled and teardown in basic-host
Update basic-host example to properly use the new notifications: - Send sendToolCancelled when tool call promise rejects - Send sendResourceTeardown when React component unmounts - Store appBridge ref for cleanup access 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 8d79f87 commit b16880d

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

examples/basic-host/src/implementation.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,19 @@ export async function initializeApp(
186186
log.info("Sending tool call input to MCP App:", input);
187187
appBridge.sendToolInput({ arguments: input });
188188

189-
// Schedule tool call result to be sent to MCP App
190-
resultPromise.then((result) => {
191-
log.info("Sending tool call result to MCP App:", result);
192-
appBridge.sendToolResult(result);
193-
});
189+
// Schedule tool call result (or cancellation) to be sent to MCP App
190+
resultPromise.then(
191+
(result) => {
192+
log.info("Sending tool call result to MCP App:", result);
193+
appBridge.sendToolResult(result);
194+
},
195+
(error) => {
196+
log.error("Tool call failed, sending cancellation to MCP App:", error);
197+
appBridge.sendToolCancelled({
198+
reason: error instanceof Error ? error.message : String(error),
199+
});
200+
},
201+
);
194202
}
195203

196204
/**

examples/basic-host/src/index.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ interface AppIFramePanelProps {
176176
}
177177
function AppIFramePanel({ toolCallInfo }: AppIFramePanelProps) {
178178
const iframeRef = useRef<HTMLIFrameElement | null>(null);
179+
const appBridgeRef = useRef<ReturnType<typeof newAppBridge> | null>(null);
179180

180181
useEffect(() => {
181182
const iframe = iframeRef.current!;
@@ -186,9 +187,20 @@ function AppIFramePanel({ toolCallInfo }: AppIFramePanelProps) {
186187
// `toolCallInfo`.
187188
if (firstTime) {
188189
const appBridge = newAppBridge(toolCallInfo.serverInfo, iframe);
190+
appBridgeRef.current = appBridge;
189191
initializeApp(iframe, appBridge, toolCallInfo);
190192
}
191193
});
194+
195+
// Cleanup: send teardown notification before unmounting
196+
return () => {
197+
if (appBridgeRef.current) {
198+
log.info("Sending teardown notification to MCP App");
199+
appBridgeRef.current.sendResourceTeardown({}).catch((err) => {
200+
log.warn("Teardown request failed (app may have already closed):", err);
201+
});
202+
}
203+
};
192204
}, [toolCallInfo]);
193205

194206
return (

0 commit comments

Comments
 (0)