import { useId, useState } from "react"; import { ChevronRight, TriangleAlert } from "lucide-react"; import { cn } from "@/lib/utils"; import type { TaskChatItem } from "./task-chat-model"; /** One MCP server that the harness could not start for this run. */ export interface TaskChatMcpStartupFailure { id: string; serverName: string; /** Provider-reported reason, already bounded by the runner. */ reason: string | null; } export const MCP_STARTUP_FAILED_CATEGORY = "mcp_server_startup_failed"; function detailValue(item: Extract, label: string) { if (item.surface !== "provider_activity") return undefined; return item.details.find((detail) => detail.label === label)?.value; } function isMcpStartupFailure(item: TaskChatItem): item is Extract { return item.kind === "protocol" && item.surface === "provider_activity" && item.family === "provider_notice" && detailValue(item, "Category") === MCP_STARTUP_FAILED_CATEGORY; } /** * Lifts MCP startup failures out of a run's items so they render once as the * run-health banner instead of as inline notice rows. Later reports for the * same server replace earlier ones. */ export function partitionRunHealthItems(items: readonly TaskChatItem[]): { failures: TaskChatMcpStartupFailure[]; rest: TaskChatItem[]; } { const byServer = new Map(); const rest: TaskChatItem[] = []; for (const item of items) { if (!isMcpStartupFailure(item)) { rest.push(item); continue; } const serverName = detailValue(item, "server") ?? "An"; byServer.set(serverName, { id: item.id, serverName, reason: detailValue(item, "reason") ?? null }); } // Keep the caller's array identity when nothing was lifted out. return { failures: [...byServer.values()], rest: byServer.size === 0 ? (items as TaskChatItem[]) : rest }; } function failureLine(failure: TaskChatMcpStartupFailure) { return ( <> {failure.serverName} MCP server failed to start {failure.reason ? : {failure.reason} : null} ); } /** * Run-level health warning shown directly under the runner header. A failed * MCP server explains a whole class of runs where the agent says it cannot * reach its tools, so it stays visible above the transcript instead of * scrolling away as one more activity row. */ export function TaskChatRunHealthBanner({ failures }: { failures: readonly TaskChatMcpStartupFailure[] }) { const [open, setOpen] = useState(false); const listId = `task-chat-run-health-${useId().replaceAll(":", "")}`; if (failures.length === 0) return null; const single = failures.length === 1 ? failures[0] : null; return (
{open ? (
{single ? null : failures.map((failure) => (

{failureLine(failure)}

))}

The agent ran without these tools. Fix the server's command or credentials, then try again.

) : null}
); }