Skip to content

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

fix(mcp-ui): queue concurrent tool confirmations instead of overwriting

oxoxDev7bfe869d4745
diff --git a/app/src/features/conversations/tools/mcpUi/McpAppFrame.test.tsx b/app/src/features/conversations/tools/mcpUi/McpAppFrame.test.tsx
index 91d55d7196..3ba6bdb480 100644
--- a/app/src/features/conversations/tools/mcpUi/McpAppFrame.test.tsx
+++ b/app/src/features/conversations/tools/mcpUi/McpAppFrame.test.tsx
@@ -130,6 +130,50 @@ describe('McpAppFrame tool decisions', () => {
expect(api.toolCall).not.toHaveBeenCalledWith('shop', 'place_order', { item: 'laptop' }, true);
});
+ it('queues concurrent confirmations for different calls and answers them in order', async () => {
+ const { handlers } = await mountFrame();
+
+ let first!: Promise<unknown>;
+ let second!: Promise<unknown>;
+ act(() => {
+ first = handlers.callTool('place_order', { item: 'tea' });
+ second = handlers.callTool('place_order', { item: 'cake' });
+ });
+ await screen.findByTestId('mcp-ui-confirm');
+ expect(screen.getAllByTestId('mcp-ui-confirm')).toHaveLength(1);
+
+ fireEvent.click(screen.getByRole('button', { name: 'Allow' }));
+ await expect(first).resolves.toEqual({ tool: 'place_order' });
+
+ await waitFor(() => expect(screen.getByTestId('mcp-ui-confirm')).toBeInTheDocument());
+ fireEvent.click(screen.getByRole('button', { name: 'Allow' }));
+ await expect(second).resolves.toEqual({ tool: 'place_order' });
+ expect(screen.queryByTestId('mcp-ui-confirm')).toBeNull();
+
+ let again!: Promise<unknown>;
+ act(() => {
+ again = handlers.callTool('place_order', { item: 'tea' });
+ });
+ await expect(again).resolves.toEqual({ tool: 'place_order' });
+ expect(screen.queryByTestId('mcp-ui-confirm')).toBeNull();
+ });
+
+ it('rejects still-queued confirmations when the view unmounts', async () => {
+ const { view, handlers } = await mountFrame();
+
+ let first!: Promise<unknown>;
+ let second!: Promise<unknown>;
+ act(() => {
+ first = handlers.callTool('place_order', { item: 'tea' });
+ second = handlers.callTool('place_order', { item: 'cake' });
+ });
+ await screen.findByTestId('mcp-ui-confirm');
+ const settled = Promise.allSettled([first, second]);
+ view.unmount();
+ const results = await settled;
+ expect(results.map(result => result.status)).toEqual(['rejected', 'rejected']);
+ });
+
it('keeps one bridge and hands it newer data for the same widget', async () => {
const { view } = await mountFrame();
view.rerender(<McpAppFrame presentation={presentation({ q: 2 })} />);
diff --git a/app/src/features/conversations/tools/mcpUi/McpAppFrame.tsx b/app/src/features/conversations/tools/mcpUi/McpAppFrame.tsx
index 906480d8e6..7227ce1009 100644
--- a/app/src/features/conversations/tools/mcpUi/McpAppFrame.tsx
+++ b/app/src/features/conversations/tools/mcpUi/McpAppFrame.tsx
@@ -1,5 +1,5 @@
import debug from 'debug';
-import { useEffect, useMemo, useRef, useState } from 'react';
+import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import Button from '../../../../components/ui/Button';
import { ConfirmDialog } from '../../../../components/ui/ConfirmDialog';
@@ -17,6 +17,7 @@ const log = debug('mcp-ui:frame');
const DEFAULT_HEIGHT = 360;
interface PendingCall {
+ key: string;
name: string;
args: Record<string, unknown>;
resolve: () => void;
@@ -38,12 +39,31 @@ export function McpAppFrame({ presentation }: { presentation: McpUiPresentation
const [state, setState] = useState<LoadState>({ status: 'loading' });
const [height, setHeight] = useState(DEFAULT_HEIGHT);
const [contentWidth, setContentWidth] = useState<number | null>(null);
- const [pending, setPending] = useState<PendingCall | null>(null);
+ const [queue, setQueue] = useState<PendingCall[]>([]);
+ const queueRef = useRef<PendingCall[]>([]);
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;
+ const pending = queue[0] ?? null;
+
+ const updateQueue = useCallback((next: (current: PendingCall[]) => PendingCall[]) => {
+ queueRef.current = next(queueRef.current);
+ setQueue(queueRef.current);
+ }, []);
+
+ useEffect(() => {
+ const decisions = decisionsRef.current;
+ return () => {
+ const orphaned = queueRef.current;
+ queueRef.current = [];
+ for (const call of orphaned) {
+ decisions.delete(call.key);
+ call.reject(new Error('The view was closed'));
+ }
+ };
+ }, []);
useEffect(() => {
presentationRef.current = presentation;
@@ -94,7 +114,10 @@ export function McpAppFrame({ presentation }: { presentation: McpUiPresentation
let decision = decisionsRef.current.get(decisionKey);
if (!decision) {
decision = new Promise<void>((resolve, reject) =>
- setPending({ name, args, resolve, reject })
+ updateQueue(current => [
+ ...current,
+ { key: decisionKey, name, args, resolve, reject },
+ ])
);
decision.catch(() => undefined);
decisionsRef.current.set(decisionKey, decision);
@@ -127,7 +150,7 @@ export function McpAppFrame({ presentation }: { presentation: McpUiPresentation
window.removeEventListener('message', onMessage);
if (bridgeRef.current === bridge) bridgeRef.current = null;
};
- }, [resource, theme, serverId, locale]);
+ }, [resource, theme, serverId, locale, updateQueue]);
if (state.status === 'unavailable') {
return (
@@ -181,11 +204,11 @@ export function McpAppFrame({ presentation }: { presentation: McpUiPresentation
testId="mcp-ui-confirm"
onConfirm={() => {
pending.resolve();
- setPending(null);
+ updateQueue(current => current.slice(1));
}}
onCancel={() => {
pending.reject(new Error('The user declined this action'));
- setPending(null);
+ updateQueue(current => current.slice(1));
}}
/>
) : null}