@mcp-b/global 2.0.2-canary.20260125211849 → 2.0.3-canary.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -39
- package/dist/index.d.ts +5 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.iife.js +2 -10
- package/dist/index.js +27 -47
- package/dist/index.js.map +1 -1
- package/package.json +8 -5
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["logger","logger","validatedTool: ValidatedToolDescriptor","args: Record<string, unknown>","templateParams: string[]","argsSchema: InputSchema | undefined","argsValidator: z.ZodType | undefined","_exhaustive: never","parsedUri: URL","paramNames: string[]","params: Record<string, string>","bridge","customTransport: Transport | undefined","McpServer","bridge: MCPBridge","options: WebModelContextInitOptions","tabServerOptions: TabServerConfig"],"sources":["../src/logger.ts","../src/validation.ts","../src/global.ts","../src/index.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2025 Google LLC\n * SPDX-License-Identifier: Apache-2.0\n */\n\n/**\n * Lightweight logging system for @mcp-b/global\n *\n * Design Decision: This implements a custom logger instead of using the 'debug'\n * package to reduce bundle size and eliminate external dependencies in the\n * browser build. The API is intentionally simpler, focusing on the specific\n * needs of browser-based MCP implementations.\n *\n * Configuration via localStorage:\n * - localStorage.setItem('WEBMCP_DEBUG', '*') - enable all debug logging\n * - localStorage.setItem('WEBMCP_DEBUG', 'WebModelContext') - enable specific namespace\n * - localStorage.setItem('WEBMCP_DEBUG', 'WebModelContext,NativeAdapter') - multiple namespaces\n * - localStorage.setItem('WEBMCP_DEBUG', 'WebModelContext:') - enable namespace and sub-namespaces\n * - localStorage.removeItem('WEBMCP_DEBUG') - disable debug logging (default)\n *\n * Environment Support:\n * - Automatically detects localStorage availability\n * - Gracefully degrades to \"disabled\" state when localStorage is inaccessible\n * - Never throws errors from configuration checks (safe for private browsing mode)\n */\n\n/** localStorage key for debug configuration */\nconst DEBUG_CONFIG_KEY = 'WEBMCP_DEBUG' as const;\n\n/**\n * Check if debug logging is enabled for a namespace\n *\n * Supports namespace hierarchy via colons. Setting 'WebModelContext' will match\n * both 'WebModelContext' and 'WebModelContext:init', but NOT 'WebModelContextTesting'.\n */\nfunction isDebugEnabled(namespace: string): boolean {\n if (typeof window === 'undefined' || !window.localStorage) {\n return false;\n }\n\n try {\n const debugConfig = localStorage.getItem(DEBUG_CONFIG_KEY);\n if (!debugConfig) return false;\n\n if (debugConfig === '*') return true;\n\n const patterns = debugConfig.split(',').map((p) => p.trim());\n return patterns.some((pattern) => namespace === pattern || namespace.startsWith(`${pattern}:`));\n } catch (err) {\n // localStorage might throw in some browsers (private mode, disabled storage)\n // Log once to console so developers know debug logging is disabled\n if (typeof console !== 'undefined' && console.warn) {\n const message = err instanceof Error ? err.message : String(err);\n console.warn(`[WebMCP] localStorage access failed, debug logging disabled: ${message}`);\n }\n return false;\n }\n}\n\n/**\n * No-op function for disabled log levels\n */\nconst noop = (): void => {};\n\n/**\n * Logger interface with standard log levels\n *\n * All methods accept the same argument patterns as their console.* equivalents,\n * supporting format strings, object inspection, and multiple arguments.\n */\nexport interface Logger {\n /** Debug-level logging (disabled by default, enable via WEBMCP_DEBUG) */\n debug(message?: unknown, ...optionalParams: unknown[]): void;\n /** Info-level logging (disabled by default, enable via WEBMCP_DEBUG) */\n info(message?: unknown, ...optionalParams: unknown[]): void;\n /** Warning-level logging (enabled by default, not gated by WEBMCP_DEBUG) */\n warn(message?: unknown, ...optionalParams: unknown[]): void;\n /** Error-level logging (enabled by default, not gated by WEBMCP_DEBUG) */\n error(message?: unknown, ...optionalParams: unknown[]): void;\n}\n\n/**\n * Create a namespaced logger\n *\n * Uses .bind() to prepend namespace prefixes to console methods without manual\n * string concatenation. Debug enablement is determined at logger creation time\n * for performance - changes to localStorage after creation won't affect existing\n * loggers. Refresh the page to apply new WEBMCP_DEBUG settings.\n *\n * @param namespace - Namespace for the logger (e.g., 'WebModelContext', 'NativeAdapter')\n * @returns Logger instance with debug, info, warn, error methods\n *\n * @example\n * ```typescript\n * const logger = createLogger('WebModelContext');\n * logger.debug('Tool registered:', toolName); // Only shown if WEBMCP_DEBUG includes 'WebModelContext'\n * logger.error('Execution failed:', error); // Always enabled\n * ```\n */\nexport function createLogger(namespace: string): Logger {\n const prefix = `[${namespace}]`;\n\n // Note: Debug enablement is checked once at creation time for performance.\n // Changes to localStorage after creation won't affect existing loggers.\n const isDebug = isDebugEnabled(namespace);\n\n // Create bound console methods that include the namespace prefix\n const boundWarn = console.warn.bind(console, prefix);\n const boundError = console.error.bind(console, prefix);\n const boundLog = console.log.bind(console, prefix);\n\n return {\n // Warnings and errors are always enabled (production-safe)\n warn: boundWarn,\n error: boundError,\n\n // Debug and info are conditional - use bound methods or no-ops\n debug: isDebug ? boundLog : noop,\n info: isDebug ? boundLog : noop,\n };\n}\n","import { z } from 'zod';\nimport { createLogger } from './logger.js';\nimport type { InputSchema } from './types.js';\n\nconst logger = createLogger('WebModelContext');\n\n/**\n * Result of Zod schema detection with version information\n */\nexport interface ZodSchemaDetection {\n isZodSchema: boolean;\n hasZod4: boolean;\n hasZod3: boolean;\n}\n\n/**\n * Detect if a schema is a Zod schema object and which version.\n *\n * Uses duck-typing to detect Zod schemas:\n * - Zod 4 schemas have `_zod` property\n * - Zod 3 schemas have `_def` property (but not `_zod`)\n */\nexport function detectZodSchema(schema: unknown): ZodSchemaDetection {\n if (typeof schema !== 'object' || schema === null) {\n return { isZodSchema: false, hasZod4: false, hasZod3: false };\n }\n\n // JSON Schema has a 'type' property that's a string\n if ('type' in schema && typeof (schema as { type: unknown }).type === 'string') {\n return { isZodSchema: false, hasZod4: false, hasZod3: false };\n }\n\n const values = Object.values(schema);\n if (values.length === 0) {\n return { isZodSchema: false, hasZod4: false, hasZod3: false };\n }\n\n let hasZod4 = false;\n let hasZod3 = false;\n\n for (const val of values) {\n if (val == null || typeof val !== 'object') continue;\n const obj = val as object;\n\n if ('_zod' in obj) {\n hasZod4 = true;\n } else if ('_def' in obj) {\n // Has _def but not _zod = Zod 3\n hasZod3 = true;\n }\n }\n\n return {\n isZodSchema: hasZod4 || hasZod3,\n hasZod4,\n hasZod3,\n };\n}\n\n/**\n * Detect if a schema is a Zod schema object (Record<string, ZodType>)\n * or a JSON Schema object.\n *\n * Uses duck-typing to detect Zod schemas:\n * - Zod 4 schemas have `_zod` property\n * - Zod 3 schemas have `_def` property\n */\nexport function isZodSchema(schema: unknown): boolean {\n return detectZodSchema(schema).isZodSchema;\n}\n\n/**\n * Convert JSON Schema to Zod validator\n * Uses Zod 4's native z.fromJSONSchema() for conversion\n */\nexport function jsonSchemaToZod(jsonSchema: InputSchema): z.ZodType {\n try {\n // Zod 4 has native fromJSONSchema support\n const zodSchema = z.fromJSONSchema(jsonSchema as z.core.JSONSchema.BaseSchema);\n return zodSchema;\n } catch (error) {\n logger.warn('Failed to convert JSON Schema to Zod:', error);\n return z.object({}).passthrough();\n }\n}\n\n/**\n * Convert Zod schema object to JSON Schema\n * Uses Zod 4's native z.toJSONSchema() for conversion\n *\n * @param schema - Record of Zod type definitions (e.g., { name: z.string(), age: z.number() })\n * @returns JSON Schema object compatible with MCP InputSchema\n */\nexport function zodToJsonSchema(schema: Record<string, z.ZodTypeAny>): InputSchema {\n const zodObject = z.object(schema);\n const jsonSchema = z.toJSONSchema(zodObject);\n\n // Remove $schema field as it's not needed for MCP\n const { $schema: _, ...rest } = jsonSchema as { $schema?: string } & InputSchema;\n return rest as InputSchema;\n}\n\n/**\n * Error thrown when Zod 3 schemas are detected.\n * Zod 4 is required for schema conversion.\n */\nexport class Zod3SchemaError extends Error {\n constructor() {\n super(\n 'Zod 3 schema detected. This package requires Zod 4 for schema support.\\n\\n' +\n 'Solutions:\\n' +\n ' 1. Upgrade to zod@4.x: pnpm add zod@4\\n' +\n ' 2. If using zod@3.25+, import from the v4 subpath:\\n' +\n ' import { z } from \"zod/v4\"\\n' +\n ' 3. Use JSON Schema instead of Zod schemas\\n\\n' +\n 'See https://zod.dev/v4/versioning for more information.'\n );\n this.name = 'Zod3SchemaError';\n }\n}\n\n/**\n * Normalize a schema to both JSON Schema and Zod formats\n * Detects which format is provided and converts to the other\n *\n * @throws {Zod3SchemaError} If Zod 3 schemas are detected\n */\nexport function normalizeSchema(schema: InputSchema | Record<string, z.ZodTypeAny>): {\n jsonSchema: InputSchema;\n zodValidator: z.ZodType;\n} {\n const detection = detectZodSchema(schema);\n\n if (detection.hasZod3 && !detection.hasZod4) {\n throw new Zod3SchemaError();\n }\n\n if (detection.isZodSchema) {\n const jsonSchema = zodToJsonSchema(schema as Record<string, z.ZodTypeAny>);\n const zodValidator = z.object(schema as Record<string, z.ZodTypeAny>);\n return { jsonSchema, zodValidator };\n }\n\n const jsonSchema = schema as InputSchema;\n const zodValidator = jsonSchemaToZod(jsonSchema);\n return { jsonSchema, zodValidator };\n}\n\n/**\n * Validate data with Zod schema and return formatted result\n */\nexport function validateWithZod(\n data: unknown,\n validator: z.ZodType\n): { success: true; data: unknown } | { success: false; error: string } {\n const result = validator.safeParse(data);\n\n if (!result.success) {\n const errors = result.error.issues\n .map((err) => ` - ${err.path.join('.') || 'root'}: ${err.message}`)\n .join('\\n');\n return {\n success: false,\n error: `Validation failed:\\n${errors}`,\n };\n }\n\n return {\n success: true,\n data: result.data,\n };\n}\n","import {\n IframeChildTransport,\n type IframeChildTransportOptions,\n TabServerTransport,\n type TabServerTransportOptions,\n} from '@mcp-b/transports';\nimport type {\n Prompt,\n PromptMessage,\n Resource,\n ResourceContents,\n Transport,\n} from '@mcp-b/webmcp-ts-sdk';\nimport {\n CallToolRequestSchema,\n GetPromptRequestSchema,\n ListPromptsRequestSchema,\n ListResourcesRequestSchema,\n ListToolsRequestSchema,\n Server as McpServer,\n ReadResourceRequestSchema,\n} from '@mcp-b/webmcp-ts-sdk';\nimport type { z } from 'zod';\nimport { createLogger } from './logger.js';\nimport type {\n ElicitationParams,\n ElicitationResult,\n InputSchema,\n InternalModelContext,\n MCPBridge,\n ModelContext,\n ModelContextInput,\n ModelContextTesting,\n PromptDescriptor,\n ResourceDescriptor,\n SamplingRequestParams,\n SamplingResult,\n ToolCallEvent,\n ToolDescriptor,\n ToolResponse,\n ValidatedPromptDescriptor,\n ValidatedResourceDescriptor,\n ValidatedToolDescriptor,\n WebModelContextInitOptions,\n ZodSchemaObject,\n} from './types.js';\nimport { jsonSchemaToZod, normalizeSchema, validateWithZod } from './validation.js';\n\n// Create namespaced loggers for different components\nconst logger = createLogger('WebModelContext');\nconst nativeLogger = createLogger('NativeAdapter');\nconst bridgeLogger = createLogger('MCPBridge');\nconst testingLogger = createLogger('ModelContextTesting');\n\ndeclare global {\n interface Window {\n __webModelContextOptions?: WebModelContextInitOptions;\n }\n}\n\n/**\n * Marker property name used to identify polyfill implementations.\n * This constant ensures single source of truth for the marker used in\n * both detection (detectNativeAPI) and definition (WebModelContextTesting).\n */\nconst POLYFILL_MARKER_PROPERTY = '__isWebMCPPolyfill' as const;\n\n/**\n * Type guard interface for objects that may have the polyfill marker.\n * Used for type-safe detection of polyfill vs native implementations.\n */\ninterface MayHavePolyfillMarker {\n [POLYFILL_MARKER_PROPERTY]?: true;\n}\n\n/**\n * Detect if the native Chromium Web Model Context API is available.\n * Checks for both navigator.modelContext and navigator.modelContextTesting,\n * and verifies they are native implementations (not polyfills).\n *\n * Detection uses a marker property (`__isWebMCPPolyfill`) on the testing API\n * to reliably distinguish polyfills from native implementations. This approach\n * works correctly even when class names are minified in production builds.\n *\n * @returns Detection result with flags for native context and testing API availability\n */\nfunction detectNativeAPI(): {\n hasNativeContext: boolean;\n hasNativeTesting: boolean;\n} {\n /* c8 ignore next 2 */\n if (typeof window === 'undefined' || typeof navigator === 'undefined') {\n return { hasNativeContext: false, hasNativeTesting: false };\n }\n\n const modelContext = navigator.modelContext;\n const modelContextTesting = navigator.modelContextTesting;\n\n if (!modelContext || !modelContextTesting) {\n return {\n hasNativeContext: Boolean(modelContext),\n hasNativeTesting: Boolean(modelContextTesting),\n };\n }\n\n // Check for polyfill marker property.\n // This is more reliable than constructor name checking, which fails when\n // class names are minified in production builds.\n const isPolyfill =\n POLYFILL_MARKER_PROPERTY in modelContextTesting &&\n (modelContextTesting as MayHavePolyfillMarker)[POLYFILL_MARKER_PROPERTY] === true;\n\n if (isPolyfill) {\n return { hasNativeContext: false, hasNativeTesting: false };\n }\n\n return { hasNativeContext: true, hasNativeTesting: true };\n}\n\n/**\n * Adapter that wraps the native Chromium Web Model Context API.\n * Synchronizes tool changes from the native API to the MCP bridge,\n * enabling MCP clients to stay in sync with the native tool registry.\n *\n * Key features:\n * - Listens to native tool changes via registerToolsChangedCallback()\n * - Syncs native tools to MCP bridge automatically\n * - Delegates tool execution to native API\n * - Converts native results to MCP ToolResponse format\n *\n * @class NativeModelContextAdapter\n * @implements {InternalModelContext}\n */\nclass NativeModelContextAdapter implements InternalModelContext {\n private nativeContext: ModelContext;\n private nativeTesting: ModelContextTesting;\n private bridge: MCPBridge;\n private syncInProgress = false;\n\n /**\n * Creates a new NativeModelContextAdapter.\n *\n * @param {MCPBridge} bridge - The MCP bridge instance\n * @param {ModelContext} nativeContext - The native navigator.modelContext\n * @param {ModelContextTesting} nativeTesting - The native navigator.modelContextTesting\n */\n constructor(bridge: MCPBridge, nativeContext: ModelContext, nativeTesting: ModelContextTesting) {\n this.bridge = bridge;\n this.nativeContext = nativeContext;\n this.nativeTesting = nativeTesting;\n\n this.nativeTesting.registerToolsChangedCallback(() => {\n this.syncToolsFromNative();\n });\n\n this.syncToolsFromNative();\n }\n\n /**\n * Synchronizes tools from the native API to the MCP bridge.\n * Fetches all tools from navigator.modelContextTesting.listTools()\n * and updates the bridge's tool registry.\n *\n * @private\n */\n private syncToolsFromNative(): void {\n if (this.syncInProgress) {\n return;\n }\n\n this.syncInProgress = true;\n\n try {\n const nativeTools = this.nativeTesting.listTools();\n\n this.bridge.tools.clear();\n\n for (const toolInfo of nativeTools) {\n try {\n const inputSchema = JSON.parse(toolInfo.inputSchema);\n\n const validatedTool: ValidatedToolDescriptor = {\n name: toolInfo.name,\n description: toolInfo.description,\n inputSchema: inputSchema,\n execute: async (args: Record<string, unknown>) => {\n const result = await this.nativeTesting.executeTool(\n toolInfo.name,\n JSON.stringify(args)\n );\n return this.convertToToolResponse(result);\n },\n inputValidator: jsonSchemaToZod(inputSchema),\n };\n\n this.bridge.tools.set(toolInfo.name, validatedTool);\n } catch (error) {\n nativeLogger.error(`Failed to sync tool \"${toolInfo.name}\":`, error);\n }\n }\n\n this.notifyMCPServers();\n } finally {\n this.syncInProgress = false;\n }\n }\n\n /**\n * Converts native API result to MCP ToolResponse format.\n * Native API returns simplified values (string, number, object, etc.)\n * which need to be wrapped in the MCP CallToolResult format.\n *\n * @param {unknown} result - The result from native executeTool()\n * @returns {ToolResponse} Formatted MCP ToolResponse\n * @private\n */\n private convertToToolResponse(result: unknown): ToolResponse {\n if (typeof result === 'string') {\n return { content: [{ type: 'text', text: result }] };\n }\n\n if (result === undefined || result === null) {\n return { content: [{ type: 'text', text: '' }] };\n }\n\n if (typeof result === 'object') {\n return {\n content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],\n structuredContent: result as Record<string, unknown>,\n };\n }\n\n return {\n content: [{ type: 'text', text: String(result) }],\n };\n }\n\n /**\n * Notifies all connected MCP servers that the tools list has changed.\n *\n * @private\n */\n private notifyMCPServers(): void {\n if (this.bridge.tabServer?.notification) {\n this.bridge.tabServer.notification({\n method: 'notifications/tools/list_changed',\n params: {},\n });\n }\n\n if (this.bridge.iframeServer?.notification) {\n this.bridge.iframeServer.notification({\n method: 'notifications/tools/list_changed',\n params: {},\n });\n }\n }\n\n /**\n * Provides context (tools) to AI models via the native API.\n * Delegates to navigator.modelContext.provideContext().\n * Tool change callback will fire and trigger sync automatically.\n *\n * @param {ModelContextInput} context - Context containing tools to register\n */\n provideContext(context: ModelContextInput): void {\n this.nativeContext.provideContext(context);\n }\n\n /**\n * Registers a single tool dynamically via the native API.\n * Delegates to navigator.modelContext.registerTool().\n * Tool change callback will fire and trigger sync automatically.\n *\n * @param {ToolDescriptor} tool - The tool descriptor to register\n * @returns {{unregister: () => void}} Object with unregister function\n */\n registerTool<\n TInputSchema extends ZodSchemaObject = Record<string, never>,\n TOutputSchema extends ZodSchemaObject = Record<string, never>,\n >(tool: ToolDescriptor<TInputSchema, TOutputSchema>): { unregister: () => void } {\n const result = this.nativeContext.registerTool(tool);\n return result;\n }\n\n /**\n * Unregisters a tool by name via the native API.\n * Delegates to navigator.modelContext.unregisterTool().\n *\n * @param {string} name - Name of the tool to unregister\n */\n unregisterTool(name: string): void {\n this.nativeContext.unregisterTool(name);\n }\n\n /**\n * Clears all registered tools via the native API.\n * Delegates to navigator.modelContext.clearContext().\n */\n clearContext(): void {\n this.nativeContext.clearContext();\n }\n\n /**\n * Executes a tool via the native API.\n * Delegates to navigator.modelContextTesting.executeTool() with JSON string args.\n *\n * @param {string} toolName - Name of the tool to execute\n * @param {Record<string, unknown>} args - Arguments to pass to the tool\n * @returns {Promise<ToolResponse>} The tool's response in MCP format\n * @internal\n */\n async executeTool(toolName: string, args: Record<string, unknown>): Promise<ToolResponse> {\n try {\n const result = await this.nativeTesting.executeTool(toolName, JSON.stringify(args));\n return this.convertToToolResponse(result);\n } catch (error) {\n nativeLogger.error(`Error executing tool \"${toolName}\":`, error);\n return {\n content: [\n {\n type: 'text',\n text: `Error: ${error instanceof Error ? error.message : String(error)}`,\n },\n ],\n isError: true,\n };\n }\n }\n\n /**\n * Lists all registered tools from the MCP bridge.\n * Returns tools synced from the native API.\n *\n * @returns {Array<{name: string, description: string, inputSchema: InputSchema}>} Array of tool descriptors\n */\n listTools() {\n return Array.from(this.bridge.tools.values()).map((tool) => ({\n name: tool.name,\n description: tool.description,\n inputSchema: tool.inputSchema,\n ...(tool.outputSchema && { outputSchema: tool.outputSchema }),\n ...(tool.annotations && { annotations: tool.annotations }),\n }));\n }\n\n // ==================== RESOURCE METHODS (not yet supported by native API) ====================\n\n /**\n * Registers a resource dynamically.\n * Note: Native Chromium API does not yet support resources.\n * This is a polyfill-only feature.\n */\n registerResource(_resource: ResourceDescriptor): { unregister: () => void } {\n nativeLogger.warn('registerResource is not supported by native API');\n return { unregister: () => {} };\n }\n\n /**\n * Unregisters a resource by URI.\n * Note: Native Chromium API does not yet support resources.\n */\n unregisterResource(_uri: string): void {\n nativeLogger.warn('unregisterResource is not supported by native API');\n }\n\n /**\n * Lists all registered resources.\n * Note: Native Chromium API does not yet support resources.\n */\n listResources(): Resource[] {\n return [];\n }\n\n /**\n * Lists all resource templates.\n * Note: Native Chromium API does not yet support resources.\n */\n listResourceTemplates(): Array<{\n uriTemplate: string;\n name: string;\n description?: string;\n mimeType?: string;\n }> {\n return [];\n }\n\n /**\n * Reads a resource by URI.\n * Note: Native Chromium API does not yet support resources.\n * @internal\n */\n async readResource(_uri: string): Promise<{ contents: ResourceContents[] }> {\n throw new Error('[Native Adapter] readResource is not supported by native API');\n }\n\n // ==================== PROMPT METHODS (not yet supported by native API) ====================\n\n /**\n * Registers a prompt dynamically.\n * Note: Native Chromium API does not yet support prompts.\n * This is a polyfill-only feature.\n */\n registerPrompt<TArgsSchema extends ZodSchemaObject = Record<string, never>>(\n _prompt: PromptDescriptor<TArgsSchema>\n ): { unregister: () => void } {\n nativeLogger.warn('registerPrompt is not supported by native API');\n return { unregister: () => {} };\n }\n\n /**\n * Unregisters a prompt by name.\n * Note: Native Chromium API does not yet support prompts.\n */\n unregisterPrompt(_name: string): void {\n nativeLogger.warn('unregisterPrompt is not supported by native API');\n }\n\n /**\n * Lists all registered prompts.\n * Note: Native Chromium API does not yet support prompts.\n */\n listPrompts(): Prompt[] {\n return [];\n }\n\n /**\n * Gets a prompt with arguments.\n * Note: Native Chromium API does not yet support prompts.\n * @internal\n */\n async getPrompt(\n _name: string,\n _args?: Record<string, unknown>\n ): Promise<{ messages: PromptMessage[] }> {\n throw new Error('[Native Adapter] getPrompt is not supported by native API');\n }\n\n /**\n * Adds an event listener for tool call events.\n * Delegates to the native API's addEventListener.\n *\n * @param {'toolcall'} type - Event type\n * @param {(event: ToolCallEvent) => void | Promise<void>} listener - Event handler\n * @param {boolean | AddEventListenerOptions} [options] - Event listener options\n */\n addEventListener(\n type: 'toolcall',\n listener: (event: ToolCallEvent) => void | Promise<void>,\n options?: boolean | AddEventListenerOptions\n ): void {\n this.nativeContext.addEventListener(type, listener, options);\n }\n\n /**\n * Removes an event listener for tool call events.\n * Delegates to the native API's removeEventListener.\n *\n * @param {'toolcall'} type - Event type\n * @param {(event: ToolCallEvent) => void | Promise<void>} listener - Event handler\n * @param {boolean | EventListenerOptions} [options] - Event listener options\n */\n removeEventListener(\n type: 'toolcall',\n listener: (event: ToolCallEvent) => void | Promise<void>,\n options?: boolean | EventListenerOptions\n ): void {\n this.nativeContext.removeEventListener(type, listener, options);\n }\n\n /**\n * Dispatches a tool call event.\n * Delegates to the native API's dispatchEvent.\n *\n * @param {Event} event - The event to dispatch\n * @returns {boolean} False if event was cancelled, true otherwise\n */\n dispatchEvent(event: Event): boolean {\n return this.nativeContext.dispatchEvent(event);\n }\n\n // ==================== SAMPLING METHODS ====================\n\n /**\n * Request an LLM completion from the connected client.\n * Note: Native Chromium API does not yet support sampling.\n * This is handled by the polyfill.\n */\n async createMessage(params: SamplingRequestParams): Promise<SamplingResult> {\n const server = this.bridge.tabServer;\n\n // Access the underlying Server instance to call createMessage\n const underlyingServer = (\n server as unknown as {\n server: { createMessage: (params: unknown) => Promise<SamplingResult> };\n }\n ).server;\n\n if (!underlyingServer?.createMessage) {\n throw new Error('Sampling is not supported: no connected client with sampling capability');\n }\n\n return underlyingServer.createMessage(params);\n }\n\n // ==================== ELICITATION METHODS ====================\n\n /**\n * Request user input from the connected client.\n * Note: Native Chromium API does not yet support elicitation.\n * This is handled by the polyfill.\n */\n async elicitInput(params: ElicitationParams): Promise<ElicitationResult> {\n const server = this.bridge.tabServer;\n\n // Access the underlying Server instance to call elicitInput\n const underlyingServer = (\n server as unknown as {\n server: { elicitInput: (params: unknown) => Promise<ElicitationResult> };\n }\n ).server;\n\n if (!underlyingServer?.elicitInput) {\n throw new Error(\n 'Elicitation is not supported: no connected client with elicitation capability'\n );\n }\n\n return underlyingServer.elicitInput(params);\n }\n}\n\n/**\n * ToolCallEvent implementation for the Web Model Context API.\n * Represents an event fired when a tool is called, allowing event listeners\n * to intercept and provide custom responses.\n *\n * @class WebToolCallEvent\n * @extends {Event}\n * @implements {ToolCallEvent}\n */\nclass WebToolCallEvent extends Event implements ToolCallEvent {\n public name: string;\n public arguments: Record<string, unknown>;\n private _response: ToolResponse | null = null;\n private _responded = false;\n\n /**\n * Creates a new ToolCallEvent.\n *\n * @param {string} toolName - Name of the tool being called\n * @param {Record<string, unknown>} args - Validated arguments for the tool\n */\n constructor(toolName: string, args: Record<string, unknown>) {\n super('toolcall', { cancelable: true });\n this.name = toolName;\n this.arguments = args;\n }\n\n /**\n * Provides a response for this tool call, preventing the default tool execution.\n *\n * @param {ToolResponse} response - The response to use instead of executing the tool\n * @throws {Error} If a response has already been provided\n */\n respondWith(response: ToolResponse): void {\n if (this._responded) {\n throw new Error('Response already provided for this tool call');\n }\n this._response = response;\n this._responded = true;\n }\n\n /**\n * Gets the response provided via respondWith().\n *\n * @returns {ToolResponse | null} The response, or null if none provided\n */\n getResponse(): ToolResponse | null {\n return this._response;\n }\n\n /**\n * Checks whether a response has been provided for this tool call.\n *\n * @returns {boolean} True if respondWith() was called\n */\n hasResponse(): boolean {\n return this._responded;\n }\n}\n\n/**\n * Time window in milliseconds to detect rapid duplicate tool registrations.\n * Used to filter out double-registrations caused by React Strict Mode.\n */\nconst RAPID_DUPLICATE_WINDOW_MS = 50;\n\n/**\n * Types of lists that can trigger change notifications.\n * Single source of truth for notification batching logic.\n */\ntype ListChangeType = 'tools' | 'resources' | 'prompts';\n\n/**\n * Testing API implementation for the Model Context Protocol.\n * Provides debugging, mocking, and testing capabilities for tool execution.\n * Implements both Chromium native methods and polyfill-specific extensions.\n *\n * @class WebModelContextTesting\n * @implements {ModelContextTesting}\n */\nclass WebModelContextTesting implements ModelContextTesting {\n /**\n * Marker property to identify this as a polyfill implementation.\n * Used by detectNativeAPI() to distinguish polyfill from native Chromium API.\n * This approach works reliably even when class names are minified in production builds.\n *\n * @see POLYFILL_MARKER_PROPERTY - The constant defining this property name\n * @see MayHavePolyfillMarker - The interface for type-safe detection\n */\n readonly [POLYFILL_MARKER_PROPERTY] = true as const;\n\n private toolCallHistory: Array<{\n toolName: string;\n arguments: Record<string, unknown>;\n timestamp: number;\n }> = [];\n private mockResponses: Map<string, ToolResponse> = new Map();\n private toolsChangedCallbacks: Set<() => void> = new Set();\n private bridge: MCPBridge;\n\n /**\n * Creates a new WebModelContextTesting instance.\n *\n * @param {MCPBridge} bridge - The MCP bridge instance to test\n */\n constructor(bridge: MCPBridge) {\n this.bridge = bridge;\n }\n\n /**\n * Records a tool call in the history.\n * Called internally by WebModelContext when tools are executed.\n *\n * @param {string} toolName - Name of the tool that was called\n * @param {Record<string, unknown>} args - Arguments passed to the tool\n * @internal\n */\n recordToolCall(toolName: string, args: Record<string, unknown>): void {\n this.toolCallHistory.push({\n toolName,\n arguments: args,\n timestamp: Date.now(),\n });\n }\n\n /**\n * Checks if a mock response is registered for a specific tool.\n *\n * @param {string} toolName - Name of the tool to check\n * @returns {boolean} True if a mock response exists\n * @internal\n */\n hasMockResponse(toolName: string): boolean {\n return this.mockResponses.has(toolName);\n }\n\n /**\n * Retrieves the mock response for a specific tool.\n *\n * @param {string} toolName - Name of the tool\n * @returns {ToolResponse | undefined} The mock response, or undefined if none exists\n * @internal\n */\n getMockResponse(toolName: string): ToolResponse | undefined {\n return this.mockResponses.get(toolName);\n }\n\n /**\n * Notifies all registered callbacks that the tools list has changed.\n * Called internally when tools are registered, unregistered, or cleared.\n *\n * @internal\n */\n notifyToolsChanged(): void {\n for (const callback of this.toolsChangedCallbacks) {\n try {\n callback();\n } catch (error) {\n testingLogger.error('Error in tools changed callback:', error);\n }\n }\n }\n\n /**\n * Executes a tool directly with JSON string input (Chromium native API).\n * Parses the JSON input, validates it, and executes the tool.\n *\n * @param {string} toolName - Name of the tool to execute\n * @param {string} inputArgsJson - JSON string of input arguments\n * @returns {Promise<unknown>} The tool's result, or undefined on error\n * @throws {SyntaxError} If the input JSON is invalid\n * @throws {Error} If the tool does not exist\n */\n async executeTool(toolName: string, inputArgsJson: string): Promise<unknown> {\n let args: Record<string, unknown>;\n try {\n args = JSON.parse(inputArgsJson);\n } catch (error) {\n throw new SyntaxError(\n `Invalid JSON input: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n\n const tool = this.bridge.tools.get(toolName);\n if (!tool) {\n throw new Error(`Tool not found: ${toolName}`);\n }\n\n const result = await this.bridge.modelContext.executeTool(toolName, args);\n\n if (result.isError) {\n return undefined;\n }\n\n if (result.structuredContent) {\n return result.structuredContent;\n }\n\n if (result.content && result.content.length > 0) {\n const firstContent = result.content[0];\n if (firstContent && firstContent.type === 'text') {\n return firstContent.text;\n }\n }\n\n return undefined;\n }\n\n /**\n * Lists all registered tools with inputSchema as JSON string (Chromium native API).\n * Returns an array of ToolInfo objects where inputSchema is stringified.\n *\n * @returns {Array<{name: string, description: string, inputSchema: string}>} Array of tool information\n */\n listTools(): Array<{ name: string; description: string; inputSchema: string }> {\n const tools = this.bridge.modelContext.listTools();\n return tools.map((tool) => ({\n name: tool.name,\n description: tool.description,\n inputSchema: JSON.stringify(tool.inputSchema),\n }));\n }\n\n /**\n * Registers a callback that fires when the tools list changes (Chromium native API).\n * The callback will be invoked on registerTool, unregisterTool, provideContext, and clearContext.\n *\n * @param {() => void} callback - Function to call when tools change\n */\n registerToolsChangedCallback(callback: () => void): void {\n this.toolsChangedCallbacks.add(callback);\n }\n\n /**\n * Gets all tool calls that have been recorded (polyfill extension).\n *\n * @returns {Array<{toolName: string, arguments: Record<string, unknown>, timestamp: number}>} Tool call history\n */\n getToolCalls(): Array<{\n toolName: string;\n arguments: Record<string, unknown>;\n timestamp: number;\n }> {\n return [...this.toolCallHistory];\n }\n\n /**\n * Clears the tool call history (polyfill extension).\n */\n clearToolCalls(): void {\n this.toolCallHistory = [];\n }\n\n /**\n * Sets a mock response for a specific tool (polyfill extension).\n * When set, the tool's execute function will be bypassed.\n *\n * @param {string} toolName - Name of the tool to mock\n * @param {ToolResponse} response - The mock response to return\n */\n setMockToolResponse(toolName: string, response: ToolResponse): void {\n this.mockResponses.set(toolName, response);\n }\n\n /**\n * Clears the mock response for a specific tool (polyfill extension).\n *\n * @param {string} toolName - Name of the tool\n */\n clearMockToolResponse(toolName: string): void {\n this.mockResponses.delete(toolName);\n }\n\n /**\n * Clears all mock tool responses (polyfill extension).\n */\n clearAllMockToolResponses(): void {\n this.mockResponses.clear();\n }\n\n /**\n * Gets the current tools registered in the system (polyfill extension).\n *\n * @returns {ReturnType<InternalModelContext['listTools']>} Array of registered tools\n */\n getRegisteredTools(): ReturnType<InternalModelContext['listTools']> {\n return this.bridge.modelContext.listTools();\n }\n\n /**\n * Resets the entire testing state (polyfill extension).\n * Clears both tool call history and all mock responses.\n */\n reset(): void {\n this.clearToolCalls();\n this.clearAllMockToolResponses();\n }\n}\n\n/**\n * ModelContext implementation that bridges to the Model Context Protocol SDK.\n * Implements the W3C Web Model Context API proposal with two-bucket tool management:\n * - Bucket A (provideContextTools): Tools registered via provideContext()\n * - Bucket B (dynamicTools): Tools registered via registerTool()\n *\n * This separation ensures that component-scoped dynamic tools persist across\n * app-level provideContext() calls.\n *\n * @class WebModelContext\n * @implements {InternalModelContext}\n */\nclass WebModelContext implements InternalModelContext {\n private bridge: MCPBridge;\n private eventTarget: EventTarget;\n\n // Tool storage (Bucket A = provideContext, Bucket B = dynamic)\n private provideContextTools: Map<string, ValidatedToolDescriptor>;\n private dynamicTools: Map<string, ValidatedToolDescriptor>;\n\n // Resource storage (Bucket A = provideContext, Bucket B = dynamic)\n private provideContextResources: Map<string, ValidatedResourceDescriptor>;\n private dynamicResources: Map<string, ValidatedResourceDescriptor>;\n\n // Prompt storage (Bucket A = provideContext, Bucket B = dynamic)\n private provideContextPrompts: Map<string, ValidatedPromptDescriptor>;\n private dynamicPrompts: Map<string, ValidatedPromptDescriptor>;\n\n // Registration tracking for duplicate detection\n private toolRegistrationTimestamps: Map<string, number>;\n private resourceRegistrationTimestamps: Map<string, number>;\n private promptRegistrationTimestamps: Map<string, number>;\n\n // Unregister functions for dynamic registrations\n private toolUnregisterFunctions: Map<string, () => void>;\n private resourceUnregisterFunctions: Map<string, () => void>;\n private promptUnregisterFunctions: Map<string, () => void>;\n\n /**\n * Tracks which list change notifications are pending.\n * Uses microtask-based batching to coalesce rapid registrations\n * (e.g., React mount phase) into a single notification per list type.\n */\n private pendingNotifications = new Set<ListChangeType>();\n\n private testingAPI?: WebModelContextTesting;\n\n /**\n * Creates a new WebModelContext instance.\n *\n * @param {MCPBridge} bridge - The MCP bridge to use for communication\n */\n constructor(bridge: MCPBridge) {\n this.bridge = bridge;\n this.eventTarget = new EventTarget();\n\n // Initialize tool storage\n this.provideContextTools = new Map();\n this.dynamicTools = new Map();\n this.toolRegistrationTimestamps = new Map();\n this.toolUnregisterFunctions = new Map();\n\n // Initialize resource storage\n this.provideContextResources = new Map();\n this.dynamicResources = new Map();\n this.resourceRegistrationTimestamps = new Map();\n this.resourceUnregisterFunctions = new Map();\n\n // Initialize prompt storage\n this.provideContextPrompts = new Map();\n this.dynamicPrompts = new Map();\n this.promptRegistrationTimestamps = new Map();\n this.promptUnregisterFunctions = new Map();\n }\n\n /**\n * Sets the testing API instance.\n * Called during initialization to enable testing features.\n *\n * @param {WebModelContextTesting} testingAPI - The testing API instance\n * @internal\n */\n setTestingAPI(testingAPI: WebModelContextTesting): void {\n this.testingAPI = testingAPI;\n }\n\n /**\n * Adds an event listener for tool call events.\n *\n * @param {'toolcall'} type - Event type (only 'toolcall' is supported)\n * @param {(event: ToolCallEvent) => void | Promise<void>} listener - Event handler function\n * @param {boolean | AddEventListenerOptions} [options] - Event listener options\n */\n addEventListener(\n type: 'toolcall',\n listener: (event: ToolCallEvent) => void | Promise<void>,\n options?: boolean | AddEventListenerOptions\n ): void {\n this.eventTarget.addEventListener(type, listener as EventListener, options);\n }\n\n /**\n * Removes an event listener for tool call events.\n *\n * @param {'toolcall'} type - Event type (only 'toolcall' is supported)\n * @param {(event: ToolCallEvent) => void | Promise<void>} listener - Event handler function\n * @param {boolean | EventListenerOptions} [options] - Event listener options\n */\n removeEventListener(\n type: 'toolcall',\n listener: (event: ToolCallEvent) => void | Promise<void>,\n options?: boolean | EventListenerOptions\n ): void {\n this.eventTarget.removeEventListener(type, listener as EventListener, options);\n }\n\n /**\n * Dispatches a tool call event to all registered listeners.\n *\n * @param {Event} event - The event to dispatch\n * @returns {boolean} False if event was cancelled, true otherwise\n */\n dispatchEvent(event: Event): boolean {\n return this.eventTarget.dispatchEvent(event);\n }\n\n /**\n * Provides context (tools, resources, prompts) to AI models by registering base items (Bucket A).\n * Clears and replaces all previously registered base items while preserving\n * dynamic items registered via register* methods.\n *\n * @param {ModelContextInput} context - Context containing tools, resources, and prompts to register\n * @throws {Error} If a name/uri collides with existing dynamic items\n */\n provideContext(context: ModelContextInput): void {\n // Clear base items (Bucket A)\n this.provideContextTools.clear();\n this.provideContextResources.clear();\n this.provideContextPrompts.clear();\n\n // Register tools\n for (const tool of context.tools ?? []) {\n // Validate tool name and log warnings for potential compatibility issues\n // NOTE: Similar validation exists in @mcp-b/chrome-devtools-mcp/src/tools/WebMCPToolHub.ts\n // Keep both implementations in sync when making changes.\n if (tool.name.startsWith('_')) {\n logger.warn(\n `⚠️ Warning: Tool name \"${tool.name}\" starts with underscore. ` +\n 'This may cause compatibility issues with some MCP clients. ' +\n 'Consider using a letter as the first character.'\n );\n }\n if (/^[0-9]/.test(tool.name)) {\n logger.warn(\n `⚠️ Warning: Tool name \"${tool.name}\" starts with a number. ` +\n 'This may cause compatibility issues. ' +\n 'Consider using a letter as the first character.'\n );\n }\n if (tool.name.startsWith('-')) {\n logger.warn(\n `⚠️ Warning: Tool name \"${tool.name}\" starts with hyphen. ` +\n 'This may cause compatibility issues. ' +\n 'Consider using a letter as the first character.'\n );\n }\n\n if (this.dynamicTools.has(tool.name)) {\n throw new Error(\n `[Web Model Context] Tool name collision: \"${tool.name}\" is already registered via registerTool(). ` +\n 'Please use a different name or unregister the dynamic tool first.'\n );\n }\n\n const { jsonSchema: inputJson, zodValidator: inputZod } = normalizeSchema(tool.inputSchema);\n const normalizedOutput = tool.outputSchema ? normalizeSchema(tool.outputSchema) : null;\n\n const validatedTool: ValidatedToolDescriptor = {\n name: tool.name,\n description: tool.description,\n inputSchema: inputJson,\n ...(normalizedOutput && { outputSchema: normalizedOutput.jsonSchema }),\n ...(tool.annotations && { annotations: tool.annotations }),\n execute: tool.execute,\n inputValidator: inputZod,\n ...(normalizedOutput && { outputValidator: normalizedOutput.zodValidator }),\n };\n\n this.provideContextTools.set(tool.name, validatedTool);\n }\n\n // Register resources\n for (const resource of context.resources ?? []) {\n if (this.dynamicResources.has(resource.uri)) {\n throw new Error(\n `[Web Model Context] Resource URI collision: \"${resource.uri}\" is already registered via registerResource(). ` +\n 'Please use a different URI or unregister the dynamic resource first.'\n );\n }\n\n const validatedResource = this.validateResource(resource);\n this.provideContextResources.set(resource.uri, validatedResource);\n }\n\n // Register prompts\n for (const prompt of context.prompts ?? []) {\n if (this.dynamicPrompts.has(prompt.name)) {\n throw new Error(\n `[Web Model Context] Prompt name collision: \"${prompt.name}\" is already registered via registerPrompt(). ` +\n 'Please use a different name or unregister the dynamic prompt first.'\n );\n }\n\n const validatedPrompt = this.validatePrompt(prompt);\n this.provideContextPrompts.set(prompt.name, validatedPrompt);\n }\n\n // Update bridge and schedule notifications (batched via microtask)\n this.updateBridgeTools();\n this.updateBridgeResources();\n this.updateBridgePrompts();\n\n this.scheduleListChanged('tools');\n this.scheduleListChanged('resources');\n this.scheduleListChanged('prompts');\n }\n\n /**\n * Validates and normalizes a resource descriptor.\n * @private\n */\n private validateResource(resource: ResourceDescriptor): ValidatedResourceDescriptor {\n // Extract template parameters from URI (e.g., \"file://{path}\" -> [\"path\"])\n // Limit parameter name length to 100 chars to prevent ReDoS on malicious input\n const templateParamRegex = /\\{([^}]{1,100})\\}/g;\n const templateParams: string[] = [];\n for (const match of resource.uri.matchAll(templateParamRegex)) {\n const paramName = match[1]!;\n templateParams.push(paramName);\n }\n\n return {\n uri: resource.uri,\n name: resource.name,\n description: resource.description,\n mimeType: resource.mimeType,\n read: resource.read,\n isTemplate: templateParams.length > 0,\n templateParams,\n };\n }\n\n /**\n * Validates and normalizes a prompt descriptor.\n * @private\n */\n private validatePrompt<TArgsSchema extends ZodSchemaObject>(\n prompt: PromptDescriptor<TArgsSchema>\n ): ValidatedPromptDescriptor {\n let argsSchema: InputSchema | undefined;\n let argsValidator: z.ZodType | undefined;\n\n if (prompt.argsSchema) {\n const normalized = normalizeSchema(prompt.argsSchema);\n argsSchema = normalized.jsonSchema;\n argsValidator = normalized.zodValidator;\n }\n\n return {\n name: prompt.name,\n description: prompt.description,\n argsSchema,\n get: prompt.get as (args: Record<string, unknown>) => Promise<{ messages: PromptMessage[] }>,\n argsValidator,\n };\n }\n\n /**\n * Registers a single tool dynamically (Bucket B).\n * Dynamic tools persist across provideContext() calls and can be independently managed.\n *\n * @param {ToolDescriptor} tool - The tool descriptor to register\n * @returns {{unregister: () => void}} Object with unregister function\n * @throws {Error} If tool name collides with existing tools\n */\n registerTool<\n TInputSchema extends ZodSchemaObject = Record<string, never>,\n TOutputSchema extends ZodSchemaObject = Record<string, never>,\n >(tool: ToolDescriptor<TInputSchema, TOutputSchema>): { unregister: () => void } {\n // Validate tool name and log warnings for potential compatibility issues\n // NOTE: Similar validation exists in @mcp-b/chrome-devtools-mcp/src/tools/WebMCPToolHub.ts\n // Keep both implementations in sync when making changes.\n if (tool.name.startsWith('_')) {\n logger.warn(\n `⚠️ Warning: Tool name \"${tool.name}\" starts with underscore. ` +\n 'This may cause compatibility issues with some MCP clients. ' +\n 'Consider using a letter as the first character.'\n );\n }\n if (/^[0-9]/.test(tool.name)) {\n logger.warn(\n `⚠️ Warning: Tool name \"${tool.name}\" starts with a number. ` +\n 'This may cause compatibility issues. ' +\n 'Consider using a letter as the first character.'\n );\n }\n if (tool.name.startsWith('-')) {\n logger.warn(\n `⚠️ Warning: Tool name \"${tool.name}\" starts with hyphen. ` +\n 'This may cause compatibility issues. ' +\n 'Consider using a letter as the first character.'\n );\n }\n\n const now = Date.now();\n const lastRegistration = this.toolRegistrationTimestamps.get(tool.name);\n\n if (lastRegistration && now - lastRegistration < RAPID_DUPLICATE_WINDOW_MS) {\n logger.warn(\n `Tool \"${tool.name}\" registered multiple times within ${RAPID_DUPLICATE_WINDOW_MS}ms. ` +\n 'This is likely due to React Strict Mode double-mounting. Ignoring duplicate registration.'\n );\n\n const existingUnregister = this.toolUnregisterFunctions.get(tool.name);\n if (existingUnregister) {\n return { unregister: existingUnregister };\n }\n }\n\n if (this.provideContextTools.has(tool.name)) {\n throw new Error(\n `[Web Model Context] Tool name collision: \"${tool.name}\" is already registered via provideContext(). ` +\n 'Please use a different name or update your provideContext() call.'\n );\n }\n\n if (this.dynamicTools.has(tool.name)) {\n throw new Error(\n `[Web Model Context] Tool name collision: \"${tool.name}\" is already registered via registerTool(). ` +\n 'Please unregister it first or use a different name.'\n );\n }\n\n const { jsonSchema: inputJson, zodValidator: inputZod } = normalizeSchema(tool.inputSchema);\n\n const normalizedOutput = tool.outputSchema ? normalizeSchema(tool.outputSchema) : null;\n\n const validatedTool: ValidatedToolDescriptor = {\n name: tool.name,\n description: tool.description,\n inputSchema: inputJson,\n ...(normalizedOutput && { outputSchema: normalizedOutput.jsonSchema }),\n ...(tool.annotations && { annotations: tool.annotations }),\n execute: tool.execute as (args: Record<string, unknown>) => Promise<ToolResponse>,\n inputValidator: inputZod,\n ...(normalizedOutput && { outputValidator: normalizedOutput.zodValidator }),\n };\n\n this.dynamicTools.set(tool.name, validatedTool);\n this.toolRegistrationTimestamps.set(tool.name, now);\n this.updateBridgeTools();\n this.scheduleListChanged('tools');\n\n const unregisterFn = () => {\n if (this.provideContextTools.has(tool.name)) {\n throw new Error(\n `[Web Model Context] Cannot unregister tool \"${tool.name}\": ` +\n 'This tool was registered via provideContext(). Use provideContext() to update the base tool set.'\n );\n }\n\n if (!this.dynamicTools.has(tool.name)) {\n logger.warn(`Tool \"${tool.name}\" is not registered, ignoring unregister call`);\n return;\n }\n\n this.dynamicTools.delete(tool.name);\n this.toolRegistrationTimestamps.delete(tool.name);\n this.toolUnregisterFunctions.delete(tool.name);\n this.updateBridgeTools();\n this.scheduleListChanged('tools');\n };\n\n this.toolUnregisterFunctions.set(tool.name, unregisterFn);\n\n return { unregister: unregisterFn };\n }\n\n // ==================== RESOURCE METHODS ====================\n\n /**\n * Registers a single resource dynamically (Bucket B).\n * Dynamic resources persist across provideContext() calls and can be independently managed.\n *\n * @param {ResourceDescriptor} resource - The resource descriptor to register\n * @returns {{unregister: () => void}} Object with unregister function\n * @throws {Error} If resource URI collides with existing resources\n */\n registerResource(resource: ResourceDescriptor): { unregister: () => void } {\n const now = Date.now();\n const lastRegistration = this.resourceRegistrationTimestamps.get(resource.uri);\n\n if (lastRegistration && now - lastRegistration < RAPID_DUPLICATE_WINDOW_MS) {\n logger.warn(\n `Resource \"${resource.uri}\" registered multiple times within ${RAPID_DUPLICATE_WINDOW_MS}ms. ` +\n 'This is likely due to React Strict Mode double-mounting. Ignoring duplicate registration.'\n );\n\n const existingUnregister = this.resourceUnregisterFunctions.get(resource.uri);\n if (existingUnregister) {\n return { unregister: existingUnregister };\n }\n }\n\n if (this.provideContextResources.has(resource.uri)) {\n throw new Error(\n `[Web Model Context] Resource URI collision: \"${resource.uri}\" is already registered via provideContext(). ` +\n 'Please use a different URI or update your provideContext() call.'\n );\n }\n\n if (this.dynamicResources.has(resource.uri)) {\n throw new Error(\n `[Web Model Context] Resource URI collision: \"${resource.uri}\" is already registered via registerResource(). ` +\n 'Please unregister it first or use a different URI.'\n );\n }\n\n const validatedResource = this.validateResource(resource);\n this.dynamicResources.set(resource.uri, validatedResource);\n this.resourceRegistrationTimestamps.set(resource.uri, now);\n this.updateBridgeResources();\n this.scheduleListChanged('resources');\n\n const unregisterFn = () => {\n if (this.provideContextResources.has(resource.uri)) {\n throw new Error(\n `[Web Model Context] Cannot unregister resource \"${resource.uri}\": ` +\n 'This resource was registered via provideContext(). Use provideContext() to update the base resource set.'\n );\n }\n\n if (!this.dynamicResources.has(resource.uri)) {\n logger.warn(`Resource \"${resource.uri}\" is not registered, ignoring unregister call`);\n return;\n }\n\n this.dynamicResources.delete(resource.uri);\n this.resourceRegistrationTimestamps.delete(resource.uri);\n this.resourceUnregisterFunctions.delete(resource.uri);\n this.updateBridgeResources();\n this.scheduleListChanged('resources');\n };\n\n this.resourceUnregisterFunctions.set(resource.uri, unregisterFn);\n\n return { unregister: unregisterFn };\n }\n\n /**\n * Unregisters a resource by URI.\n * Can unregister resources from either Bucket A (provideContext) or Bucket B (registerResource).\n *\n * @param {string} uri - URI of the resource to unregister\n */\n unregisterResource(uri: string): void {\n const inProvideContext = this.provideContextResources.has(uri);\n const inDynamic = this.dynamicResources.has(uri);\n\n if (!inProvideContext && !inDynamic) {\n logger.warn(`Resource \"${uri}\" is not registered, ignoring unregister call`);\n return;\n }\n\n if (inProvideContext) {\n this.provideContextResources.delete(uri);\n }\n\n if (inDynamic) {\n this.dynamicResources.delete(uri);\n this.resourceRegistrationTimestamps.delete(uri);\n this.resourceUnregisterFunctions.delete(uri);\n }\n\n this.updateBridgeResources();\n this.scheduleListChanged('resources');\n }\n\n /**\n * Lists all registered resources in MCP format.\n * Returns static resources from both buckets (not templates).\n *\n * @returns {Resource[]} Array of resource descriptors\n */\n listResources(): Resource[] {\n return Array.from(this.bridge.resources.values())\n .filter((r) => !r.isTemplate)\n .map((resource) => ({\n uri: resource.uri,\n name: resource.name,\n description: resource.description,\n mimeType: resource.mimeType,\n }));\n }\n\n /**\n * Lists all registered resource templates.\n * Returns only resources with URI templates (dynamic resources).\n *\n * @returns {Array<{uriTemplate: string, name: string, description?: string, mimeType?: string}>}\n */\n listResourceTemplates(): Array<{\n uriTemplate: string;\n name: string;\n description?: string;\n mimeType?: string;\n }> {\n return Array.from(this.bridge.resources.values())\n .filter((r) => r.isTemplate)\n .map((resource) => ({\n uriTemplate: resource.uri,\n name: resource.name,\n ...(resource.description !== undefined && { description: resource.description }),\n ...(resource.mimeType !== undefined && { mimeType: resource.mimeType }),\n }));\n }\n\n // ==================== PROMPT METHODS ====================\n\n /**\n * Registers a single prompt dynamically (Bucket B).\n * Dynamic prompts persist across provideContext() calls and can be independently managed.\n *\n * @param {PromptDescriptor} prompt - The prompt descriptor to register\n * @returns {{unregister: () => void}} Object with unregister function\n * @throws {Error} If prompt name collides with existing prompts\n */\n registerPrompt<TArgsSchema extends ZodSchemaObject = Record<string, never>>(\n prompt: PromptDescriptor<TArgsSchema>\n ): { unregister: () => void } {\n const now = Date.now();\n const lastRegistration = this.promptRegistrationTimestamps.get(prompt.name);\n\n if (lastRegistration && now - lastRegistration < RAPID_DUPLICATE_WINDOW_MS) {\n logger.warn(\n `Prompt \"${prompt.name}\" registered multiple times within ${RAPID_DUPLICATE_WINDOW_MS}ms. ` +\n 'This is likely due to React Strict Mode double-mounting. Ignoring duplicate registration.'\n );\n\n const existingUnregister = this.promptUnregisterFunctions.get(prompt.name);\n if (existingUnregister) {\n return { unregister: existingUnregister };\n }\n }\n\n if (this.provideContextPrompts.has(prompt.name)) {\n throw new Error(\n `[Web Model Context] Prompt name collision: \"${prompt.name}\" is already registered via provideContext(). ` +\n 'Please use a different name or update your provideContext() call.'\n );\n }\n\n if (this.dynamicPrompts.has(prompt.name)) {\n throw new Error(\n `[Web Model Context] Prompt name collision: \"${prompt.name}\" is already registered via registerPrompt(). ` +\n 'Please unregister it first or use a different name.'\n );\n }\n\n const validatedPrompt = this.validatePrompt(prompt);\n this.dynamicPrompts.set(prompt.name, validatedPrompt);\n this.promptRegistrationTimestamps.set(prompt.name, now);\n this.updateBridgePrompts();\n this.scheduleListChanged('prompts');\n\n const unregisterFn = () => {\n if (this.provideContextPrompts.has(prompt.name)) {\n throw new Error(\n `[Web Model Context] Cannot unregister prompt \"${prompt.name}\": ` +\n 'This prompt was registered via provideContext(). Use provideContext() to update the base prompt set.'\n );\n }\n\n if (!this.dynamicPrompts.has(prompt.name)) {\n logger.warn(`Prompt \"${prompt.name}\" is not registered, ignoring unregister call`);\n return;\n }\n\n this.dynamicPrompts.delete(prompt.name);\n this.promptRegistrationTimestamps.delete(prompt.name);\n this.promptUnregisterFunctions.delete(prompt.name);\n this.updateBridgePrompts();\n this.scheduleListChanged('prompts');\n };\n\n this.promptUnregisterFunctions.set(prompt.name, unregisterFn);\n\n return { unregister: unregisterFn };\n }\n\n /**\n * Unregisters a prompt by name.\n * Can unregister prompts from either Bucket A (provideContext) or Bucket B (registerPrompt).\n *\n * @param {string} name - Name of the prompt to unregister\n */\n unregisterPrompt(name: string): void {\n const inProvideContext = this.provideContextPrompts.has(name);\n const inDynamic = this.dynamicPrompts.has(name);\n\n if (!inProvideContext && !inDynamic) {\n logger.warn(`Prompt \"${name}\" is not registered, ignoring unregister call`);\n return;\n }\n\n if (inProvideContext) {\n this.provideContextPrompts.delete(name);\n }\n\n if (inDynamic) {\n this.dynamicPrompts.delete(name);\n this.promptRegistrationTimestamps.delete(name);\n this.promptUnregisterFunctions.delete(name);\n }\n\n this.updateBridgePrompts();\n this.scheduleListChanged('prompts');\n }\n\n /**\n * Lists all registered prompts in MCP format.\n * Returns prompts from both buckets.\n *\n * @returns {Prompt[]} Array of prompt descriptors\n */\n listPrompts(): Prompt[] {\n return Array.from(this.bridge.prompts.values()).map((prompt) => ({\n name: prompt.name,\n description: prompt.description,\n arguments: prompt.argsSchema?.properties\n ? Object.entries(prompt.argsSchema.properties).map(([name, schema]) => ({\n name,\n description: (schema as { description?: string }).description,\n required: prompt.argsSchema?.required?.includes(name) ?? false,\n }))\n : undefined,\n }));\n }\n\n /**\n * Unregisters a tool by name (Chromium native API).\n * Can unregister tools from either Bucket A (provideContext) or Bucket B (registerTool).\n *\n * @param {string} name - Name of the tool to unregister\n */\n unregisterTool(name: string): void {\n const inProvideContext = this.provideContextTools.has(name);\n const inDynamic = this.dynamicTools.has(name);\n\n if (!inProvideContext && !inDynamic) {\n logger.warn(`Tool \"${name}\" is not registered, ignoring unregister call`);\n return;\n }\n\n if (inProvideContext) {\n this.provideContextTools.delete(name);\n }\n\n if (inDynamic) {\n this.dynamicTools.delete(name);\n this.toolRegistrationTimestamps.delete(name);\n this.toolUnregisterFunctions.delete(name);\n }\n\n this.updateBridgeTools();\n this.scheduleListChanged('tools');\n }\n\n /**\n * Clears all registered context from both buckets (Chromium native API).\n * Removes all tools, resources, and prompts registered via provideContext() and register* methods.\n */\n clearContext(): void {\n // Clear tools\n this.provideContextTools.clear();\n this.dynamicTools.clear();\n this.toolRegistrationTimestamps.clear();\n this.toolUnregisterFunctions.clear();\n\n // Clear resources\n this.provideContextResources.clear();\n this.dynamicResources.clear();\n this.resourceRegistrationTimestamps.clear();\n this.resourceUnregisterFunctions.clear();\n\n // Clear prompts\n this.provideContextPrompts.clear();\n this.dynamicPrompts.clear();\n this.promptRegistrationTimestamps.clear();\n this.promptUnregisterFunctions.clear();\n\n // Update bridge\n this.updateBridgeTools();\n this.updateBridgeResources();\n this.updateBridgePrompts();\n\n // Schedule notifications (batched via microtask)\n this.scheduleListChanged('tools');\n this.scheduleListChanged('resources');\n this.scheduleListChanged('prompts');\n }\n\n /**\n * Updates the bridge tools map with merged tools from both buckets.\n * The final tool list is the union of Bucket A (provideContext) and Bucket B (dynamic).\n *\n * @private\n */\n private updateBridgeTools(): void {\n this.bridge.tools.clear();\n\n for (const [name, tool] of this.provideContextTools) {\n this.bridge.tools.set(name, tool);\n }\n\n for (const [name, tool] of this.dynamicTools) {\n this.bridge.tools.set(name, tool);\n }\n }\n\n /**\n * Notifies all servers and testing callbacks that the tools list has changed.\n * Sends MCP notifications to connected servers and invokes registered testing callbacks.\n *\n * @private\n */\n private notifyToolsListChanged(): void {\n if (this.bridge.tabServer.notification) {\n this.bridge.tabServer.notification({\n method: 'notifications/tools/list_changed',\n params: {},\n });\n }\n\n if (this.bridge.iframeServer?.notification) {\n this.bridge.iframeServer.notification({\n method: 'notifications/tools/list_changed',\n params: {},\n });\n }\n\n if (this.testingAPI && 'notifyToolsChanged' in this.testingAPI) {\n (this.testingAPI as WebModelContextTesting).notifyToolsChanged();\n }\n }\n\n /**\n * Updates the bridge resources map with merged resources from both buckets.\n *\n * @private\n */\n private updateBridgeResources(): void {\n this.bridge.resources.clear();\n\n for (const [uri, resource] of this.provideContextResources) {\n this.bridge.resources.set(uri, resource);\n }\n\n for (const [uri, resource] of this.dynamicResources) {\n this.bridge.resources.set(uri, resource);\n }\n }\n\n /**\n * Notifies all servers that the resources list has changed.\n *\n * @private\n */\n private notifyResourcesListChanged(): void {\n if (this.bridge.tabServer.notification) {\n this.bridge.tabServer.notification({\n method: 'notifications/resources/list_changed',\n params: {},\n });\n }\n\n if (this.bridge.iframeServer?.notification) {\n this.bridge.iframeServer.notification({\n method: 'notifications/resources/list_changed',\n params: {},\n });\n }\n }\n\n /**\n * Updates the bridge prompts map with merged prompts from both buckets.\n *\n * @private\n */\n private updateBridgePrompts(): void {\n this.bridge.prompts.clear();\n\n for (const [name, prompt] of this.provideContextPrompts) {\n this.bridge.prompts.set(name, prompt);\n }\n\n for (const [name, prompt] of this.dynamicPrompts) {\n this.bridge.prompts.set(name, prompt);\n }\n }\n\n /**\n * Notifies all servers that the prompts list has changed.\n *\n * @private\n */\n private notifyPromptsListChanged(): void {\n if (this.bridge.tabServer.notification) {\n this.bridge.tabServer.notification({\n method: 'notifications/prompts/list_changed',\n params: {},\n });\n }\n\n if (this.bridge.iframeServer?.notification) {\n this.bridge.iframeServer.notification({\n method: 'notifications/prompts/list_changed',\n params: {},\n });\n }\n }\n\n /**\n * Schedules a list changed notification using microtask batching.\n * Multiple calls for the same list type within the same task are coalesced\n * into a single notification. This dramatically reduces notification spam\n * during React mount/unmount cycles.\n *\n * @param listType - The type of list that changed ('tools' | 'resources' | 'prompts')\n * @private\n */\n private scheduleListChanged(listType: ListChangeType): void {\n if (this.pendingNotifications.has(listType)) return;\n\n this.pendingNotifications.add(listType);\n queueMicrotask(() => {\n this.pendingNotifications.delete(listType);\n\n // Dispatch to the appropriate notification method\n // Exhaustive switch ensures compile-time safety when adding new list types\n switch (listType) {\n case 'tools':\n this.notifyToolsListChanged();\n break;\n case 'resources':\n this.notifyResourcesListChanged();\n break;\n case 'prompts':\n this.notifyPromptsListChanged();\n break;\n default: {\n // Exhaustiveness check: TypeScript will error if a case is missing\n const _exhaustive: never = listType;\n logger.error(`Unknown list type: ${_exhaustive}`);\n }\n }\n });\n }\n\n /**\n * Reads a resource by URI (internal use only by MCP bridge).\n * Handles both static resources and URI templates.\n *\n * @param {string} uri - The URI of the resource to read\n * @returns {Promise<{contents: ResourceContents[]}>} The resource contents\n * @throws {Error} If resource is not found\n * @internal\n */\n async readResource(uri: string): Promise<{ contents: ResourceContents[] }> {\n // First, try to find an exact match (static resource)\n const staticResource = this.bridge.resources.get(uri);\n if (staticResource && !staticResource.isTemplate) {\n try {\n // Try to parse as URL, but fall back to a pseudo-URL for custom schemes\n let parsedUri: URL;\n try {\n parsedUri = new URL(uri);\n } catch {\n // Custom URI scheme (e.g., \"iframe://config\", \"prefix_iframe://config\")\n // Create a pseudo-URL with the original URI as the pathname\n parsedUri = new URL(`custom-scheme:///${encodeURIComponent(uri)}`);\n // Store original URI for handlers that need it\n (parsedUri as URL & { originalUri: string }).originalUri = uri;\n }\n return await staticResource.read(parsedUri);\n } catch (error) {\n logger.error(`Error reading resource ${uri}:`, error);\n throw error;\n }\n }\n\n // Try to match against URI templates\n for (const resource of this.bridge.resources.values()) {\n if (!resource.isTemplate) continue;\n\n const params = this.matchUriTemplate(resource.uri, uri);\n if (params) {\n try {\n // Try to parse as URL, but fall back to a pseudo-URL for custom schemes\n let parsedUri: URL;\n try {\n parsedUri = new URL(uri);\n } catch {\n // Custom URI scheme - create a pseudo-URL\n parsedUri = new URL(`custom-scheme:///${encodeURIComponent(uri)}`);\n (parsedUri as URL & { originalUri: string }).originalUri = uri;\n }\n return await resource.read(parsedUri, params);\n } catch (error) {\n logger.error(`Error reading resource ${uri}:`, error);\n throw error;\n }\n }\n }\n\n throw new Error(`Resource not found: ${uri}`);\n }\n\n /**\n * Matches a URI against a URI template and extracts parameters.\n *\n * @param {string} template - The URI template (e.g., \"file://{path}\")\n * @param {string} uri - The actual URI to match\n * @returns {Record<string, string> | null} Extracted parameters or null if no match\n * @private\n */\n private matchUriTemplate(template: string, uri: string): Record<string, string> | null {\n // Convert template to regex pattern\n // e.g., \"file://{path}\" -> \"^file://(.+)$\" with capture group for \"path\"\n const paramNames: string[] = [];\n let regexPattern = template.replace(/[.*+?^${}()|[\\]\\\\]/g, (char) => {\n // Don't escape { and } - we'll handle them specially\n if (char === '{' || char === '}') return char;\n return `\\\\${char}`;\n });\n\n // Replace {param} with capture groups\n regexPattern = regexPattern.replace(/\\{([^}]+)\\}/g, (_, paramName) => {\n paramNames.push(paramName);\n return '(.+)';\n });\n\n const regex = new RegExp(`^${regexPattern}$`);\n const match = uri.match(regex);\n\n if (!match) return null;\n\n const params: Record<string, string> = {};\n for (let i = 0; i < paramNames.length; i++) {\n const paramName = paramNames[i]!;\n const paramValue = match[i + 1]!;\n params[paramName] = paramValue;\n }\n\n return params;\n }\n\n /**\n * Gets a prompt with arguments (internal use only by MCP bridge).\n *\n * @param {string} name - Name of the prompt\n * @param {Record<string, unknown>} args - Arguments to pass to the prompt\n * @returns {Promise<{messages: PromptMessage[]}>} The prompt messages\n * @throws {Error} If prompt is not found\n * @internal\n */\n async getPrompt(\n name: string,\n args?: Record<string, unknown>\n ): Promise<{ messages: PromptMessage[] }> {\n const prompt = this.bridge.prompts.get(name);\n if (!prompt) {\n throw new Error(`Prompt not found: ${name}`);\n }\n\n // Validate arguments if schema is defined\n if (prompt.argsValidator && args) {\n const validation = validateWithZod(args, prompt.argsValidator);\n if (!validation.success) {\n logger.error(`Argument validation failed for prompt ${name}:`, validation.error);\n throw new Error(`Argument validation error for prompt \"${name}\":\\n${validation.error}`);\n }\n }\n\n try {\n return await prompt.get(args ?? {});\n } catch (error) {\n logger.error(`Error getting prompt ${name}:`, error);\n throw error;\n }\n }\n\n /**\n * Executes a tool with validation and event dispatch.\n * Follows this sequence:\n * 1. Validates input arguments against schema\n * 2. Records tool call in testing API (if available)\n * 3. Checks for mock response (if testing)\n * 4. Dispatches 'toolcall' event to listeners\n * 5. Executes tool function if not prevented\n * 6. Validates output (permissive mode - warns only)\n *\n * @param {string} toolName - Name of the tool to execute\n * @param {Record<string, unknown>} args - Arguments to pass to the tool\n * @returns {Promise<ToolResponse>} The tool's response\n * @throws {Error} If tool is not found\n * @internal\n */\n async executeTool(toolName: string, args: Record<string, unknown>): Promise<ToolResponse> {\n const tool = this.bridge.tools.get(toolName);\n if (!tool) {\n throw new Error(`Tool not found: ${toolName}`);\n }\n\n const validation = validateWithZod(args, tool.inputValidator);\n if (!validation.success) {\n logger.error(`Input validation failed for ${toolName}:`, validation.error);\n return {\n content: [\n {\n type: 'text',\n text: `Input validation error for tool \"${toolName}\":\\n${validation.error}`,\n },\n ],\n isError: true,\n };\n }\n\n const validatedArgs = validation.data as Record<string, unknown>;\n\n if (this.testingAPI) {\n this.testingAPI.recordToolCall(toolName, validatedArgs);\n }\n\n if (this.testingAPI?.hasMockResponse(toolName)) {\n const mockResponse = this.testingAPI.getMockResponse(toolName);\n if (mockResponse) {\n return mockResponse;\n }\n }\n\n const event = new WebToolCallEvent(toolName, validatedArgs);\n\n this.dispatchEvent(event);\n\n if (event.defaultPrevented && event.hasResponse()) {\n const response = event.getResponse();\n if (response) {\n return response;\n }\n }\n\n try {\n const response = await tool.execute(validatedArgs);\n\n if (tool.outputValidator && response.structuredContent) {\n const outputValidation = validateWithZod(response.structuredContent, tool.outputValidator);\n if (!outputValidation.success) {\n logger.warn(`Output validation failed for ${toolName}:`, outputValidation.error);\n }\n }\n\n // Log navigation tools for debugging\n if (\n response.metadata &&\n typeof response.metadata === 'object' &&\n 'willNavigate' in response.metadata\n ) {\n logger.info(`Tool \"${toolName}\" will trigger navigation`, response.metadata);\n }\n\n return response;\n } catch (error) {\n logger.error(`Error executing tool ${toolName}:`, error);\n return {\n content: [\n {\n type: 'text',\n text: `Error: ${error instanceof Error ? error.message : String(error)}`,\n },\n ],\n isError: true,\n };\n }\n }\n\n /**\n * Lists all registered tools in MCP format.\n * Returns tools from both buckets with full MCP specification including\n * annotations and output schemas.\n *\n * @returns {Array<{name: string, description: string, inputSchema: InputSchema, outputSchema?: InputSchema, annotations?: ToolAnnotations}>} Array of tool descriptors\n */\n listTools() {\n return Array.from(this.bridge.tools.values()).map((tool) => ({\n name: tool.name,\n description: tool.description,\n inputSchema: tool.inputSchema,\n ...(tool.outputSchema && { outputSchema: tool.outputSchema }),\n ...(tool.annotations && { annotations: tool.annotations }),\n }));\n }\n\n // ==================== SAMPLING METHODS ====================\n\n /**\n * Request an LLM completion from the connected client.\n * This sends a sampling request to the connected MCP client.\n *\n * @param {SamplingRequestParams} params - Parameters for the sampling request\n * @returns {Promise<SamplingResult>} The LLM completion result\n */\n async createMessage(params: SamplingRequestParams): Promise<SamplingResult> {\n const server = this.bridge.tabServer;\n\n // Access the underlying Server instance to call createMessage\n const underlyingServer = (\n server as unknown as {\n server: { createMessage: (params: unknown) => Promise<SamplingResult> };\n }\n ).server;\n\n if (!underlyingServer?.createMessage) {\n throw new Error('Sampling is not supported: no connected client with sampling capability');\n }\n\n return underlyingServer.createMessage(params);\n }\n\n // ==================== ELICITATION METHODS ====================\n\n /**\n * Request user input from the connected client.\n * This sends an elicitation request to the connected MCP client.\n *\n * @param {ElicitationParams} params - Parameters for the elicitation request\n * @returns {Promise<ElicitationResult>} The user's response\n */\n async elicitInput(params: ElicitationParams): Promise<ElicitationResult> {\n const server = this.bridge.tabServer;\n\n // Access the underlying Server instance to call elicitInput\n const underlyingServer = (\n server as unknown as {\n server: { elicitInput: (params: unknown) => Promise<ElicitationResult> };\n }\n ).server;\n\n if (!underlyingServer?.elicitInput) {\n throw new Error(\n 'Elicitation is not supported: no connected client with elicitation capability'\n );\n }\n\n return underlyingServer.elicitInput(params);\n }\n}\n\n/**\n * Initializes the MCP bridge with dual-server support.\n * Creates TabServer for same-window communication and optionally IframeChildServer\n * for parent-child iframe communication.\n *\n * @param {WebModelContextInitOptions} [options] - Configuration options\n * @returns {MCPBridge} The initialized MCP bridge\n */\nfunction initializeMCPBridge(options?: WebModelContextInitOptions): MCPBridge {\n const hostname = window.location.hostname || 'localhost';\n const transportOptions = options?.transport;\n\n const setupServerHandlers = (server: McpServer, bridge: MCPBridge) => {\n // ==================== TOOL HANDLERS ====================\n server.setRequestHandler(ListToolsRequestSchema, async () => {\n return {\n tools: bridge.modelContext.listTools(),\n };\n });\n\n server.setRequestHandler(CallToolRequestSchema, async (request) => {\n const toolName = request.params.name;\n const args = (request.params.arguments || {}) as Record<string, unknown>;\n\n try {\n const response = await bridge.modelContext.executeTool(toolName, args);\n return {\n content: response.content,\n isError: response.isError,\n ...(response.structuredContent && { structuredContent: response.structuredContent }),\n };\n } catch (error) {\n bridgeLogger.error(`Error calling tool ${toolName}:`, error);\n throw error;\n }\n });\n\n // ==================== RESOURCE HANDLERS ====================\n server.setRequestHandler(ListResourcesRequestSchema, async () => {\n return {\n resources: bridge.modelContext.listResources(),\n // Note: Resource templates are included in the resources list as the MCP SDK\n // doesn't export ListResourceTemplatesRequestSchema separately.\n // Clients can identify templates by checking for URI patterns containing {param}.\n };\n });\n\n server.setRequestHandler(ReadResourceRequestSchema, async (request) => {\n try {\n return await bridge.modelContext.readResource(request.params.uri);\n } catch (error) {\n bridgeLogger.error(`Error reading resource ${request.params.uri}:`, error);\n throw error;\n }\n });\n\n // ==================== PROMPT HANDLERS ====================\n server.setRequestHandler(ListPromptsRequestSchema, async () => {\n return {\n prompts: bridge.modelContext.listPrompts(),\n };\n });\n\n server.setRequestHandler(GetPromptRequestSchema, async (request) => {\n try {\n return await bridge.modelContext.getPrompt(\n request.params.name,\n request.params.arguments as Record<string, unknown> | undefined\n );\n } catch (error) {\n bridgeLogger.error(`Error getting prompt ${request.params.name}:`, error);\n throw error;\n }\n });\n\n // Note: Sampling and elicitation are server-to-client requests.\n // The server calls createMessage() and elicitInput() methods on the Server instance.\n // These are NOT request handlers - the client handles these requests.\n };\n\n const customTransport: Transport | undefined = transportOptions?.create?.();\n\n if (customTransport) {\n const server = new McpServer(\n {\n name: hostname,\n version: '1.0.0',\n },\n {\n capabilities: {\n tools: { listChanged: true },\n resources: { listChanged: true },\n prompts: { listChanged: true },\n },\n }\n );\n\n const bridge: MCPBridge = {\n tabServer: server,\n tools: new Map(),\n resources: new Map(),\n prompts: new Map(),\n modelContext: undefined as unknown as InternalModelContext,\n isInitialized: true,\n };\n\n const modelContext = new WebModelContext(bridge);\n bridge.modelContext = modelContext;\n\n setupServerHandlers(server, bridge);\n server.connect(customTransport);\n\n return bridge;\n }\n\n const tabServerEnabled = transportOptions?.tabServer !== false;\n const tabServer = new McpServer(\n {\n name: `${hostname}-tab`,\n version: '1.0.0',\n },\n {\n capabilities: {\n tools: { listChanged: true },\n resources: { listChanged: true },\n prompts: { listChanged: true },\n },\n }\n );\n\n const bridge: MCPBridge = {\n tabServer,\n tools: new Map(),\n resources: new Map(),\n prompts: new Map(),\n modelContext: undefined as unknown as InternalModelContext,\n isInitialized: true,\n };\n\n const modelContext = new WebModelContext(bridge);\n bridge.modelContext = modelContext;\n\n setupServerHandlers(tabServer, bridge);\n\n if (tabServerEnabled) {\n const tabServerOptions: Partial<TabServerTransportOptions> =\n typeof transportOptions?.tabServer === 'object' ? transportOptions.tabServer : {};\n const { allowedOrigins, ...restTabServerOptions } = tabServerOptions;\n\n const tabTransport = new TabServerTransport({\n allowedOrigins: allowedOrigins ?? ['*'],\n ...(restTabServerOptions as Omit<TabServerTransportOptions, 'allowedOrigins'>),\n });\n\n tabServer.connect(tabTransport);\n }\n\n const isInIframe = typeof window !== 'undefined' && window.parent !== window;\n const iframeServerConfig = transportOptions?.iframeServer;\n const iframeServerEnabled =\n iframeServerConfig !== false && (iframeServerConfig !== undefined || isInIframe);\n\n if (iframeServerEnabled) {\n const iframeServer = new McpServer(\n {\n name: `${hostname}-iframe`,\n version: '1.0.0',\n },\n {\n capabilities: {\n tools: { listChanged: true },\n resources: { listChanged: true },\n prompts: { listChanged: true },\n },\n }\n );\n\n setupServerHandlers(iframeServer, bridge);\n\n const iframeServerOptions: Partial<IframeChildTransportOptions> =\n typeof iframeServerConfig === 'object' ? iframeServerConfig : {};\n const { allowedOrigins, ...restIframeServerOptions } = iframeServerOptions;\n\n const iframeTransport = new IframeChildTransport({\n allowedOrigins: allowedOrigins ?? ['*'],\n ...(restIframeServerOptions as Omit<IframeChildTransportOptions, 'allowedOrigins'>),\n });\n\n iframeServer.connect(iframeTransport);\n bridge.iframeServer = iframeServer;\n }\n\n return bridge;\n}\n\n/**\n * Initializes the Web Model Context API on window.navigator.\n * Creates and exposes navigator.modelContext and navigator.modelContextTesting.\n * Automatically detects and uses native Chromium implementation if available.\n *\n * @param {WebModelContextInitOptions} [options] - Configuration options\n * @throws {Error} If initialization fails\n * @example\n * ```typescript\n * import { initializeWebModelContext } from '@mcp-b/global';\n *\n * initializeWebModelContext({\n * transport: {\n * tabServer: {\n * allowedOrigins: ['https://example.com']\n * }\n * }\n * });\n * ```\n */\nexport function initializeWebModelContext(options?: WebModelContextInitOptions): void {\n /* c8 ignore next 4 */\n if (typeof window === 'undefined') {\n logger.warn('Not in browser environment, skipping initialization');\n return;\n }\n\n const effectiveOptions = options ?? window.__webModelContextOptions;\n const native = detectNativeAPI();\n\n if (native.hasNativeContext && native.hasNativeTesting) {\n const nativeContext = window.navigator.modelContext;\n const nativeTesting = window.navigator.modelContextTesting;\n\n if (!nativeContext || !nativeTesting) {\n logger.error('Native API detection mismatch');\n return;\n }\n\n logger.info('✅ Native Chromium API detected');\n logger.info(' Using native implementation with MCP bridge synchronization');\n logger.info(' Native API will automatically collect tools from embedded iframes');\n\n try {\n const bridge = initializeMCPBridge(effectiveOptions);\n\n const adapter = new NativeModelContextAdapter(bridge, nativeContext, nativeTesting);\n\n bridge.modelContext = adapter;\n bridge.modelContextTesting = nativeTesting;\n\n Object.defineProperty(window, '__mcpBridge', {\n value: bridge,\n writable: false,\n configurable: true,\n });\n\n logger.info('✅ MCP bridge synced with native API');\n logger.info(' MCP clients will receive automatic tool updates from native registry');\n } catch (error) {\n logger.error('Failed to initialize native adapter:', error);\n throw error;\n }\n\n return;\n }\n\n if (native.hasNativeContext && !native.hasNativeTesting) {\n logger.warn('Partial native API detected');\n logger.warn(' navigator.modelContext exists but navigator.modelContextTesting is missing');\n logger.warn(' Cannot sync with native API. Please enable experimental features:');\n logger.warn(' - Navigate to chrome://flags');\n logger.warn(' - Enable \"Experimental Web Platform Features\"');\n logger.warn(' - Or launch with: --enable-experimental-web-platform-features');\n logger.warn(' Skipping initialization to avoid conflicts');\n return;\n }\n\n if (window.navigator.modelContext) {\n logger.warn('window.navigator.modelContext already exists, skipping initialization');\n return;\n }\n\n logger.info('Native API not detected, installing polyfill');\n\n try {\n const bridge = initializeMCPBridge(effectiveOptions);\n\n Object.defineProperty(window.navigator, 'modelContext', {\n value: bridge.modelContext,\n writable: false,\n configurable: false,\n });\n\n Object.defineProperty(window, '__mcpBridge', {\n value: bridge,\n writable: false,\n configurable: true,\n });\n\n logger.info('✅ window.navigator.modelContext initialized successfully');\n\n testingLogger.info('Installing polyfill');\n testingLogger.info(' 💡 To use the native implementation in Chromium:');\n testingLogger.info(' - Navigate to chrome://flags');\n testingLogger.info(' - Enable \"Experimental Web Platform Features\"');\n testingLogger.info(' - Or launch with: --enable-experimental-web-platform-features');\n\n const testingAPI = new WebModelContextTesting(bridge);\n bridge.modelContextTesting = testingAPI;\n\n (bridge.modelContext as WebModelContext).setTestingAPI(testingAPI);\n\n Object.defineProperty(window.navigator, 'modelContextTesting', {\n value: testingAPI,\n writable: false,\n configurable: true,\n });\n\n testingLogger.info('✅ Polyfill installed at window.navigator.modelContextTesting');\n } catch (error) {\n logger.error('Failed to initialize:', error);\n throw error;\n }\n}\n\n/**\n * Cleans up the Web Model Context API.\n * Closes all MCP servers and removes API from window.navigator.\n * Useful for testing and hot module replacement.\n *\n * @example\n * ```typescript\n * import { cleanupWebModelContext } from '@mcp-b/global';\n *\n * cleanupWebModelContext();\n * ```\n */\nexport function cleanupWebModelContext(): void {\n /* c8 ignore next */\n if (typeof window === 'undefined') return;\n\n if (window.__mcpBridge) {\n try {\n window.__mcpBridge.tabServer.close();\n\n if (window.__mcpBridge.iframeServer) {\n window.__mcpBridge.iframeServer.close();\n }\n } catch (error) {\n logger.warn('Error closing MCP servers:', error);\n }\n }\n\n delete (window.navigator as unknown as { modelContext?: unknown }).modelContext;\n delete (window.navigator as unknown as { modelContextTesting?: unknown }).modelContextTesting;\n delete (window as unknown as { __mcpBridge?: unknown }).__mcpBridge;\n\n logger.info('Cleaned up');\n}\n","import { initializeWebModelContext } from './global.js';\nimport { createLogger } from './logger.js';\nimport type { TransportConfiguration, WebModelContextInitOptions } from './types.js';\n\nconst logger = createLogger('WebModelContext');\n\ntype TabServerConfig = NonNullable<TransportConfiguration['tabServer']>;\n\nfunction mergeTransportOptions(\n base: TransportConfiguration,\n override: TransportConfiguration\n): TransportConfiguration {\n if (!base) {\n return override;\n }\n if (!override) {\n return base;\n }\n\n return {\n ...base,\n ...override,\n tabServer: {\n ...(base.tabServer ?? {}),\n ...(override.tabServer ?? {}),\n },\n };\n}\n\nfunction mergeInitOptions(\n base?: WebModelContextInitOptions,\n override?: WebModelContextInitOptions\n): WebModelContextInitOptions | undefined {\n if (!base) {\n return override;\n }\n if (!override) {\n return base;\n }\n\n return {\n ...base,\n ...override,\n transport: mergeTransportOptions(base.transport ?? {}, override.transport ?? {}),\n };\n}\n\nfunction parseScriptTagOptions(\n script: HTMLScriptElement | null\n): WebModelContextInitOptions | undefined {\n if (!script || !script.dataset) {\n return undefined;\n }\n\n const { dataset } = script;\n\n if (dataset.webmcpOptions) {\n try {\n return JSON.parse(dataset.webmcpOptions) as WebModelContextInitOptions;\n } catch (error) {\n logger.error('Invalid JSON in data-webmcp-options:', error);\n return undefined;\n }\n }\n\n const options: WebModelContextInitOptions = {};\n let hasOptions = false;\n\n if (dataset.webmcpAutoInitialize !== undefined) {\n options.autoInitialize = dataset.webmcpAutoInitialize !== 'false';\n hasOptions = true;\n }\n\n const tabServerOptions: TabServerConfig = {};\n let hasTabServerOptions = false;\n\n if (dataset.webmcpAllowedOrigins) {\n const origins = dataset.webmcpAllowedOrigins\n .split(',')\n .map((origin) => origin.trim())\n .filter((origin) => origin.length > 0);\n\n if (origins.length > 0) {\n tabServerOptions.allowedOrigins = origins;\n hasOptions = true;\n hasTabServerOptions = true;\n }\n }\n\n if (dataset.webmcpChannelId) {\n tabServerOptions.channelId = dataset.webmcpChannelId;\n hasOptions = true;\n hasTabServerOptions = true;\n }\n\n if (hasTabServerOptions) {\n options.transport = {\n ...(options.transport ?? {}),\n tabServer: {\n ...(options.transport?.tabServer ?? {}),\n ...tabServerOptions,\n },\n };\n }\n\n return hasOptions ? options : undefined;\n}\n\nif (typeof window !== 'undefined' && typeof document !== 'undefined') {\n const globalOptions = window.__webModelContextOptions;\n const scriptElement = document.currentScript as HTMLScriptElement | null;\n const scriptOptions = parseScriptTagOptions(scriptElement);\n const mergedOptions =\n mergeInitOptions(globalOptions, scriptOptions) ?? globalOptions ?? scriptOptions;\n\n if (mergedOptions) {\n window.__webModelContextOptions = mergedOptions;\n }\n\n const shouldAutoInitialize = mergedOptions?.autoInitialize !== false;\n\n try {\n if (shouldAutoInitialize) {\n initializeWebModelContext(mergedOptions);\n }\n } catch (error) {\n logger.error('Auto-initialization failed:', error);\n }\n}\n\nexport { cleanupWebModelContext, initializeWebModelContext } from './global.js';\nexport { createLogger } from './logger.js';\nexport type * from './types.js';\nexport { zodToJsonSchema } from './valid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;AAErE,KAAI,cACF,QAAO,2BAA2B;CAGpC,MAAM,uBAAuB,eAAe,mBAAmB;AAE/D,KAAI;AACF,MAAI,qBACF,2BAA0B,cAAc;UAEnC,OAAO;AACd,SAAO,MAAM,+BAA+B,MAAM"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["logger","logger","validatedTool: ValidatedToolDescriptor","args: Record<string, unknown>","templateParams: string[]","argsSchema: InputSchema | undefined","argsValidator: z.ZodType | undefined","_exhaustive: never","parsedUri: URL","paramNames: string[]","params: Record<string, string>","bridge","customTransport: Transport | undefined","McpServer","bridge: MCPBridge","options: WebModelContextInitOptions","tabServerOptions: TabServerConfig"],"sources":["../src/logger.ts","../src/validation.ts","../src/global.ts","../src/index.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2025 Google LLC\n * SPDX-License-Identifier: Apache-2.0\n */\n\n/**\n * Lightweight logging system for @mcp-b/global\n *\n * Design Decision: This implements a custom logger instead of using the 'debug'\n * package to reduce bundle size and eliminate external dependencies in the\n * browser build. The API is intentionally simpler, focusing on the specific\n * needs of browser-based MCP implementations.\n *\n * Configuration via localStorage:\n * - localStorage.setItem('WEBMCP_DEBUG', '*') - enable all debug logging\n * - localStorage.setItem('WEBMCP_DEBUG', 'WebModelContext') - enable specific namespace\n * - localStorage.setItem('WEBMCP_DEBUG', 'WebModelContext,NativeAdapter') - multiple namespaces\n * - localStorage.setItem('WEBMCP_DEBUG', 'WebModelContext:') - enable namespace and sub-namespaces\n * - localStorage.removeItem('WEBMCP_DEBUG') - disable debug logging (default)\n *\n * Environment Support:\n * - Automatically detects localStorage availability\n * - Gracefully degrades to \"disabled\" state when localStorage is inaccessible\n * - Never throws errors from configuration checks (safe for private browsing mode)\n */\n\n/** localStorage key for debug configuration */\nconst DEBUG_CONFIG_KEY = 'WEBMCP_DEBUG' as const;\n\n/**\n * Check if debug logging is enabled for a namespace\n *\n * Supports namespace hierarchy via colons. Setting 'WebModelContext' will match\n * both 'WebModelContext' and 'WebModelContext:init', but NOT 'WebModelContextTesting'.\n */\nfunction isDebugEnabled(namespace: string): boolean {\n if (typeof window === 'undefined' || !window.localStorage) {\n return false;\n }\n\n try {\n const debugConfig = localStorage.getItem(DEBUG_CONFIG_KEY);\n if (!debugConfig) return false;\n\n if (debugConfig === '*') return true;\n\n const patterns = debugConfig.split(',').map((p) => p.trim());\n return patterns.some((pattern) => namespace === pattern || namespace.startsWith(`${pattern}:`));\n } catch (err) {\n // localStorage might throw in some browsers (private mode, disabled storage)\n // Log once to console so developers know debug logging is disabled\n if (typeof console !== 'undefined' && console.warn) {\n const message = err instanceof Error ? err.message : String(err);\n console.warn(`[WebMCP] localStorage access failed, debug logging disabled: ${message}`);\n }\n return false;\n }\n}\n\n/**\n * No-op function for disabled log levels\n */\nconst noop = (): void => {};\n\n/**\n * Logger interface with standard log levels\n *\n * All methods accept the same argument patterns as their console.* equivalents,\n * supporting format strings, object inspection, and multiple arguments.\n */\nexport interface Logger {\n /** Debug-level logging (disabled by default, enable via WEBMCP_DEBUG) */\n debug(message?: unknown, ...optionalParams: unknown[]): void;\n /** Info-level logging (disabled by default, enable via WEBMCP_DEBUG) */\n info(message?: unknown, ...optionalParams: unknown[]): void;\n /** Warning-level logging (enabled by default, not gated by WEBMCP_DEBUG) */\n warn(message?: unknown, ...optionalParams: unknown[]): void;\n /** Error-level logging (enabled by default, not gated by WEBMCP_DEBUG) */\n error(message?: unknown, ...optionalParams: unknown[]): void;\n}\n\n/**\n * Create a namespaced logger\n *\n * Uses .bind() to prepend namespace prefixes to console methods without manual\n * string concatenation. Debug enablement is determined at logger creation time\n * for performance - changes to localStorage after creation won't affect existing\n * loggers. Refresh the page to apply new WEBMCP_DEBUG settings.\n *\n * @param namespace - Namespace for the logger (e.g., 'WebModelContext', 'NativeAdapter')\n * @returns Logger instance with debug, info, warn, error methods\n *\n * @example\n * ```typescript\n * const logger = createLogger('WebModelContext');\n * logger.debug('Tool registered:', toolName); // Only shown if WEBMCP_DEBUG includes 'WebModelContext'\n * logger.error('Execution failed:', error); // Always enabled\n * ```\n */\nexport function createLogger(namespace: string): Logger {\n const prefix = `[${namespace}]`;\n\n // Note: Debug enablement is checked once at creation time for performance.\n // Changes to localStorage after creation won't affect existing loggers.\n const isDebug = isDebugEnabled(namespace);\n\n // Create bound console methods that include the namespace prefix\n const boundWarn = console.warn.bind(console, prefix);\n const boundError = console.error.bind(console, prefix);\n const boundLog = console.log.bind(console, prefix);\n\n return {\n // Warnings and errors are always enabled (production-safe)\n warn: boundWarn,\n error: boundError,\n\n // Debug and info are conditional - use bound methods or no-ops\n debug: isDebug ? boundLog : noop,\n info: isDebug ? boundLog : noop,\n };\n}\n","// Import from 'zod' - this works with both Zod 3.25+ (compat layer) and Zod 4\n// In Zod 3.25+, 'zod' exports the Zod 3 compat layer which includes Zod 4 features\n// In Zod 4.x, 'zod' exports Zod 4 directly\n// Users can import { z } from 'zod' and pass schemas to these functions\nimport { z } from 'zod';\nimport { createLogger } from './logger.js';\nimport type { InputSchema } from './types.js';\n\nconst logger = createLogger('WebModelContext');\n\n/**\n * Detect if a schema is a Zod schema object (Record<string, ZodType>)\n * or a JSON Schema object.\n *\n * Uses duck-typing to detect Zod schemas:\n * - Zod 4 schemas have `_zod` property\n * - Zod 3 schemas have `_def` property\n *\n * Both are supported as of Zod 3.25+ (which includes Zod 4 under the hood).\n */\nexport function isZodSchema(schema: unknown): boolean {\n if (typeof schema !== 'object' || schema === null) {\n return false;\n }\n\n // JSON Schema has a 'type' property that's a string\n if ('type' in schema && typeof (schema as { type: unknown }).type === 'string') {\n return false;\n }\n\n const values = Object.values(schema);\n if (values.length === 0) {\n return false;\n }\n\n for (const val of values) {\n if (val == null || typeof val !== 'object') continue;\n const obj = val as object;\n\n // Zod 4 native or Zod 3.25+ compat layer (both have _zod or _def)\n if ('_zod' in obj || '_def' in obj) {\n return true;\n }\n }\n\n return false;\n}\n\n/**\n * Convert JSON Schema to Zod validator.\n * Uses z.fromJSONSchema() which is available in Zod 4.2+.\n *\n * Works with both Zod 3.25+ and Zod 4.\n */\nexport function jsonSchemaToZod(jsonSchema: InputSchema): z.ZodType {\n try {\n const zodSchema = z.fromJSONSchema(jsonSchema as z.core.JSONSchema.BaseSchema);\n return zodSchema;\n } catch (error) {\n logger.warn('Failed to convert JSON Schema to Zod:', error);\n return z.object({}).passthrough();\n }\n}\n\n/**\n * Convert Zod schema object to JSON Schema.\n * Uses the toJSONSchema function from zod/v4.\n *\n * Works with schemas created from both `import { z } from 'zod'` (Zod 3.25+ compat)\n * and `import { z } from 'zod/v4'` (native Zod 4).\n *\n * @param schema - Record of Zod type definitions (e.g., { name: z.string(), age: z.number() })\n * @returns JSON Schema object compatible with MCP InputSchema\n */\nexport function zodToJsonSchema(schema: Record<string, z.ZodTypeAny>): InputSchema {\n const zodObject = z.object(schema);\n // Use z.toJSONSchema() - available in both Zod 3.25+ (via compat) and Zod 4\n const jsonSchema = z.toJSONSchema(zodObject);\n\n // Remove $schema field as it's not needed for MCP\n const { $schema: _, ...rest } = jsonSchema as { $schema?: string } & InputSchema;\n return rest as InputSchema;\n}\n\n/**\n * Normalize a schema to both JSON Schema and Zod formats.\n * Detects which format is provided and converts to the other.\n *\n * Supports:\n * - Zod schemas from `import { z } from 'zod'` (Zod 3.25+ compat layer)\n * - Zod schemas from `import { z } from 'zod/v4'` (native Zod 4)\n * - Plain JSON Schema objects\n */\nexport function normalizeSchema(schema: InputSchema | Record<string, z.ZodTypeAny>): {\n jsonSchema: InputSchema;\n zodValidator: z.ZodType;\n} {\n if (isZodSchema(schema)) {\n const jsonSchema = zodToJsonSchema(schema as Record<string, z.ZodTypeAny>);\n const zodValidator = z.object(schema as Record<string, z.ZodTypeAny>);\n return { jsonSchema, zodValidator };\n }\n\n const jsonSchema = schema as InputSchema;\n const zodValidator = jsonSchemaToZod(jsonSchema);\n return { jsonSchema, zodValidator };\n}\n\n/**\n * Validate data with Zod schema and return formatted result\n */\nexport function validateWithZod(\n data: unknown,\n validator: z.ZodType\n): { success: true; data: unknown } | { success: false; error: string } {\n const result = validator.safeParse(data);\n\n if (!result.success) {\n const errors = result.error.issues\n .map((err) => ` - ${err.path.join('.') || 'root'}: ${err.message}`)\n .join('\\n');\n return {\n success: false,\n error: `Validation failed:\\n${errors}`,\n };\n }\n\n return {\n success: true,\n data: result.data,\n };\n}\n","import {\n IframeChildTransport,\n type IframeChildTransportOptions,\n TabServerTransport,\n type TabServerTransportOptions,\n} from '@mcp-b/transports';\nimport type {\n Prompt,\n PromptMessage,\n Resource,\n ResourceContents,\n Transport,\n} from '@mcp-b/webmcp-ts-sdk';\nimport {\n CallToolRequestSchema,\n GetPromptRequestSchema,\n ListPromptsRequestSchema,\n ListResourcesRequestSchema,\n ListToolsRequestSchema,\n Server as McpServer,\n ReadResourceRequestSchema,\n} from '@mcp-b/webmcp-ts-sdk';\nimport type { z } from 'zod';\nimport { createLogger } from './logger.js';\nimport type {\n ElicitationParams,\n ElicitationResult,\n InputSchema,\n InternalModelContext,\n MCPBridge,\n ModelContext,\n ModelContextInput,\n ModelContextTesting,\n PromptDescriptor,\n ResourceDescriptor,\n SamplingRequestParams,\n SamplingResult,\n ToolCallEvent,\n ToolDescriptor,\n ToolResponse,\n ValidatedPromptDescriptor,\n ValidatedResourceDescriptor,\n ValidatedToolDescriptor,\n WebModelContextInitOptions,\n ZodSchemaObject,\n} from './types.js';\nimport { jsonSchemaToZod, normalizeSchema, validateWithZod } from './validation.js';\n\n// Create namespaced loggers for different components\nconst logger = createLogger('WebModelContext');\nconst nativeLogger = createLogger('NativeAdapter');\nconst bridgeLogger = createLogger('MCPBridge');\nconst testingLogger = createLogger('ModelContextTesting');\n\ndeclare global {\n interface Window {\n __webModelContextOptions?: WebModelContextInitOptions;\n }\n}\n\n/**\n * Marker property name used to identify polyfill implementations.\n * This constant ensures single source of truth for the marker used in\n * both detection (detectNativeAPI) and definition (WebModelContextTesting).\n */\nconst POLYFILL_MARKER_PROPERTY = '__isWebMCPPolyfill' as const;\n\n/**\n * Type guard interface for objects that may have the polyfill marker.\n * Used for type-safe detection of polyfill vs native implementations.\n */\ninterface MayHavePolyfillMarker {\n [POLYFILL_MARKER_PROPERTY]?: true;\n}\n\n/**\n * Detect if the native Chromium Web Model Context API is available.\n * Checks for both navigator.modelContext and navigator.modelContextTesting,\n * and verifies they are native implementations (not polyfills).\n *\n * Detection uses a marker property (`__isWebMCPPolyfill`) on the testing API\n * to reliably distinguish polyfills from native implementations. This approach\n * works correctly even when class names are minified in production builds.\n *\n * @returns Detection result with flags for native context and testing API availability\n */\nfunction detectNativeAPI(): {\n hasNativeContext: boolean;\n hasNativeTesting: boolean;\n} {\n /* c8 ignore next 2 */\n if (typeof window === 'undefined' || typeof navigator === 'undefined') {\n return { hasNativeContext: false, hasNativeTesting: false };\n }\n\n const modelContext = navigator.modelContext;\n const modelContextTesting = navigator.modelContextTesting;\n\n if (!modelContext || !modelContextTesting) {\n return {\n hasNativeContext: Boolean(modelContext),\n hasNativeTesting: Boolean(modelContextTesting),\n };\n }\n\n // Check for polyfill marker property.\n // This is more reliable than constructor name checking, which fails when\n // class names are minified in production builds.\n const isPolyfill =\n POLYFILL_MARKER_PROPERTY in modelContextTesting &&\n (modelContextTesting as MayHavePolyfillMarker)[POLYFILL_MARKER_PROPERTY] === true;\n\n if (isPolyfill) {\n return { hasNativeContext: false, hasNativeTesting: false };\n }\n\n return { hasNativeContext: true, hasNativeTesting: true };\n}\n\n/**\n * Adapter that wraps the native Chromium Web Model Context API.\n * Synchronizes tool changes from the native API to the MCP bridge,\n * enabling MCP clients to stay in sync with the native tool registry.\n *\n * Key features:\n * - Listens to native tool changes via registerToolsChangedCallback()\n * - Syncs native tools to MCP bridge automatically\n * - Delegates tool execution to native API\n * - Converts native results to MCP ToolResponse format\n *\n * @class NativeModelContextAdapter\n * @implements {InternalModelContext}\n */\nclass NativeModelContextAdapter implements InternalModelContext {\n private nativeContext: ModelContext;\n private nativeTesting: ModelContextTesting;\n private bridge: MCPBridge;\n private syncInProgress = false;\n\n /**\n * Creates a new NativeModelContextAdapter.\n *\n * @param {MCPBridge} bridge - The MCP bridge instance\n * @param {ModelContext} nativeContext - The native navigator.modelContext\n * @param {ModelContextTesting} nativeTesting - The native navigator.modelContextTesting\n */\n constructor(bridge: MCPBridge, nativeContext: ModelContext, nativeTesting: ModelContextTesting) {\n this.bridge = bridge;\n this.nativeContext = nativeContext;\n this.nativeTesting = nativeTesting;\n\n this.nativeTesting.registerToolsChangedCallback(() => {\n this.syncToolsFromNative();\n });\n\n this.syncToolsFromNative();\n }\n\n /**\n * Synchronizes tools from the native API to the MCP bridge.\n * Fetches all tools from navigator.modelContextTesting.listTools()\n * and updates the bridge's tool registry.\n *\n * @private\n */\n private syncToolsFromNative(): void {\n if (this.syncInProgress) {\n return;\n }\n\n this.syncInProgress = true;\n\n try {\n const nativeTools = this.nativeTesting.listTools();\n\n this.bridge.tools.clear();\n\n for (const toolInfo of nativeTools) {\n try {\n const inputSchema = JSON.parse(toolInfo.inputSchema);\n\n const validatedTool: ValidatedToolDescriptor = {\n name: toolInfo.name,\n description: toolInfo.description,\n inputSchema: inputSchema,\n execute: async (args: Record<string, unknown>) => {\n const result = await this.nativeTesting.executeTool(\n toolInfo.name,\n JSON.stringify(args)\n );\n return this.convertToToolResponse(result);\n },\n inputValidator: jsonSchemaToZod(inputSchema),\n };\n\n this.bridge.tools.set(toolInfo.name, validatedTool);\n } catch (error) {\n nativeLogger.error(`Failed to sync tool \"${toolInfo.name}\":`, error);\n }\n }\n\n this.notifyMCPServers();\n } finally {\n this.syncInProgress = false;\n }\n }\n\n /**\n * Converts native API result to MCP ToolResponse format.\n * Native API returns simplified values (string, number, object, etc.)\n * which need to be wrapped in the MCP CallToolResult format.\n *\n * @param {unknown} result - The result from native executeTool()\n * @returns {ToolResponse} Formatted MCP ToolResponse\n * @private\n */\n private convertToToolResponse(result: unknown): ToolResponse {\n if (typeof result === 'string') {\n return { content: [{ type: 'text', text: result }] };\n }\n\n if (result === undefined || result === null) {\n return { content: [{ type: 'text', text: '' }] };\n }\n\n if (typeof result === 'object') {\n return {\n content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],\n structuredContent: result as Record<string, unknown>,\n };\n }\n\n return {\n content: [{ type: 'text', text: String(result) }],\n };\n }\n\n /**\n * Notifies all connected MCP servers that the tools list has changed.\n *\n * @private\n */\n private notifyMCPServers(): void {\n if (this.bridge.tabServer?.notification) {\n this.bridge.tabServer.notification({\n method: 'notifications/tools/list_changed',\n params: {},\n });\n }\n\n if (this.bridge.iframeServer?.notification) {\n this.bridge.iframeServer.notification({\n method: 'notifications/tools/list_changed',\n params: {},\n });\n }\n }\n\n /**\n * Provides context (tools) to AI models via the native API.\n * Delegates to navigator.modelContext.provideContext().\n * Tool change callback will fire and trigger sync automatically.\n *\n * @param {ModelContextInput} context - Context containing tools to register\n */\n provideContext(context: ModelContextInput): void {\n this.nativeContext.provideContext(context);\n }\n\n /**\n * Registers a single tool dynamically via the native API.\n * Delegates to navigator.modelContext.registerTool().\n * Tool change callback will fire and trigger sync automatically.\n *\n * @param {ToolDescriptor} tool - The tool descriptor to register\n * @returns {{unregister: () => void}} Object with unregister function\n */\n registerTool<\n TInputSchema extends ZodSchemaObject = Record<string, never>,\n TOutputSchema extends ZodSchemaObject = Record<string, never>,\n >(tool: ToolDescriptor<TInputSchema, TOutputSchema>): { unregister: () => void } {\n const result = this.nativeContext.registerTool(tool);\n return result;\n }\n\n /**\n * Unregisters a tool by name via the native API.\n * Delegates to navigator.modelContext.unregisterTool().\n *\n * @param {string} name - Name of the tool to unregister\n */\n unregisterTool(name: string): void {\n this.nativeContext.unregisterTool(name);\n }\n\n /**\n * Clears all registered tools via the native API.\n * Delegates to navigator.modelContext.clearContext().\n */\n clearContext(): void {\n this.nativeContext.clearContext();\n }\n\n /**\n * Executes a tool via the native API.\n * Delegates to navigator.modelContextTesting.executeTool() with JSON string args.\n *\n * @param {string} toolName - Name of the tool to execute\n * @param {Record<string, unknown>} args - Arguments to pass to the tool\n * @returns {Promise<ToolResponse>} The tool's response in MCP format\n * @internal\n */\n async executeTool(toolName: string, args: Record<string, unknown>): Promise<ToolResponse> {\n try {\n const result = await this.nativeTesting.executeTool(toolName, JSON.stringify(args));\n return this.convertToToolResponse(result);\n } catch (error) {\n nativeLogger.error(`Error executing tool \"${toolName}\":`, error);\n return {\n content: [\n {\n type: 'text',\n text: `Error: ${error instanceof Error ? error.message : String(error)}`,\n },\n ],\n isError: true,\n };\n }\n }\n\n /**\n * Lists all registered tools from the MCP bridge.\n * Returns tools synced from the native API.\n *\n * @returns {Array<{name: string, description: string, inputSchema: InputSchema}>} Array of tool descriptors\n */\n listTools() {\n return Array.from(this.bridge.tools.values()).map((tool) => ({\n name: tool.name,\n description: tool.description,\n inputSchema: tool.inputSchema,\n ...(tool.outputSchema && { outputSchema: tool.outputSchema }),\n ...(tool.annotations && { annotations: tool.annotations }),\n }));\n }\n\n // ==================== RESOURCE METHODS (not yet supported by native API) ====================\n\n /**\n * Registers a resource dynamically.\n * Note: Native Chromium API does not yet support resources.\n * This is a polyfill-only feature.\n */\n registerResource(_resource: ResourceDescriptor): { unregister: () => void } {\n nativeLogger.warn('registerResource is not supported by native API');\n return { unregister: () => {} };\n }\n\n /**\n * Unregisters a resource by URI.\n * Note: Native Chromium API does not yet support resources.\n */\n unregisterResource(_uri: string): void {\n nativeLogger.warn('unregisterResource is not supported by native API');\n }\n\n /**\n * Lists all registered resources.\n * Note: Native Chromium API does not yet support resources.\n */\n listResources(): Resource[] {\n return [];\n }\n\n /**\n * Lists all resource templates.\n * Note: Native Chromium API does not yet support resources.\n */\n listResourceTemplates(): Array<{\n uriTemplate: string;\n name: string;\n description?: string;\n mimeType?: string;\n }> {\n return [];\n }\n\n /**\n * Reads a resource by URI.\n * Note: Native Chromium API does not yet support resources.\n * @internal\n */\n async readResource(_uri: string): Promise<{ contents: ResourceContents[] }> {\n throw new Error('[Native Adapter] readResource is not supported by native API');\n }\n\n // ==================== PROMPT METHODS (not yet supported by native API) ====================\n\n /**\n * Registers a prompt dynamically.\n * Note: Native Chromium API does not yet support prompts.\n * This is a polyfill-only feature.\n */\n registerPrompt<TArgsSchema extends ZodSchemaObject = Record<string, never>>(\n _prompt: PromptDescriptor<TArgsSchema>\n ): { unregister: () => void } {\n nativeLogger.warn('registerPrompt is not supported by native API');\n return { unregister: () => {} };\n }\n\n /**\n * Unregisters a prompt by name.\n * Note: Native Chromium API does not yet support prompts.\n */\n unregisterPrompt(_name: string): void {\n nativeLogger.warn('unregisterPrompt is not supported by native API');\n }\n\n /**\n * Lists all registered prompts.\n * Note: Native Chromium API does not yet support prompts.\n */\n listPrompts(): Prompt[] {\n return [];\n }\n\n /**\n * Gets a prompt with arguments.\n * Note: Native Chromium API does not yet support prompts.\n * @internal\n */\n async getPrompt(\n _name: string,\n _args?: Record<string, unknown>\n ): Promise<{ messages: PromptMessage[] }> {\n throw new Error('[Native Adapter] getPrompt is not supported by native API');\n }\n\n /**\n * Adds an event listener for tool call events.\n * Delegates to the native API's addEventListener.\n *\n * @param {'toolcall'} type - Event type\n * @param {(event: ToolCallEvent) => void | Promise<void>} listener - Event handler\n * @param {boolean | AddEventListenerOptions} [options] - Event listener options\n */\n addEventListener(\n type: 'toolcall',\n listener: (event: ToolCallEvent) => void | Promise<void>,\n options?: boolean | AddEventListenerOptions\n ): void {\n this.nativeContext.addEventListener(type, listener, options);\n }\n\n /**\n * Removes an event listener for tool call events.\n * Delegates to the native API's removeEventListener.\n *\n * @param {'toolcall'} type - Event type\n * @param {(event: ToolCallEvent) => void | Promise<void>} listener - Event handler\n * @param {boolean | EventListenerOptions} [options] - Event listener options\n */\n removeEventListener(\n type: 'toolcall',\n listener: (event: ToolCallEvent) => void | Promise<void>,\n options?: boolean | EventListenerOptions\n ): void {\n this.nativeContext.removeEventListener(type, listener, options);\n }\n\n /**\n * Dispatches a tool call event.\n * Delegates to the native API's dispatchEvent.\n *\n * @param {Event} event - The event to dispatch\n * @returns {boolean} False if event was cancelled, true otherwise\n */\n dispatchEvent(event: Event): boolean {\n return this.nativeContext.dispatchEvent(event);\n }\n\n // ==================== SAMPLING METHODS ====================\n\n /**\n * Request an LLM completion from the connected client.\n * Note: Native Chromium API does not yet support sampling.\n * This is handled by the polyfill.\n */\n async createMessage(params: SamplingRequestParams): Promise<SamplingResult> {\n const server = this.bridge.tabServer;\n\n // Access the underlying Server instance to call createMessage\n const underlyingServer = (\n server as unknown as {\n server: { createMessage: (params: unknown) => Promise<SamplingResult> };\n }\n ).server;\n\n if (!underlyingServer?.createMessage) {\n throw new Error('Sampling is not supported: no connected client with sampling capability');\n }\n\n return underlyingServer.createMessage(params);\n }\n\n // ==================== ELICITATION METHODS ====================\n\n /**\n * Request user input from the connected client.\n * Note: Native Chromium API does not yet support elicitation.\n * This is handled by the polyfill.\n */\n async elicitInput(params: ElicitationParams): Promise<ElicitationResult> {\n const server = this.bridge.tabServer;\n\n // Access the underlying Server instance to call elicitInput\n const underlyingServer = (\n server as unknown as {\n server: { elicitInput: (params: unknown) => Promise<ElicitationResult> };\n }\n ).server;\n\n if (!underlyingServer?.elicitInput) {\n throw new Error(\n 'Elicitation is not supported: no connected client with elicitation capability'\n );\n }\n\n return underlyingServer.elicitInput(params);\n }\n}\n\n/**\n * ToolCallEvent implementation for the Web Model Context API.\n * Represents an event fired when a tool is called, allowing event listeners\n * to intercept and provide custom responses.\n *\n * @class WebToolCallEvent\n * @extends {Event}\n * @implements {ToolCallEvent}\n */\nclass WebToolCallEvent extends Event implements ToolCallEvent {\n public name: string;\n public arguments: Record<string, unknown>;\n private _response: ToolResponse | null = null;\n private _responded = false;\n\n /**\n * Creates a new ToolCallEvent.\n *\n * @param {string} toolName - Name of the tool being called\n * @param {Record<string, unknown>} args - Validated arguments for the tool\n */\n constructor(toolName: string, args: Record<string, unknown>) {\n super('toolcall', { cancelable: true });\n this.name = toolName;\n this.arguments = args;\n }\n\n /**\n * Provides a response for this tool call, preventing the default tool execution.\n *\n * @param {ToolResponse} response - The response to use instead of executing the tool\n * @throws {Error} If a response has already been provided\n */\n respondWith(response: ToolResponse): void {\n if (this._responded) {\n throw new Error('Response already provided for this tool call');\n }\n this._response = response;\n this._responded = true;\n }\n\n /**\n * Gets the response provided via respondWith().\n *\n * @returns {ToolResponse | null} The response, or null if none provided\n */\n getResponse(): ToolResponse | null {\n return this._response;\n }\n\n /**\n * Checks whether a response has been provided for this tool call.\n *\n * @returns {boolean} True if respondWith() was called\n */\n hasResponse(): boolean {\n return this._responded;\n }\n}\n\n/**\n * Time window in milliseconds to detect rapid duplicate tool registrations.\n * Used to filter out double-registrations caused by React Strict Mode.\n */\nconst RAPID_DUPLICATE_WINDOW_MS = 50;\n\n/**\n * Types of lists that can trigger change notifications.\n * Single source of truth for notification batching logic.\n */\ntype ListChangeType = 'tools' | 'resources' | 'prompts';\n\n/**\n * Testing API implementation for the Model Context Protocol.\n * Provides debugging, mocking, and testing capabilities for tool execution.\n * Implements both Chromium native methods and polyfill-specific extensions.\n *\n * @class WebModelContextTesting\n * @implements {ModelContextTesting}\n */\nclass WebModelContextTesting implements ModelContextTesting {\n /**\n * Marker property to identify this as a polyfill implementation.\n * Used by detectNativeAPI() to distinguish polyfill from native Chromium API.\n * This approach works reliably even when class names are minified in production builds.\n *\n * @see POLYFILL_MARKER_PROPERTY - The constant defining this property name\n * @see MayHavePolyfillMarker - The interface for type-safe detection\n */\n readonly [POLYFILL_MARKER_PROPERTY] = true as const;\n\n private toolCallHistory: Array<{\n toolName: string;\n arguments: Record<string, unknown>;\n timestamp: number;\n }> = [];\n private mockResponses: Map<string, ToolResponse> = new Map();\n private toolsChangedCallbacks: Set<() => void> = new Set();\n private bridge: MCPBridge;\n\n /**\n * Creates a new WebModelContextTesting instance.\n *\n * @param {MCPBridge} bridge - The MCP bridge instance to test\n */\n constructor(bridge: MCPBridge) {\n this.bridge = bridge;\n }\n\n /**\n * Records a tool call in the history.\n * Called internally by WebModelContext when tools are executed.\n *\n * @param {string} toolName - Name of the tool that was called\n * @param {Record<string, unknown>} args - Arguments passed to the tool\n * @internal\n */\n recordToolCall(toolName: string, args: Record<string, unknown>): void {\n this.toolCallHistory.push({\n toolName,\n arguments: args,\n timestamp: Date.now(),\n });\n }\n\n /**\n * Checks if a mock response is registered for a specific tool.\n *\n * @param {string} toolName - Name of the tool to check\n * @returns {boolean} True if a mock response exists\n * @internal\n */\n hasMockResponse(toolName: string): boolean {\n return this.mockResponses.has(toolName);\n }\n\n /**\n * Retrieves the mock response for a specific tool.\n *\n * @param {string} toolName - Name of the tool\n * @returns {ToolResponse | undefined} The mock response, or undefined if none exists\n * @internal\n */\n getMockResponse(toolName: string): ToolResponse | undefined {\n return this.mockResponses.get(toolName);\n }\n\n /**\n * Notifies all registered callbacks that the tools list has changed.\n * Called internally when tools are registered, unregistered, or cleared.\n *\n * @internal\n */\n notifyToolsChanged(): void {\n for (const callback of this.toolsChangedCallbacks) {\n try {\n callback();\n } catch (error) {\n testingLogger.error('Error in tools changed callback:', error);\n }\n }\n }\n\n /**\n * Executes a tool directly with JSON string input (Chromium native API).\n * Parses the JSON input, validates it, and executes the tool.\n *\n * @param {string} toolName - Name of the tool to execute\n * @param {string} inputArgsJson - JSON string of input arguments\n * @returns {Promise<unknown>} The tool's result, or undefined on error\n * @throws {SyntaxError} If the input JSON is invalid\n * @throws {Error} If the tool does not exist\n */\n async executeTool(toolName: string, inputArgsJson: string): Promise<unknown> {\n let args: Record<string, unknown>;\n try {\n args = JSON.parse(inputArgsJson);\n } catch (error) {\n throw new SyntaxError(\n `Invalid JSON input: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n\n const tool = this.bridge.tools.get(toolName);\n if (!tool) {\n throw new Error(`Tool not found: ${toolName}`);\n }\n\n const result = await this.bridge.modelContext.executeTool(toolName, args);\n\n if (result.isError) {\n return undefined;\n }\n\n if (result.structuredContent) {\n return result.structuredContent;\n }\n\n if (result.content && result.content.length > 0) {\n const firstContent = result.content[0];\n if (firstContent && firstContent.type === 'text') {\n return firstContent.text;\n }\n }\n\n return undefined;\n }\n\n /**\n * Lists all registered tools with inputSchema as JSON string (Chromium native API).\n * Returns an array of ToolInfo objects where inputSchema is stringified.\n *\n * @returns {Array<{name: string, description: string, inputSchema: string}>} Array of tool information\n */\n listTools(): Array<{ name: string; description: string; inputSchema: string }> {\n const tools = this.bridge.modelContext.listTools();\n return tools.map((tool) => ({\n name: tool.name,\n description: tool.description,\n inputSchema: JSON.stringify(tool.inputSchema),\n }));\n }\n\n /**\n * Registers a callback that fires when the tools list changes (Chromium native API).\n * The callback will be invoked on registerTool, unregisterTool, provideContext, and clearContext.\n *\n * @param {() => void} callback - Function to call when tools change\n */\n registerToolsChangedCallback(callback: () => void): void {\n this.toolsChangedCallbacks.add(callback);\n }\n\n /**\n * Gets all tool calls that have been recorded (polyfill extension).\n *\n * @returns {Array<{toolName: string, arguments: Record<string, unknown>, timestamp: number}>} Tool call history\n */\n getToolCalls(): Array<{\n toolName: string;\n arguments: Record<string, unknown>;\n timestamp: number;\n }> {\n return [...this.toolCallHistory];\n }\n\n /**\n * Clears the tool call history (polyfill extension).\n */\n clearToolCalls(): void {\n this.toolCallHistory = [];\n }\n\n /**\n * Sets a mock response for a specific tool (polyfill extension).\n * When set, the tool's execute function will be bypassed.\n *\n * @param {string} toolName - Name of the tool to mock\n * @param {ToolResponse} response - The mock response to return\n */\n setMockToolResponse(toolName: string, response: ToolResponse): void {\n this.mockResponses.set(toolName, response);\n }\n\n /**\n * Clears the mock response for a specific tool (polyfill extension).\n *\n * @param {string} toolName - Name of the tool\n */\n clearMockToolResponse(toolName: string): void {\n this.mockResponses.delete(toolName);\n }\n\n /**\n * Clears all mock tool responses (polyfill extension).\n */\n clearAllMockToolResponses(): void {\n this.mockResponses.clear();\n }\n\n /**\n * Gets the current tools registered in the system (polyfill extension).\n *\n * @returns {ReturnType<InternalModelContext['listTools']>} Array of registered tools\n */\n getRegisteredTools(): ReturnType<InternalModelContext['listTools']> {\n return this.bridge.modelContext.listTools();\n }\n\n /**\n * Resets the entire testing state (polyfill extension).\n * Clears both tool call history and all mock responses.\n */\n reset(): void {\n this.clearToolCalls();\n this.clearAllMockToolResponses();\n }\n}\n\n/**\n * ModelContext implementation that bridges to the Model Context Protocol SDK.\n * Implements the W3C Web Model Context API proposal with two-bucket tool management:\n * - Bucket A (provideContextTools): Tools registered via provideContext()\n * - Bucket B (dynamicTools): Tools registered via registerTool()\n *\n * This separation ensures that component-scoped dynamic tools persist across\n * app-level provideContext() calls.\n *\n * @class WebModelContext\n * @implements {InternalModelContext}\n */\nclass WebModelContext implements InternalModelContext {\n private bridge: MCPBridge;\n private eventTarget: EventTarget;\n\n // Tool storage (Bucket A = provideContext, Bucket B = dynamic)\n private provideContextTools: Map<string, ValidatedToolDescriptor>;\n private dynamicTools: Map<string, ValidatedToolDescriptor>;\n\n // Resource storage (Bucket A = provideContext, Bucket B = dynamic)\n private provideContextResources: Map<string, ValidatedResourceDescriptor>;\n private dynamicResources: Map<string, ValidatedResourceDescriptor>;\n\n // Prompt storage (Bucket A = provideContext, Bucket B = dynamic)\n private provideContextPrompts: Map<string, ValidatedPromptDescriptor>;\n private dynamicPrompts: Map<string, ValidatedPromptDescriptor>;\n\n // Registration tracking for duplicate detection\n private toolRegistrationTimestamps: Map<string, number>;\n private resourceRegistrationTimestamps: Map<string, number>;\n private promptRegistrationTimestamps: Map<string, number>;\n\n // Unregister functions for dynamic registrations\n private toolUnregisterFunctions: Map<string, () => void>;\n private resourceUnregisterFunctions: Map<string, () => void>;\n private promptUnregisterFunctions: Map<string, () => void>;\n\n /**\n * Tracks which list change notifications are pending.\n * Uses microtask-based batching to coalesce rapid registrations\n * (e.g., React mount phase) into a single notification per list type.\n */\n private pendingNotifications = new Set<ListChangeType>();\n\n private testingAPI?: WebModelContextTesting;\n\n /**\n * Creates a new WebModelContext instance.\n *\n * @param {MCPBridge} bridge - The MCP bridge to use for communication\n */\n constructor(bridge: MCPBridge) {\n this.bridge = bridge;\n this.eventTarget = new EventTarget();\n\n // Initialize tool storage\n this.provideContextTools = new Map();\n this.dynamicTools = new Map();\n this.toolRegistrationTimestamps = new Map();\n this.toolUnregisterFunctions = new Map();\n\n // Initialize resource storage\n this.provideContextResources = new Map();\n this.dynamicResources = new Map();\n this.resourceRegistrationTimestamps = new Map();\n this.resourceUnregisterFunctions = new Map();\n\n // Initialize prompt storage\n this.provideContextPrompts = new Map();\n this.dynamicPrompts = new Map();\n this.promptRegistrationTimestamps = new Map();\n this.promptUnregisterFunctions = new Map();\n }\n\n /**\n * Sets the testing API instance.\n * Called during initialization to enable testing features.\n *\n * @param {WebModelContextTesting} testingAPI - The testing API instance\n * @internal\n */\n setTestingAPI(testingAPI: WebModelContextTesting): void {\n this.testingAPI = testingAPI;\n }\n\n /**\n * Adds an event listener for tool call events.\n *\n * @param {'toolcall'} type - Event type (only 'toolcall' is supported)\n * @param {(event: ToolCallEvent) => void | Promise<void>} listener - Event handler function\n * @param {boolean | AddEventListenerOptions} [options] - Event listener options\n */\n addEventListener(\n type: 'toolcall',\n listener: (event: ToolCallEvent) => void | Promise<void>,\n options?: boolean | AddEventListenerOptions\n ): void {\n this.eventTarget.addEventListener(type, listener as EventListener, options);\n }\n\n /**\n * Removes an event listener for tool call events.\n *\n * @param {'toolcall'} type - Event type (only 'toolcall' is supported)\n * @param {(event: ToolCallEvent) => void | Promise<void>} listener - Event handler function\n * @param {boolean | EventListenerOptions} [options] - Event listener options\n */\n removeEventListener(\n type: 'toolcall',\n listener: (event: ToolCallEvent) => void | Promise<void>,\n options?: boolean | EventListenerOptions\n ): void {\n this.eventTarget.removeEventListener(type, listener as EventListener, options);\n }\n\n /**\n * Dispatches a tool call event to all registered listeners.\n *\n * @param {Event} event - The event to dispatch\n * @returns {boolean} False if event was cancelled, true otherwise\n */\n dispatchEvent(event: Event): boolean {\n return this.eventTarget.dispatchEvent(event);\n }\n\n /**\n * Provides context (tools, resources, prompts) to AI models by registering base items (Bucket A).\n * Clears and replaces all previously registered base items while preserving\n * dynamic items registered via register* methods.\n *\n * @param {ModelContextInput} context - Context containing tools, resources, and prompts to register\n * @throws {Error} If a name/uri collides with existing dynamic items\n */\n provideContext(context: ModelContextInput): void {\n // Clear base items (Bucket A)\n this.provideContextTools.clear();\n this.provideContextResources.clear();\n this.provideContextPrompts.clear();\n\n // Register tools\n for (const tool of context.tools ?? []) {\n // Validate tool name and log warnings for potential compatibility issues\n // NOTE: Similar validation exists in @mcp-b/chrome-devtools-mcp/src/tools/WebMCPToolHub.ts\n // Keep both implementations in sync when making changes.\n if (tool.name.startsWith('_')) {\n logger.warn(\n `⚠️ Warning: Tool name \"${tool.name}\" starts with underscore. ` +\n 'This may cause compatibility issues with some MCP clients. ' +\n 'Consider using a letter as the first character.'\n );\n }\n if (/^[0-9]/.test(tool.name)) {\n logger.warn(\n `⚠️ Warning: Tool name \"${tool.name}\" starts with a number. ` +\n 'This may cause compatibility issues. ' +\n 'Consider using a letter as the first character.'\n );\n }\n if (tool.name.startsWith('-')) {\n logger.warn(\n `⚠️ Warning: Tool name \"${tool.name}\" starts with hyphen. ` +\n 'This may cause compatibility issues. ' +\n 'Consider using a letter as the first character.'\n );\n }\n\n if (this.dynamicTools.has(tool.name)) {\n throw new Error(\n `[Web Model Context] Tool name collision: \"${tool.name}\" is already registered via registerTool(). ` +\n 'Please use a different name or unregister the dynamic tool first.'\n );\n }\n\n const { jsonSchema: inputJson, zodValidator: inputZod } = normalizeSchema(tool.inputSchema);\n const normalizedOutput = tool.outputSchema ? normalizeSchema(tool.outputSchema) : null;\n\n const validatedTool: ValidatedToolDescriptor = {\n name: tool.name,\n description: tool.description,\n inputSchema: inputJson,\n ...(normalizedOutput && { outputSchema: normalizedOutput.jsonSchema }),\n ...(tool.annotations && { annotations: tool.annotations }),\n execute: tool.execute,\n inputValidator: inputZod,\n ...(normalizedOutput && { outputValidator: normalizedOutput.zodValidator }),\n };\n\n this.provideContextTools.set(tool.name, validatedTool);\n }\n\n // Register resources\n for (const resource of context.resources ?? []) {\n if (this.dynamicResources.has(resource.uri)) {\n throw new Error(\n `[Web Model Context] Resource URI collision: \"${resource.uri}\" is already registered via registerResource(). ` +\n 'Please use a different URI or unregister the dynamic resource first.'\n );\n }\n\n const validatedResource = this.validateResource(resource);\n this.provideContextResources.set(resource.uri, validatedResource);\n }\n\n // Register prompts\n for (const prompt of context.prompts ?? []) {\n if (this.dynamicPrompts.has(prompt.name)) {\n throw new Error(\n `[Web Model Context] Prompt name collision: \"${prompt.name}\" is already registered via registerPrompt(). ` +\n 'Please use a different name or unregister the dynamic prompt first.'\n );\n }\n\n const validatedPrompt = this.validatePrompt(prompt);\n this.provideContextPrompts.set(prompt.name, validatedPrompt);\n }\n\n // Update bridge and schedule notifications (batched via microtask)\n this.updateBridgeTools();\n this.updateBridgeResources();\n this.updateBridgePrompts();\n\n this.scheduleListChanged('tools');\n this.scheduleListChanged('resources');\n this.scheduleListChanged('prompts');\n }\n\n /**\n * Validates and normalizes a resource descriptor.\n * @private\n */\n private validateResource(resource: ResourceDescriptor): ValidatedResourceDescriptor {\n // Extract template parameters from URI (e.g., \"file://{path}\" -> [\"path\"])\n // Limit parameter name length to 100 chars to prevent ReDoS on malicious input\n const templateParamRegex = /\\{([^}]{1,100})\\}/g;\n const templateParams: string[] = [];\n for (const match of resource.uri.matchAll(templateParamRegex)) {\n const paramName = match[1]!;\n templateParams.push(paramName);\n }\n\n return {\n uri: resource.uri,\n name: resource.name,\n description: resource.description,\n mimeType: resource.mimeType,\n read: resource.read,\n isTemplate: templateParams.length > 0,\n templateParams,\n };\n }\n\n /**\n * Validates and normalizes a prompt descriptor.\n * @private\n */\n private validatePrompt<TArgsSchema extends ZodSchemaObject>(\n prompt: PromptDescriptor<TArgsSchema>\n ): ValidatedPromptDescriptor {\n let argsSchema: InputSchema | undefined;\n let argsValidator: z.ZodType | undefined;\n\n if (prompt.argsSchema) {\n const normalized = normalizeSchema(prompt.argsSchema);\n argsSchema = normalized.jsonSchema;\n argsValidator = normalized.zodValidator;\n }\n\n return {\n name: prompt.name,\n description: prompt.description,\n argsSchema,\n get: prompt.get as (args: Record<string, unknown>) => Promise<{ messages: PromptMessage[] }>,\n argsValidator,\n };\n }\n\n /**\n * Registers a single tool dynamically (Bucket B).\n * Dynamic tools persist across provideContext() calls and can be independently managed.\n *\n * @param {ToolDescriptor} tool - The tool descriptor to register\n * @returns {{unregister: () => void}} Object with unregister function\n * @throws {Error} If tool name collides with existing tools\n */\n registerTool<\n TInputSchema extends ZodSchemaObject = Record<string, never>,\n TOutputSchema extends ZodSchemaObject = Record<string, never>,\n >(tool: ToolDescriptor<TInputSchema, TOutputSchema>): { unregister: () => void } {\n // Validate tool name and log warnings for potential compatibility issues\n // NOTE: Similar validation exists in @mcp-b/chrome-devtools-mcp/src/tools/WebMCPToolHub.ts\n // Keep both implementations in sync when making changes.\n if (tool.name.startsWith('_')) {\n logger.warn(\n `⚠️ Warning: Tool name \"${tool.name}\" starts with underscore. ` +\n 'This may cause compatibility issues with some MCP clients. ' +\n 'Consider using a letter as the first character.'\n );\n }\n if (/^[0-9]/.test(tool.name)) {\n logger.warn(\n `⚠️ Warning: Tool name \"${tool.name}\" starts with a number. ` +\n 'This may cause compatibility issues. ' +\n 'Consider using a letter as the first character.'\n );\n }\n if (tool.name.startsWith('-')) {\n logger.warn(\n `⚠️ Warning: Tool name \"${tool.name}\" starts with hyphen. ` +\n 'This may cause compatibility issues. ' +\n 'Consider using a letter as the first character.'\n );\n }\n\n const now = Date.now();\n const lastRegistration = this.toolRegistrationTimestamps.get(tool.name);\n\n if (lastRegistration && now - lastRegistration < RAPID_DUPLICATE_WINDOW_MS) {\n logger.warn(\n `Tool \"${tool.name}\" registered multiple times within ${RAPID_DUPLICATE_WINDOW_MS}ms. ` +\n 'This is likely due to React Strict Mode double-mounting. Ignoring duplicate registration.'\n );\n\n const existingUnregister = this.toolUnregisterFunctions.get(tool.name);\n if (existingUnregister) {\n return { unregister: existingUnregister };\n }\n }\n\n if (this.provideContextTools.has(tool.name)) {\n throw new Error(\n `[Web Model Context] Tool name collision: \"${tool.name}\" is already registered via provideContext(). ` +\n 'Please use a different name or update your provideContext() call.'\n );\n }\n\n if (this.dynamicTools.has(tool.name)) {\n throw new Error(\n `[Web Model Context] Tool name collision: \"${tool.name}\" is already registered via registerTool(). ` +\n 'Please unregister it first or use a different name.'\n );\n }\n\n const { jsonSchema: inputJson, zodValidator: inputZod } = normalizeSchema(tool.inputSchema);\n\n const normalizedOutput = tool.outputSchema ? normalizeSchema(tool.outputSchema) : null;\n\n const validatedTool: ValidatedToolDescriptor = {\n name: tool.name,\n description: tool.description,\n inputSchema: inputJson,\n ...(normalizedOutput && { outputSchema: normalizedOutput.jsonSchema }),\n ...(tool.annotations && { annotations: tool.annotations }),\n execute: tool.execute as (args: Record<string, unknown>) => Promise<ToolResponse>,\n inputValidator: inputZod,\n ...(normalizedOutput && { outputValidator: normalizedOutput.zodValidator }),\n };\n\n this.dynamicTools.set(tool.name, validatedTool);\n this.toolRegistrationTimestamps.set(tool.name, now);\n this.updateBridgeTools();\n this.scheduleListChanged('tools');\n\n const unregisterFn = () => {\n if (this.provideContextTools.has(tool.name)) {\n throw new Error(\n `[Web Model Context] Cannot unregister tool \"${tool.name}\": ` +\n 'This tool was registered via provideContext(). Use provideContext() to update the base tool set.'\n );\n }\n\n if (!this.dynamicTools.has(tool.name)) {\n logger.warn(`Tool \"${tool.name}\" is not registered, ignoring unregister call`);\n return;\n }\n\n this.dynamicTools.delete(tool.name);\n this.toolRegistrationTimestamps.delete(tool.name);\n this.toolUnregisterFunctions.delete(tool.name);\n this.updateBridgeTools();\n this.scheduleListChanged('tools');\n };\n\n this.toolUnregisterFunctions.set(tool.name, unregisterFn);\n\n return { unregister: unregisterFn };\n }\n\n // ==================== RESOURCE METHODS ====================\n\n /**\n * Registers a single resource dynamically (Bucket B).\n * Dynamic resources persist across provideContext() calls and can be independently managed.\n *\n * @param {ResourceDescriptor} resource - The resource descriptor to register\n * @returns {{unregister: () => void}} Object with unregister function\n * @throws {Error} If resource URI collides with existing resources\n */\n registerResource(resource: ResourceDescriptor): { unregister: () => void } {\n const now = Date.now();\n const lastRegistration = this.resourceRegistrationTimestamps.get(resource.uri);\n\n if (lastRegistration && now - lastRegistration < RAPID_DUPLICATE_WINDOW_MS) {\n logger.warn(\n `Resource \"${resource.uri}\" registered multiple times within ${RAPID_DUPLICATE_WINDOW_MS}ms. ` +\n 'This is likely due to React Strict Mode double-mounting. Ignoring duplicate registration.'\n );\n\n const existingUnregister = this.resourceUnregisterFunctions.get(resource.uri);\n if (existingUnregister) {\n return { unregister: existingUnregister };\n }\n }\n\n if (this.provideContextResources.has(resource.uri)) {\n throw new Error(\n `[Web Model Context] Resource URI collision: \"${resource.uri}\" is already registered via provideContext(). ` +\n 'Please use a different URI or update your provideContext() call.'\n );\n }\n\n if (this.dynamicResources.has(resource.uri)) {\n throw new Error(\n `[Web Model Context] Resource URI collision: \"${resource.uri}\" is already registered via registerResource(). ` +\n 'Please unregister it first or use a different URI.'\n );\n }\n\n const validatedResource = this.validateResource(resource);\n this.dynamicResources.set(resource.uri, validatedResource);\n this.resourceRegistrationTimestamps.set(resource.uri, now);\n this.updateBridgeResources();\n this.scheduleListChanged('resources');\n\n const unregisterFn = () => {\n if (this.provideContextResources.has(resource.uri)) {\n throw new Error(\n `[Web Model Context] Cannot unregister resource \"${resource.uri}\": ` +\n 'This resource was registered via provideContext(). Use provideContext() to update the base resource set.'\n );\n }\n\n if (!this.dynamicResources.has(resource.uri)) {\n logger.warn(`Resource \"${resource.uri}\" is not registered, ignoring unregister call`);\n return;\n }\n\n this.dynamicResources.delete(resource.uri);\n this.resourceRegistrationTimestamps.delete(resource.uri);\n this.resourceUnregisterFunctions.delete(resource.uri);\n this.updateBridgeResources();\n this.scheduleListChanged('resources');\n };\n\n this.resourceUnregisterFunctions.set(resource.uri, unregisterFn);\n\n return { unregister: unregisterFn };\n }\n\n /**\n * Unregisters a resource by URI.\n * Can unregister resources from either Bucket A (provideContext) or Bucket B (registerResource).\n *\n * @param {string} uri - URI of the resource to unregister\n */\n unregisterResource(uri: string): void {\n const inProvideContext = this.provideContextResources.has(uri);\n const inDynamic = this.dynamicResources.has(uri);\n\n if (!inProvideContext && !inDynamic) {\n logger.warn(`Resource \"${uri}\" is not registered, ignoring unregister call`);\n return;\n }\n\n if (inProvideContext) {\n this.provideContextResources.delete(uri);\n }\n\n if (inDynamic) {\n this.dynamicResources.delete(uri);\n this.resourceRegistrationTimestamps.delete(uri);\n this.resourceUnregisterFunctions.delete(uri);\n }\n\n this.updateBridgeResources();\n this.scheduleListChanged('resources');\n }\n\n /**\n * Lists all registered resources in MCP format.\n * Returns static resources from both buckets (not templates).\n *\n * @returns {Resource[]} Array of resource descriptors\n */\n listResources(): Resource[] {\n return Array.from(this.bridge.resources.values())\n .filter((r) => !r.isTemplate)\n .map((resource) => ({\n uri: resource.uri,\n name: resource.name,\n description: resource.description,\n mimeType: resource.mimeType,\n }));\n }\n\n /**\n * Lists all registered resource templates.\n * Returns only resources with URI templates (dynamic resources).\n *\n * @returns {Array<{uriTemplate: string, name: string, description?: string, mimeType?: string}>}\n */\n listResourceTemplates(): Array<{\n uriTemplate: string;\n name: string;\n description?: string;\n mimeType?: string;\n }> {\n return Array.from(this.bridge.resources.values())\n .filter((r) => r.isTemplate)\n .map((resource) => ({\n uriTemplate: resource.uri,\n name: resource.name,\n ...(resource.description !== undefined && { description: resource.description }),\n ...(resource.mimeType !== undefined && { mimeType: resource.mimeType }),\n }));\n }\n\n // ==================== PROMPT METHODS ====================\n\n /**\n * Registers a single prompt dynamically (Bucket B).\n * Dynamic prompts persist across provideContext() calls and can be independently managed.\n *\n * @param {PromptDescriptor} prompt - The prompt descriptor to register\n * @returns {{unregister: () => void}} Object with unregister function\n * @throws {Error} If prompt name collides with existing prompts\n */\n registerPrompt<TArgsSchema extends ZodSchemaObject = Record<string, never>>(\n prompt: PromptDescriptor<TArgsSchema>\n ): { unregister: () => void } {\n const now = Date.now();\n const lastRegistration = this.promptRegistrationTimestamps.get(prompt.name);\n\n if (lastRegistration && now - lastRegistration < RAPID_DUPLICATE_WINDOW_MS) {\n logger.warn(\n `Prompt \"${prompt.name}\" registered multiple times within ${RAPID_DUPLICATE_WINDOW_MS}ms. ` +\n 'This is likely due to React Strict Mode double-mounting. Ignoring duplicate registration.'\n );\n\n const existingUnregister = this.promptUnregisterFunctions.get(prompt.name);\n if (existingUnregister) {\n return { unregister: existingUnregister };\n }\n }\n\n if (this.provideContextPrompts.has(prompt.name)) {\n throw new Error(\n `[Web Model Context] Prompt name collision: \"${prompt.name}\" is already registered via provideContext(). ` +\n 'Please use a different name or update your provideContext() call.'\n );\n }\n\n if (this.dynamicPrompts.has(prompt.name)) {\n throw new Error(\n `[Web Model Context] Prompt name collision: \"${prompt.name}\" is already registered via registerPrompt(). ` +\n 'Please unregister it first or use a different name.'\n );\n }\n\n const validatedPrompt = this.validatePrompt(prompt);\n this.dynamicPrompts.set(prompt.name, validatedPrompt);\n this.promptRegistrationTimestamps.set(prompt.name, now);\n this.updateBridgePrompts();\n this.scheduleListChanged('prompts');\n\n const unregisterFn = () => {\n if (this.provideContextPrompts.has(prompt.name)) {\n throw new Error(\n `[Web Model Context] Cannot unregister prompt \"${prompt.name}\": ` +\n 'This prompt was registered via provideContext(). Use provideContext() to update the base prompt set.'\n );\n }\n\n if (!this.dynamicPrompts.has(prompt.name)) {\n logger.warn(`Prompt \"${prompt.name}\" is not registered, ignoring unregister call`);\n return;\n }\n\n this.dynamicPrompts.delete(prompt.name);\n this.promptRegistrationTimestamps.delete(prompt.name);\n this.promptUnregisterFunctions.delete(prompt.name);\n this.updateBridgePrompts();\n this.scheduleListChanged('prompts');\n };\n\n this.promptUnregisterFunctions.set(prompt.name, unregisterFn);\n\n return { unregister: unregisterFn };\n }\n\n /**\n * Unregisters a prompt by name.\n * Can unregister prompts from either Bucket A (provideContext) or Bucket B (registerPrompt).\n *\n * @param {string} name - Name of the prompt to unregister\n */\n unregisterPrompt(name: string): void {\n const inProvideContext = this.provideContextPrompts.has(name);\n const inDynamic = this.dynamicPrompts.has(name);\n\n if (!inProvideContext && !inDynamic) {\n logger.warn(`Prompt \"${name}\" is not registered, ignoring unregister call`);\n return;\n }\n\n if (inProvideContext) {\n this.provideContextPrompts.delete(name);\n }\n\n if (inDynamic) {\n this.dynamicPrompts.delete(name);\n this.promptRegistrationTimestamps.delete(name);\n this.promptUnregisterFunctions.delete(name);\n }\n\n this.updateBridgePrompts();\n this.scheduleListChanged('prompts');\n }\n\n /**\n * Lists all registered prompts in MCP format.\n * Returns prompts from both buckets.\n *\n * @returns {Prompt[]} Array of prompt descriptors\n */\n listPrompts(): Prompt[] {\n return Array.from(this.bridge.prompts.values()).map((prompt) => ({\n name: prompt.name,\n description: prompt.description,\n arguments: prompt.argsSchema?.properties\n ? Object.entries(prompt.argsSchema.properties).map(([name, schema]) => ({\n name,\n description: (schema as { description?: string }).description,\n required: prompt.argsSchema?.required?.includes(name) ?? false,\n }))\n : undefined,\n }));\n }\n\n /**\n * Unregisters a tool by name (Chromium native API).\n * Can unregister tools from either Bucket A (provideContext) or Bucket B (registerTool).\n *\n * @param {string} name - Name of the tool to unregister\n */\n unregisterTool(name: string): void {\n const inProvideContext = this.provideContextTools.has(name);\n const inDynamic = this.dynamicTools.has(name);\n\n if (!inProvideContext && !inDynamic) {\n logger.warn(`Tool \"${name}\" is not registered, ignoring unregister call`);\n return;\n }\n\n if (inProvideContext) {\n this.provideContextTools.delete(name);\n }\n\n if (inDynamic) {\n this.dynamicTools.delete(name);\n this.toolRegistrationTimestamps.delete(name);\n this.toolUnregisterFunctions.delete(name);\n }\n\n this.updateBridgeTools();\n this.scheduleListChanged('tools');\n }\n\n /**\n * Clears all registered context from both buckets (Chromium native API).\n * Removes all tools, resources, and prompts registered via provideContext() and register* methods.\n */\n clearContext(): void {\n // Clear tools\n this.provideContextTools.clear();\n this.dynamicTools.clear();\n this.toolRegistrationTimestamps.clear();\n this.toolUnregisterFunctions.clear();\n\n // Clear resources\n this.provideContextResources.clear();\n this.dynamicResources.clear();\n this.resourceRegistrationTimestamps.clear();\n this.resourceUnregisterFunctions.clear();\n\n // Clear prompts\n this.provideContextPrompts.clear();\n this.dynamicPrompts.clear();\n this.promptRegistrationTimestamps.clear();\n this.promptUnregisterFunctions.clear();\n\n // Update bridge\n this.updateBridgeTools();\n this.updateBridgeResources();\n this.updateBridgePrompts();\n\n // Schedule notifications (batched via microtask)\n this.scheduleListChanged('tools');\n this.scheduleListChanged('resources');\n this.scheduleListChanged('prompts');\n }\n\n /**\n * Updates the bridge tools map with merged tools from both buckets.\n * The final tool list is the union of Bucket A (provideContext) and Bucket B (dynamic).\n *\n * @private\n */\n private updateBridgeTools(): void {\n this.bridge.tools.clear();\n\n for (const [name, tool] of this.provideContextTools) {\n this.bridge.tools.set(name, tool);\n }\n\n for (const [name, tool] of this.dynamicTools) {\n this.bridge.tools.set(name, tool);\n }\n }\n\n /**\n * Notifies all servers and testing callbacks that the tools list has changed.\n * Sends MCP notifications to connected servers and invokes registered testing callbacks.\n *\n * @private\n */\n private notifyToolsListChanged(): void {\n if (this.bridge.tabServer.notification) {\n this.bridge.tabServer.notification({\n method: 'notifications/tools/list_changed',\n params: {},\n });\n }\n\n if (this.bridge.iframeServer?.notification) {\n this.bridge.iframeServer.notification({\n method: 'notifications/tools/list_changed',\n params: {},\n });\n }\n\n if (this.testingAPI && 'notifyToolsChanged' in this.testingAPI) {\n (this.testingAPI as WebModelContextTesting).notifyToolsChanged();\n }\n }\n\n /**\n * Updates the bridge resources map with merged resources from both buckets.\n *\n * @private\n */\n private updateBridgeResources(): void {\n this.bridge.resources.clear();\n\n for (const [uri, resource] of this.provideContextResources) {\n this.bridge.resources.set(uri, resource);\n }\n\n for (const [uri, resource] of this.dynamicResources) {\n this.bridge.resources.set(uri, resource);\n }\n }\n\n /**\n * Notifies all servers that the resources list has changed.\n *\n * @private\n */\n private notifyResourcesListChanged(): void {\n if (this.bridge.tabServer.notification) {\n this.bridge.tabServer.notification({\n method: 'notifications/resources/list_changed',\n params: {},\n });\n }\n\n if (this.bridge.iframeServer?.notification) {\n this.bridge.iframeServer.notification({\n method: 'notifications/resources/list_changed',\n params: {},\n });\n }\n }\n\n /**\n * Updates the bridge prompts map with merged prompts from both buckets.\n *\n * @private\n */\n private updateBridgePrompts(): void {\n this.bridge.prompts.clear();\n\n for (const [name, prompt] of this.provideContextPrompts) {\n this.bridge.prompts.set(name, prompt);\n }\n\n for (const [name, prompt] of this.dynamicPrompts) {\n this.bridge.prompts.set(name, prompt);\n }\n }\n\n /**\n * Notifies all servers that the prompts list has changed.\n *\n * @private\n */\n private notifyPromptsListChanged(): void {\n if (this.bridge.tabServer.notification) {\n this.bridge.tabServer.notification({\n method: 'notifications/prompts/list_changed',\n params: {},\n });\n }\n\n if (this.bridge.iframeServer?.notification) {\n this.bridge.iframeServer.notification({\n method: 'notifications/prompts/list_changed',\n params: {},\n });\n }\n }\n\n /**\n * Schedules a list changed notification using microtask batching.\n * Multiple calls for the same list type within the same task are coalesced\n * into a single notification. This dramatically reduces notification spam\n * during React mount/unmount cycles.\n *\n * @param listType - The type of list that changed ('tools' | 'resources' | 'prompts')\n * @private\n */\n private scheduleListChanged(listType: ListChangeType): void {\n if (this.pendingNotifications.has(listType)) return;\n\n this.pendingNotifications.add(listType);\n queueMicrotask(() => {\n this.pendingNotifications.delete(listType);\n\n // Dispatch to the appropriate notification method\n // Exhaustive switch ensures compile-time safety when adding new list types\n switch (listType) {\n case 'tools':\n this.notifyToolsListChanged();\n break;\n case 'resources':\n this.notifyResourcesListChanged();\n break;\n case 'prompts':\n this.notifyPromptsListChanged();\n break;\n default: {\n // Exhaustiveness check: TypeScript will error if a case is missing\n const _exhaustive: never = listType;\n logger.error(`Unknown list type: ${_exhaustive}`);\n }\n }\n });\n }\n\n /**\n * Reads a resource by URI (internal use only by MCP bridge).\n * Handles both static resources and URI templates.\n *\n * @param {string} uri - The URI of the resource to read\n * @returns {Promise<{contents: ResourceContents[]}>} The resource contents\n * @throws {Error} If resource is not found\n * @internal\n */\n async readResource(uri: string): Promise<{ contents: ResourceContents[] }> {\n // First, try to find an exact match (static resource)\n const staticResource = this.bridge.resources.get(uri);\n if (staticResource && !staticResource.isTemplate) {\n try {\n // Try to parse as URL, but fall back to a pseudo-URL for custom schemes\n let parsedUri: URL;\n try {\n parsedUri = new URL(uri);\n } catch {\n // Custom URI scheme (e.g., \"iframe://config\", \"prefix_iframe://config\")\n // Create a pseudo-URL with the original URI as the pathname\n parsedUri = new URL(`custom-scheme:///${encodeURIComponent(uri)}`);\n // Store original URI for handlers that need it\n (parsedUri as URL & { originalUri: string }).originalUri = uri;\n }\n return await staticResource.read(parsedUri);\n } catch (error) {\n logger.error(`Error reading resource ${uri}:`, error);\n throw error;\n }\n }\n\n // Try to match against URI templates\n for (const resource of this.bridge.resources.values()) {\n if (!resource.isTemplate) continue;\n\n const params = this.matchUriTemplate(resource.uri, uri);\n if (params) {\n try {\n // Try to parse as URL, but fall back to a pseudo-URL for custom schemes\n let parsedUri: URL;\n try {\n parsedUri = new URL(uri);\n } catch {\n // Custom URI scheme - create a pseudo-URL\n parsedUri = new URL(`custom-scheme:///${encodeURIComponent(uri)}`);\n (parsedUri as URL & { originalUri: string }).originalUri = uri;\n }\n return await resource.read(parsedUri, params);\n } catch (error) {\n logger.error(`Error reading resource ${uri}:`, error);\n throw error;\n }\n }\n }\n\n throw new Error(`Resource not found: ${uri}`);\n }\n\n /**\n * Matches a URI against a URI template and extracts parameters.\n *\n * @param {string} template - The URI template (e.g., \"file://{path}\")\n * @param {string} uri - The actual URI to match\n * @returns {Record<string, string> | null} Extracted parameters or null if no match\n * @private\n */\n private matchUriTemplate(template: string, uri: string): Record<string, string> | null {\n // Convert template to regex pattern\n // e.g., \"file://{path}\" -> \"^file://(.+)$\" with capture group for \"path\"\n const paramNames: string[] = [];\n let regexPattern = template.replace(/[.*+?^${}()|[\\]\\\\]/g, (char) => {\n // Don't escape { and } - we'll handle them specially\n if (char === '{' || char === '}') return char;\n return `\\\\${char}`;\n });\n\n // Replace {param} with capture groups\n regexPattern = regexPattern.replace(/\\{([^}]+)\\}/g, (_, paramName) => {\n paramNames.push(paramName);\n return '(.+)';\n });\n\n const regex = new RegExp(`^${regexPattern}$`);\n const match = uri.match(regex);\n\n if (!match) return null;\n\n const params: Record<string, string> = {};\n for (let i = 0; i < paramNames.length; i++) {\n const paramName = paramNames[i]!;\n const paramValue = match[i + 1]!;\n params[paramName] = paramValue;\n }\n\n return params;\n }\n\n /**\n * Gets a prompt with arguments (internal use only by MCP bridge).\n *\n * @param {string} name - Name of the prompt\n * @param {Record<string, unknown>} args - Arguments to pass to the prompt\n * @returns {Promise<{messages: PromptMessage[]}>} The prompt messages\n * @throws {Error} If prompt is not found\n * @internal\n */\n async getPrompt(\n name: string,\n args?: Record<string, unknown>\n ): Promise<{ messages: PromptMessage[] }> {\n const prompt = this.bridge.prompts.get(name);\n if (!prompt) {\n throw new Error(`Prompt not found: ${name}`);\n }\n\n // Validate arguments if schema is defined\n if (prompt.argsValidator && args) {\n const validation = validateWithZod(args, prompt.argsValidator);\n if (!validation.success) {\n logger.error(`Argument validation failed for prompt ${name}:`, validation.error);\n throw new Error(`Argument validation error for prompt \"${name}\":\\n${validation.error}`);\n }\n }\n\n try {\n return await prompt.get(args ?? {});\n } catch (error) {\n logger.error(`Error getting prompt ${name}:`, error);\n throw error;\n }\n }\n\n /**\n * Executes a tool with validation and event dispatch.\n * Follows this sequence:\n * 1. Validates input arguments against schema\n * 2. Records tool call in testing API (if available)\n * 3. Checks for mock response (if testing)\n * 4. Dispatches 'toolcall' event to listeners\n * 5. Executes tool function if not prevented\n * 6. Validates output (permissive mode - warns only)\n *\n * @param {string} toolName - Name of the tool to execute\n * @param {Record<string, unknown>} args - Arguments to pass to the tool\n * @returns {Promise<ToolResponse>} The tool's response\n * @throws {Error} If tool is not found\n * @internal\n */\n async executeTool(toolName: string, args: Record<string, unknown>): Promise<ToolResponse> {\n const tool = this.bridge.tools.get(toolName);\n if (!tool) {\n throw new Error(`Tool not found: ${toolName}`);\n }\n\n const validation = validateWithZod(args, tool.inputValidator);\n if (!validation.success) {\n logger.error(`Input validation failed for ${toolName}:`, validation.error);\n return {\n content: [\n {\n type: 'text',\n text: `Input validation error for tool \"${toolName}\":\\n${validation.error}`,\n },\n ],\n isError: true,\n };\n }\n\n const validatedArgs = validation.data as Record<string, unknown>;\n\n if (this.testingAPI) {\n this.testingAPI.recordToolCall(toolName, validatedArgs);\n }\n\n if (this.testingAPI?.hasMockResponse(toolName)) {\n const mockResponse = this.testingAPI.getMockResponse(toolName);\n if (mockResponse) {\n return mockResponse;\n }\n }\n\n const event = new WebToolCallEvent(toolName, validatedArgs);\n\n this.dispatchEvent(event);\n\n if (event.defaultPrevented && event.hasResponse()) {\n const response = event.getResponse();\n if (response) {\n return response;\n }\n }\n\n try {\n const response = await tool.execute(validatedArgs);\n\n if (tool.outputValidator && response.structuredContent) {\n const outputValidation = validateWithZod(response.structuredContent, tool.outputValidator);\n if (!outputValidation.success) {\n logger.warn(`Output validation failed for ${toolName}:`, outputValidation.error);\n }\n }\n\n // Log navigation tools for debugging\n if (\n response.metadata &&\n typeof response.metadata === 'object' &&\n 'willNavigate' in response.metadata\n ) {\n logger.info(`Tool \"${toolName}\" will trigger navigation`, response.metadata);\n }\n\n return response;\n } catch (error) {\n logger.error(`Error executing tool ${toolName}:`, error);\n return {\n content: [\n {\n type: 'text',\n text: `Error: ${error instanceof Error ? error.message : String(error)}`,\n },\n ],\n isError: true,\n };\n }\n }\n\n /**\n * Lists all registered tools in MCP format.\n * Returns tools from both buckets with full MCP specification including\n * annotations and output schemas.\n *\n * @returns {Array<{name: string, description: string, inputSchema: InputSchema, outputSchema?: InputSchema, annotations?: ToolAnnotations}>} Array of tool descriptors\n */\n listTools() {\n return Array.from(this.bridge.tools.values()).map((tool) => ({\n name: tool.name,\n description: tool.description,\n inputSchema: tool.inputSchema,\n ...(tool.outputSchema && { outputSchema: tool.outputSchema }),\n ...(tool.annotations && { annotations: tool.annotations }),\n }));\n }\n\n // ==================== SAMPLING METHODS ====================\n\n /**\n * Request an LLM completion from the connected client.\n * This sends a sampling request to the connected MCP client.\n *\n * @param {SamplingRequestParams} params - Parameters for the sampling request\n * @returns {Promise<SamplingResult>} The LLM completion result\n */\n async createMessage(params: SamplingRequestParams): Promise<SamplingResult> {\n const server = this.bridge.tabServer;\n\n // Access the underlying Server instance to call createMessage\n const underlyingServer = (\n server as unknown as {\n server: { createMessage: (params: unknown) => Promise<SamplingResult> };\n }\n ).server;\n\n if (!underlyingServer?.createMessage) {\n throw new Error('Sampling is not supported: no connected client with sampling capability');\n }\n\n return underlyingServer.createMessage(params);\n }\n\n // ==================== ELICITATION METHODS ====================\n\n /**\n * Request user input from the connected client.\n * This sends an elicitation request to the connected MCP client.\n *\n * @param {ElicitationParams} params - Parameters for the elicitation request\n * @returns {Promise<ElicitationResult>} The user's response\n */\n async elicitInput(params: ElicitationParams): Promise<ElicitationResult> {\n const server = this.bridge.tabServer;\n\n // Access the underlying Server instance to call elicitInput\n const underlyingServer = (\n server as unknown as {\n server: { elicitInput: (params: unknown) => Promise<ElicitationResult> };\n }\n ).server;\n\n if (!underlyingServer?.elicitInput) {\n throw new Error(\n 'Elicitation is not supported: no connected client with elicitation capability'\n );\n }\n\n return underlyingServer.elicitInput(params);\n }\n}\n\n/**\n * Initializes the MCP bridge with dual-server support.\n * Creates TabServer for same-window communication and optionally IframeChildServer\n * for parent-child iframe communication.\n *\n * @param {WebModelContextInitOptions} [options] - Configuration options\n * @returns {MCPBridge} The initialized MCP bridge\n */\nfunction initializeMCPBridge(options?: WebModelContextInitOptions): MCPBridge {\n const hostname = window.location.hostname || 'localhost';\n const transportOptions = options?.transport;\n\n const setupServerHandlers = (server: McpServer, bridge: MCPBridge) => {\n // ==================== TOOL HANDLERS ====================\n server.setRequestHandler(ListToolsRequestSchema, async () => {\n return {\n tools: bridge.modelContext.listTools(),\n };\n });\n\n server.setRequestHandler(CallToolRequestSchema, async (request) => {\n const toolName = request.params.name;\n const args = (request.params.arguments || {}) as Record<string, unknown>;\n\n try {\n const response = await bridge.modelContext.executeTool(toolName, args);\n return {\n content: response.content,\n isError: response.isError,\n ...(response.structuredContent && { structuredContent: response.structuredContent }),\n };\n } catch (error) {\n bridgeLogger.error(`Error calling tool ${toolName}:`, error);\n throw error;\n }\n });\n\n // ==================== RESOURCE HANDLERS ====================\n server.setRequestHandler(ListResourcesRequestSchema, async () => {\n return {\n resources: bridge.modelContext.listResources(),\n // Note: Resource templates are included in the resources list as the MCP SDK\n // doesn't export ListResourceTemplatesRequestSchema separately.\n // Clients can identify templates by checking for URI patterns containing {param}.\n };\n });\n\n server.setRequestHandler(ReadResourceRequestSchema, async (request) => {\n try {\n return await bridge.modelContext.readResource(request.params.uri);\n } catch (error) {\n bridgeLogger.error(`Error reading resource ${request.params.uri}:`, error);\n throw error;\n }\n });\n\n // ==================== PROMPT HANDLERS ====================\n server.setRequestHandler(ListPromptsRequestSchema, async () => {\n return {\n prompts: bridge.modelContext.listPrompts(),\n };\n });\n\n server.setRequestHandler(GetPromptRequestSchema, async (request) => {\n try {\n return await bridge.modelContext.getPrompt(\n request.params.name,\n request.params.arguments as Record<string, unknown> | undefined\n );\n } catch (error) {\n bridgeLogger.error(`Error getting prompt ${request.params.name}:`, error);\n throw error;\n }\n });\n\n // Note: Sampling and elicitation are server-to-client requests.\n // The server calls createMessage() and elicitInput() methods on the Server instance.\n // These are NOT request handlers - the client handles these requests.\n };\n\n const customTransport: Transport | undefined = transportOptions?.create?.();\n\n if (customTransport) {\n const server = new McpServer(\n {\n name: hostname,\n version: '1.0.0',\n },\n {\n capabilities: {\n tools: { listChanged: true },\n resources: { listChanged: true },\n prompts: { listChanged: true },\n },\n }\n );\n\n const bridge: MCPBridge = {\n tabServer: server,\n tools: new Map(),\n resources: new Map(),\n prompts: new Map(),\n modelContext: undefined as unknown as InternalModelContext,\n isInitialized: true,\n };\n\n const modelContext = new WebModelContext(bridge);\n bridge.modelContext = modelContext;\n\n setupServerHandlers(server, bridge);\n server.connect(customTransport);\n\n return bridge;\n }\n\n const tabServerEnabled = transportOptions?.tabServer !== false;\n const tabServer = new McpServer(\n {\n name: `${hostname}-tab`,\n version: '1.0.0',\n },\n {\n capabilities: {\n tools: { listChanged: true },\n resources: { listChanged: true },\n prompts: { listChanged: true },\n },\n }\n );\n\n const bridge: MCPBridge = {\n tabServer,\n tools: new Map(),\n resources: new Map(),\n prompts: new Map(),\n modelContext: undefined as unknown as InternalModelContext,\n isInitialized: true,\n };\n\n const modelContext = new WebModelContext(bridge);\n bridge.modelContext = modelContext;\n\n setupServerHandlers(tabServer, bridge);\n\n if (tabServerEnabled) {\n const tabServerOptions: Partial<TabServerTransportOptions> =\n typeof transportOptions?.tabServer === 'object' ? transportOptions.tabServer : {};\n const { allowedOrigins, ...restTabServerOptions } = tabServerOptions;\n\n const tabTransport = new TabServerTransport({\n allowedOrigins: allowedOrigins ?? ['*'],\n ...(restTabServerOptions as Omit<TabServerTransportOptions, 'allowedOrigins'>),\n });\n\n tabServer.connect(tabTransport);\n }\n\n const isInIframe = typeof window !== 'undefined' && window.parent !== window;\n const iframeServerConfig = transportOptions?.iframeServer;\n const iframeServerEnabled =\n iframeServerConfig !== false && (iframeServerConfig !== undefined || isInIframe);\n\n if (iframeServerEnabled) {\n const iframeServer = new McpServer(\n {\n name: `${hostname}-iframe`,\n version: '1.0.0',\n },\n {\n capabilities: {\n tools: { listChanged: true },\n resources: { listChanged: true },\n prompts: { listChanged: true },\n },\n }\n );\n\n setupServerHandlers(iframeServer, bridge);\n\n const iframeServerOptions: Partial<IframeChildTransportOptions> =\n typeof iframeServerConfig === 'object' ? iframeServerConfig : {};\n const { allowedOrigins, ...restIframeServerOptions } = iframeServerOptions;\n\n const iframeTransport = new IframeChildTransport({\n allowedOrigins: allowedOrigins ?? ['*'],\n ...(restIframeServerOptions as Omit<IframeChildTransportOptions, 'allowedOrigins'>),\n });\n\n iframeServer.connect(iframeTransport);\n bridge.iframeServer = iframeServer;\n }\n\n return bridge;\n}\n\n/**\n * Initializes the Web Model Context API on window.navigator.\n * Creates and exposes navigator.modelContext and navigator.modelContextTesting.\n * Automatically detects and uses native Chromium implementation if available.\n *\n * @param {WebModelContextInitOptions} [options] - Configuration options\n * @throws {Error} If initialization fails\n * @example\n * ```typescript\n * import { initializeWebModelContext } from '@mcp-b/global';\n *\n * initializeWebModelContext({\n * transport: {\n * tabServer: {\n * allowedOrigins: ['https://example.com']\n * }\n * }\n * });\n * ```\n */\nexport function initializeWebModelContext(options?: WebModelContextInitOptions): void {\n /* c8 ignore next 4 */\n if (typeof window === 'undefined') {\n logger.warn('Not in browser environment, skipping initialization');\n return;\n }\n\n const effectiveOptions = options ?? window.__webModelContextOptions;\n const native = detectNativeAPI();\n\n if (native.hasNativeContext && native.hasNativeTesting) {\n const nativeContext = window.navigator.modelContext;\n const nativeTesting = window.navigator.modelContextTesting;\n\n if (!nativeContext || !nativeTesting) {\n logger.error('Native API detection mismatch');\n return;\n }\n\n logger.info('✅ Native Chromium API detected');\n logger.info(' Using native implementation with MCP bridge synchronization');\n logger.info(' Native API will automatically collect tools from embedded iframes');\n\n try {\n const bridge = initializeMCPBridge(effectiveOptions);\n\n const adapter = new NativeModelContextAdapter(bridge, nativeContext, nativeTesting);\n\n bridge.modelContext = adapter;\n bridge.modelContextTesting = nativeTesting;\n\n Object.defineProperty(window, '__mcpBridge', {\n value: bridge,\n writable: false,\n configurable: true,\n });\n\n logger.info('✅ MCP bridge synced with native API');\n logger.info(' MCP clients will receive automatic tool updates from native registry');\n } catch (error) {\n logger.error('Failed to initialize native adapter:', error);\n throw error;\n }\n\n return;\n }\n\n if (native.hasNativeContext && !native.hasNativeTesting) {\n logger.warn('Partial native API detected');\n logger.warn(' navigator.modelContext exists but navigator.modelContextTesting is missing');\n logger.warn(' Cannot sync with native API. Please enable experimental features:');\n logger.warn(' - Navigate to chrome://flags');\n logger.warn(' - Enable \"Experimental Web Platform Features\"');\n logger.warn(' - Or launch with: --enable-experimental-web-platform-features');\n logger.warn(' Skipping initialization to avoid conflicts');\n return;\n }\n\n if (window.navigator.modelContext) {\n logger.warn('window.navigator.modelContext already exists, skipping initialization');\n return;\n }\n\n logger.info('Native API not detected, installing polyfill');\n\n try {\n const bridge = initializeMCPBridge(effectiveOptions);\n\n Object.defineProperty(window.navigator, 'modelContext', {\n value: bridge.modelContext,\n writable: false,\n configurable: false,\n });\n\n Object.defineProperty(window, '__mcpBridge', {\n value: bridge,\n writable: false,\n configurable: true,\n });\n\n logger.info('✅ window.navigator.modelContext initialized successfully');\n\n testingLogger.info('Installing polyfill');\n testingLogger.info(' 💡 To use the native implementation in Chromium:');\n testingLogger.info(' - Navigate to chrome://flags');\n testingLogger.info(' - Enable \"Experimental Web Platform Features\"');\n testingLogger.info(' - Or launch with: --enable-experimental-web-platform-features');\n\n const testingAPI = new WebModelContextTesting(bridge);\n bridge.modelContextTesting = testingAPI;\n\n (bridge.modelContext as WebModelContext).setTestingAPI(testingAPI);\n\n Object.defineProperty(window.navigator, 'modelContextTesting', {\n value: testingAPI,\n writable: false,\n configurable: true,\n });\n\n testingLogger.info('✅ Polyfill installed at window.navigator.modelContextTesting');\n } catch (error) {\n logger.error('Failed to initialize:', error);\n throw error;\n }\n}\n\n/**\n * Cleans up the Web Model Context API.\n * Closes all MCP servers and removes API from window.navigator.\n * Useful for testing and hot module replacement.\n *\n * @example\n * ```typescript\n * import { cleanupWebModelContext } from '@mcp-b/global';\n *\n * cleanupWebModelContext();\n * ```\n */\nexport function cleanupWebModelContext(): void {\n /* c8 ignore next */\n if (typeof window === 'undefined') return;\n\n if (window.__mcpBridge) {\n try {\n window.__mcpBridge.tabServer.close();\n\n if (window.__mcpBridge.iframeServer) {\n window.__mcpBridge.iframeServer.close();\n }\n } catch (error) {\n logger.warn('Error closing MCP servers:', error);\n }\n }\n\n delete (window.navigator as unknown as { modelContext?: unknown }).modelContext;\n delete (window.navigator as unknown as { modelContextTesting?: unknown }).modelContextTesting;\n delete (window as unknown as { __mcpBridge?: unknown }).__mcpBridge;\n\n logger.info('Cleaned up');\n}\n","import { initializeWebModelContext } from './global.js';\nimport { createLogger } from './logger.js';\nimport type { TransportConfiguration, WebModelContextInitOptions } from './types.js';\n\nconst logger = createLogger('WebModelContext');\n\ntype TabServerConfig = NonNullable<TransportConfiguration['tabServer']>;\n\nfunction mergeTransportOptions(\n base: TransportConfiguration,\n override: TransportConfiguration\n): TransportConfiguration {\n if (!base) {\n return override;\n }\n if (!override) {\n return base;\n }\n\n return {\n ...base,\n ...override,\n tabServer: {\n ...(base.tabServer ?? {}),\n ...(override.tabServer ?? {}),\n },\n };\n}\n\nfunction mergeInitOptions(\n base?: WebModelContextInitOptions,\n override?: WebModelContextInitOptions\n): WebModelContextInitOptions | undefined {\n if (!base) {\n return override;\n }\n if (!override) {\n return base;\n }\n\n return {\n ...base,\n ...override,\n transport: mergeTransportOptions(base.transport ?? {}, override.transport ?? {}),\n };\n}\n\nfunction parseScriptTagOptions(\n script: HTMLScriptElement | null\n): WebModelContextInitOptions | undefined {\n if (!script || !script.dataset) {\n return undefined;\n }\n\n const { dataset } = script;\n\n if (dataset.webmcpOptions) {\n try {\n return JSON.parse(dataset.webmcpOptions) as WebModelContextInitOptions;\n } catch (error) {\n logger.error('Invalid JSON in data-webmcp-options:', error);\n return undefined;\n }\n }\n\n const options: WebModelContextInitOptions = {};\n let hasOptions = false;\n\n if (dataset.webmcpAutoInitialize !== undefined) {\n options.autoInitialize = dataset.webmcpAutoInitialize !== 'false';\n hasOptions = true;\n }\n\n const tabServerOptions: TabServerConfig = {};\n let hasTabServerOptions = false;\n\n if (dataset.webmcpAllowedOrigins) {\n const origins = dataset.webmcpAllowedOrigins\n .split(',')\n .map((origin) => origin.trim())\n .filter((origin) => origin.length > 0);\n\n if (origins.length > 0) {\n tabServerOptions.allowedOrigins = origins;\n hasOptions = true;\n hasTabServerOptions = true;\n }\n }\n\n if (dataset.webmcpChannelId) {\n tabServerOptions.channelId = dataset.webmcpChannelId;\n hasOptions = true;\n hasTabServerOptions = true;\n }\n\n if (hasTabServerOptions) {\n options.transport = {\n ...(options.transport ?? {}),\n tabServer: {\n ...(options.transport?.tabServer ?? {}),\n ...tabServerOptions,\n },\n };\n }\n\n return hasOptions ? options : undefined;\n}\n\nif (typeof window !== 'undefined' && typeof document !== 'undefined') {\n const globalOptions = window.__webModelContextOptions;\n const scriptElement = document.currentScript as HTMLScriptElement | null;\n const scriptOptions = parseScriptTagOptions(scriptElement);\n const mergedOptions =\n mergeInitOptions(globalOptions, scriptOptions) ?? globalOptions ?? scriptOptions;\n\n if (mergedOptions) {\n window.__webModelContextOptions = mergedOptions;\n }\n\n const shouldAutoInitialize = mergedOptions?.autoInitialize !== false;\n\n try {\n if (shouldAutoInitialize) {\n initializeWebModelContext(mergedOptions);\n }\n } catch (error) {\n logger.error('Auto-initialization failed:', error);\n }\n}\n\nexport { cleanupWebModelContext, initializeWebModelContext } from './global.js';\nexport { createLogger } from './logger.js';\nexport type * from './types.js';\nexport { zodToJsonSchema } from './validation.js';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,MAAM,mBAAmB;;;;;;;AAQzB,SAAS,eAAe,WAA4B;AAClD,KAAI,OAAO,WAAW,eAAe,CAAC,OAAO,aAC3C,QAAO;AAGT,KAAI;EACF,MAAM,cAAc,aAAa,QAAQ,iBAAiB;AAC1D,MAAI,CAAC,YAAa,QAAO;AAEzB,MAAI,gBAAgB,IAAK,QAAO;AAGhC,SADiB,YAAY,MAAM,IAAI,CAAC,KAAK,MAAM,EAAE,MAAM,CAAC,CAC5C,MAAM,YAAY,cAAc,WAAW,UAAU,WAAW,GAAG,QAAQ,GAAG,CAAC;UACxF,KAAK;AAGZ,MAAI,OAAO,YAAY,eAAe,QAAQ,MAAM;GAClD,MAAM,UAAU,eAAe,QAAQ,IAAI,UAAU,OAAO,IAAI;AAChE,WAAQ,KAAK,gEAAgE,UAAU;;AAEzF,SAAO;;;;;;AAOX,MAAM,aAAmB;;;;;;;;;;;;;;;;;;;AAqCzB,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