import { ReactNode } from 'react'; import * as z3 from 'zod/v3'; import * as z4 from 'zod/v4/core'; type MaybePromise = T | Promise; /** Second argument to a tool's execute callback / useMcpTool handler (Chrome 153+ shape). */ interface ToolExecuteCallbackOptions { signal: AbortSignal; } /** Options for ModelContext.executeTool and UseMcpToolReturn.execute. */ interface ExecuteToolOptions { signal?: AbortSignal; } interface InputSchemaProperty { type: string; description?: string; [key: string]: unknown; } interface InputSchema { type: string; properties?: Record; required?: readonly string[]; [key: string]: unknown; } interface TextContent { type: "text"; text: string; } interface ImageContent { type: "image"; data: string; mimeType: string; } interface TextResourceContents { uri: string; mimeType?: string; text: string; } interface BlobResourceContents { uri: string; mimeType?: string; blob: string; } type ResourceContents = TextResourceContents | BlobResourceContents; interface EmbeddedResource { type: "resource"; resource: ResourceContents; } interface ResourceLink { type: "resource_link"; uri: string; name?: string; description?: string; mimeType?: string; } type ContentBlock = TextContent | ImageContent | EmbeddedResource | ResourceLink; interface CallToolResult { content: ContentBlock[]; structuredContent?: Record; isError?: boolean; } interface ToolAnnotations { /** The tool only reads data and has no side effects. */ readOnlyHint?: boolean; /** The tool's output may include content the page author doesn't control. */ untrustedContentHint?: boolean; /** * Running the tool has significant real-world or irreversible effects, like booking a * flight or sending money. Agents may ask the user to confirm before calling it. */ consequentialHint?: boolean; /** The tool is meant for debugging and developer tooling, not end users. */ debugging?: boolean; } interface ToolDescriptor> { name: string; title?: string; description: string; inputSchema?: InputSchema; outputSchema?: InputSchema; annotations?: ToolAnnotations; execute: (input: TArgs, options: ToolExecuteCallbackOptions) => MaybePromise; } interface McpToolConfigBase { name: string; title?: string; description: string; annotations?: ToolAnnotations; exposedTo?: string[]; onSuccess?: (result: CallToolResult) => void; onError?: (error: Error) => void; } type ZodObjectSchema = z3.AnyZodObject | z4.$ZodObject; /** Parsed schema output — same semantics as Zod's `z.infer`. */ type ZodParsed = T extends z4.$ZodType ? z4.output : T extends z3.ZodTypeAny ? z3.infer : never; interface McpToolConfigZod extends McpToolConfigBase { input: T; inputSchema?: never; output?: ZodObjectSchema; outputSchema?: never; handler: (args: ZodParsed, ctx: ToolExecuteCallbackOptions) => MaybePromise; } interface McpToolConfigJsonSchema extends McpToolConfigBase { input?: never; inputSchema?: InputSchema; output?: never; outputSchema?: InputSchema; handler: (args: Record, ctx: ToolExecuteCallbackOptions) => MaybePromise; } interface ToolExecutionState { isExecuting: boolean; lastResult: TResult | null; error: Error | null; executionCount: number; } interface UseMcpToolReturn { state: ToolExecutionState; execute: (input?: Record, options?: ExecuteToolOptions) => Promise; reset: () => void; } interface WebMCPProviderProps { name: string; version: string; children: ReactNode; } interface WebMCPStatus { available: boolean; } interface RegisterToolOptions { signal?: AbortSignal; exposedTo?: string[]; } /** * Tool metadata returned by ModelContext.getTools(). * `inputSchema` is an object on Chrome 154+ and this library's polyfill, but a * JSON string on Chrome <=153 — consumers must handle both: * `typeof s === "string" ? JSON.parse(s) : s`. */ interface RegisteredTool { name: string; title?: string; description: string; inputSchema?: InputSchema | string; annotations?: ToolAnnotations; window?: Window; origin?: string; } interface ModelContextGetToolOptions { fromOrigins?: string[]; } /** Fired at `document.modelContext` when a tool execution begins (Chrome 156.0.8076.0+). */ interface ToolActivatedEvent extends Event { readonly toolName: string; } /** Fired at `document.modelContext` when a pending tool execution is cancelled (Chrome 156.0.8076.0+). */ interface ToolCancelEvent extends Event { readonly toolName: string; } interface ModelContextEventMap { toolchange: Event; toolactivated: ToolActivatedEvent; toolcancel: ToolCancelEvent; } interface ModelContext extends EventTarget { registerTool(tool: ToolDescriptor, options?: RegisterToolOptions): Promise; getTools?(options?: ModelContextGetToolOptions): Promise; /** * Input defaults to {} only when input and options are omitted or undefined. * Pass {} with options. JSON strings are a legacy polyfill extension. */ executeTool?(tool: RegisteredTool, inputArguments?: string | object, options?: ExecuteToolOptions): Promise; ontoolchange: ((this: ModelContext, ev: Event) => unknown) | null; ontoolactivated: ((this: ModelContext, ev: ToolActivatedEvent) => unknown) | null; ontoolcancel: ((this: ModelContext, ev: ToolCancelEvent) => unknown) | null; addEventListener(type: K, listener: (ev: ModelContextEventMap[K]) => unknown, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void; removeEventListener(type: K, listener: (ev: ModelContextEventMap[K]) => unknown, options?: boolean | EventListenerOptions): void; removeEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | EventListenerOptions): void; } /** @deprecated Removed from native Chrome in 152; use ModelContext.getTools()/executeTool(). Will be removed in webmcp-react 2.0.0. */ interface ModelContextTestingToolInfo { name: string; description: string; inputSchema?: string; } /** @deprecated Removed from native Chrome in 152. Will be removed in webmcp-react 2.0.0. */ interface ModelContextTestingExecuteToolOptions { signal?: AbortSignal; } /** @deprecated Removed from native Chrome in 152; use document.modelContext.getTools()/executeTool(). Will be removed in webmcp-react 2.0.0. */ interface ModelContextTesting { listTools(): ModelContextTestingToolInfo[]; executeTool(toolName: string, inputArgsJson: string, options?: ModelContextTestingExecuteToolOptions): Promise; registerToolsChangedCallback(callback: () => void): void; getCrossDocumentScriptToolResult(): Promise; } declare global { interface Document { modelContext?: ModelContext; } interface Navigator { modelContextTesting?: ModelContextTesting; } } export type { BlobResourceContents, CallToolResult, ContentBlock, EmbeddedResource, ExecuteToolOptions, ImageContent, InputSchema, InputSchemaProperty, MaybePromise, McpToolConfigJsonSchema, McpToolConfigZod, ModelContext, ModelContextEventMap, ModelContextGetToolOptions, ModelContextTesting, ModelContextTestingExecuteToolOptions, ModelContextTestingToolInfo, RegisterToolOptions, RegisteredTool, ResourceContents, ResourceLink, TextContent, TextResourceContents, ToolActivatedEvent, ToolAnnotations, ToolCancelEvent, ToolDescriptor, ToolExecuteCallbackOptions, ToolExecutionState, UseMcpToolReturn, WebMCPProviderProps, WebMCPStatus, ZodObjectSchema, ZodParsed };