import type { ContentBlock } from "@modelcontextprotocol/server"; import type { DeepPartial, RegisteredTools } from "../types/register.js"; import type { ToolContextError } from "../types/result-types.js"; type ToolOutput = Name extends keyof RegisteredTools ? RegisteredTools[Name]["output"] : unknown; type ToolInput = Name extends keyof RegisteredTools ? RegisteredTools[Name]["input"] : unknown; /** No structured result, tool error, or cancellation has been latched yet. */ interface PendingToolContext { status: "pending"; /** Latest complete or partial arguments; each notification replaces it. */ toolInput: DeepPartial> | undefined; toolOutput: undefined; content: undefined; meta: undefined; error?: undefined; } /** First non-error result carrying `structuredContent`. */ interface ReadyToolContext { status: "ready"; /** Latest arguments delivered before the result, when available. */ toolInput: ToolInput | undefined; /** Typed `structuredContent` from the latched rendering result. */ toolOutput: ToolOutput; content: ContentBlock[] | undefined; /** View-only result `_meta`, when present. */ meta: Record | undefined; error?: undefined; } /** * First tool result carrying `isError: true`, or a host cancellation of the * rendering invocation before any result arrived. */ interface ErrorToolContext { status: "error"; /** Latest arguments delivered before the error, when available. */ toolInput: ToolInput | undefined; toolOutput: undefined; /** Error result content; `undefined` for a cancellation. */ content: ContentBlock[] | undefined; /** Error result `_meta`; `undefined` for a cancellation. */ meta: Record | undefined; /** * {@link ToolError} for an `isError: true` result, or * {@link ToolCancelledError} when the host cancelled the call. */ error: ToolContextError; } /** * The rendering invocation's latched lifecycle. * * Partial and complete input notifications replace the same `toolInput` * snapshot while pending. Because the MCP Apps notification has no tool name * or request id, the first structured result, tool error, or cancellation is * assumed to belong to the rendering invocation and becomes terminal. * Content-only successes are valid ambient activity and are ignored. */ export type ToolContextHandle = PendingToolContext | ReadyToolContext | ErrorToolContext; /** * Read the tool invocation that rendered this View. * * The hook starts pending with an optional progressive `toolInput`, then * latches the first structured success or tool error for the View's lifetime. * If the host cancels the call first, the hook latches `status: "error"` with * a {@link ToolCancelledError}. Later lifecycle notifications cannot overwrite * that terminal context. * * @example * ```tsx * function ProductSearchResult() { * const view = useToolContext<"search-fruits">(); * * if (view.status === "error") { * return ; * } * if (view.status === "pending") { * return ; * } * return ; * } * ``` */ export declare function useToolContext(): ToolContextHandle; export {};