( env?: Record<string, string>, )
| 83 | * and the in-process path in the MCP client. |
| 84 | */ |
| 85 | export function createChromeContext( |
| 86 | env?: Record<string, string>, |
| 87 | ): ClaudeForChromeContext { |
| 88 | const logger = new DebugLogger() |
| 89 | const chromeBridgeUrl = getChromeBridgeUrl() |
| 90 | logger.info(`Bridge URL: ${chromeBridgeUrl ?? 'none (using native socket)'}`) |
| 91 | const rawPermissionMode = |
| 92 | env?.CLAUDE_CHROME_PERMISSION_MODE ?? |
| 93 | process.env.CLAUDE_CHROME_PERMISSION_MODE |
| 94 | let initialPermissionMode: PermissionMode | undefined |
| 95 | if (rawPermissionMode) { |
| 96 | if (isPermissionMode(rawPermissionMode)) { |
| 97 | initialPermissionMode = rawPermissionMode |
| 98 | } else { |
| 99 | logger.warn( |
| 100 | `Invalid CLAUDE_CHROME_PERMISSION_MODE "${rawPermissionMode}". Valid values: ${PERMISSION_MODES.join(', ')}`, |
| 101 | ) |
| 102 | } |
| 103 | } |
| 104 | return { |
| 105 | serverName: 'Claude in Chrome', |
| 106 | logger, |
| 107 | socketPath: getSecureSocketPath(), |
| 108 | getSocketPaths: getAllSocketPaths, |
| 109 | clientTypeId: 'claude-code', |
| 110 | onAuthenticationError: () => { |
| 111 | logger.warn( |
| 112 | 'Authentication error occurred. Please ensure you are logged into the Claude browser extension with the same claude.ai account as Claude Code.', |
| 113 | ) |
| 114 | }, |
| 115 | onToolCallDisconnected: () => { |
| 116 | return `Browser extension is not connected. Please ensure the Claude browser extension is installed and running (${EXTENSION_DOWNLOAD_URL}), and that you are logged into claude.ai with the same account as Claude Code. If this is your first time connecting to Chrome, you may need to restart Chrome for the installation to take effect. If you continue to experience issues, please report a bug: ${BUG_REPORT_URL}` |
| 117 | }, |
| 118 | onExtensionPaired: (deviceId: string, name: string) => { |
| 119 | saveGlobalConfig(config => { |
| 120 | if ( |
| 121 | config.chromeExtension?.pairedDeviceId === deviceId && |
| 122 | config.chromeExtension?.pairedDeviceName === name |
| 123 | ) { |
| 124 | return config |
| 125 | } |
| 126 | return { |
| 127 | ...config, |
| 128 | chromeExtension: { |
| 129 | pairedDeviceId: deviceId, |
| 130 | pairedDeviceName: name, |
| 131 | }, |
| 132 | } |
| 133 | }) |
| 134 | logger.info(`Paired with "${name}" (${deviceId.slice(0, 8)})`) |
| 135 | }, |
| 136 | getPersistedDeviceId: () => { |
| 137 | return getGlobalConfig().chromeExtension?.pairedDeviceId |
| 138 | }, |
| 139 | ...(chromeBridgeUrl && { |
| 140 | bridgeConfig: { |
| 141 | url: chromeBridgeUrl, |
| 142 | getUserId: async () => { |
no test coverage detected