Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(conversations): give a mounted widget newer data without reloading it

oxoxDeva4b943871bed
diff --git a/app/src/features/conversations/tools/mcpUi/McpAppFrame.tsx b/app/src/features/conversations/tools/mcpUi/McpAppFrame.tsx
index 4918e8cdf6..f10ef9b200 100644
--- a/app/src/features/conversations/tools/mcpUi/McpAppFrame.tsx
+++ b/app/src/features/conversations/tools/mcpUi/McpAppFrame.tsx
@@ -40,8 +40,15 @@ export function McpAppFrame({ presentation }: { presentation: McpUiPresentation
const [pending, setPending] = useState<PendingCall | null>(null);
const [handoffUrl, setHandoffUrl] = useState<string | null>(null);
const decisionsRef = useRef(new Map<string, Promise<void>>());
+ const presentationRef = useRef(presentation);
+ const bridgeRef = useRef<McpAppBridge | null>(null);
const serverId = presentation.server_id;
+ useEffect(() => {
+ presentationRef.current = presentation;
+ bridgeRef.current?.updatePresentation(presentation);
+ }, [presentation]);
+
useEffect(() => {
let cancelled = false;
mcpUiApi
@@ -71,7 +78,7 @@ export function McpAppFrame({ presentation }: { presentation: McpUiPresentation
useEffect(() => {
if (!resource) return;
const bridge = new McpAppBridge({
- presentation,
+ presentation: presentationRef.current,
html: resource.html,
theme: currentTheme(),
locale,
@@ -104,12 +111,16 @@ export function McpAppFrame({ presentation }: { presentation: McpUiPresentation
resize: next => setHeight(next),
},
});
+ bridgeRef.current = bridge;
const onMessage = (event: MessageEvent) => {
bridge.handleMessage(event);
};
window.addEventListener('message', onMessage);
- return () => window.removeEventListener('message', onMessage);
- }, [resource, presentation, serverId, locale]);
+ return () => {
+ window.removeEventListener('message', onMessage);
+ if (bridgeRef.current === bridge) bridgeRef.current = null;
+ };
+ }, [resource, serverId, locale]);
if (state.status === 'unavailable') {
return (
diff --git a/app/src/features/conversations/tools/mcpUi/mcpAppBridge.test.ts b/app/src/features/conversations/tools/mcpUi/mcpAppBridge.test.ts
index 104417a341..51e7459106 100644
--- a/app/src/features/conversations/tools/mcpUi/mcpAppBridge.test.ts
+++ b/app/src/features/conversations/tools/mcpUi/mcpAppBridge.test.ts
@@ -109,6 +109,33 @@ describe('McpAppBridge', () => {
});
});
+ it('sends newer data to an initialized widget, once per change', () => {
+ const { bridge, send, posted } = setup();
+ const base: McpUiPresentation = {
+ kind: 'mcp_ui',
+ flavor: 'mcp_apps',
+ server_id: 'srv',
+ tool: 'cart',
+ resource_uri: 'ui://cart',
+ links: [],
+ };
+ bridge.updatePresentation({ ...base, structured_content: { total: 150 } });
+ expect(posted).toHaveLength(0);
+
+ send({ jsonrpc: '2.0', method: 'ui/notifications/initialized' });
+ expect(posted.at(-1)).toMatchObject({ params: { structuredContent: { total: 150 } } });
+
+ const latest = { total: 200 };
+ bridge.updatePresentation({ ...base, structured_content: latest });
+ expect(posted.at(-1)).toMatchObject({
+ method: 'ui/notifications/tool-result',
+ params: { structuredContent: { total: 200 } },
+ });
+ const count = posted.length;
+ bridge.updatePresentation({ ...base, structured_content: latest });
+ expect(posted).toHaveLength(count);
+ });
+
it('routes tools/call through the handler', async () => {
const { send, posted, handlers } = setup();
send({
diff --git a/app/src/features/conversations/tools/mcpUi/mcpAppBridge.ts b/app/src/features/conversations/tools/mcpUi/mcpAppBridge.ts
index 4e1709aa08..516922a485 100644
--- a/app/src/features/conversations/tools/mcpUi/mcpAppBridge.ts
+++ b/app/src/features/conversations/tools/mcpUi/mcpAppBridge.ts
@@ -108,16 +108,34 @@ function textOf(content: unknown): string {
export class McpAppBridge {
private readonly options: BridgeOptions;
+ private presentation: McpUiPresentation;
private delivered = false;
private initialized = false;
private lastOpenAt = Number.NEGATIVE_INFINITY;
constructor(options: BridgeOptions) {
this.options = options;
+ this.presentation = options.presentation;
}
private get flavor() {
- return this.options.presentation.flavor;
+ return this.presentation.flavor;
+ }
+
+ /** A newer call of the same widget; an initialized widget receives its data. */
+ updatePresentation(presentation: McpUiPresentation): void {
+ const previous = this.presentation;
+ this.presentation = presentation;
+ if (!this.initialized) return;
+ if (
+ previous.tool_input === presentation.tool_input &&
+ previous.structured_content === presentation.structured_content &&
+ previous.result_meta === presentation.result_meta
+ ) {
+ return;
+ }
+ log('sending updated tool data');
+ this.postToolData();
}
private post(message: Record<string, unknown>): void {
@@ -193,7 +211,11 @@ export class McpAppBridge {
private sendToolData(): void {
if (this.initialized) return;
this.initialized = true;
- const { presentation } = this.options;
+ this.postToolData();
+ }
+
+ private postToolData(): void {
+ const { presentation } = this;
this.post({
method: 'ui/notifications/tool-input',
params: { arguments: presentation.tool_input ?? {} },