import { useId, useMemo, useState, type FormEvent, type ReactNode } from "react"; import { LoaderCircleIcon, MessageCircleQuestionIcon } from "lucide-react"; import { mcpElicitationKind, mcpElicitationChoices, mcpElicitationDefaults, type McpElicitationApprovalRequest, type McpElicitationContent, type McpElicitationField, type McpElicitationValue, } from "@opengeni/contracts/mcp-elicitation-model"; import { cn } from "../lib/cn"; import { useOpenGeniText } from "../messages/runtime"; import { DecisionActions, DecisionButton } from "./decision-card"; import { DeclineButton, ToolReviewFrame } from "./tool-action-review"; /** More choices than this become a drop-down instead of a list. */ const LISTED_CHOICES = 6; type Draft = Record>; const inputClass = "w-full rounded-og-md border border-og-border bg-og-surface-1 px-3 py-2 text-og-sm text-og-fg outline-hidden placeholder:text-og-fg-subtle focus:border-og-accent disabled:opacity-60 aria-[invalid=true]:border-og-status-failed"; /** * An MCP server's question in the middle of a tool call, answered in place: * its message, its form (prefilled with the server's defaults), and Decline. * A question with no fields is a plain confirmation answered with Allow. */ export function McpElicitationRequest({ elicitation, title, subtitle, submitting, disabled, onAnswer, onDecline, details, }: { elicitation: McpElicitationApprovalRequest; /** What the paused call does, e.g. "Send message". */ title: ReactNode; subtitle?: ReactNode; submitting: "approve" | "reject" | null; disabled: boolean; onAnswer: (content: McpElicitationContent) => void; onDecline: () => void; /** A link to the paused call's full arguments. */ details?: ReactNode; }) { const t = useOpenGeniText(); const formId = useId(); const titleId = useId(); const [draft, setDraft] = useState(() => Object.fromEntries( elicitation.requests.map((request) => [ request.id, mcpElicitationDefaults(request.requestedSchema), ]), ), ); const kind = mcpElicitationKind(elicitation); const busy = disabled || submitting !== null; const submit = (event: FormEvent) => { event.preventDefault(); if (busy || !event.currentTarget.reportValidity()) return; onAnswer(answerContent(elicitation, draft)); }; return ( ); } /** What the person entered, without fields they left empty. */ function answerContent( elicitation: McpElicitationApprovalRequest, draft: Draft, ): McpElicitationContent { const content: McpElicitationContent = {}; for (const request of elicitation.requests) { const values: Record = {}; for (const [name, value] of Object.entries(draft[request.id] ?? {})) { if (value === undefined || value === "") continue; const field = request.requestedSchema.properties[name]; values[name] = field?.type === "string" && "format" in field && field.format === "date-time" ? new Date(value as string).toISOString() : value; } content[request.id] = values; } return content; } function ElicitationField({ id, name, field, required, value, onChange, }: { id: string; name: string; field: McpElicitationField; required: boolean; value: McpElicitationValue | undefined; onChange: (value: McpElicitationValue | undefined) => void; }) { const t = useOpenGeniText(); const label = field.title ?? name; const helpId = `${id}-help`; const describedBy = field.description ? helpId : undefined; const choices = useMemo(() => mcpElicitationChoices(field), [field]); const heading = ( {label} {required ? null : ( {t("approvalSurface.optional")} )} ); const help = field.description ? (

{field.description}

) : null; if (field.type === "boolean") { return (
{help}
); } if (choices && (field.type === "array" || choices.length <= LISTED_CHOICES)) { const multiple = field.type === "array"; const selected = multiple ? ((value as string[] | undefined) ?? []) : [value]; return (
{heading} {help} {/* Rows bleed into the gutter so each control lines up with the labels. */}
{choices.map((choice, index) => { const checked = selected.includes(choice.value); return ( ); })}
); } const control = choices ? ( ) : field.type === "number" || field.type === "integer" ? ( // A number input's value is a valid number or empty. onChange(event.target.value === "" ? undefined : Number(event.target.value)) } className={cn(inputClass, "max-w-40")} /> ) : ( } required={required} value={typeof value === "string" ? value : ""} describedBy={describedBy} onChange={onChange} /> ); return (
{help} {control}
); } function TextControl({ id, field, required, value, describedBy, onChange, }: { id: string; field: { minLength?: number; maxLength?: number; format?: string }; required: boolean; value: string; describedBy: string | undefined; onChange: (value: string | undefined) => void; }) { const shared = { id, required, minLength: field.minLength, maxLength: field.maxLength, "aria-describedby": describedBy, className: inputClass, }; if (field.format === "date-time") { return ( onChange(event.target.value || undefined)} className={cn(inputClass, "max-w-64")} /> ); } if (field.format) { const type = field.format === "uri" ? "url" : field.format; return ( onChange(event.target.value || undefined)} {...(type === "date" ? { className: cn(inputClass, "max-w-56") } : {})} /> ); } // A short answer is one line; anything that may run long can wrap. return field.maxLength !== undefined && field.maxLength <= 200 ? ( onChange(event.target.value || undefined)} /> ) : (