@mcp-b/global 1.1.0 → 1.1.1

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/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["jsonSchemaToZod","convertJsonSchemaToZod","properties: Record<string, { type: string; description?: string; [key: string]: unknown }>","required: string[]","enumValues: unknown[] | undefined","items: unknown | undefined","propertySchema: { type: string; description?: string; [key: string]: unknown }","validatedTool: ValidatedToolDescriptor","bridge","customTransport: Transport | undefined","McpServer","bridge: MCPBridge","options: WebModelContextInitOptions","tabServerOptions: TabServerConfig"],"sources":["../src/validation.ts","../src/global.ts","../src/index.ts"],"sourcesContent":["// validation.ts - JSON Schema <-> Zod conversion and validation utilities\n\nimport { jsonSchemaToZod as convertJsonSchemaToZod } from '@composio/json-schema-to-zod';\nimport { z } from 'zod';\nimport type { InputSchema } from './types.js';\n\n/**\n * Detect if a schema is a Zod schema object (Record<string, ZodType>)\n * or a JSON Schema object\n */\nexport function isZodSchema(schema: unknown): boolean {\n if (typeof schema !== 'object' || schema === null) {\n return false;\n }\n\n // JSON Schema always has a 'type' property at the root\n if ('type' in schema && typeof (schema as { type: unknown }).type === 'string') {\n return false; // This is JSON Schema\n }\n\n // Check if any property value is a Zod type instance\n const values = Object.values(schema);\n if (values.length === 0) {\n return false; // Empty object, treat as JSON Schema\n }\n\n // If any value is a ZodType, it's a Zod schema\n return values.some((val) => val instanceof z.ZodType);\n}\n\n/**\n * Convert JSON Schema to Zod validator\n * Uses @composio/json-schema-to-zod for conversion\n */\nexport function jsonSchemaToZod(jsonSchema: InputSchema): z.ZodType {\n try {\n // convertJsonSchemaToZod returns a Zod schema from JSON Schema\n const zodSchema = convertJsonSchemaToZod(jsonSchema as unknown as object);\n return zodSchema;\n } catch (error) {\n console.warn('[Web Model Context] Failed to convert JSON Schema to Zod:', error);\n // Fallback: accept anything with passthrough\n return z.object({}).passthrough();\n }\n}\n\n/**\n * Convert Zod schema object to JSON Schema\n * Based on react-webmcp implementation\n */\nexport function zodToJsonSchema(schema: Record<string, z.ZodTypeAny>): InputSchema {\n const properties: Record<string, { type: string; description?: string; [key: string]: unknown }> =\n {};\n const required: string[] = [];\n\n for (const [key, zodType] of Object.entries(schema)) {\n // Extract description if available\n const description = (zodType as { description?: string }).description || undefined;\n\n // Infer JSON Schema type from Zod type\n let type = 'string';\n let enumValues: unknown[] | undefined;\n let items: unknown | undefined;\n\n if (zodType instanceof z.ZodString) {\n type = 'string';\n } else if (zodType instanceof z.ZodNumber) {\n type = 'number';\n } else if (zodType instanceof z.ZodBoolean) {\n type = 'boolean';\n } else if (zodType instanceof z.ZodArray) {\n type = 'array';\n // Try to get array item type\n const elementType = (zodType as { element?: z.ZodTypeAny }).element;\n if (elementType instanceof z.ZodString) {\n items = { type: 'string' };\n } else if (elementType instanceof z.ZodNumber) {\n items = { type: 'number' };\n } else if (elementType instanceof z.ZodBoolean) {\n items = { type: 'boolean' };\n } else {\n items = { type: 'string' };\n }\n } else if (zodType instanceof z.ZodObject) {\n type = 'object';\n } else if (zodType instanceof z.ZodEnum) {\n type = 'string';\n // Extract enum values\n const enumDef = (zodType as { _def?: { values?: unknown[] } })._def;\n if (enumDef?.values) {\n enumValues = enumDef.values;\n }\n }\n\n const propertySchema: { type: string; description?: string; [key: string]: unknown } = { type };\n if (description) {\n propertySchema.description = description;\n }\n if (enumValues) {\n propertySchema.enum = enumValues;\n }\n if (items) {\n propertySchema.items = items;\n }\n\n properties[key] = propertySchema;\n\n // Check if field is required (not optional)\n if (!zodType.isOptional()) {\n required.push(key);\n }\n }\n\n return {\n type: 'object',\n properties,\n ...(required.length > 0 && { required }),\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 */\nexport function normalizeSchema(schema: InputSchema | Record<string, z.ZodTypeAny>): {\n jsonSchema: InputSchema;\n zodValidator: z.ZodType;\n} {\n const isZod = isZodSchema(schema);\n\n if (isZod) {\n // Input is Zod schema object → convert to JSON Schema and wrap in z.object()\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 // Input is JSON Schema → convert to Zod\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 // Format Zod errors into readable message\n const errors = result.error.errors\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 { Transport } from '@mcp-b/webmcp-ts-sdk';\nimport {\n CallToolRequestSchema,\n ListToolsRequestSchema,\n Server as McpServer,\n} from '@mcp-b/webmcp-ts-sdk';\nimport type {\n InternalModelContext,\n MCPBridge,\n ModelContextInput,\n ToolCallEvent,\n ToolDescriptor,\n ToolResponse,\n ValidatedToolDescriptor,\n WebModelContextInitOptions,\n} from './types.js';\nimport { normalizeSchema, validateWithZod } from './validation.js';\n\ndeclare global {\n interface Window {\n __webModelContextOptions?: WebModelContextInitOptions;\n }\n}\n\n/**\n * Custom ToolCallEvent implementation\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 constructor(toolName: string, args: Record<string, unknown>) {\n super('toolcall', { cancelable: true });\n this.name = toolName;\n this.arguments = args;\n }\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 getResponse(): ToolResponse | null {\n return this._response;\n }\n\n hasResponse(): boolean {\n return this._responded;\n }\n}\n\n/**\n * Time window (in ms) to detect rapid duplicate registrations\n * Registrations within this window are likely due to React Strict Mode\n */\nconst RAPID_DUPLICATE_WINDOW_MS = 50;\n\n/**\n * ModelContext implementation that bridges to MCP SDK\n * Implements the W3C Web Model Context API proposal with two-bucket tool management\n *\n * Two-Bucket System:\n * - Bucket A (provideContextTools): Tools registered via provideContext() - base/app-level tools\n * - Bucket B (dynamicTools): Tools registered via registerTool() - component-scoped tools\n *\n * Benefits:\n * - provideContext() only clears Bucket A, leaving Bucket B intact\n * - Components can manage their own tool lifecycle independently\n * - Final tool list = Bucket A + Bucket B (merged, with collision detection)\n */\nclass WebModelContext implements InternalModelContext {\n private bridge: MCPBridge;\n private eventTarget: EventTarget;\n private provideContextTools: Map<string, ValidatedToolDescriptor>;\n private dynamicTools: Map<string, ValidatedToolDescriptor>;\n private registrationTimestamps: Map<string, number>;\n private unregisterFunctions: Map<string, () => void>;\n\n constructor(bridge: MCPBridge) {\n this.bridge = bridge;\n this.eventTarget = new EventTarget();\n this.provideContextTools = new Map();\n this.dynamicTools = new Map();\n this.registrationTimestamps = new Map();\n this.unregisterFunctions = new Map();\n }\n\n /**\n * Add event listener (compatible with ModelContext interface)\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 * Remove event listener\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 * Dispatch event\n */\n dispatchEvent(event: Event): boolean {\n return this.eventTarget.dispatchEvent(event);\n }\n\n /**\n * Provide context (tools) to AI models\n * Clears and replaces Bucket A (provideContext tools), leaving Bucket B (dynamic tools) intact\n */\n provideContext(context: ModelContextInput): void {\n console.log(`[Web Model Context] Registering ${context.tools.length} tools via provideContext`);\n\n this.provideContextTools.clear();\n\n for (const tool of context.tools) {\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\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 this.updateBridgeTools();\n\n this.notifyToolsListChanged();\n }\n\n /**\n * Register a single tool dynamically (Bucket B)\n * Returns an object with an unregister function to remove the tool\n * Tools registered via this method persist across provideContext() calls\n */\n registerTool(tool: ToolDescriptor<any, any>): { unregister: () => void } {\n console.log(`[Web Model Context] Registering tool dynamically: ${tool.name}`);\n\n const now = Date.now();\n const lastRegistration = this.registrationTimestamps.get(tool.name);\n\n if (lastRegistration && now - lastRegistration < RAPID_DUPLICATE_WINDOW_MS) {\n console.warn(\n `[Web Model Context] 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.unregisterFunctions.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,\n inputValidator: inputZod,\n ...(normalizedOutput && { outputValidator: normalizedOutput.zodValidator }),\n };\n\n this.dynamicTools.set(tool.name, validatedTool);\n\n this.registrationTimestamps.set(tool.name, now);\n\n this.updateBridgeTools();\n\n this.notifyToolsListChanged();\n\n const unregisterFn = () => {\n console.log(`[Web Model Context] Unregistering tool: ${tool.name}`);\n\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 console.warn(\n `[Web Model Context] Tool \"${tool.name}\" is not registered, ignoring unregister call`\n );\n return;\n }\n\n this.dynamicTools.delete(tool.name);\n\n this.registrationTimestamps.delete(tool.name);\n this.unregisterFunctions.delete(tool.name);\n\n this.updateBridgeTools();\n\n this.notifyToolsListChanged();\n };\n\n this.unregisterFunctions.set(tool.name, unregisterFn);\n\n return { unregister: unregisterFn };\n }\n\n /**\n * Update the bridge tools map with merged tools from both buckets\n * Final tool list = Bucket A (provideContext) + Bucket B (dynamic)\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 console.log(\n `[Web Model Context] Updated bridge with ${this.provideContextTools.size} base tools + ${this.dynamicTools.size} dynamic tools = ${this.bridge.tools.size} total`\n );\n }\n\n /**\n * Notify all servers that the tools list has changed\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\n /**\n * Execute a tool with hybrid approach:\n * 1. Validate input arguments\n * 2. Dispatch toolcall event first\n * 3. If not prevented, call tool's execute function\n * 4. Validate output (permissive mode - warn only)\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 console.log(`[Web Model Context] Validating input for tool: ${toolName}`);\n const validation = validateWithZod(args, tool.inputValidator);\n if (!validation.success) {\n console.error(\n `[Web Model Context] Input validation failed for ${toolName}:`,\n validation.error\n );\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 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 console.log(`[Web Model Context] Tool ${toolName} handled by event listener`);\n return response;\n }\n }\n\n console.log(`[Web Model Context] Executing tool: ${toolName}`);\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 console.warn(\n `[Web Model Context] Output validation failed for ${toolName}:`,\n outputValidation.error\n );\n }\n }\n\n return response;\n } catch (error) {\n console.error(`[Web Model Context] 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 * Get list of registered tools in MCP format\n * Includes full MCP spec: annotations, outputSchema, etc.\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\n/**\n * Initialize the MCP bridge with dual-server support\n * Creates both TabServer (same-window) and IframeChildServer (parent-child) by default\n */\nfunction initializeMCPBridge(options?: WebModelContextInitOptions): MCPBridge {\n console.log('[Web Model Context] Initializing MCP bridge');\n\n const hostname = window.location.hostname || 'localhost';\n const transportOptions = options?.transport;\n\n const setupServerHandlers = (server: McpServer, bridge: MCPBridge) => {\n server.setRequestHandler(ListToolsRequestSchema, async () => {\n console.log('[MCP Bridge] Handling list_tools request');\n return {\n tools: bridge.modelContext.listTools(),\n };\n });\n\n server.setRequestHandler(CallToolRequestSchema, async (request) => {\n console.log(`[MCP Bridge] Handling call_tool request: ${request.params.name}`);\n\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 in MCP SDK format\n return {\n content: response.content,\n isError: response.isError,\n };\n } catch (error) {\n console.error(`[MCP Bridge] Error calling tool ${toolName}:`, error);\n throw error;\n }\n });\n };\n\n const customTransport: Transport | undefined = transportOptions?.create?.();\n\n if (customTransport) {\n console.log('[Web Model Context] Using custom transport');\n\n const server = new McpServer(\n {\n name: hostname,\n version: '1.0.0',\n },\n {\n capabilities: {\n tools: {\n listChanged: true,\n },\n },\n }\n );\n\n const bridge: MCPBridge = {\n tabServer: server,\n tools: 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 console.log('[Web Model Context] MCP server connected with custom transport');\n return bridge;\n }\n\n console.log('[Web Model Context] Using dual-server mode');\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: {\n listChanged: true,\n },\n },\n }\n );\n\n // Create bridge object (modelContext is assigned after instantiation)\n const bridge: MCPBridge = {\n tabServer,\n tools: new Map(),\n modelContext: undefined as unknown as InternalModelContext,\n isInitialized: true,\n };\n\n // Create modelContext and attach to bridge\n const modelContext = new WebModelContext(bridge);\n bridge.modelContext = modelContext;\n\n // Set up handlers for tab server\n setupServerHandlers(tabServer, bridge);\n\n // Connect tab server transport\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 console.log('[Web Model Context] Tab server connected');\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 console.log('[Web Model Context] Enabling iframe server');\n\n const iframeServer = new McpServer(\n {\n name: `${hostname}-iframe`,\n version: '1.0.0',\n },\n {\n capabilities: {\n tools: {\n listChanged: true,\n },\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 console.log('[Web Model Context] Iframe server connected');\n }\n\n return bridge;\n}\n\n/**\n * Initialize the Web Model Context API (window.navigator.modelContext)\n */\nexport function initializeWebModelContext(options?: WebModelContextInitOptions): void {\n if (typeof window === 'undefined') {\n console.warn('[Web Model Context] Not in browser environment, skipping initialization');\n return;\n }\n\n const effectiveOptions = options ?? window.__webModelContextOptions;\n\n if (window.navigator.modelContext) {\n console.warn(\n '[Web Model Context] window.navigator.modelContext already exists, skipping initialization'\n );\n return;\n }\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 console.log('✅ [Web Model Context] window.navigator.modelContext initialized successfully');\n } catch (error) {\n console.error('[Web Model Context] Failed to initialize:', error);\n throw error;\n }\n}\n\n/**\n * Cleanup function (for testing/development)\n */\nexport function cleanupWebModelContext(): void {\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 console.warn('[Web Model Context] Error closing MCP servers:', error);\n }\n }\n\n delete (window.navigator as unknown as { modelContext?: unknown }).modelContext;\n delete (window as unknown as { __mcpBridge?: unknown }).__mcpBridge;\n\n console.log('[Web Model Context] Cleaned up');\n}\n","// index.ts - Entry point for Web Model Context API polyfill\n\nimport { initializeWebModelContext } from './global.js';\nimport type { TransportConfiguration, WebModelContextInitOptions } from './types.js';\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 console.error('[Web Model Context] 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\n// Auto-initialize immediately when script loads in browser environments\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 console.error('[Web Model Context] Auto-initialization failed:', error);\n }\n}\n\n// For manual initialization (when using as ES module)\nexport { cleanupWebModelContext, initializeWebModelContext } from './global.js';\nexport type * from './types.js';\n"],"mappings":";;;;;;;;;;AAUA,SAAgB,YAAY,QAA0B;AACpD,KAAI,OAAO,WAAW,YAAY,WAAW,KAC3C,QAAO;AAIT,KAAI,UAAU,UAAU,OAAQ,OAA6B,SAAS,SACpE,QAAO;CAIT,MAAM,SAAS,OAAO,OAAO,OAAO;AACpC,KAAI,OAAO,WAAW,EACpB,QAAO;AAIT,QAAO,OAAO,MAAM,QAAQ,eAAe,EAAE,QAAQ;;;;;;AAOvD,SAAgBA,kBAAgB,YAAoC;AAClE,KAAI;AAGF,SADkBC,gBAAuB,WAAgC;UAElE,OAAO;AACd,UAAQ,KAAK,6DAA6D,MAAM;AAEhF,SAAO,EAAE,OAAO,EAAE,CAAC,CAAC,aAAa;;;;;;;AAQrC,SAAgB,gBAAgB,QAAmD;CACjF,MAAMC,aACJ,EAAE;CACJ,MAAMC,WAAqB,EAAE;AAE7B,MAAK,MAAM,CAAC,KAAK,YAAY,OAAO,QAAQ,OAAO,EAAE;EAEnD,MAAM,cAAe,QAAqC,eAAe;EAGzE,IAAI,OAAO;EACX,IAAIC;EACJ,IAAIC;AAEJ,MAAI,mBAAmB,EAAE,UACvB,QAAO;WACE,mBAAmB,EAAE,UAC9B,QAAO;WACE,mBAAmB,EAAE,WAC9B,QAAO;WACE,mBAAmB,EAAE,UAAU;AACxC,UAAO;GAEP,MAAM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Q,cAAc;UACjC,OAAO;AACd,UAAQ,MAAM,4DAA4D,MAAM;AAChF;;CAIJ,MAAME,UAAsC,EAAE;CAC9C,IAAI,aAAa;AAEjB,KAAI,QAAQ,yBAAyB,QAAW;AAC9C,UAAQ,iBAAiB,QAAQ,yBAAyB;AAC1D,eAAa;;CAGf,MAAMC,mBAAoC,EAAE;CAC5C,IAAI,sBAAsB;AAE1B,KAAI,QAAQ,sBAAsB;EAChC,MAAM,UAAU,QAAQ,qBACrB,MAAM,IAAI,CACV,KAAK,WAAW,OAAO,MAAM,CAAC,CAC9B,QAAQ,WAAW,OAAO,SAAS,EAAE;AAExC,MAAI,QAAQ,SAAS,GAAG;AACtB,oBAAiB,iBAAiB;AAClC,gBAAa;AACb,yBAAsB;;;AAI1B,KAAI,QAAQ,iBAAiB;AAC3B,mBAAiB,YAAY,QAAQ;AACrC,eAAa;AACb,wBAAsB;;AAGxB,KAAI,oBACF,SAAQ,YAAY;EAClB,GAAI,QAAQ,aAAa,EAAE;EAC3B,WAAW;GACT,GAAI,QAAQ,WAAW,aAAa,EAAE;GACtC,GAAG;GACJ;EACF;AAGH,QAAO,aAAa,UAAU;;AAIhC,IAAI,OAAO,WAAW,eAAe,OAAO,aAAa,aAAa;CACpE,MAAM,gBAAgB,OAAO;CAC7B,MAAM,gBAAgB,SAAS;CAC/B,MAAM,gBAAgB,sBAAsB,cAAc;CAC1D,MAAM,gBACJ,iBAAiB,eAAe,cAAc,IAAI,iBAAiB;AAErE,KAAI,cACF,QAAO,2BAA2B;CAGpC,MAAM,uBAAuB,eAAe,mBAAmB;AAE/D,KAAI;AACF,MAAI,qBACF,2BAA0B,cAAc;UAEnC,OAAO;AACd,UAAQ,MAAM,mDAAmD,MAAM"}
1
+ {"version":3,"file":"index.js","names":["jsonSchemaToZod","convertJsonSchemaToZod","properties: Record<string, { type: string; description?: string; [key: string]: unknown }>","required: string[]","enumValues: unknown[] | undefined","items: unknown | undefined","propertySchema: { type: string; description?: string; [key: string]: unknown }","validatedTool: ValidatedToolDescriptor","jsonSchemaToZod","args: Record<string, unknown>","bridge","customTransport: Transport | undefined","McpServer","bridge: MCPBridge","options: WebModelContextInitOptions","tabServerOptions: TabServerConfig"],"sources":["../src/validation.ts","../src/global.ts","../src/index.ts"],"sourcesContent":["import { jsonSchemaToZod as convertJsonSchemaToZod } from '@composio/json-schema-to-zod';\nimport { z } from 'zod';\nimport type { InputSchema } from './types.js';\n\n/**\n * Detect if a schema is a Zod schema object (Record<string, ZodType>)\n * or a JSON Schema object\n */\nexport function isZodSchema(schema: unknown): boolean {\n if (typeof schema !== 'object' || schema === null) {\n return false;\n }\n\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 return values.some((val) => val instanceof z.ZodType);\n}\n\n/**\n * Convert JSON Schema to Zod validator\n * Uses @composio/json-schema-to-zod for conversion\n */\nexport function jsonSchemaToZod(jsonSchema: InputSchema): z.ZodType {\n try {\n const zodSchema = convertJsonSchemaToZod(jsonSchema as unknown as object);\n return zodSchema;\n } catch (error) {\n console.warn('[Web Model Context] 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 * Based on react-webmcp implementation\n */\nexport function zodToJsonSchema(schema: Record<string, z.ZodTypeAny>): InputSchema {\n const properties: Record<string, { type: string; description?: string; [key: string]: unknown }> =\n {};\n const required: string[] = [];\n\n for (const [key, zodType] of Object.entries(schema)) {\n const description = (zodType as { description?: string }).description || undefined;\n\n let type = 'string';\n let enumValues: unknown[] | undefined;\n let items: unknown | undefined;\n\n if (zodType instanceof z.ZodString) {\n type = 'string';\n } else if (zodType instanceof z.ZodNumber) {\n type = 'number';\n } else if (zodType instanceof z.ZodBoolean) {\n type = 'boolean';\n } else if (zodType instanceof z.ZodArray) {\n type = 'array';\n const elementType = (zodType as { element?: z.ZodTypeAny }).element;\n if (elementType instanceof z.ZodString) {\n items = { type: 'string' };\n } else if (elementType instanceof z.ZodNumber) {\n items = { type: 'number' };\n } else if (elementType instanceof z.ZodBoolean) {\n items = { type: 'boolean' };\n } else {\n items = { type: 'string' };\n }\n } else if (zodType instanceof z.ZodObject) {\n type = 'object';\n } else if (zodType instanceof z.ZodEnum) {\n type = 'string';\n const enumDef = (zodType as { _def?: { values?: unknown[] } })._def;\n if (enumDef?.values) {\n enumValues = enumDef.values;\n }\n }\n\n const propertySchema: { type: string; description?: string; [key: string]: unknown } = { type };\n if (description) {\n propertySchema.description = description;\n }\n if (enumValues) {\n propertySchema.enum = enumValues;\n }\n if (items) {\n propertySchema.items = items;\n }\n\n properties[key] = propertySchema;\n\n if (!zodType.isOptional()) {\n required.push(key);\n }\n }\n\n return {\n type: 'object',\n properties,\n ...(required.length > 0 && { required }),\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 */\nexport function normalizeSchema(schema: InputSchema | Record<string, z.ZodTypeAny>): {\n jsonSchema: InputSchema;\n zodValidator: z.ZodType;\n} {\n const isZod = isZodSchema(schema);\n\n if (isZod) {\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.errors\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 { Transport } from '@mcp-b/webmcp-ts-sdk';\nimport {\n CallToolRequestSchema,\n ListToolsRequestSchema,\n Server as McpServer,\n} from '@mcp-b/webmcp-ts-sdk';\nimport type {\n InternalModelContext,\n MCPBridge,\n ModelContext,\n ModelContextInput,\n ModelContextTesting,\n ToolCallEvent,\n ToolDescriptor,\n ToolResponse,\n ValidatedToolDescriptor,\n WebModelContextInitOptions,\n ZodSchemaObject,\n} from './types.js';\nimport { jsonSchemaToZod, normalizeSchema, validateWithZod } from './validation.js';\n\ndeclare global {\n interface Window {\n __webModelContextOptions?: WebModelContextInitOptions;\n }\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) by examining\n * the constructor name.\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 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 { hasNativeContext: false, hasNativeTesting: false };\n }\n\n const testingConstructorName = modelContextTesting.constructor?.name || '';\n const isPolyfill = testingConstructorName.includes('WebModelContext');\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 console.log('[Native Adapter] Tool change detected from native API');\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 console.log(`[Native Adapter] Syncing ${nativeTools.length} tools from native API`);\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 console.error(`[Native Adapter] 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 console.log('[Native Adapter] Delegating provideContext to native API');\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 console.log(`[Native Adapter] Delegating registerTool(\"${tool.name}\") to native API`);\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 console.log(`[Native Adapter] Delegating unregisterTool(\"${name}\") to native API`);\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 console.log('[Native Adapter] Delegating clearContext to native API');\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 console.log(`[Native Adapter] Executing tool \"${toolName}\" via native API`);\n try {\n const result = await this.nativeTesting.executeTool(toolName, JSON.stringify(args));\n return this.convertToToolResponse(result);\n } catch (error) {\n console.error(`[Native Adapter] 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 /**\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\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 * 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 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 console.error('[Model Context Testing] 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 console.log(`[Model Context Testing] Executing tool: ${toolName}`);\n\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 console.log('[Model Context Testing] Tools changed callback registered');\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 console.log('[Model Context Testing] Tool call history cleared');\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 console.log(`[Model Context Testing] Mock response set for tool: ${toolName}`);\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 console.log(`[Model Context Testing] Mock response cleared for tool: ${toolName}`);\n }\n\n /**\n * Clears all mock tool responses (polyfill extension).\n */\n clearAllMockToolResponses(): void {\n this.mockResponses.clear();\n console.log('[Model Context Testing] All mock responses cleared');\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 console.log('[Model Context Testing] Testing state reset');\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 private provideContextTools: Map<string, ValidatedToolDescriptor>;\n private dynamicTools: Map<string, ValidatedToolDescriptor>;\n private registrationTimestamps: Map<string, number>;\n private unregisterFunctions: Map<string, () => void>;\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 this.provideContextTools = new Map();\n this.dynamicTools = new Map();\n this.registrationTimestamps = new Map();\n this.unregisterFunctions = 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) to AI models by registering base tools (Bucket A).\n * Clears and replaces all previously registered base tools while preserving\n * dynamic tools registered via registerTool().\n *\n * @param {ModelContextInput} context - Context containing tools to register\n * @throws {Error} If a tool name collides with an existing dynamic tool\n */\n provideContext(context: ModelContextInput): void {\n console.log(`[Web Model Context] Registering ${context.tools.length} tools via provideContext`);\n\n this.provideContextTools.clear();\n\n for (const tool of context.tools) {\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\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 this.updateBridgeTools();\n\n this.notifyToolsListChanged();\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 console.log(`[Web Model Context] Registering tool dynamically: ${tool.name}`);\n\n const now = Date.now();\n const lastRegistration = this.registrationTimestamps.get(tool.name);\n\n if (lastRegistration && now - lastRegistration < RAPID_DUPLICATE_WINDOW_MS) {\n console.warn(\n `[Web Model Context] 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.unregisterFunctions.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\n this.registrationTimestamps.set(tool.name, now);\n\n this.updateBridgeTools();\n\n this.notifyToolsListChanged();\n\n const unregisterFn = () => {\n console.log(`[Web Model Context] Unregistering tool: ${tool.name}`);\n\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 console.warn(\n `[Web Model Context] Tool \"${tool.name}\" is not registered, ignoring unregister call`\n );\n return;\n }\n\n this.dynamicTools.delete(tool.name);\n\n this.registrationTimestamps.delete(tool.name);\n this.unregisterFunctions.delete(tool.name);\n\n this.updateBridgeTools();\n\n this.notifyToolsListChanged();\n };\n\n this.unregisterFunctions.set(tool.name, unregisterFn);\n\n return { unregister: unregisterFn };\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 console.log(`[Web Model Context] Unregistering tool: ${name}`);\n\n const inProvideContext = this.provideContextTools.has(name);\n const inDynamic = this.dynamicTools.has(name);\n\n if (!inProvideContext && !inDynamic) {\n console.warn(\n `[Web Model Context] Tool \"${name}\" is not registered, ignoring unregister call`\n );\n return;\n }\n\n if (inProvideContext) {\n this.provideContextTools.delete(name);\n }\n\n if (inDynamic) {\n this.dynamicTools.delete(name);\n this.registrationTimestamps.delete(name);\n this.unregisterFunctions.delete(name);\n }\n\n this.updateBridgeTools();\n this.notifyToolsListChanged();\n }\n\n /**\n * Clears all registered tools from both buckets (Chromium native API).\n * Removes all tools registered via provideContext() and registerTool().\n */\n clearContext(): void {\n console.log('[Web Model Context] Clearing all tools');\n\n this.provideContextTools.clear();\n this.dynamicTools.clear();\n this.registrationTimestamps.clear();\n this.unregisterFunctions.clear();\n\n this.updateBridgeTools();\n this.notifyToolsListChanged();\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 console.log(\n `[Web Model Context] Updated bridge with ${this.provideContextTools.size} base tools + ${this.dynamicTools.size} dynamic tools = ${this.bridge.tools.size} total`\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 * 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 console.log(`[Web Model Context] Validating input for tool: ${toolName}`);\n const validation = validateWithZod(args, tool.inputValidator);\n if (!validation.success) {\n console.error(\n `[Web Model Context] Input validation failed for ${toolName}:`,\n validation.error\n );\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 console.log(`[Web Model Context] Returning mock response for tool: ${toolName}`);\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 console.log(`[Web Model Context] Tool ${toolName} handled by event listener`);\n return response;\n }\n }\n\n console.log(`[Web Model Context] Executing tool: ${toolName}`);\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 console.warn(\n `[Web Model Context] Output validation failed for ${toolName}:`,\n outputValidation.error\n );\n }\n }\n\n return response;\n } catch (error) {\n console.error(`[Web Model Context] 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\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 console.log('[Web Model Context] Initializing MCP bridge');\n\n const hostname = window.location.hostname || 'localhost';\n const transportOptions = options?.transport;\n\n const setupServerHandlers = (server: McpServer, bridge: MCPBridge) => {\n server.setRequestHandler(ListToolsRequestSchema, async () => {\n console.log('[MCP Bridge] Handling list_tools request');\n return {\n tools: bridge.modelContext.listTools(),\n };\n });\n\n server.setRequestHandler(CallToolRequestSchema, async (request) => {\n console.log(`[MCP Bridge] Handling call_tool request: ${request.params.name}`);\n\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 };\n } catch (error) {\n console.error(`[MCP Bridge] Error calling tool ${toolName}:`, error);\n throw error;\n }\n });\n };\n\n const customTransport: Transport | undefined = transportOptions?.create?.();\n\n if (customTransport) {\n console.log('[Web Model Context] Using custom transport');\n\n const server = new McpServer(\n {\n name: hostname,\n version: '1.0.0',\n },\n {\n capabilities: {\n tools: {\n listChanged: true,\n },\n },\n }\n );\n\n const bridge: MCPBridge = {\n tabServer: server,\n tools: 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 console.log('[Web Model Context] MCP server connected with custom transport');\n return bridge;\n }\n\n console.log('[Web Model Context] Using dual-server mode');\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: {\n listChanged: true,\n },\n },\n }\n );\n\n const bridge: MCPBridge = {\n tabServer,\n tools: 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 console.log('[Web Model Context] Tab server connected');\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 console.log('[Web Model Context] Enabling iframe server');\n\n const iframeServer = new McpServer(\n {\n name: `${hostname}-iframe`,\n version: '1.0.0',\n },\n {\n capabilities: {\n tools: {\n listChanged: true,\n },\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 console.log('[Web Model Context] Iframe server connected');\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 if (typeof window === 'undefined') {\n console.warn('[Web Model Context] 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 console.error('[Web Model Context] Native API detection mismatch');\n return;\n }\n\n console.log('✅ [Web Model Context] Native Chromium API detected');\n console.log(' Using native implementation with MCP bridge synchronization');\n console.log(' 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 console.log('✅ [Web Model Context] MCP bridge synced with native API');\n console.log(' MCP clients will receive automatic tool updates from native registry');\n } catch (error) {\n console.error('[Web Model Context] Failed to initialize native adapter:', error);\n throw error;\n }\n\n return;\n }\n\n if (native.hasNativeContext && !native.hasNativeTesting) {\n console.warn('[Web Model Context] Partial native API detected');\n console.warn(' navigator.modelContext exists but navigator.modelContextTesting is missing');\n console.warn(' Cannot sync with native API. Please enable experimental features:');\n console.warn(' - Navigate to chrome://flags');\n console.warn(' - Enable \"Experimental Web Platform Features\"');\n console.warn(' - Or launch with: --enable-experimental-web-platform-features');\n console.warn(' Skipping initialization to avoid conflicts');\n return;\n }\n\n if (window.navigator.modelContext) {\n console.warn(\n '[Web Model Context] window.navigator.modelContext already exists, skipping initialization'\n );\n return;\n }\n\n console.log('[Web Model Context] 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 console.log('✅ [Web Model Context] window.navigator.modelContext initialized successfully');\n\n console.log('[Model Context Testing] Installing polyfill');\n console.log(' 💡 To use the native implementation in Chromium:');\n console.log(' - Navigate to chrome://flags');\n console.log(' - Enable \"Experimental Web Platform Features\"');\n console.log(' - 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 console.log(\n '✅ [Model Context Testing] Polyfill installed at window.navigator.modelContextTesting'\n );\n } catch (error) {\n console.error('[Web Model Context] 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 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 console.warn('[Web Model Context] 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 console.log('[Web Model Context] Cleaned up');\n}\n","import { initializeWebModelContext } from './global.js';\nimport type { TransportConfiguration, WebModelContextInitOptions } from './types.js';\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 console.error('[Web Model Context] 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 console.error('[Web Model Context] Auto-initialization failed:', error);\n }\n}\n\nexport { cleanupWebModelContext, initializeWebModelContext } from './global.js';\nexport type * from './types.js';\n"],"mappings":";;;;;;;;;;AAQA,SAAgB,YAAY,QAA0B;AACpD,KAAI,OAAO,WAAW,YAAY,WAAW,KAC3C,QAAO;AAGT,KAAI,UAAU,UAAU,OAAQ,OAA6B,SAAS,SACpE,QAAO;CAGT,MAAM,SAAS,OAAO,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B,MAAM,IAAI,CACV,KAAK,WAAW,OAAO,MAAM,CAAC,CAC9B,QAAQ,WAAW,OAAO,SAAS,EAAE;AAExC,MAAI,QAAQ,SAAS,GAAG;AACtB,oBAAiB,iBAAiB;AAClC,gBAAa;AACb,yBAAsB;;;AAI1B,KAAI,QAAQ,iBAAiB;AAC3B,mBAAiB,YAAY,QAAQ;AACrC,eAAa;AACb,wBAAsB;;AAGxB,KAAI,oBACF,SAAQ,YAAY;EAClB,GAAI,QAAQ,aAAa,EAAE;EAC3B,WAAW;GACT,GAAI,QAAQ,WAAW,aAAa,EAAE;GACtC,GAAG;GACJ;EACF;AAGH,QAAO,aAAa,UAAU;;AAGhC,IAAI,OAAO,WAAW,eAAe,OAAO,aAAa,aAAa;CACpE,MAAM,gBAAgB,OAAO;CAC7B,MAAM,gBAAgB,SAAS;CAC/B,MAAM,gBAAgB,sBAAsB,cAAc;CAC1D,MAAM,gBACJ,iBAAiB,eAAe,cAAc,IAAI,iBAAiB;AAErE,KAAI,cACF,QAAO,2BAA2B;CAGpC,MAAM,uBAAuB,eAAe,mBAAmB;AAE/D,KAAI;AACF,MAAI,qBACF,2BAA0B,cAAc;UAEnC,OAAO;AACd,UAAQ,MAAM,mDAAmD,MAAM"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mcp-b/global",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "W3C Web Model Context API polyfill - implements window.navigator.modelContext bridging to Model Context Protocol",
5
5
  "keywords": [
6
6
  "mcp",
@@ -48,8 +48,8 @@
48
48
  "dependencies": {
49
49
  "@composio/json-schema-to-zod": "^0.1.17",
50
50
  "zod": "3.25.76",
51
- "@mcp-b/transports": "1.1.0",
52
- "@mcp-b/webmcp-ts-sdk": "1.0.1"
51
+ "@mcp-b/webmcp-ts-sdk": "1.0.1",
52
+ "@mcp-b/transports": "1.1.1"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@types/node": "22.17.2",