Skip to content

The Open Standard for Generative UI

Add cases for MCP events (#815)

AB01f2fc495b59
diff --git a/packages/react-headless/src/stream/adapters/openai-responses.ts b/packages/react-headless/src/stream/adapters/openai-responses.ts
index 384959745..486411721 100644
--- a/packages/react-headless/src/stream/adapters/openai-responses.ts
+++ b/packages/react-headless/src/stream/adapters/openai-responses.ts
@@ -70,6 +70,27 @@ export const openAIResponsesAdapter = (): StreamProtocolAdapter => ({
toolCallId: item.id,
toolCallName: "thesys_web_search",
};
+ } else if (item.type === "mcp_call") {
+ yield {
+ type: EventType.TOOL_CALL_START,
+ toolCallId: item.id,
+ toolCallName: item.name,
+ };
+ } else if (item.type === "mcp_list_tools") {
+ yield {
+ type: EventType.TOOL_CALL_START,
+ toolCallId: item.id,
+ toolCallName: "mcp_list_tools",
+ };
+ yield {
+ type: EventType.TOOL_CALL_ARGS,
+ toolCallId: item.id,
+ delta: JSON.stringify({ server_label: item.server_label }),
+ };
+ yield {
+ type: EventType.TOOL_CALL_END,
+ toolCallId: item.id,
+ };
}
break;
}
@@ -108,11 +129,61 @@ export const openAIResponsesAdapter = (): StreamProtocolAdapter => ({
break;
}
+ case "response.mcp_call_arguments.delta":
+ yield {
+ type: EventType.TOOL_CALL_ARGS,
+ toolCallId: event.item_id,
+ delta: event.delta,
+ };
+ break;
+
+ case "response.mcp_call_arguments.done":
+ yield {
+ type: EventType.TOOL_CALL_END,
+ toolCallId: event.item_id,
+ };
+ break;
+
case "response.output_item.done": {
- // web_search delivers its result on the done item — there's no
- // function_call_output for it. Every other item type closes via its
- // own event (function_call → function_call_arguments.done, message →
- // output_text.done), so this case handles web_search only.
+ // Server-executed tools deliver their result on the done item —
+ // there's no function_call_output for them: mcp_call carries
+ // output/error, web_search carries output/action. Every other item
+ // type closes via its own event (function_call →
+ // function_call_arguments.done, message → output_text.done).
+ if (event.item.type === "mcp_call") {
+ const mcp = event.item;
+ const errorText =
+ typeof mcp.error === "string" && mcp.error.length > 0 ? mcp.error : undefined;
+ yield {
+ type: EventType.TOOL_CALL_RESULT,
+ messageId: mcp.id,
+ toolCallId: mcp.id,
+ content: stringifyOutput(mcp.output),
+ ...(errorText ? { isError: true, error: errorText } : {}),
+ };
+ break;
+ }
+
+ if (event.item.type === "mcp_list_tools") {
+ const list = event.item;
+ // Summarize to names only
+ const toolNames = list.tools.map((t) => t.name);
+ const listError =
+ typeof list.error === "string" && list.error.length > 0 ? list.error : undefined;
+ yield {
+ type: EventType.TOOL_CALL_RESULT,
+ messageId: list.id,
+ toolCallId: list.id,
+ content: JSON.stringify({
+ server_label: list.server_label,
+ tool_count: toolNames.length,
+ tools: toolNames,
+ }),
+ ...(listError ? { isError: true, error: listError } : {}),
+ };
+ break;
+ }
+
const item = event.item as {
type?: string;
id?: string;
@@ -164,10 +235,12 @@ export const openAIResponsesAdapter = (): StreamProtocolAdapter => ({
// Intentionally unhandled — these are lifecycle/metadata events:
// response.created, response.in_progress, response.completed,
- // response.content_part.added, response.content_part.done, and
- // web_search's *.in_progress/searching/completed status events (the
- // web_search_call output_item.added/.done handling above covers it),
- // etc.
+ // response.content_part.added, response.content_part.done,
+ // web_search's *.in_progress/searching/completed status events,
+ // response.mcp_call.in_progress/.completed/.failed and
+ // response.mcp_list_tools.* (the respective output_item.added/.done
+ // handling above covers all three), and the mcp_approval_request
+ // output item (HITL approval flow — no tray), etc.
default:
break;
}
diff --git a/packages/react-headless/src/stream/formats/openai-conversation-message-format.ts b/packages/react-headless/src/stream/formats/openai-conversation-message-format.ts
index fc74a83cd..dfdec80da 100644
--- a/packages/react-headless/src/stream/formats/openai-conversation-message-format.ts
+++ b/packages/react-headless/src/stream/formats/openai-conversation-message-format.ts
@@ -212,6 +212,35 @@ function fromItems(items: ConversationItem[]): Message[] {
break;
}
+ case "mcp_call": {
+ if (!currentAssistant) {
+ currentAssistant = { id: crypto.randomUUID(), role: "assistant" };
+ }
+
+ currentAssistant = {
+ ...currentAssistant,
+ toolCalls: [
+ ...(currentAssistant.toolCalls ?? []),
+ {
+ id: item.id,
+ type: "function" as const,
+ function: { name: item.name, arguments: item.arguments },
+ },
+ ],
+ };
+
+ const errorText =
+ typeof item.error === "string" && item.error.length > 0 ? item.error : undefined;
+ pendingToolMsgs.push({
+ id: `${item.id}-output`,
+ role: "tool",
+ content: item.output ?? "",
+ toolCallId: item.id,
+ ...(errorText ? { error: errorText } : {}),
+ } as ToolMessage);
+ break;
+ }
+
default:
// file_search, web_search, computer_call, reasoning, etc. — skip
break;