Skip to content

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

feat(conversations): show tool widgets after the activity group, not in the card

oxoxDevb4e8d0fc7926
diff --git a/app/src/components/assistant-ui/thread.mcpWidgets.test.tsx b/app/src/components/assistant-ui/thread.mcpWidgets.test.tsx
new file mode 100644
index 0000000000..b94094fab0
--- /dev/null
+++ b/app/src/components/assistant-ui/thread.mcpWidgets.test.tsx
@@ -0,0 +1,142 @@
+import type { McpUiPresentation } from '@/features/conversations/tools/mcpUi/types';
+import {
+ AssistantRuntimeProvider,
+ type ThreadMessageLike,
+ useExternalStoreRuntime,
+} from '@assistant-ui/react';
+import { render, screen } from '@testing-library/react';
+import { useEffect } from 'react';
+import { describe, expect, it, vi } from 'vitest';
+
+import { Thread } from './thread';
+
+const frames = vi.hoisted(() => ({ mounts: 0 }));
+
+vi.mock('@/utils/tauriCommands/common', async importOriginal => ({
+ ...(await importOriginal<typeof import('@/utils/tauriCommands/common')>()),
+ isTauri: () => true,
+}));
+
+vi.mock('@/features/conversations/tools/mcpUi/McpAppFrame', () => ({
+ McpAppFrame: ({ presentation }: { presentation: McpUiPresentation }) => {
+ useEffect(() => {
+ frames.mounts += 1;
+ }, []);
+ return (
+ <div
+ data-testid="mcp-ui-frame"
+ data-uri={presentation.resource_uri}
+ data-data={JSON.stringify(presentation.structured_content)}
+ />
+ );
+ },
+}));
+
+type Part = Exclude<ThreadMessageLike['content'], string>[number];
+
+const widgetCall = (id: string, uri: string, data: unknown): Part =>
+ ({
+ type: 'tool-call',
+ toolCallId: id,
+ toolName: 'mcp_shop_tool',
+ args: {},
+ argsText: '{}',
+ result: 'ok',
+ artifact: {
+ kind: 'openhuman-tool',
+ structured: {
+ kind: 'mcp_ui',
+ flavor: 'mcp_apps',
+ server_id: 'shop',
+ tool: 'tool',
+ resource_uri: uri,
+ structured_content: data,
+ links: [{ url: 'https://shop.example.com/item/1', kind: 'external' }],
+ },
+ },
+ }) as never;
+
+const turn = (
+ content: Part[],
+ status: ThreadMessageLike['status'] = { type: 'complete', reason: 'stop' }
+): ThreadMessageLike[] => [
+ { role: 'user', content: [{ type: 'text', text: 'shop' }] },
+ { role: 'assistant', status, content },
+];
+
+function Harness({ messages, isRunning }: { messages: ThreadMessageLike[]; isRunning: boolean }) {
+ const runtime = useExternalStoreRuntime({
+ messages,
+ isRunning,
+ convertMessage: (m: ThreadMessageLike) => m,
+ onNew: async () => {},
+ });
+ return (
+ <AssistantRuntimeProvider runtime={runtime}>
+ <Thread />
+ </AssistantRuntimeProvider>
+ );
+}
+
+const frameUris = () =>
+ screen.getAllByTestId('mcp-ui-frame').map(frame => frame.getAttribute('data-uri'));
+
+describe('MCP widgets in an assistant message', () => {
+ it('render outside the collapsed activity group, before the answer', () => {
+ render(
+ <Harness
+ messages={turn([
+ { type: 'reasoning', text: 'looking' },
+ widgetCall('t1', 'ui://search', { q: 1 }),
+ { type: 'text', text: 'final answer' },
+ ])}
+ isRunning={false}
+ />
+ );
+
+ const frame = screen.getByTestId('mcp-ui-frame');
+ expect(frame.closest('[data-slot=tool-group-root]')).toBeNull();
+ const answer = screen.getByText('final answer');
+ expect(frame.compareDocumentPosition(answer) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
+ expect(screen.queryByTestId('mcp-ui-link')).toBeNull();
+ });
+
+ it('show each template once, with its latest data, in order of latest call', () => {
+ render(
+ <Harness
+ messages={turn([
+ widgetCall('t1', 'ui://cart', { n: 1 }),
+ widgetCall('t2', 'ui://search', { q: 1 }),
+ widgetCall('t3', 'ui://cart', { n: 2 }),
+ widgetCall('t4', 'ui://cart', { n: 3 }),
+ { type: 'text', text: 'done' },
+ ])}
+ isRunning={false}
+ />
+ );
+
+ expect(frameUris()).toEqual(['ui://search', 'ui://cart']);
+ const cart = screen.getAllByTestId('mcp-ui-frame')[1]!;
+ expect(cart.getAttribute('data-data')).toBe(JSON.stringify({ n: 3 }));
+ });
+
+ it('keep the same frame mounted when the turn completes', () => {
+ frames.mounts = 0;
+ const running = turn([widgetCall('t1', 'ui://search', { q: 1 })], { type: 'running' });
+ const { rerender } = render(<Harness messages={running} isRunning />);
+ expect(screen.getByTestId('mcp-ui-frame')).toBeInTheDocument();
+
+ rerender(
+ <Harness
+ messages={turn([
+ widgetCall('t1', 'ui://search', { q: 1 }),
+ { type: 'text', text: 'final answer' },
+ ])}
+ isRunning={false}
+ />
+ );
+
+ expect(screen.getAllByTestId('mcp-ui-frame')).toHaveLength(1);
+ expect(frames.mounts).toBe(1);
+ });
+});
diff --git a/app/src/components/assistant-ui/thread.tsx b/app/src/components/assistant-ui/thread.tsx
index e20e0331a6..dbdbc72fa9 100644
--- a/app/src/components/assistant-ui/thread.tsx
+++ b/app/src/components/assistant-ui/thread.tsx
@@ -31,6 +31,8 @@ import {
useAuiReloadCapability,
} from '@/features/conversations/components/aui/auiThreadState';
import { onComposerPrefill } from '@/features/conversations/tools/mcpUi/composerPrefill';
+import { McpWidgetStrip } from '@/features/conversations/tools/mcpUi/McpWidgetStrip';
+import { collectMessageWidgets } from '@/features/conversations/tools/mcpUi/messageWidgets';
import { useT } from '@/lib/i18n/I18nContext';
import { CHAT_ERROR_METADATA_KEY } from '@/store/threadSlice';
import { fullTimestamp, relativeTime } from '@/utils/relativeTime';
@@ -1158,6 +1160,8 @@ const AssistantMessage: FC = () => {
MessageTasks,
} = useContext(MessageComponentsContext);
const stopped = useAuiState(isStoppedRun);
+ const parts = useAuiState(s => s.message.parts);
+ const widgets = useMemo(() => collectMessageWidgets(parts), [parts]);
const ACTION_BAR_PT = 'pt-1.5';
// `min-h` reserves the bar's height (`pt-1.5` + a `size-6` button = 7.5) so a
@@ -1211,8 +1215,17 @@ const AssistantMessage: FC = () => {
})}>
{({ part, children }) => {
switch (part.type) {
- case 'group-activity':
- return <ActivityGroup group={part}>{children}</ActivityGroup>;
+ case 'group-activity': {
+ const groupWidgets = widgets.filter(widget =>
+ part.indices.includes(widget.partIndex)
+ );
+ return (
+ <>
+ <ActivityGroup group={part}>{children}</ActivityGroup>
+ <McpWidgetStrip widgets={groupWidgets} />
+ </>
+ );
+ }
case 'group-source':
return SourceGroup ? <SourceGroupSlot Component={SourceGroup} /> : null;
case 'text':
diff --git a/app/src/features/conversations/tools/mcpUi/McpUiBody.test.tsx b/app/src/features/conversations/tools/mcpUi/McpUiBody.test.tsx
index ee831a26c8..3334a9e794 100644
--- a/app/src/features/conversations/tools/mcpUi/McpUiBody.test.tsx
+++ b/app/src/features/conversations/tools/mcpUi/McpUiBody.test.tsx
@@ -6,7 +6,6 @@ import { McpUiBody } from './McpUiBody';
const tauri = vi.hoisted(() => ({ value: false }));
vi.mock('../../../../utils/tauriCommands/common', () => ({ isTauri: () => tauri.value }));
-vi.mock('./McpAppFrame', () => ({ McpAppFrame: () => <div data-testid="mcp-ui-frame" /> }));
describe('McpUiBody', () => {
it('renders nothing for other structured payloads', () => {
@@ -31,7 +30,7 @@ describe('McpUiBody', () => {
expect(screen.getByTestId('mcp-ui-link')).toBeInTheDocument();
});
- it('shows only the widget when it can be sandboxed', () => {
+ it('leaves a sandboxable widget to the message strip', () => {
tauri.value = true;
try {
render(
@@ -46,8 +45,8 @@ describe('McpUiBody', () => {
}}
/>
);
- expect(screen.getByTestId('mcp-ui-frame')).toBeInTheDocument();
- expect(screen.queryByTestId('mcp-ui-link')).toBeNull();
+ expect(screen.queryByTestId('mcp-ui-body')).toBeNull();
+ expect(screen.queryByTestId('mcp-ui-frame')).toBeNull();
} finally {
tauri.value = false;
}
diff --git a/app/src/features/conversations/tools/mcpUi/McpUiBody.tsx b/app/src/features/conversations/tools/mcpUi/McpUiBody.tsx
index 2af7baeb12..456f0c054b 100644
--- a/app/src/features/conversations/tools/mcpUi/McpUiBody.tsx
+++ b/app/src/features/conversations/tools/mcpUi/McpUiBody.tsx
@@ -1,23 +1,19 @@
import { isTauri } from '../../../../utils/tauriCommands/common';
import { LinkActions } from './LinkActions';
-import { McpAppFrame } from './McpAppFrame';
import { hasWidget, readMcpUiPresentation } from './types';
/**
- * What a tool call that offered UI shows outside its collapsed card: the
- * widget, when there is one and this host can sandbox it, or otherwise the
- * links the result offered.
+ * What a tool call that offered UI shows under its card: the links the result
+ * offered, unless its widget is shown on its own after the activity group.
*/
export function McpUiBody({ structured }: { structured: unknown }) {
const presentation = readMcpUiPresentation(structured);
if (!presentation) return null;
- const showFrame = hasWidget(presentation) && isTauri();
- const showLinks = !showFrame && presentation.links.length > 0;
- if (!showFrame && !showLinks) return null;
+ if (hasWidget(presentation) && isTauri()) return null;
+ if (presentation.links.length === 0) return null;
return (
<div className="flex flex-col gap-2" data-testid="mcp-ui-body">
- {showFrame ? <McpAppFrame presentation={presentation} /> : null}
- {showLinks ? <LinkActions links={presentation.links} /> : null}
+ <LinkActions links={presentation.links} />
</div>
);
}