/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { ensureBrowserConnected, ensureBrowserLaunched } from './browser.js';
import { loadIssueDescriptions } from './issue-descriptions.js';
import { logger } from './logger.js';
import { McpContext } from './McpContext.js';
import { Mutex } from './Mutex.js';
import { ClearcutLogger } from './telemetry/ClearcutLogger.js';
import { FilePersistence } from './telemetry/persistence.js';
import { McpServer, SetLevelRequestSchema, ListRootsResultSchema, RootsListChangedNotificationSchema, } from './third_party/index.js';
import { ToolHandler } from './ToolHandler.js';
import { createTools } from './tools/tools.js';
import { VERSION } from './version.js';
export { buildFlag } from './ToolHandler.js';
export async function createMcpServer(serverArgs, options) {
    if (serverArgs.usageStatistics) {
        ClearcutLogger.initialize({
            persistence: new FilePersistence(),
            logFile: serverArgs.logFile,
            appVersion: VERSION,
            clearcutEndpoint: serverArgs.clearcutEndpoint,
            clearcutForceFlushIntervalMs: serverArgs.clearcutForceFlushIntervalMs,
            clearcutIncludePidHeader: serverArgs.clearcutIncludePidHeader,
        });
    }
    const server = new McpServer({
        name: 'chrome_devtools',
        title: 'Chrome DevTools MCP server',
        version: VERSION,
    }, { capabilities: { logging: {} } });
    server.server.setRequestHandler(SetLevelRequestSchema, () => {
        return {};
    });
    const updateRoots = async () => {
        if (!server.server.getClientCapabilities()?.roots) {
            return;
        }
        try {
            const roots = await server.server.request({ method: 'roots/list' }, ListRootsResultSchema);
            context?.setRoots(roots.roots);
        }
        catch (e) {
            logger('Failed to list roots', e);
        }
    };
    server.server.oninitialized = () => {
        const clientName = server.server.getClientVersion()?.name;
        if (clientName) {
            ClearcutLogger.get()?.setClientName(clientName);
        }
        if (server.server.getClientCapabilities()?.roots) {
            void updateRoots();
            server.server.setNotificationHandler(RootsListChangedNotificationSchema, () => {
                void updateRoots();
            });
        }
    };
    let context;
    async function getContext() {
        const chromeArgs = (serverArgs.chromeArg ?? []).map(String);
        const ignoreDefaultChromeArgs = (serverArgs.ignoreDefaultChromeArg ?? []).map(String);
        if (serverArgs.proxyServer) {
            chromeArgs.push(`--proxy-server=${serverArgs.proxyServer}`);
        }
        const devtools = serverArgs.experimentalDevtools ?? false;
        const browser = serverArgs.browserUrl || serverArgs.wsEndpoint || serverArgs.autoConnect
            ? await ensureBrowserConnected({
                browserURL: serverArgs.browserUrl,
                wsEndpoint: serverArgs.wsEndpoint,
                wsHeaders: serverArgs.wsHeaders,
                // Important: only pass channel, if autoConnect is true.
                channel: serverArgs.autoConnect
                    ? serverArgs.channel
                    : undefined,
                userDataDir: serverArgs.userDataDir,
                devtools,
            })
            : await ensureBrowserLaunched({
                headless: serverArgs.headless,
                executablePath: serverArgs.executablePath,
                channel: serverArgs.channel,
                isolated: serverArgs.isolated ?? false,
                userDataDir: serverArgs.userDataDir,
                logFile: options.logFile,
                viewport: serverArgs.viewport,
                chromeArgs,
                ignoreDefaultChromeArgs,
                acceptInsecureCerts: serverArgs.acceptInsecureCerts,
                devtools,
                enableExtensions: serverArgs.categoryExtensions,
                viaCli: serverArgs.viaCli,
            });
        if (context?.browser !== browser) {
            context = await McpContext.from(browser, logger, {
                experimentalDevToolsDebugging: devtools,
                experimentalIncludeAllPages: serverArgs.experimentalIncludeAllPages,
                performanceCrux: serverArgs.performanceCrux,
            });
            await updateRoots();
        }
        return context;
    }
    const toolMutex = new Mutex();
    function registerTool(tool) {
        const toolHandler = new ToolHandler(tool, serverArgs, getContext, toolMutex);
        if (!toolHandler.shouldRegister) {
            return;
        }
        server.registerTool(tool.name, {
            description: tool.description,
            inputSchema: toolHandler.registeredInputSchema,
            annotations: tool.annotations,
        }, async (params) => {
            return await toolHandler.handle(params);
        });
    }
    const tools = createTools(serverArgs);
    for (const tool of tools) {
        registerTool(tool);
    }
    await loadIssueDescriptions();
    return { server };
}
export const logDisclaimers = (args) => {
    console.error(`chrome-devtools-mcp exposes content of the browser instance to the MCP clients allowing them to inspect,
debug, and modify any data in the browser or DevTools.
Avoid sharing sensitive or personal information that you do not want to share with MCP clients.`);
    if (!args.slim && args.performanceCrux) {
        console.error(`Performance tools may send trace URLs to the Google CrUX API to fetch real-user experience data. To disable, run with --no-performance-crux.`);
    }
    if (!args.slim && args.usageStatistics) {
        console.error(`
Google collects usage statistics to improve Chrome DevTools MCP. To opt-out, run with --no-usage-statistics.
For more details, visit: https://github.com/ChromeDevTools/chrome-devtools-mcp#usage-statistics`);
    }
};
//# sourceMappingURL=index.js.map