browser-devtools-mcp 0.4.4 → 0.5.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 +21 -4
- package/dist/cli/runner.js +1 -1
- package/dist/{core-3M2NFQVF.js → core-3YBKJFSF.js} +2 -2
- package/dist/{core-FPD4CWST.js → core-5DYSM5YY.js} +1 -1
- package/dist/{core-FKJAPNIK.js → core-RE2PLB4I.js} +14 -10
- package/dist/daemon-server.js +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -47,6 +47,7 @@ Choose the platform by running the appropriate MCP server or CLI:
|
|
|
47
47
|
- **OpenTelemetry Integration**: Automatic trace injection into web pages, UI trace collection, and backend trace correlation via trace context propagation
|
|
48
48
|
- **JavaScript Execution**: Use the **execute** tool to batch-execute tool calls and run custom JavaScript; on the browser platform the VM receives \`page\` (Playwright Page) so you can use \`page.evaluate()\` or the Playwright API
|
|
49
49
|
- **Session Management**: Long-lived, session-based debugging with automatic cleanup
|
|
50
|
+
- **Closed tab**: If the MCP session’s browser tab was closed, the next tool run opens a new tab for that session (previous element refs are invalid until you take a new snapshot). Session network-idle / in-flight tracking state is also reset for the new tab.
|
|
50
51
|
- **Multiple Transport Modes**: Supports both stdio and HTTP transports
|
|
51
52
|
|
|
52
53
|
### Node Platform Capabilities
|
|
@@ -91,7 +92,7 @@ Choose the platform by running the appropriate MCP server or CLI:
|
|
|
91
92
|
- **Trace ID Management**: Get, set, and generate OpenTelemetry compatible trace IDs for distributed tracing across API calls
|
|
92
93
|
|
|
93
94
|
### Synchronization Tools
|
|
94
|
-
- **Wait for Network Idle**: Wait until the page reaches a network-idle condition based on in-flight request count, useful for SPA pages and before taking screenshots
|
|
95
|
+
- **Wait for Network Idle**: Wait until the page reaches a network-idle condition based on the session’s tracked in-flight request count (tunable via `sync_wait-for-network-idle` like `idleTimeMs` / `maxConnections`), useful for SPA pages and before taking screenshots
|
|
95
96
|
|
|
96
97
|
### Accessibility (A11Y) Tools
|
|
97
98
|
- **ARIA Snapshots**: Capture semantic structure and accessibility roles in YAML format. Returns a tree with element refs (e1, e2, ...) and a `refs` map; refs are stored in session context for use in interaction tools (click, fill, hover, select, drag, scroll, press-key) as the selector (e.g. `e1`, `@e1`, `ref=e1`). You can also use Playwright-style expressions in those tools: `getByRole('button', { name: 'Login' })`, `getByLabel('Email')`, `getByText('Register')`, `getByPlaceholder('…')`, `getByTitle('…')`, `getByAltText('…')`, `getByTestId('…')`, or CSS selectors. Refs are valid until the next ARIA snapshot or navigation—re-snapshot after page/DOM changes. Options: **interactiveOnly** (only interactive elements get refs); **cursorInteractive: true** (also assign refs to elements that are clickable/focusable by CSS but have no ARIA role, e.g. custom div/span buttons); **selector** (scope the snapshot to an element).
|
|
@@ -873,7 +874,7 @@ browser-devtools-cli tools info navigation_go-to
|
|
|
873
874
|
# Parameters:
|
|
874
875
|
# --url <string> (required)
|
|
875
876
|
# The URL to navigate to
|
|
876
|
-
# --wait-until <load | domcontentloaded |
|
|
877
|
+
# --wait-until <load | domcontentloaded | commit> (optional)
|
|
877
878
|
# When to consider navigation succeeded
|
|
878
879
|
# Default: "load"
|
|
879
880
|
#
|
|
@@ -1170,6 +1171,9 @@ Tool inputs are validated with a strict schema; unknown or misspelled argument k
|
|
|
1170
1171
|
| `BROWSER_HEADLESS_ENABLE` | Run browser in headless mode | `true` |
|
|
1171
1172
|
| `BROWSER_PERSISTENT_ENABLE` | Use persistent browser context (preserves cookies, localStorage, etc.). **Required for React tools to work optimally.** | `false` |
|
|
1172
1173
|
| `BROWSER_PERSISTENT_USER_DATA_DIR` | Directory for persistent browser context user data | `./browser-devtools-mcp` |
|
|
1174
|
+
| `BROWSER_CDP_ENDPOINT_URL` | CDP attach: `http://host:port`, or `ws://…` directly. HTTP URLs are resolved like agent-browser (`/json/version`, `/json/list`, then `ws://…/devtools/browser`). Chromium only. | (unset) |
|
|
1175
|
+
| `BROWSER_CDP_ENABLE` | When `true` and no endpoint URL: probes **127.0.0.1:9222** then **:9229** (HTTP + WebSocket CDP discovery). With `BROWSER_CDP_ENDPOINT_URL`, connects only to that host/port. | `false` |
|
|
1176
|
+
| `BROWSER_CDP_OPEN_INSPECT` | On **loopback** CDP failure, if **Chrome is running**, opens **chrome://inspect/#remote-debugging** so you can enable remote debugging. The server **does not start Chrome** — start it yourself with `--remote-debugging-port=9222`. Set `false` to never open. | `true` |
|
|
1173
1177
|
| `BROWSER_USE_INSTALLED_ON_SYSTEM` | Use system-installed Chrome browser instead of Playwright's bundled browser | `false` |
|
|
1174
1178
|
| `BROWSER_EXECUTABLE_PATH` | Custom browser executable path | (uses Playwright default) |
|
|
1175
1179
|
| `OTEL_ENABLE` | Enable OpenTelemetry integration | `false` |
|
|
@@ -1183,6 +1187,19 @@ Tool inputs are validated with a strict schema; unknown or misspelled argument k
|
|
|
1183
1187
|
| `FIGMA_ACCESS_TOKEN` | Figma API access token for design comparison | (none) |
|
|
1184
1188
|
| `FIGMA_API_BASE_URL` | Figma API base URL | `https://api.figma.com/v1` |
|
|
1185
1189
|
|
|
1190
|
+
#### CDP attach (no auto-launch)
|
|
1191
|
+
|
|
1192
|
+
When `BROWSER_CDP_ENABLE` or `BROWSER_CDP_ENDPOINT_URL` is set, the server **only attaches** to an existing Chrome that already exposes CDP (e.g. started with `--remote-debugging-port=9222`). Without an explicit URL it tries **9222** then **9229** on loopback; with `BROWSER_CDP_ENDPOINT_URL` it uses that host/port only. It verifies reachability, then `connectOverCDP`. **`context.newPage()` opens a new tab in that browser.**
|
|
1193
|
+
|
|
1194
|
+
If CDP is **not** reachable on loopback and **Chrome appears to be running**, `BROWSER_CDP_OPEN_INSPECT` (default `true`) opens **chrome://inspect/#remote-debugging** so you can turn on remote debugging. Start Chrome with debugging, then retry the tool.
|
|
1195
|
+
|
|
1196
|
+
##### CDP errors (what the message means)
|
|
1197
|
+
|
|
1198
|
+
| Prefix | Stage | Typical cause |
|
|
1199
|
+
|--------|--------|----------------|
|
|
1200
|
+
| `[CDP discovery]` | Finding the DevTools port / URL | Chrome not in remote-debugging mode, wrong URL, or nothing listening on the probed port(s). |
|
|
1201
|
+
| `[CDP connect]` | Playwright `connectOverCDP` | Approve Chrome’s remote-debugging prompt; or set `BROWSER_CDP_ENDPOINT_URL` to the correct `ws://` / `http://` endpoint. |
|
|
1202
|
+
|
|
1186
1203
|
## Telemetry
|
|
1187
1204
|
|
|
1188
1205
|
Browser DevTools MCP collects **anonymous usage data** to understand which tools are used, detect errors, and improve the product over time. Telemetry is **opt-out** — it is enabled by default and can be disabled at any time with zero friction.
|
|
@@ -1480,7 +1497,7 @@ Once disabled, no data is sent and no network requests are made to PostHog.
|
|
|
1480
1497
|
**Parameters:**
|
|
1481
1498
|
- `url` (string, required): URL to navigate to (must include scheme)
|
|
1482
1499
|
- `timeout` (number, optional): Maximum operation time in milliseconds (default: 0 - no timeout)
|
|
1483
|
-
- `waitUntil` (enum, optional):
|
|
1500
|
+
- `waitUntil` (enum, optional): Playwright main-frame lifecycle — "load", "domcontentloaded", or "commit" (default: "load"). Not Playwright `networkidle`; use `waitForNavigation` for session network-idle after navigation.
|
|
1484
1501
|
- `includeSnapshot` (boolean, optional): When true (default), take an ARIA snapshot with refs after navigation and include `output` and `refs` in the response; when false, only url/status/ok are returned.
|
|
1485
1502
|
- `snapshotOptions` (object, optional): When includeSnapshot is true, options for the snapshot. **interactiveOnly** (boolean, default false): only interactive elements get refs; **cursorInteractive** (boolean, default false): include cursor-interactive elements (same as a11y_take-aria-snapshot).
|
|
1486
1503
|
- `includeScreenshot` (boolean, optional): When true, take a screenshot after navigation; saved to disk, path returned in `screenshotFilePath`. Default false.
|
|
@@ -1507,7 +1524,7 @@ Once disabled, no data is sent and no network requests are made to PostHog.
|
|
|
1507
1524
|
|
|
1508
1525
|
**Parameters:**
|
|
1509
1526
|
- `timeout` (number, optional): Maximum operation time in milliseconds (default: 0 - no timeout)
|
|
1510
|
-
- `waitUntil` (enum, optional):
|
|
1527
|
+
- `waitUntil` (enum, optional): Playwright main-frame lifecycle — "load", "domcontentloaded", or "commit" (default: "load"). Not Playwright `networkidle`; use `waitForNavigation` for session network-idle after navigation.
|
|
1511
1528
|
- `includeSnapshot` (boolean, optional): When true (default), take an ARIA snapshot with refs after reload and include `output` and `refs`; when false, only url/status/ok.
|
|
1512
1529
|
- `snapshotOptions` (object, optional): When includeSnapshot is true. **interactiveOnly** (boolean, default false), **cursorInteractive** (boolean, default false) — same as a11y_take-aria-snapshot.
|
|
1513
1530
|
- `includeScreenshot` (boolean, optional): When true, take a screenshot after reload; saved to disk. Default false.
|
package/dist/cli/runner.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{isToolEnabled,platformInfo}from"../core-FKJAPNIK.js";import{AMAZON_BEDROCK_ENABLE,AMAZON_BEDROCK_IMAGE_EMBED_MODEL_ID,AMAZON_BEDROCK_TEXT_EMBED_MODEL_ID,AMAZON_BEDROCK_VISION_MODEL_ID,AWS_PROFILE,AWS_REGION,DAEMON_PORT,DAEMON_SESSION_IDLE_CHECK_SECONDS,DAEMON_SESSION_IDLE_SECONDS,FIGMA_ACCESS_TOKEN,FIGMA_API_BASE_URL,OTEL_ENABLE,OTEL_EXPORTER_HTTP_URL,OTEL_EXPORTER_TYPE,OTEL_SERVICE_NAME,OTEL_SERVICE_VERSION}from"../core-3M2NFQVF.js";import{Command,Option}from"commander";import{ZodFirstPartyTypeKind}from"zod";function _unwrapZodType(zodType){let current=zodType,isOptional=!1,defaultValue;for(;;){let typeName=current._def.typeName;if(typeName===ZodFirstPartyTypeKind.ZodOptional)isOptional=!0,current=current._def.innerType;else if(typeName===ZodFirstPartyTypeKind.ZodDefault)isOptional=!0,defaultValue=current._def.defaultValue(),current=current._def.innerType;else if(typeName===ZodFirstPartyTypeKind.ZodNullable)isOptional=!0,current=current._def.innerType;else break}return{innerType:current,isOptional,defaultValue}}function _getDescription(zodType){return zodType._def.description}function _toCamelCase(str){return str.replace(/[-_]([a-z])/g,(_,letter)=>letter.toUpperCase())}function _toKebabCase(str){return str.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()}function _createOption(name,zodType){let{innerType,isOptional,defaultValue}=_unwrapZodType(zodType),description=_getDescription(zodType)||`The ${name} value`,flagName=_toKebabCase(name),typeName=innerType._def.typeName,option;switch(typeName){case ZodFirstPartyTypeKind.ZodString:option=new Option(`--${flagName} <string>`,description);break;case ZodFirstPartyTypeKind.ZodNumber:option=new Option(`--${flagName} <number>`,description),option.argParser(value=>{let n=Number(value);if(!Number.isFinite(n))throw new Error(`Invalid number: ${value}`);return n});break;case ZodFirstPartyTypeKind.ZodBoolean:option=new Option(`--${flagName}`,description);break;case ZodFirstPartyTypeKind.ZodEnum:let enumValues=innerType._def.values;option=new Option(`--${flagName} <choice>`,description).choices(enumValues);break;case ZodFirstPartyTypeKind.ZodArray:option=new Option(`--${flagName} <value...>`,description);break;case ZodFirstPartyTypeKind.ZodObject:case ZodFirstPartyTypeKind.ZodRecord:option=new Option(`--${flagName} <json>`,description),option.argParser(value=>{try{return JSON.parse(value)}catch{throw new Error(`Invalid JSON: ${value}`)}});break;case ZodFirstPartyTypeKind.ZodAny:case ZodFirstPartyTypeKind.ZodUnknown:option=new Option(`--${flagName} <value>`,description),option.argParser(value=>{try{return JSON.parse(value)}catch{return value}});break;case ZodFirstPartyTypeKind.ZodLiteral:let literalValue=innerType._def.value;typeof literalValue=="boolean"?option=new Option(`--${flagName}`,description):(option=new Option(`--${flagName} <value>`,description),option.default(literalValue));break;case ZodFirstPartyTypeKind.ZodUnion:let unionOptions=innerType._def.options;if(unionOptions.every(opt=>opt._def.typeName===ZodFirstPartyTypeKind.ZodLiteral)){let choices=unionOptions.map(opt=>String(opt._def.value));option=new Option(`--${flagName} <choice>`,description).choices(choices)}else option=new Option(`--${flagName} <value>`,description),option.argParser(value=>{try{return JSON.parse(value)}catch{return value}});break;default:option=new Option(`--${flagName} <value>`,description);break}return defaultValue!==void 0&&option.default(defaultValue),!isOptional&&defaultValue===void 0&&option.makeOptionMandatory(!0),option}function _generateOptionsFromSchema(schema,toolName){let options=[];for(let[name,zodType]of Object.entries(schema)){let option=_createOption(name,zodType);option&&options.push(option)}return options}function _parseOptionsToToolInput(options){let result={};for(let[key,value]of Object.entries(options)){let camelKey=_toCamelCase(key);value!==void 0&&(result[camelKey]=value)}return result}function _parseToolName(toolName){let underscoreIndex=toolName.indexOf("_");return underscoreIndex===-1?{domain:"default",commandName:toolName}:{domain:toolName.substring(0,underscoreIndex),commandName:toolName.substring(underscoreIndex+1)}}function registerToolCommands(program,tools2,handler){let domainCommands=new Map;for(let tool of tools2){let{domain,commandName}=_parseToolName(tool.name()),domainCommand=domainCommands.get(domain);domainCommand||(domainCommand=new Command(domain).description(`${domain.charAt(0).toUpperCase()+domain.slice(1)} commands`),domainCommands.set(domain,domainCommand),program.addCommand(domainCommand));let toolCommand=new Command(commandName).description(tool.description().trim()),options=_generateOptionsFromSchema(tool.inputSchema(),tool.name());for(let option of options)toolCommand.addOption(option);toolCommand.action(async opts=>{let toolInput=_parseOptionsToToolInput(opts),globalOptions=program.opts();await handler(tool.name(),toolInput,globalOptions)}),domainCommand.addCommand(toolCommand)}}import{spawn,execSync}from"node:child_process";import{createRequire}from"node:module";import*as path from"node:path";import*as readline from"node:readline";import{fileURLToPath}from"node:url";import{Command as Command2,Option as Option2}from"commander";var require2=createRequire(import.meta.url),__filename=fileURLToPath(import.meta.url),__dirname=path.dirname(__filename),cliProvider=platformInfo.cliInfo.cliProvider,tools=cliProvider.tools.filter(isToolEnabled),DEFAULT_TIMEOUT=3e4,verboseEnabled=!1,quietEnabled=!1;function _debug(message,data){if(verboseEnabled){let timestamp=new Date().toISOString();data!==void 0?console.error(`[${timestamp}] [DEBUG] ${message}`,data):console.error(`[${timestamp}] [DEBUG] ${message}`)}}function _output(message){quietEnabled||console.log(message)}function _error(message){console.error(message)}async function _isDaemonRunning(port){_debug(`Checking if daemon is running on port ${port}`);try{let response=await fetch(`http://localhost:${port}/health`,{method:"GET",signal:AbortSignal.timeout(3e3)});if(response.ok){let isRunning=(await response.json()).status==="ok";return _debug(`Daemon health check result: ${isRunning?"running":"not running"}`),isRunning}return _debug(`Daemon health check failed: HTTP ${response.status}`),!1}catch(err){return _debug(`Daemon health check error: ${err.message}`),!1}}function _buildDaemonEnv(opts){return cliProvider.buildEnv(opts)}function _startDaemonDetached(opts){let daemonServerPath=path.join(__dirname,"..","daemon-server.js"),env=_buildDaemonEnv(opts);_debug(`Starting daemon server from: ${daemonServerPath}`),_debug(`Daemon port: ${opts.port}`);let child=spawn(process.execPath,[daemonServerPath,"--port",String(opts.port)],{detached:!0,stdio:"ignore",env});child.unref(),_debug(`Daemon process spawned with PID: ${child.pid}`),_output(`Started daemon server as detached process (PID: ${child.pid})`)}async function _ensureDaemonRunning(opts){if(await _isDaemonRunning(opts.port))_debug("Daemon is already running");else{_output(`Daemon server is not running on port ${opts.port}, starting...`),_startDaemonDetached(opts);let maxRetries=10,retryDelay=500;_debug(`Waiting for daemon to be ready (max ${maxRetries} retries, ${retryDelay}ms delay)`);for(let i=0;i<maxRetries;i++)if(await new Promise(resolve=>setTimeout(resolve,retryDelay)),_debug(`Retry ${i+1}/${maxRetries}: checking daemon status...`),await _isDaemonRunning(opts.port)){_debug("Daemon is now ready"),_output("Daemon server is ready");return}throw new Error(`Daemon server failed to start within ${maxRetries*retryDelay/1e3} seconds`)}}async function _stopDaemon(port,timeout){try{return(await fetch(`http://localhost:${port}/shutdown`,{method:"POST",signal:AbortSignal.timeout(timeout)})).ok}catch{return!1}}async function _callTool(port,toolName,toolInput,sessionId,timeout){let headers={"Content-Type":"application/json"};sessionId&&(headers["session-id"]=sessionId);let request={toolName,toolInput};_debug(`Calling tool: ${toolName}`),_debug("Tool input:",toolInput),_debug(`Session ID: ${sessionId||"(default)"}`),_debug(`Timeout: ${timeout||"none"}`);let startTime=Date.now(),response=await fetch(`http://localhost:${port}/call`,{method:"POST",headers,body:JSON.stringify(request),signal:timeout?AbortSignal.timeout(timeout):void 0}),duration=Date.now()-startTime;if(_debug(`Tool call completed in ${duration}ms, status: ${response.status}`),!response.ok){let errorBody=await response.json().catch(()=>({}));_debug("Tool call error:",errorBody);let message=errorBody?.toolError?.message||errorBody?.error?.message||`HTTP ${response.status}: ${response.statusText}`;throw new Error(message)}let result=await response.json();return _debug("Tool call result:",result.toolError?{error:result.toolError}:{success:!0}),result}async function _deleteSession(port,sessionId,timeout){try{return(await fetch(`http://localhost:${port}/session`,{method:"DELETE",headers:{"session-id":sessionId},signal:AbortSignal.timeout(timeout)})).ok}catch{return!1}}async function _getDaemonInfo(port,timeout){try{let response=await fetch(`http://localhost:${port}/info`,{method:"GET",signal:AbortSignal.timeout(timeout)});return response.ok?await response.json():null}catch{return null}}async function _listSessions(port,timeout){try{let response=await fetch(`http://localhost:${port}/sessions`,{method:"GET",signal:AbortSignal.timeout(timeout)});return response.ok?await response.json():null}catch{return null}}async function _getSessionInfo(port,sessionId,timeout){try{let response=await fetch(`http://localhost:${port}/session`,{method:"GET",headers:{"session-id":sessionId},signal:AbortSignal.timeout(timeout)});return response.ok?await response.json():null}catch{return null}}function _formatUptime(seconds){let days=Math.floor(seconds/86400),hours=Math.floor(seconds%86400/3600),minutes=Math.floor(seconds%3600/60),secs=seconds%60,parts=[];return days>0&&parts.push(`${days}d`),hours>0&&parts.push(`${hours}h`),minutes>0&&parts.push(`${minutes}m`),parts.push(`${secs}s`),parts.join(" ")}function _formatTimestamp(timestamp){return new Date(timestamp).toISOString()}function _getZodTypeName(schema){let typeName=schema._def.typeName;return typeName==="ZodOptional"||typeName==="ZodNullable"||typeName==="ZodDefault"?_getZodTypeName(schema._def.innerType):typeName==="ZodArray"?`${_getZodTypeName(schema._def.type)}[]`:typeName==="ZodEnum"?schema._def.values.join(" | "):typeName==="ZodLiteral"?JSON.stringify(schema._def.value):typeName==="ZodUnion"?schema._def.options.map(opt=>_getZodTypeName(opt)).join(" | "):{ZodString:"string",ZodNumber:"number",ZodBoolean:"boolean",ZodObject:"object",ZodRecord:"Record<string, any>",ZodAny:"any"}[typeName]||typeName.replace("Zod","").toLowerCase()}function _getZodDescription(schema){if(schema._def.description)return schema._def.description;if(schema._def.typeName==="ZodOptional"||schema._def.typeName==="ZodNullable"||schema._def.typeName==="ZodDefault")return _getZodDescription(schema._def.innerType)}function _isZodOptional(schema){let typeName=schema._def.typeName;return typeName==="ZodOptional"||typeName==="ZodNullable"}function _hasZodDefault(schema){return schema._def.typeName==="ZodDefault"?!0:schema._def.typeName==="ZodOptional"||schema._def.typeName==="ZodNullable"?_hasZodDefault(schema._def.innerType):!1}function _getZodDefault(schema){if(schema._def.typeName==="ZodDefault")return schema._def.defaultValue();if(schema._def.typeName==="ZodOptional"||schema._def.typeName==="ZodNullable")return _getZodDefault(schema._def.innerType)}function _formatOutput(output,indent=0){let prefix=" ".repeat(indent);if(output==null)return`${prefix}(empty)`;if(typeof output=="string")return output.split(`
|
|
2
|
+
import{isToolEnabled,platformInfo}from"../core-RE2PLB4I.js";import{AMAZON_BEDROCK_ENABLE,AMAZON_BEDROCK_IMAGE_EMBED_MODEL_ID,AMAZON_BEDROCK_TEXT_EMBED_MODEL_ID,AMAZON_BEDROCK_VISION_MODEL_ID,AWS_PROFILE,AWS_REGION,DAEMON_PORT,DAEMON_SESSION_IDLE_CHECK_SECONDS,DAEMON_SESSION_IDLE_SECONDS,FIGMA_ACCESS_TOKEN,FIGMA_API_BASE_URL,OTEL_ENABLE,OTEL_EXPORTER_HTTP_URL,OTEL_EXPORTER_TYPE,OTEL_SERVICE_NAME,OTEL_SERVICE_VERSION}from"../core-3YBKJFSF.js";import{Command,Option}from"commander";import{ZodFirstPartyTypeKind}from"zod";function _unwrapZodType(zodType){let current=zodType,isOptional=!1,defaultValue;for(;;){let typeName=current._def.typeName;if(typeName===ZodFirstPartyTypeKind.ZodOptional)isOptional=!0,current=current._def.innerType;else if(typeName===ZodFirstPartyTypeKind.ZodDefault)isOptional=!0,defaultValue=current._def.defaultValue(),current=current._def.innerType;else if(typeName===ZodFirstPartyTypeKind.ZodNullable)isOptional=!0,current=current._def.innerType;else break}return{innerType:current,isOptional,defaultValue}}function _getDescription(zodType){return zodType._def.description}function _toCamelCase(str){return str.replace(/[-_]([a-z])/g,(_,letter)=>letter.toUpperCase())}function _toKebabCase(str){return str.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()}function _createOption(name,zodType){let{innerType,isOptional,defaultValue}=_unwrapZodType(zodType),description=_getDescription(zodType)||`The ${name} value`,flagName=_toKebabCase(name),typeName=innerType._def.typeName,option;switch(typeName){case ZodFirstPartyTypeKind.ZodString:option=new Option(`--${flagName} <string>`,description);break;case ZodFirstPartyTypeKind.ZodNumber:option=new Option(`--${flagName} <number>`,description),option.argParser(value=>{let n=Number(value);if(!Number.isFinite(n))throw new Error(`Invalid number: ${value}`);return n});break;case ZodFirstPartyTypeKind.ZodBoolean:option=new Option(`--${flagName}`,description);break;case ZodFirstPartyTypeKind.ZodEnum:let enumValues=innerType._def.values;option=new Option(`--${flagName} <choice>`,description).choices(enumValues);break;case ZodFirstPartyTypeKind.ZodArray:option=new Option(`--${flagName} <value...>`,description);break;case ZodFirstPartyTypeKind.ZodObject:case ZodFirstPartyTypeKind.ZodRecord:option=new Option(`--${flagName} <json>`,description),option.argParser(value=>{try{return JSON.parse(value)}catch{throw new Error(`Invalid JSON: ${value}`)}});break;case ZodFirstPartyTypeKind.ZodAny:case ZodFirstPartyTypeKind.ZodUnknown:option=new Option(`--${flagName} <value>`,description),option.argParser(value=>{try{return JSON.parse(value)}catch{return value}});break;case ZodFirstPartyTypeKind.ZodLiteral:let literalValue=innerType._def.value;typeof literalValue=="boolean"?option=new Option(`--${flagName}`,description):(option=new Option(`--${flagName} <value>`,description),option.default(literalValue));break;case ZodFirstPartyTypeKind.ZodUnion:let unionOptions=innerType._def.options;if(unionOptions.every(opt=>opt._def.typeName===ZodFirstPartyTypeKind.ZodLiteral)){let choices=unionOptions.map(opt=>String(opt._def.value));option=new Option(`--${flagName} <choice>`,description).choices(choices)}else option=new Option(`--${flagName} <value>`,description),option.argParser(value=>{try{return JSON.parse(value)}catch{return value}});break;default:option=new Option(`--${flagName} <value>`,description);break}return defaultValue!==void 0&&option.default(defaultValue),!isOptional&&defaultValue===void 0&&option.makeOptionMandatory(!0),option}function _generateOptionsFromSchema(schema,toolName){let options=[];for(let[name,zodType]of Object.entries(schema)){let option=_createOption(name,zodType);option&&options.push(option)}return options}function _parseOptionsToToolInput(options){let result={};for(let[key,value]of Object.entries(options)){let camelKey=_toCamelCase(key);value!==void 0&&(result[camelKey]=value)}return result}function _parseToolName(toolName){let underscoreIndex=toolName.indexOf("_");return underscoreIndex===-1?{domain:"default",commandName:toolName}:{domain:toolName.substring(0,underscoreIndex),commandName:toolName.substring(underscoreIndex+1)}}function registerToolCommands(program,tools2,handler){let domainCommands=new Map;for(let tool of tools2){let{domain,commandName}=_parseToolName(tool.name()),domainCommand=domainCommands.get(domain);domainCommand||(domainCommand=new Command(domain).description(`${domain.charAt(0).toUpperCase()+domain.slice(1)} commands`),domainCommands.set(domain,domainCommand),program.addCommand(domainCommand));let toolCommand=new Command(commandName).description(tool.description().trim()),options=_generateOptionsFromSchema(tool.inputSchema(),tool.name());for(let option of options)toolCommand.addOption(option);toolCommand.action(async opts=>{let toolInput=_parseOptionsToToolInput(opts),globalOptions=program.opts();await handler(tool.name(),toolInput,globalOptions)}),domainCommand.addCommand(toolCommand)}}import{spawn,execSync}from"node:child_process";import{createRequire}from"node:module";import*as path from"node:path";import*as readline from"node:readline";import{fileURLToPath}from"node:url";import{Command as Command2,Option as Option2}from"commander";var require2=createRequire(import.meta.url),__filename=fileURLToPath(import.meta.url),__dirname=path.dirname(__filename),cliProvider=platformInfo.cliInfo.cliProvider,tools=cliProvider.tools.filter(isToolEnabled),DEFAULT_TIMEOUT=3e4,verboseEnabled=!1,quietEnabled=!1;function _debug(message,data){if(verboseEnabled){let timestamp=new Date().toISOString();data!==void 0?console.error(`[${timestamp}] [DEBUG] ${message}`,data):console.error(`[${timestamp}] [DEBUG] ${message}`)}}function _output(message){quietEnabled||console.log(message)}function _error(message){console.error(message)}async function _isDaemonRunning(port){_debug(`Checking if daemon is running on port ${port}`);try{let response=await fetch(`http://localhost:${port}/health`,{method:"GET",signal:AbortSignal.timeout(3e3)});if(response.ok){let isRunning=(await response.json()).status==="ok";return _debug(`Daemon health check result: ${isRunning?"running":"not running"}`),isRunning}return _debug(`Daemon health check failed: HTTP ${response.status}`),!1}catch(err){return _debug(`Daemon health check error: ${err.message}`),!1}}function _buildDaemonEnv(opts){return cliProvider.buildEnv(opts)}function _startDaemonDetached(opts){let daemonServerPath=path.join(__dirname,"..","daemon-server.js"),env=_buildDaemonEnv(opts);_debug(`Starting daemon server from: ${daemonServerPath}`),_debug(`Daemon port: ${opts.port}`);let child=spawn(process.execPath,[daemonServerPath,"--port",String(opts.port)],{detached:!0,stdio:"ignore",env});child.unref(),_debug(`Daemon process spawned with PID: ${child.pid}`),_output(`Started daemon server as detached process (PID: ${child.pid})`)}async function _ensureDaemonRunning(opts){if(await _isDaemonRunning(opts.port))_debug("Daemon is already running");else{_output(`Daemon server is not running on port ${opts.port}, starting...`),_startDaemonDetached(opts);let maxRetries=10,retryDelay=500;_debug(`Waiting for daemon to be ready (max ${maxRetries} retries, ${retryDelay}ms delay)`);for(let i=0;i<maxRetries;i++)if(await new Promise(resolve=>setTimeout(resolve,retryDelay)),_debug(`Retry ${i+1}/${maxRetries}: checking daemon status...`),await _isDaemonRunning(opts.port)){_debug("Daemon is now ready"),_output("Daemon server is ready");return}throw new Error(`Daemon server failed to start within ${maxRetries*retryDelay/1e3} seconds`)}}async function _stopDaemon(port,timeout){try{return(await fetch(`http://localhost:${port}/shutdown`,{method:"POST",signal:AbortSignal.timeout(timeout)})).ok}catch{return!1}}async function _callTool(port,toolName,toolInput,sessionId,timeout){let headers={"Content-Type":"application/json"};sessionId&&(headers["session-id"]=sessionId);let request={toolName,toolInput};_debug(`Calling tool: ${toolName}`),_debug("Tool input:",toolInput),_debug(`Session ID: ${sessionId||"(default)"}`),_debug(`Timeout: ${timeout||"none"}`);let startTime=Date.now(),response=await fetch(`http://localhost:${port}/call`,{method:"POST",headers,body:JSON.stringify(request),signal:timeout?AbortSignal.timeout(timeout):void 0}),duration=Date.now()-startTime;if(_debug(`Tool call completed in ${duration}ms, status: ${response.status}`),!response.ok){let errorBody=await response.json().catch(()=>({}));_debug("Tool call error:",errorBody);let message=errorBody?.toolError?.message||errorBody?.error?.message||`HTTP ${response.status}: ${response.statusText}`;throw new Error(message)}let result=await response.json();return _debug("Tool call result:",result.toolError?{error:result.toolError}:{success:!0}),result}async function _deleteSession(port,sessionId,timeout){try{return(await fetch(`http://localhost:${port}/session`,{method:"DELETE",headers:{"session-id":sessionId},signal:AbortSignal.timeout(timeout)})).ok}catch{return!1}}async function _getDaemonInfo(port,timeout){try{let response=await fetch(`http://localhost:${port}/info`,{method:"GET",signal:AbortSignal.timeout(timeout)});return response.ok?await response.json():null}catch{return null}}async function _listSessions(port,timeout){try{let response=await fetch(`http://localhost:${port}/sessions`,{method:"GET",signal:AbortSignal.timeout(timeout)});return response.ok?await response.json():null}catch{return null}}async function _getSessionInfo(port,sessionId,timeout){try{let response=await fetch(`http://localhost:${port}/session`,{method:"GET",headers:{"session-id":sessionId},signal:AbortSignal.timeout(timeout)});return response.ok?await response.json():null}catch{return null}}function _formatUptime(seconds){let days=Math.floor(seconds/86400),hours=Math.floor(seconds%86400/3600),minutes=Math.floor(seconds%3600/60),secs=seconds%60,parts=[];return days>0&&parts.push(`${days}d`),hours>0&&parts.push(`${hours}h`),minutes>0&&parts.push(`${minutes}m`),parts.push(`${secs}s`),parts.join(" ")}function _formatTimestamp(timestamp){return new Date(timestamp).toISOString()}function _getZodTypeName(schema){let typeName=schema._def.typeName;return typeName==="ZodOptional"||typeName==="ZodNullable"||typeName==="ZodDefault"?_getZodTypeName(schema._def.innerType):typeName==="ZodArray"?`${_getZodTypeName(schema._def.type)}[]`:typeName==="ZodEnum"?schema._def.values.join(" | "):typeName==="ZodLiteral"?JSON.stringify(schema._def.value):typeName==="ZodUnion"?schema._def.options.map(opt=>_getZodTypeName(opt)).join(" | "):{ZodString:"string",ZodNumber:"number",ZodBoolean:"boolean",ZodObject:"object",ZodRecord:"Record<string, any>",ZodAny:"any"}[typeName]||typeName.replace("Zod","").toLowerCase()}function _getZodDescription(schema){if(schema._def.description)return schema._def.description;if(schema._def.typeName==="ZodOptional"||schema._def.typeName==="ZodNullable"||schema._def.typeName==="ZodDefault")return _getZodDescription(schema._def.innerType)}function _isZodOptional(schema){let typeName=schema._def.typeName;return typeName==="ZodOptional"||typeName==="ZodNullable"}function _hasZodDefault(schema){return schema._def.typeName==="ZodDefault"?!0:schema._def.typeName==="ZodOptional"||schema._def.typeName==="ZodNullable"?_hasZodDefault(schema._def.innerType):!1}function _getZodDefault(schema){if(schema._def.typeName==="ZodDefault")return schema._def.defaultValue();if(schema._def.typeName==="ZodOptional"||schema._def.typeName==="ZodNullable")return _getZodDefault(schema._def.innerType)}function _formatOutput(output,indent=0){let prefix=" ".repeat(indent);if(output==null)return`${prefix}(empty)`;if(typeof output=="string")return output.split(`
|
|
3
3
|
`).map(line=>`${prefix}${line}`).join(`
|
|
4
4
|
`);if(typeof output=="number"||typeof output=="boolean")return`${prefix}${output}`;if(Array.isArray(output))return output.length===0?`${prefix}[]`:output.map(item=>_formatOutput(item,indent)).join(`
|
|
5
5
|
`);if(typeof output=="object"){let lines=[];for(let[key,value]of Object.entries(output))value!==void 0&&(typeof value=="object"&&value!==null&&!Array.isArray(value)?(lines.push(`${prefix}${key}:`),lines.push(_formatOutput(value,indent+1))):Array.isArray(value)?(lines.push(`${prefix}${key}:`),lines.push(_formatOutput(value,indent+1))):lines.push(`${prefix}${key}: ${value}`));return lines.join(`
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import path from"node:path";function _envStr(name){let v=process.env[name];if(!v)return;let t=v.trim();return t||void 0}function _envInt(name){let v=_envStr(name);if(!v)return;let n=Number(v);if(Number.isFinite(n))return Math.floor(n)}function _envBool(name){let v=_envStr(name);if(v)return v==="true"}function _parseKeyValueFromEnv(envValue){let headers={};if(!envValue)return headers;let pairs=envValue.split(",");for(let pair of pairs){let trimmed=pair.trim();if(!trimmed)continue;let eqIndex=trimmed.indexOf("=");if(eqIndex===-1)continue;let key=trimmed.slice(0,eqIndex).trim(),value=trimmed.slice(eqIndex+1).trim();!key||!value||(headers[key]=value)}return headers}var PORT=_envInt("PORT")??3e3,TOOL_OUTPUT_SCHEMA_DISABLE=_envBool("TOOL_OUTPUT_SCHEMA_DISABLE")??!1,AVAILABLE_TOOL_DOMAINS=(()=>{let v=_envStr("AVAILABLE_TOOL_DOMAINS");if(!v)return;let set=new Set(v.split(",").map(s=>s.trim().toLowerCase()).filter(Boolean));return set.size>0?set:void 0})(),_PLATFORM_RAW=_envStr("PLATFORM"),PLATFORM=_PLATFORM_RAW==="browser"||_PLATFORM_RAW==="node"?_PLATFORM_RAW:"browser",SESSION_IDLE_SECONDS=_envInt("SESSION_IDLE_SECONDS")??300,SESSION_IDLE_CHECK_SECONDS=_envInt("SESSION_IDLE_CHECK_SECONDS")??30,SESSION_CLOSE_ON_SOCKET_CLOSE=_envBool("SESSION_CLOSE_ON_SOCKET_CLOSE")??!1,BROWSER_HEADLESS_ENABLE=_envBool("BROWSER_HEADLESS_ENABLE")??!0,BROWSER_PERSISTENT_ENABLE=_envBool("BROWSER_PERSISTENT_ENABLE")??!1,BROWSER_PERSISTENT_USER_DATA_DIR=_envStr("BROWSER_PERSISTENT_USER_DATA_DIR")??path.join(process.cwd(),"browser-devtools-mcp"),BROWSER_USE_INSTALLED_ON_SYSTEM=_envBool("BROWSER_USE_INSTALLED_ON_SYSTEM")??!1,BROWSER_EXECUTABLE_PATH=_envStr("BROWSER_EXECUTABLE_PATH"),BROWSER_LOCALE=_envStr("BROWSER_LOCALE"),BROWSER_CONSOLE_MESSAGES_BUFFER_SIZE=_envInt("BROWSER_CONSOLE_MESSAGES_BUFFER_SIZE")??1e3,BROWSER_HTTP_REQUESTS_BUFFER_SIZE=_envInt("BROWSER_HTTP_REQUESTS_BUFFER_SIZE")??1e3,BROWSER_SERVER_INSTRUCTIONS_ENABLE=_envBool("BROWSER_SERVER_INSTRUCTIONS_ENABLE")??!0,BROWSER_POLICY_UI_DEBUGGING_ENABLE=_envBool("BROWSER_POLICY_UI_DEBUGGING_ENABLE")??!1,NODE_SERVER_INSTRUCTIONS_ENABLE=_envBool("NODE_SERVER_INSTRUCTIONS_ENABLE")??!0,NODE_POLICY_DEBUGGING_ENABLE=_envBool("NODE_POLICY_DEBUGGING_ENABLE")??!1,NODE_CONSOLE_MESSAGES_BUFFER_SIZE=_envInt("NODE_CONSOLE_MESSAGES_BUFFER_SIZE")??1e3,NODE_INSPECTOR_HOST=_envStr("NODE_INSPECTOR_HOST"),OTEL_ENABLE=_envBool("OTEL_ENABLE")??!1,OTEL_SERVICE_NAME=_envStr("OTEL_SERVICE_NAME")??"frontend",OTEL_SERVICE_VERSION=_envStr("OTEL_SERVICE_VERSION"),OTEL_ASSETS_DIR=_envStr("OTEL_ASSETS_DIR"),OTEL_INSTRUMENTATION_USER_INTERACTION_EVENTS=_envStr("OTEL_INSTRUMENTATION_USER_INTERACTION_EVENTS")?.split(",")??["click"],OTEL_EXPORTER_TYPE=_envStr("OTEL_EXPORTER_TYPE")??"none",OTEL_EXPORTER_HTTP_URL=_envStr("OTEL_EXPORTER_HTTP_URL"),OTEL_EXPORTER_HTTP_HEADERS=_parseKeyValueFromEnv(_envStr("OTEL_EXPORTER_HTTP_HEADERS")),AWS_REGION=_envStr("AWS_REGION"),AWS_PROFILE=_envStr("AWS_PROFILE"),AMAZON_BEDROCK_ENABLE=_envBool("AMAZON_BEDROCK_ENABLE")??!1,AMAZON_BEDROCK_IMAGE_EMBED_MODEL_ID=_envStr("AMAZON_BEDROCK_IMAGE_EMBED_MODEL_ID"),AMAZON_BEDROCK_TEXT_EMBED_MODEL_ID=_envStr("AMAZON_BEDROCK_TEXT_EMBED_MODEL_ID"),AMAZON_BEDROCK_VISION_MODEL_ID=_envStr("AMAZON_BEDROCK_VISION_MODEL_ID"),FIGMA_ACCESS_TOKEN=_envStr("FIGMA_ACCESS_TOKEN")??"",FIGMA_API_BASE_URL=_envStr("FIGMA_API_BASE_URL")??"https://api.figma.com/v1",DAEMON_PORT=_envInt("DAEMON_PORT")??2020,DAEMON_SESSION_IDLE_SECONDS=_envInt("DAEMON_SESSION_IDLE_SECONDS")??300,DAEMON_SESSION_IDLE_CHECK_SECONDS=_envInt("DAEMON_SESSION_IDLE_CHECK_SECONDS")??30;var ConsoleMessageLevelName=(ConsoleMessageLevelName2=>(ConsoleMessageLevelName2.ALL="all",ConsoleMessageLevelName2.DEBUG="debug",ConsoleMessageLevelName2.INFO="info",ConsoleMessageLevelName2.WARNING="warning",ConsoleMessageLevelName2.ERROR="error",ConsoleMessageLevelName2))(ConsoleMessageLevelName||{});var ConsoleMessageLevel={all:{code:-1},debug:{code:0},info:{code:1},warning:{code:2},error:{code:3}};var BANNER_TEXT="[BROWSER-DEVTOOLS]",enabled=!0,debugEnabled=process.env.DEBUG_ENABLE==="true";function _timeAsString(){return`${new Date().toLocaleTimeString(void 0,{hour:"numeric",minute:"numeric",second:"numeric",hour12:!1,timeZoneName:"short"})}`}function _normalizeArgs(...args){return isDebugEnabled()?args:(args||[]).map(arg=>arg?arg instanceof Error||arg.name&&arg.message&&arg.stack?`${arg.name}: ${arg.message}`:arg:"")}function enable(){enabled=!0}function disable(){enabled=!1}function isDebugEnabled(){return debugEnabled}function debug(...args){enabled&&isDebugEnabled()&&console.debug(BANNER_TEXT,_timeAsString(),"|","DEBUG","-",..._normalizeArgs(...args))}function info(...args){enabled&&console.info(BANNER_TEXT,_timeAsString(),"|","INFO ","-",..._normalizeArgs(...args))}function warn(...args){enabled&&console.warn(BANNER_TEXT,_timeAsString(),"|","WARN ","-",..._normalizeArgs(...args))}function error(...args){enabled&&console.error(BANNER_TEXT,_timeAsString(),"|","ERROR","-",..._normalizeArgs(...args))}function _getCircularReplacer(){let seen=new WeakSet;return(key,value)=>{if(typeof value=="object"&&value!==null){if(seen.has(value))return;seen.add(value)}return value}}function toJson(obj){return JSON.stringify(obj,_getCircularReplacer())}var BASE64_CHARS="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",BASE64_MAP=new Map;for(let i=0;i<BASE64_CHARS.length;i++)BASE64_MAP.set(BASE64_CHARS[i],i);function _decodeVLQ(encoded){let values=[],shift=0,value=0;for(let char of encoded){let digit=BASE64_MAP.get(char);if(digit===void 0)throw new Error(`Invalid base64 character: ${char}`);let hasContinuation=(digit&32)!==0;if(value+=(digit&31)<<shift,hasContinuation)shift+=5;else{let isNegative=(value&1)!==0;value=value>>1,isNegative&&(value=-value),values.push(value),value=0,shift=0}}return values}function _parseMappings(mappings,sources){let entries=[],lines=mappings.split(";"),generatedLine=0,sourceIndex=0,originalLine=0,originalColumn=0,nameIndex=0;for(let line of lines){let generatedColumn=0,segments=line.split(",");for(let segment of segments){if(!segment)continue;let values=_decodeVLQ(segment);if(values.length===0)continue;generatedColumn+=values[0];let entry={generatedLine,generatedColumn};values.length>=4&&(sourceIndex+=values[1],originalLine+=values[2],originalColumn+=values[3],entry.source=sources[sourceIndex],entry.originalLine=originalLine,entry.originalColumn=originalColumn,values.length>=5&&(nameIndex+=values[4])),entries.push(entry)}generatedLine++}return entries}var SourceMapResolver=class{scripts=new Map;scriptsByUrl=new Map;sourceToScripts=new Map;fetchCache=new Map;page;customFetcher;constructor(source){typeof source=="function"?this.customFetcher=source:this.page=source}registerScript(script){let resolved={scriptId:script.scriptId,url:script.url,sourceMapURL:script.sourceMapURL,originalSources:[]};if(this.scripts.set(script.scriptId,resolved),script.url){let existing=this.scriptsByUrl.get(script.url)||[];existing.push(resolved),this.scriptsByUrl.set(script.url,existing)}}getScriptUrl(scriptId){return this.scripts.get(scriptId)?.url}async loadSourceMap(scriptId){let script=this.scripts.get(scriptId);if(!script||!script.sourceMapURL)return!1;if(script.sourceMap)return!0;try{let sourceMap=await this._fetchSourceMap(script.sourceMapURL,script.url);if(!sourceMap)return!1;script.sourceMap=sourceMap,script.mappings=_parseMappings(sourceMap.mappings,sourceMap.sources),script.originalSources=sourceMap.sources.map(s=>this._resolveSourcePath(s,sourceMap.sourceRoot,script.url));for(let source of script.originalSources){let existing=this.sourceToScripts.get(source)||[];existing.push(script),this.sourceToScripts.set(source,existing)}return!0}catch(error2){return debug(`Failed to load source map for ${script.url}:`,error2),!1}}async loadAllSourceMaps(){let loaded=0;for(let scriptId of this.scripts.keys())await this.loadSourceMap(scriptId)&&loaded++;return loaded}originalToGenerated(originalSource,line,column=0){let normalizedSource=this._normalizeSourcePath(originalSource),scripts=this._findScriptsForSource(normalizedSource);if(scripts.length!==0)for(let script of scripts){if(!script.mappings)continue;let sourceIndex=script.originalSources.findIndex(s=>this._normalizeSourcePath(s)===normalizedSource||s.endsWith(normalizedSource)||normalizedSource.endsWith(s));if(sourceIndex===-1)continue;let bestMatch,bestDistance=1/0;for(let mapping of script.mappings)if(mapping.source===script.sourceMap?.sources[sourceIndex]&&mapping.originalLine!==void 0&&mapping.originalLine===line){let colDist=mapping.originalColumn!==void 0?Math.abs(mapping.originalColumn-column):0;colDist<bestDistance&&(bestDistance=colDist,bestMatch=mapping)}if(bestMatch)return{scriptId:script.scriptId,location:{line:bestMatch.generatedLine,column:bestMatch.generatedColumn}}}}async resolveLocationByUrl(url,line,column=1){let line0=Math.max(0,line-1),column0=Math.max(0,column-1),candidates=this.scriptsByUrl.get(url)||[];if(candidates.length===0)for(let script of this.scripts.values())(script.url===url||script.url.endsWith(url)||url.endsWith(script.url))&&candidates.push(script);for(let script of candidates){if(!await this.loadSourceMap(script.scriptId)||!script.mappings)continue;let resolved=this.generatedToOriginal(script.scriptId,line0,column0);if(resolved)return resolved}return null}generatedToOriginal(scriptId,line,column=0){let script=this.scripts.get(scriptId);if(!script?.mappings)return;let bestMatch,bestDistance=1/0;for(let mapping of script.mappings){if(mapping.generatedLine!==line||mapping.source===void 0)continue;let colDist=Math.abs(mapping.generatedColumn-column);colDist<bestDistance&&(bestDistance=colDist,bestMatch=mapping)}if(bestMatch&&bestMatch.source!==void 0&&bestMatch.originalLine!==void 0){let sourceIndex=script.sourceMap?.sources.indexOf(bestMatch.source);return{source:sourceIndex!==void 0&&sourceIndex>=0?script.originalSources[sourceIndex]:bestMatch.source,line:bestMatch.originalLine,column:bestMatch.originalColumn??0,name:bestMatch.name}}}getOriginalSources(){return Array.from(this.sourceToScripts.keys())}findScriptsForSource(sourcePattern){let normalized=this._normalizeSourcePath(sourcePattern);return this._findScriptsForSource(normalized)}hasSourceMaps(){for(let script of this.scripts.values())if(script.sourceMap)return!0;return!1}clear(){this.scripts.clear(),this.scriptsByUrl.clear(),this.sourceToScripts.clear(),this.fetchCache.clear()}_findScriptsForSource(normalizedSource){let exact=this.sourceToScripts.get(normalizedSource);if(exact&&exact.length>0)return exact;let results=[];for(let[source,scripts]of this.sourceToScripts.entries()){let normalizedKey=this._normalizeSourcePath(source);(normalizedKey.endsWith(normalizedSource)||normalizedSource.endsWith(normalizedKey))&&results.push(...scripts)}return results}_normalizeSourcePath(path3){let normalized=path3.replace(/^webpack:\/\/[^/]*\//,"").replace(/^file:\/\//,"").replace(/^\.*\//,"");for(normalized=normalized.replace(/\\/g,"/");normalized.startsWith("./");)normalized=normalized.slice(2);return normalized}_resolveSourcePath(source,sourceRoot,scriptUrl){return source.startsWith("webpack://")||source.startsWith("http://")||source.startsWith("https://")||sourceRoot&&(source=sourceRoot.replace(/\/$/,"")+"/"+source),source}async _fetchSourceMap(sourceMapURL,scriptUrl){if(sourceMapURL.startsWith("data:"))return this._parseDataUrl(sourceMapURL);let absoluteUrl=this._resolveUrl(sourceMapURL,scriptUrl);if(this.fetchCache.has(absoluteUrl))return this.fetchCache.get(absoluteUrl)||null;try{let sourceMap=null;if(this.customFetcher){let content=await this.customFetcher(absoluteUrl,scriptUrl);content&&(sourceMap=JSON.parse(content))}else this.page&&(sourceMap=await this._fetchViaBrowser(absoluteUrl));return this.fetchCache.set(absoluteUrl,sourceMap),sourceMap}catch(error2){return debug(`Failed to fetch source map from ${absoluteUrl}:`,error2),this.fetchCache.set(absoluteUrl,null),null}}_parseDataUrl(dataUrl){try{let match=dataUrl.match(/^data:[^,]*(?:;base64)?,(.*)$/);if(!match)return null;let isBase64=dataUrl.includes(";base64,"),data=match[1],json=isBase64?Buffer.from(data,"base64").toString("utf-8"):decodeURIComponent(data);return JSON.parse(json)}catch{return null}}_resolveUrl(url,baseUrl){if(url.startsWith("http://")||url.startsWith("https://"))return url;try{return new URL(url,baseUrl).href}catch{return baseUrl.replace(/[^/]*$/,"")+url}}async _fetchViaBrowser(url){if(!this.page)return null;try{let typedResult=await this.page.evaluate(async fetchUrl=>{try{let response=await fetch(fetchUrl);return response.ok?{data:await response.text()}:{error:`HTTP ${response.status}`}}catch(err){return{error:err.message||"Fetch failed"}}},url);return"error"in typedResult?(debug(`Browser fetch error for ${url}: ${typedResult.error}`),null):JSON.parse(typedResult.data)}catch(error2){return debug(`Failed to fetch ${url} via browser:`,error2),null}}};var V8API_DEFAULT_OPTIONS={maxDepth:3,maxProperties:50,maxArrayLength:100,maxStringLength:1e3},V8Api=class{cdp=null;enabled=!1;scripts=new Map;scriptsByUrl=new Map;eventHandlers={};options;cdpProvider;constructor(source,options){if(this.options={...V8API_DEFAULT_OPTIONS,...options},typeof source=="function")this.cdpProvider=source;else if(source&&typeof source.context=="function"){let page=source;this.cdpProvider=async()=>await page.context().newCDPSession(page)}else if(source&&typeof source.send=="function"){let session=source;this.cdp=session,this.cdpProvider=async()=>session}else throw new Error("V8Api: Invalid source. Expected Page, CDPSessionProvider, or ICDPSession.")}getOptions(){return{...this.options}}isEnabled(){return this.enabled}async getCdp(){return this.cdp||(this.cdp=await this.cdpProvider()),this.cdp}on(event,handler){this.eventHandlers[event]=handler}off(event){delete this.eventHandlers[event]}async enable(){if(this.enabled)return;let cdp=await this.getCdp();cdp.on("Debugger.scriptParsed",event=>{let parsed=event;if(this.scripts.set(parsed.scriptId,parsed),parsed.url){let existing=this.scriptsByUrl.get(parsed.url)||[];existing.push(parsed),this.scriptsByUrl.set(parsed.url,existing)}this.eventHandlers.scriptParsed&&this.eventHandlers.scriptParsed(parsed)}),cdp.on("Debugger.paused",event=>{let pausedEvent=event;this.eventHandlers.paused&&this.eventHandlers.paused(pausedEvent)}),cdp.on("Debugger.resumed",()=>{this.eventHandlers.resumed&&this.eventHandlers.resumed()}),await cdp.send("Debugger.enable",{maxScriptsCacheSize:1e8}),await cdp.send("Runtime.enable"),this.enabled=!0}async disable(){if(!(!this.enabled||!this.cdp)){try{await this.cdp.send("Debugger.disable"),await this.cdp.send("Runtime.disable")}catch{}this.scripts.clear(),this.scriptsByUrl.clear(),this.enabled=!1}}async detach(){if(await this.disable(),this.cdp){try{await this.cdp.detach()}catch{}this.cdp=null}}getScripts(){return Array.from(this.scripts.values())}getScript(scriptId){return this.scripts.get(scriptId)}findScriptsByUrl(urlPattern){let regex=new RegExp(urlPattern),results=[];for(let script of this.scripts.values())script.url&®ex.test(script.url)&&results.push(script);return results}async setBreakpointByUrl(options){let cdp=await this.getCdp(),params={lineNumber:options.lineNumber};options.urlRegex?params.urlRegex=options.urlRegex:options.url&&(params.url=options.url),options.columnNumber!==void 0&&(params.columnNumber=options.columnNumber),options.condition&&(params.condition=options.condition);let result=await cdp.send("Debugger.setBreakpointByUrl",params);return{breakpointId:result.breakpointId,locations:result.locations||[]}}async setBreakpoint(location,condition){let cdp=await this.getCdp(),params={location:{scriptId:location.scriptId,lineNumber:location.lineNumber,columnNumber:location.columnNumber}};condition&&(params.condition=condition);let result=await cdp.send("Debugger.setBreakpoint",params);return{breakpointId:result.breakpointId,actualLocation:result.actualLocation}}async removeBreakpoint(breakpointId){await(await this.getCdp()).send("Debugger.removeBreakpoint",{breakpointId})}async resume(){await(await this.getCdp()).send("Debugger.resume")}async stepOver(){await(await this.getCdp()).send("Debugger.stepOver")}async stepInto(){await(await this.getCdp()).send("Debugger.stepInto")}async stepOut(){await(await this.getCdp()).send("Debugger.stepOut")}async pause(){await(await this.getCdp()).send("Debugger.pause")}async setPauseOnExceptions(state){await(await this.getCdp()).send("Debugger.setPauseOnExceptions",{state})}async evaluateOnCallFrame(callFrameId,expression,options){let result=await(await this.getCdp()).send("Debugger.evaluateOnCallFrame",{callFrameId,expression,returnByValue:options?.returnByValue??!0,generatePreview:options?.generatePreview??!0,throwOnSideEffect:options?.throwOnSideEffect??!1});return{result:result.result,exceptionDetails:result.exceptionDetails}}async getProperties(objectId,options){let result=await(await this.getCdp()).send("Runtime.getProperties",{objectId,ownProperties:options?.ownProperties??!0,accessorPropertiesOnly:options?.accessorPropertiesOnly??!1,generatePreview:options?.generatePreview??!0});return{result:result.result||[],internalProperties:result.internalProperties,privateProperties:result.privateProperties}}async getScopeVariables(scope,options){if(!scope.object.objectId)return{};let opts={maxDepth:options?.maxDepth??this.options.maxDepth,maxProperties:options?.maxProperties??this.options.maxProperties,maxArrayLength:options?.maxArrayLength??this.options.maxArrayLength},{result}=await this.getProperties(scope.object.objectId,{ownProperties:!0,generatePreview:!0}),variables={},visited=new Set,propCount=0;for(let prop of result){if(propCount>=opts.maxProperties){variables.__truncated__=`${result.length-propCount} more properties`;break}prop.value&&(variables[prop.name]=await this.extractValueDeep(prop.value,opts.maxDepth,opts.maxProperties,opts.maxArrayLength,visited),propCount++)}return variables}extractValue(obj){return this._extractValueShallow(obj)}async extractValueDeep(obj,maxDepth,maxProperties,maxArrayLength,visited=new Set){let depth=maxDepth??this.options.maxDepth,props=maxProperties??this.options.maxProperties,arrLen=maxArrayLength??this.options.maxArrayLength;if(obj.value!==void 0)return typeof obj.value=="string"&&obj.value.length>this.options.maxStringLength?obj.value.slice(0,this.options.maxStringLength)+`... (${obj.value.length} chars)`:obj.value;if(obj.unserializableValue)return obj.unserializableValue==="NaN"?NaN:obj.unserializableValue==="Infinity"?1/0:obj.unserializableValue==="-Infinity"?-1/0:obj.unserializableValue==="-0"?-0:obj.unserializableValue.endsWith("n")?`BigInt(${obj.unserializableValue.slice(0,-1)})`:obj.unserializableValue;if(obj.subtype==="null")return null;if(obj.type!=="undefined"){if(obj.type==="function")return`[function${obj.description?`: ${obj.description.split(`
|
|
1
|
+
import path from"node:path";function _envStr(name){let v=process.env[name];if(!v)return;let t=v.trim();return t||void 0}function _envInt(name){let v=_envStr(name);if(!v)return;let n=Number(v);if(Number.isFinite(n))return Math.floor(n)}function _envBool(name){let v=_envStr(name);if(v)return v==="true"}function _parseKeyValueFromEnv(envValue){let headers={};if(!envValue)return headers;let pairs=envValue.split(",");for(let pair of pairs){let trimmed=pair.trim();if(!trimmed)continue;let eqIndex=trimmed.indexOf("=");if(eqIndex===-1)continue;let key=trimmed.slice(0,eqIndex).trim(),value=trimmed.slice(eqIndex+1).trim();!key||!value||(headers[key]=value)}return headers}var PORT=_envInt("PORT")??3e3,TOOL_OUTPUT_SCHEMA_DISABLE=_envBool("TOOL_OUTPUT_SCHEMA_DISABLE")??!1,AVAILABLE_TOOL_DOMAINS=(()=>{let v=_envStr("AVAILABLE_TOOL_DOMAINS");if(!v)return;let set=new Set(v.split(",").map(s=>s.trim().toLowerCase()).filter(Boolean));return set.size>0?set:void 0})(),_PLATFORM_RAW=_envStr("PLATFORM"),PLATFORM=_PLATFORM_RAW==="browser"||_PLATFORM_RAW==="node"?_PLATFORM_RAW:"browser",SESSION_IDLE_SECONDS=_envInt("SESSION_IDLE_SECONDS")??300,SESSION_IDLE_CHECK_SECONDS=_envInt("SESSION_IDLE_CHECK_SECONDS")??30,SESSION_CLOSE_ON_SOCKET_CLOSE=_envBool("SESSION_CLOSE_ON_SOCKET_CLOSE")??!1,BROWSER_HEADLESS_ENABLE=_envBool("BROWSER_HEADLESS_ENABLE")??!0,BROWSER_PERSISTENT_ENABLE=_envBool("BROWSER_PERSISTENT_ENABLE")??!1,BROWSER_PERSISTENT_USER_DATA_DIR=_envStr("BROWSER_PERSISTENT_USER_DATA_DIR")??path.join(process.cwd(),"browser-devtools-mcp"),BROWSER_USE_INSTALLED_ON_SYSTEM=_envBool("BROWSER_USE_INSTALLED_ON_SYSTEM")??!1,BROWSER_EXECUTABLE_PATH=_envStr("BROWSER_EXECUTABLE_PATH"),BROWSER_LOCALE=_envStr("BROWSER_LOCALE"),BROWSER_CONSOLE_MESSAGES_BUFFER_SIZE=_envInt("BROWSER_CONSOLE_MESSAGES_BUFFER_SIZE")??1e3,BROWSER_HTTP_REQUESTS_BUFFER_SIZE=_envInt("BROWSER_HTTP_REQUESTS_BUFFER_SIZE")??1e3,BROWSER_SERVER_INSTRUCTIONS_ENABLE=_envBool("BROWSER_SERVER_INSTRUCTIONS_ENABLE")??!0,BROWSER_POLICY_UI_DEBUGGING_ENABLE=_envBool("BROWSER_POLICY_UI_DEBUGGING_ENABLE")??!1,_BROWSER_CDP_ENDPOINT=_envStr("BROWSER_CDP_ENDPOINT_URL"),_BROWSER_CDP_ENABLE=_envBool("BROWSER_CDP_ENABLE")??!1,BROWSER_CDP_CONNECT_URL=_BROWSER_CDP_ENDPOINT??(_BROWSER_CDP_ENABLE?"http://127.0.0.1:9222":void 0),BROWSER_CDP_ENDPOINT_EXPLICIT=!!_BROWSER_CDP_ENDPOINT,BROWSER_CDP_OPEN_INSPECT=_envStr("BROWSER_CDP_OPEN_INSPECT")!=="false",NODE_SERVER_INSTRUCTIONS_ENABLE=_envBool("NODE_SERVER_INSTRUCTIONS_ENABLE")??!0,NODE_POLICY_DEBUGGING_ENABLE=_envBool("NODE_POLICY_DEBUGGING_ENABLE")??!1,NODE_CONSOLE_MESSAGES_BUFFER_SIZE=_envInt("NODE_CONSOLE_MESSAGES_BUFFER_SIZE")??1e3,NODE_INSPECTOR_HOST=_envStr("NODE_INSPECTOR_HOST"),OTEL_ENABLE=_envBool("OTEL_ENABLE")??!1,OTEL_SERVICE_NAME=_envStr("OTEL_SERVICE_NAME")??"frontend",OTEL_SERVICE_VERSION=_envStr("OTEL_SERVICE_VERSION"),OTEL_ASSETS_DIR=_envStr("OTEL_ASSETS_DIR"),OTEL_INSTRUMENTATION_USER_INTERACTION_EVENTS=_envStr("OTEL_INSTRUMENTATION_USER_INTERACTION_EVENTS")?.split(",")??["click"],OTEL_EXPORTER_TYPE=_envStr("OTEL_EXPORTER_TYPE")??"none",OTEL_EXPORTER_HTTP_URL=_envStr("OTEL_EXPORTER_HTTP_URL"),OTEL_EXPORTER_HTTP_HEADERS=_parseKeyValueFromEnv(_envStr("OTEL_EXPORTER_HTTP_HEADERS")),AWS_REGION=_envStr("AWS_REGION"),AWS_PROFILE=_envStr("AWS_PROFILE"),AMAZON_BEDROCK_ENABLE=_envBool("AMAZON_BEDROCK_ENABLE")??!1,AMAZON_BEDROCK_IMAGE_EMBED_MODEL_ID=_envStr("AMAZON_BEDROCK_IMAGE_EMBED_MODEL_ID"),AMAZON_BEDROCK_TEXT_EMBED_MODEL_ID=_envStr("AMAZON_BEDROCK_TEXT_EMBED_MODEL_ID"),AMAZON_BEDROCK_VISION_MODEL_ID=_envStr("AMAZON_BEDROCK_VISION_MODEL_ID"),FIGMA_ACCESS_TOKEN=_envStr("FIGMA_ACCESS_TOKEN")??"",FIGMA_API_BASE_URL=_envStr("FIGMA_API_BASE_URL")??"https://api.figma.com/v1",DAEMON_PORT=_envInt("DAEMON_PORT")??2020,DAEMON_SESSION_IDLE_SECONDS=_envInt("DAEMON_SESSION_IDLE_SECONDS")??300,DAEMON_SESSION_IDLE_CHECK_SECONDS=_envInt("DAEMON_SESSION_IDLE_CHECK_SECONDS")??30;var ConsoleMessageLevelName=(ConsoleMessageLevelName2=>(ConsoleMessageLevelName2.ALL="all",ConsoleMessageLevelName2.DEBUG="debug",ConsoleMessageLevelName2.INFO="info",ConsoleMessageLevelName2.WARNING="warning",ConsoleMessageLevelName2.ERROR="error",ConsoleMessageLevelName2))(ConsoleMessageLevelName||{});var ConsoleMessageLevel={all:{code:-1},debug:{code:0},info:{code:1},warning:{code:2},error:{code:3}};var BANNER_TEXT="[BROWSER-DEVTOOLS]",enabled=!0,debugEnabled=process.env.DEBUG_ENABLE==="true";function _timeAsString(){return`${new Date().toLocaleTimeString(void 0,{hour:"numeric",minute:"numeric",second:"numeric",hour12:!1,timeZoneName:"short"})}`}function _normalizeArgs(...args){return isDebugEnabled()?args:(args||[]).map(arg=>arg?arg instanceof Error||arg.name&&arg.message&&arg.stack?`${arg.name}: ${arg.message}`:arg:"")}function enable(){enabled=!0}function disable(){enabled=!1}function isDebugEnabled(){return debugEnabled}function debug(...args){enabled&&isDebugEnabled()&&console.debug(BANNER_TEXT,_timeAsString(),"|","DEBUG","-",..._normalizeArgs(...args))}function info(...args){enabled&&console.info(BANNER_TEXT,_timeAsString(),"|","INFO ","-",..._normalizeArgs(...args))}function warn(...args){enabled&&console.warn(BANNER_TEXT,_timeAsString(),"|","WARN ","-",..._normalizeArgs(...args))}function error(...args){enabled&&console.error(BANNER_TEXT,_timeAsString(),"|","ERROR","-",..._normalizeArgs(...args))}function _getCircularReplacer(){let seen=new WeakSet;return(key,value)=>{if(typeof value=="object"&&value!==null){if(seen.has(value))return;seen.add(value)}return value}}function toJson(obj){return JSON.stringify(obj,_getCircularReplacer())}var BASE64_CHARS="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",BASE64_MAP=new Map;for(let i=0;i<BASE64_CHARS.length;i++)BASE64_MAP.set(BASE64_CHARS[i],i);function _decodeVLQ(encoded){let values=[],shift=0,value=0;for(let char of encoded){let digit=BASE64_MAP.get(char);if(digit===void 0)throw new Error(`Invalid base64 character: ${char}`);let hasContinuation=(digit&32)!==0;if(value+=(digit&31)<<shift,hasContinuation)shift+=5;else{let isNegative=(value&1)!==0;value=value>>1,isNegative&&(value=-value),values.push(value),value=0,shift=0}}return values}function _parseMappings(mappings,sources){let entries=[],lines=mappings.split(";"),generatedLine=0,sourceIndex=0,originalLine=0,originalColumn=0,nameIndex=0;for(let line of lines){let generatedColumn=0,segments=line.split(",");for(let segment of segments){if(!segment)continue;let values=_decodeVLQ(segment);if(values.length===0)continue;generatedColumn+=values[0];let entry={generatedLine,generatedColumn};values.length>=4&&(sourceIndex+=values[1],originalLine+=values[2],originalColumn+=values[3],entry.source=sources[sourceIndex],entry.originalLine=originalLine,entry.originalColumn=originalColumn,values.length>=5&&(nameIndex+=values[4])),entries.push(entry)}generatedLine++}return entries}var SourceMapResolver=class{scripts=new Map;scriptsByUrl=new Map;sourceToScripts=new Map;fetchCache=new Map;page;customFetcher;constructor(source){typeof source=="function"?this.customFetcher=source:this.page=source}registerScript(script){let resolved={scriptId:script.scriptId,url:script.url,sourceMapURL:script.sourceMapURL,originalSources:[]};if(this.scripts.set(script.scriptId,resolved),script.url){let existing=this.scriptsByUrl.get(script.url)||[];existing.push(resolved),this.scriptsByUrl.set(script.url,existing)}}getScriptUrl(scriptId){return this.scripts.get(scriptId)?.url}async loadSourceMap(scriptId){let script=this.scripts.get(scriptId);if(!script||!script.sourceMapURL)return!1;if(script.sourceMap)return!0;try{let sourceMap=await this._fetchSourceMap(script.sourceMapURL,script.url);if(!sourceMap)return!1;script.sourceMap=sourceMap,script.mappings=_parseMappings(sourceMap.mappings,sourceMap.sources),script.originalSources=sourceMap.sources.map(s=>this._resolveSourcePath(s,sourceMap.sourceRoot,script.url));for(let source of script.originalSources){let existing=this.sourceToScripts.get(source)||[];existing.push(script),this.sourceToScripts.set(source,existing)}return!0}catch(error2){return debug(`Failed to load source map for ${script.url}:`,error2),!1}}async loadAllSourceMaps(){let loaded=0;for(let scriptId of this.scripts.keys())await this.loadSourceMap(scriptId)&&loaded++;return loaded}originalToGenerated(originalSource,line,column=0){let normalizedSource=this._normalizeSourcePath(originalSource),scripts=this._findScriptsForSource(normalizedSource);if(scripts.length!==0)for(let script of scripts){if(!script.mappings)continue;let sourceIndex=script.originalSources.findIndex(s=>this._normalizeSourcePath(s)===normalizedSource||s.endsWith(normalizedSource)||normalizedSource.endsWith(s));if(sourceIndex===-1)continue;let bestMatch,bestDistance=1/0;for(let mapping of script.mappings)if(mapping.source===script.sourceMap?.sources[sourceIndex]&&mapping.originalLine!==void 0&&mapping.originalLine===line){let colDist=mapping.originalColumn!==void 0?Math.abs(mapping.originalColumn-column):0;colDist<bestDistance&&(bestDistance=colDist,bestMatch=mapping)}if(bestMatch)return{scriptId:script.scriptId,location:{line:bestMatch.generatedLine,column:bestMatch.generatedColumn}}}}async resolveLocationByUrl(url,line,column=1){let line0=Math.max(0,line-1),column0=Math.max(0,column-1),candidates=this.scriptsByUrl.get(url)||[];if(candidates.length===0)for(let script of this.scripts.values())(script.url===url||script.url.endsWith(url)||url.endsWith(script.url))&&candidates.push(script);for(let script of candidates){if(!await this.loadSourceMap(script.scriptId)||!script.mappings)continue;let resolved=this.generatedToOriginal(script.scriptId,line0,column0);if(resolved)return resolved}return null}generatedToOriginal(scriptId,line,column=0){let script=this.scripts.get(scriptId);if(!script?.mappings)return;let bestMatch,bestDistance=1/0;for(let mapping of script.mappings){if(mapping.generatedLine!==line||mapping.source===void 0)continue;let colDist=Math.abs(mapping.generatedColumn-column);colDist<bestDistance&&(bestDistance=colDist,bestMatch=mapping)}if(bestMatch&&bestMatch.source!==void 0&&bestMatch.originalLine!==void 0){let sourceIndex=script.sourceMap?.sources.indexOf(bestMatch.source);return{source:sourceIndex!==void 0&&sourceIndex>=0?script.originalSources[sourceIndex]:bestMatch.source,line:bestMatch.originalLine,column:bestMatch.originalColumn??0,name:bestMatch.name}}}getOriginalSources(){return Array.from(this.sourceToScripts.keys())}findScriptsForSource(sourcePattern){let normalized=this._normalizeSourcePath(sourcePattern);return this._findScriptsForSource(normalized)}hasSourceMaps(){for(let script of this.scripts.values())if(script.sourceMap)return!0;return!1}clear(){this.scripts.clear(),this.scriptsByUrl.clear(),this.sourceToScripts.clear(),this.fetchCache.clear()}_findScriptsForSource(normalizedSource){let exact=this.sourceToScripts.get(normalizedSource);if(exact&&exact.length>0)return exact;let results=[];for(let[source,scripts]of this.sourceToScripts.entries()){let normalizedKey=this._normalizeSourcePath(source);(normalizedKey.endsWith(normalizedSource)||normalizedSource.endsWith(normalizedKey))&&results.push(...scripts)}return results}_normalizeSourcePath(path3){let normalized=path3.replace(/^webpack:\/\/[^/]*\//,"").replace(/^file:\/\//,"").replace(/^\.*\//,"");for(normalized=normalized.replace(/\\/g,"/");normalized.startsWith("./");)normalized=normalized.slice(2);return normalized}_resolveSourcePath(source,sourceRoot,scriptUrl){return source.startsWith("webpack://")||source.startsWith("http://")||source.startsWith("https://")||sourceRoot&&(source=sourceRoot.replace(/\/$/,"")+"/"+source),source}async _fetchSourceMap(sourceMapURL,scriptUrl){if(sourceMapURL.startsWith("data:"))return this._parseDataUrl(sourceMapURL);let absoluteUrl=this._resolveUrl(sourceMapURL,scriptUrl);if(this.fetchCache.has(absoluteUrl))return this.fetchCache.get(absoluteUrl)||null;try{let sourceMap=null;if(this.customFetcher){let content=await this.customFetcher(absoluteUrl,scriptUrl);content&&(sourceMap=JSON.parse(content))}else this.page&&(sourceMap=await this._fetchViaBrowser(absoluteUrl));return this.fetchCache.set(absoluteUrl,sourceMap),sourceMap}catch(error2){return debug(`Failed to fetch source map from ${absoluteUrl}:`,error2),this.fetchCache.set(absoluteUrl,null),null}}_parseDataUrl(dataUrl){try{let match=dataUrl.match(/^data:[^,]*(?:;base64)?,(.*)$/);if(!match)return null;let isBase64=dataUrl.includes(";base64,"),data=match[1],json=isBase64?Buffer.from(data,"base64").toString("utf-8"):decodeURIComponent(data);return JSON.parse(json)}catch{return null}}_resolveUrl(url,baseUrl){if(url.startsWith("http://")||url.startsWith("https://"))return url;try{return new URL(url,baseUrl).href}catch{return baseUrl.replace(/[^/]*$/,"")+url}}async _fetchViaBrowser(url){if(!this.page)return null;try{let typedResult=await this.page.evaluate(async fetchUrl=>{try{let response=await fetch(fetchUrl);return response.ok?{data:await response.text()}:{error:`HTTP ${response.status}`}}catch(err){return{error:err.message||"Fetch failed"}}},url);return"error"in typedResult?(debug(`Browser fetch error for ${url}: ${typedResult.error}`),null):JSON.parse(typedResult.data)}catch(error2){return debug(`Failed to fetch ${url} via browser:`,error2),null}}};var V8API_DEFAULT_OPTIONS={maxDepth:3,maxProperties:50,maxArrayLength:100,maxStringLength:1e3},V8Api=class{cdp=null;enabled=!1;scripts=new Map;scriptsByUrl=new Map;eventHandlers={};options;cdpProvider;constructor(source,options){if(this.options={...V8API_DEFAULT_OPTIONS,...options},typeof source=="function")this.cdpProvider=source;else if(source&&typeof source.context=="function"){let page=source;this.cdpProvider=async()=>await page.context().newCDPSession(page)}else if(source&&typeof source.send=="function"){let session=source;this.cdp=session,this.cdpProvider=async()=>session}else throw new Error("V8Api: Invalid source. Expected Page, CDPSessionProvider, or ICDPSession.")}getOptions(){return{...this.options}}isEnabled(){return this.enabled}async getCdp(){return this.cdp||(this.cdp=await this.cdpProvider()),this.cdp}on(event,handler){this.eventHandlers[event]=handler}off(event){delete this.eventHandlers[event]}async enable(){if(this.enabled)return;let cdp=await this.getCdp();cdp.on("Debugger.scriptParsed",event=>{let parsed=event;if(this.scripts.set(parsed.scriptId,parsed),parsed.url){let existing=this.scriptsByUrl.get(parsed.url)||[];existing.push(parsed),this.scriptsByUrl.set(parsed.url,existing)}this.eventHandlers.scriptParsed&&this.eventHandlers.scriptParsed(parsed)}),cdp.on("Debugger.paused",event=>{let pausedEvent=event;this.eventHandlers.paused&&this.eventHandlers.paused(pausedEvent)}),cdp.on("Debugger.resumed",()=>{this.eventHandlers.resumed&&this.eventHandlers.resumed()}),await cdp.send("Debugger.enable",{maxScriptsCacheSize:1e8}),await cdp.send("Runtime.enable"),this.enabled=!0}async disable(){if(!(!this.enabled||!this.cdp)){try{await this.cdp.send("Debugger.disable"),await this.cdp.send("Runtime.disable")}catch{}this.scripts.clear(),this.scriptsByUrl.clear(),this.enabled=!1}}async detach(){if(await this.disable(),this.cdp){try{await this.cdp.detach()}catch{}this.cdp=null}}getScripts(){return Array.from(this.scripts.values())}getScript(scriptId){return this.scripts.get(scriptId)}findScriptsByUrl(urlPattern){let regex=new RegExp(urlPattern),results=[];for(let script of this.scripts.values())script.url&®ex.test(script.url)&&results.push(script);return results}async setBreakpointByUrl(options){let cdp=await this.getCdp(),params={lineNumber:options.lineNumber};options.urlRegex?params.urlRegex=options.urlRegex:options.url&&(params.url=options.url),options.columnNumber!==void 0&&(params.columnNumber=options.columnNumber),options.condition&&(params.condition=options.condition);let result=await cdp.send("Debugger.setBreakpointByUrl",params);return{breakpointId:result.breakpointId,locations:result.locations||[]}}async setBreakpoint(location,condition){let cdp=await this.getCdp(),params={location:{scriptId:location.scriptId,lineNumber:location.lineNumber,columnNumber:location.columnNumber}};condition&&(params.condition=condition);let result=await cdp.send("Debugger.setBreakpoint",params);return{breakpointId:result.breakpointId,actualLocation:result.actualLocation}}async removeBreakpoint(breakpointId){await(await this.getCdp()).send("Debugger.removeBreakpoint",{breakpointId})}async resume(){await(await this.getCdp()).send("Debugger.resume")}async stepOver(){await(await this.getCdp()).send("Debugger.stepOver")}async stepInto(){await(await this.getCdp()).send("Debugger.stepInto")}async stepOut(){await(await this.getCdp()).send("Debugger.stepOut")}async pause(){await(await this.getCdp()).send("Debugger.pause")}async setPauseOnExceptions(state){await(await this.getCdp()).send("Debugger.setPauseOnExceptions",{state})}async evaluateOnCallFrame(callFrameId,expression,options){let result=await(await this.getCdp()).send("Debugger.evaluateOnCallFrame",{callFrameId,expression,returnByValue:options?.returnByValue??!0,generatePreview:options?.generatePreview??!0,throwOnSideEffect:options?.throwOnSideEffect??!1});return{result:result.result,exceptionDetails:result.exceptionDetails}}async getProperties(objectId,options){let result=await(await this.getCdp()).send("Runtime.getProperties",{objectId,ownProperties:options?.ownProperties??!0,accessorPropertiesOnly:options?.accessorPropertiesOnly??!1,generatePreview:options?.generatePreview??!0});return{result:result.result||[],internalProperties:result.internalProperties,privateProperties:result.privateProperties}}async getScopeVariables(scope,options){if(!scope.object.objectId)return{};let opts={maxDepth:options?.maxDepth??this.options.maxDepth,maxProperties:options?.maxProperties??this.options.maxProperties,maxArrayLength:options?.maxArrayLength??this.options.maxArrayLength},{result}=await this.getProperties(scope.object.objectId,{ownProperties:!0,generatePreview:!0}),variables={},visited=new Set,propCount=0;for(let prop of result){if(propCount>=opts.maxProperties){variables.__truncated__=`${result.length-propCount} more properties`;break}prop.value&&(variables[prop.name]=await this.extractValueDeep(prop.value,opts.maxDepth,opts.maxProperties,opts.maxArrayLength,visited),propCount++)}return variables}extractValue(obj){return this._extractValueShallow(obj)}async extractValueDeep(obj,maxDepth,maxProperties,maxArrayLength,visited=new Set){let depth=maxDepth??this.options.maxDepth,props=maxProperties??this.options.maxProperties,arrLen=maxArrayLength??this.options.maxArrayLength;if(obj.value!==void 0)return typeof obj.value=="string"&&obj.value.length>this.options.maxStringLength?obj.value.slice(0,this.options.maxStringLength)+`... (${obj.value.length} chars)`:obj.value;if(obj.unserializableValue)return obj.unserializableValue==="NaN"?NaN:obj.unserializableValue==="Infinity"?1/0:obj.unserializableValue==="-Infinity"?-1/0:obj.unserializableValue==="-0"?-0:obj.unserializableValue.endsWith("n")?`BigInt(${obj.unserializableValue.slice(0,-1)})`:obj.unserializableValue;if(obj.subtype==="null")return null;if(obj.type!=="undefined"){if(obj.type==="function")return`[function${obj.description?`: ${obj.description.split(`
|
|
2
2
|
`)[0]}`:""}]`;if(!obj.objectId)return this._extractValueShallow(obj);if(depth<=0)return this._getShallowDescription(obj);if(visited.has(obj.objectId))return"[Circular]";visited.add(obj.objectId);try{return obj.subtype==="array"?await this._extractArray(obj.objectId,depth-1,props,arrLen,visited):obj.subtype==="map"?await this._extractMap(obj.objectId,depth-1,props,arrLen,visited):obj.subtype==="set"?await this._extractSet(obj.objectId,depth-1,arrLen,visited):obj.subtype==="date"?obj.description||"[Date]":obj.subtype==="regexp"?obj.description||"[RegExp]":obj.subtype==="error"?obj.description||"[Error]":obj.subtype==="promise"?`[Promise: ${obj.description||"pending"}]`:obj.subtype==="node"||obj.className?.includes("Element")||obj.className?.includes("Node")?`[DOM: ${obj.description||obj.className||"Node"}]`:obj.type==="object"?await this._extractObject(obj.objectId,depth-1,props,arrLen,visited,obj.className):this._extractValueShallow(obj)}catch{return this._getShallowDescription(obj)}}}async _extractArray(objectId,maxDepth,maxProperties,maxArrayLength,visited){let{result}=await this.getProperties(objectId,{ownProperties:!0,generatePreview:!0}),arr=[],length=0;for(let prop of result)if(prop.name==="length"&&prop.value?.value!==void 0){length=prop.value.value;break}for(let prop of result){let index=parseInt(prop.name,10);if(!(isNaN(index)||index<0)){if(arr.length>=maxArrayLength)break;prop.value&&(arr[index]=await this.extractValueDeep(prop.value,maxDepth,maxProperties,maxArrayLength,visited))}}return length>maxArrayLength&&(arr.__truncated__=`${length-maxArrayLength} more items`),arr}async _extractMap(objectId,maxDepth,maxProperties,maxArrayLength,visited){let{internalProperties}=await this.getProperties(objectId,{ownProperties:!0,generatePreview:!0}),mapObj={__type__:"Map"},entries=[],entriesProp=internalProperties?.find(p=>p.name==="[[Entries]]");if(entriesProp?.value?.objectId){let{result:entryResults}=await this.getProperties(entriesProp.value.objectId,{ownProperties:!0,generatePreview:!0}),count=0;for(let entry of entryResults){if(count>=maxArrayLength)break;let index=parseInt(entry.name,10);if(isNaN(index)||!entry.value?.objectId)continue;let{result:kvResult}=await this.getProperties(entry.value.objectId,{ownProperties:!0,generatePreview:!0}),keyProp=kvResult.find(p=>p.name==="key"),valueProp=kvResult.find(p=>p.name==="value");if(keyProp?.value&&valueProp?.value){let key=await this.extractValueDeep(keyProp.value,maxDepth,maxProperties,maxArrayLength,visited),value=await this.extractValueDeep(valueProp.value,maxDepth,maxProperties,maxArrayLength,visited);entries.push([key,value])}count++}}return mapObj.entries=entries,mapObj}async _extractSet(objectId,maxDepth,maxArrayLength,visited){let{internalProperties}=await this.getProperties(objectId,{ownProperties:!0,generatePreview:!0}),setObj={__type__:"Set"},values=[],entriesProp=internalProperties?.find(p=>p.name==="[[Entries]]");if(entriesProp?.value?.objectId){let{result:entryResults}=await this.getProperties(entriesProp.value.objectId,{ownProperties:!0,generatePreview:!0}),count=0;for(let entry of entryResults){if(count>=maxArrayLength)break;let index=parseInt(entry.name,10);isNaN(index)||(entry.value&&values.push(await this.extractValueDeep(entry.value,maxDepth,50,maxArrayLength,visited)),count++)}}return setObj.values=values,setObj}async _extractObject(objectId,maxDepth,maxProperties,maxArrayLength,visited,className){let{result}=await this.getProperties(objectId,{ownProperties:!0,generatePreview:!0}),obj={};className&&className!=="Object"&&(obj.__class__=className);let count=0;for(let prop of result){if(count>=maxProperties){obj.__truncated__=`${result.length-count} more properties`;break}prop.isOwn&&(prop.name.startsWith("__")&&prop.name.endsWith("__")||prop.value&&(obj[prop.name]=await this.extractValueDeep(prop.value,maxDepth,maxProperties,maxArrayLength,visited),count++))}return obj}_extractValueShallow(obj){if(obj.value!==void 0)return obj.value;if(obj.unserializableValue)return obj.unserializableValue==="NaN"?NaN:obj.unserializableValue==="Infinity"?1/0:obj.unserializableValue==="-Infinity"?-1/0:obj.unserializableValue==="-0"?-0:obj.unserializableValue.endsWith("n")?`BigInt(${obj.unserializableValue.slice(0,-1)})`:obj.unserializableValue;if(obj.subtype==="null")return null;if(obj.type!=="undefined")return this._getShallowDescription(obj)}_getShallowDescription(obj){if(obj.description){let desc=obj.description.split(`
|
|
3
3
|
`)[0];return desc.length>100?`[${obj.type}${obj.subtype?`:${obj.subtype}`:""}] ${desc.slice(0,100)}...`:`[${obj.type}${obj.subtype?`:${obj.subtype}`:""}] ${desc}`}return`[${obj.type}${obj.subtype?`:${obj.subtype}`:""}]`}async getScriptSource(scriptId){return await(await this.getCdp()).send("Debugger.getScriptSource",{scriptId})}async setScriptSource(scriptId,scriptSource,dryRun){let result=await(await this.getCdp()).send("Debugger.setScriptSource",{scriptId,scriptSource,dryRun:dryRun??!1});return{callFrames:result.callFrames,stackChanged:result.stackChanged,asyncStackTrace:result.asyncStackTrace,exceptionDetails:result.exceptionDetails}}};import*as fs from"node:fs";import*as http from"node:http";import*as https from"node:https";import*as path2 from"node:path";var ProbeKind=(ProbeKind2=>(ProbeKind2.TRACEPOINT="tracepoint",ProbeKind2.LOGPOINT="logpoint",ProbeKind2))(ProbeKind||{}),DEFAULT_NODE_DEBUG_CONFIG={maxSnapshots:1e3,maxCallStackDepth:1,maxFramesWithScopes:1,maxAsyncStackSegments:10,maxFramesPerAsyncSegment:10},STORES=new Map;function _generateId(){let t=Date.now(),r=Math.floor(Math.random()*1e6);return`${t.toString(36)}-${r.toString(36)}`}function _columnForLocationKey(column){return column==null||column===0?1:column}function _locationKey(urlPattern,lineNumber,columnNumber){return`${urlPattern}:${lineNumber}:${columnNumber}`}function _cdpConsoleTypeToLevelName(type){switch(type){case"error":return"error";case"warning":case"warn":return"warning";case"info":return"info";case"debug":return"debug";default:return"info"}}function _cdpArgsToText(args){return!args||args.length===0?"":args.map(a=>a.type==="string"&&a.value!==void 0||a.value!==void 0?String(a.value):a.description?a.description:"[object]").join(" ")}function _evaluateHitCondition(hitCondition,hitCount){try{let condition=hitCondition.trim();return/^[=<>!%]/.test(condition)&&(condition=`hitCount ${condition}`),!!new Function("hitCount",`return (${condition});`)(hitCount)}catch{return!1}}async function _getOtelTraceContext(session){let expression=`(function(){
|
|
4
4
|
try {
|
|
@@ -14,4 +14,4 @@ try {
|
|
|
14
14
|
${probe.logExpression}
|
|
15
15
|
)`,evalResult=await store.v8Api.evaluateOnCallFrame(topFrame.callFrameId,wrappedExpression,{returnByValue:!0,generatePreview:!0});logResult=store.v8Api.extractValue(evalResult.result)}catch{logResult="[evaluation error]"}else logResult=void 0;isLogpoint?(callStack=[],asyncStackTrace=void 0,watchResults=void 0):(callStack=callStackFull,asyncStackTrace=asyncStackTraceFull,watchResults=store.watchExpressions.size>0?await _evaluateWatchExpressionsOnFrame(store.v8Api,topFrame.callFrameId,store.watchExpressions):void 0)}else probeId="__exception__",isLogpoint=!1,logResult=void 0,callStack=callStackFull,asyncStackTrace=asyncStackTraceFull,watchResults=store.watchExpressions.size>0?await _evaluateWatchExpressionsOnFrame(store.v8Api,topFrame.callFrameId,store.watchExpressions):void 0;let snapshot={id:_generateId(),probeId,timestamp:Date.now(),sequenceNumber:++store.snapshotSequence,url:topFrame.url||"",lineNumber:topFrame.location.lineNumber+1,columnNumber:topFrame.location.columnNumber!==void 0?topFrame.location.columnNumber+1:void 0,originalLocation,exception:exceptionInfo,callStack,asyncStackTrace,logResult,watchResults,captureTimeMs:Date.now()-startTime,traceContext:traceContext??void 0};store.snapshots.push(snapshot)}store.snapshots.length>store.config.maxSnapshots&&store.snapshots.splice(0,store.snapshots.length-store.config.maxSnapshots)}}finally{await store.v8Api.resume()}}),store.enabled=!0,store.sourceMapResolver.loadAllSourceMaps().then(()=>{store.sourceMapsLoaded=!0}).catch(()=>{})}async function disableDebugging(storeKey){let store=STORES.get(storeKey);if(store?.enabled){for(let entry of store.locationIndex.values())try{await store.v8Api.removeBreakpoint(entry.breakpointId)}catch{}store.locationIndex.clear(),store.probes.clear(),store.snapshots.length=0,store.snapshotSequence=0,store.consoleMessages.length=0,store.consoleMessageSequence=0,await store.v8Api.disable(),store.enabled=!1}}async function detachDebugging(storeKey){let store=STORES.get(storeKey);store&&(await disableDebugging(storeKey),await store.v8Api.detach(),STORES.delete(storeKey))}function isDebuggingEnabled(storeKey){return STORES.get(storeKey)?.enabled??!1}function getStore(storeKey){return STORES.get(storeKey)}async function resolveSourceLocation(storeKey,url,line,column=1){let store=STORES.get(storeKey);if(!store?.enabled)throw new Error("Not connected to Node.js process or debugging not enabled. Call debug_connect first.");let resolved=await store.sourceMapResolver.resolveLocationByUrl(url,line,column);return resolved?{source:resolved.source,line:resolved.line+1,column:resolved.column+1,name:resolved.name}:null}function listStoreKeys(){return Array.from(STORES.keys())}async function setExceptionBreakpoint(storeKey,state){let store=STORES.get(storeKey);if(!store?.enabled)throw new Error("Debugging is not enabled");await store.v8Api.setPauseOnExceptions(state),store.exceptionBreakpoint=state}function getExceptionBreakpoint(storeKey){return STORES.get(storeKey)?.exceptionBreakpoint??"none"}async function createProbe(storeKey,options){let store=STORES.get(storeKey);if(!store?.enabled)throw new Error("Debugging is not enabled");let probeId=_generateId(),columnForKey=_columnForLocationKey(options.columnNumber),locationKey=_locationKey(options.urlPattern,options.lineNumber,columnForKey),existingEntry=store.locationIndex.get(locationKey);if(existingEntry){existingEntry.refCount++;let probe2={id:probeId,kind:options.kind,enabled:!0,urlPattern:options.urlPattern,lineNumber:options.lineNumber,columnNumber:options.columnNumber,condition:options.condition,logExpression:options.logExpression,hitCondition:options.hitCondition,v8BreakpointIds:[existingEntry.breakpointId],resolvedLocations:existingEntry.resolvedLocations,hitCount:0,createdAt:Date.now()};return store.probes.set(probeId,probe2),probe2}let fullCondition=options.condition?`(${options.condition})`:"true",line0based=options.lineNumber-1,column0based=columnForKey-1,resolved=store.sourceMapResolver.originalToGenerated(options.urlPattern,line0based,column0based),breakpointId,resolvedLocationsCount=0;if(resolved)try{breakpointId=(await store.v8Api.setBreakpoint({scriptId:resolved.scriptId,lineNumber:resolved.location.line,columnNumber:resolved.location.column},fullCondition)).breakpointId,resolvedLocationsCount=1}catch{let scriptUrl=store.sourceMapResolver.getScriptUrl(resolved.scriptId);if(scriptUrl){let result=await store.v8Api.setBreakpointByUrl({url:scriptUrl,lineNumber:resolved.location.line,columnNumber:resolved.location.column,condition:fullCondition});breakpointId=result.breakpointId,resolvedLocationsCount=result.locations.length}else throw new Error("Failed to set breakpoint at resolved location and could not fall back (script URL unknown). A probe may already exist at this line; remove it first or use a different line.")}else{let urlRegex=options.urlPattern.replace(/\\([.*+?^${}()|[\]\\/-])/g,"$1").replace(/[.*+?^${}()|[\]\\]/g,"\\$&").replace(/\\\*/g,".*").replace(/\\\?/g,"."),result=await store.v8Api.setBreakpointByUrl({urlRegex,lineNumber:options.lineNumber-1,columnNumber:options.columnNumber!=null?Math.max(0,options.columnNumber-1):void 0,condition:fullCondition});breakpointId=result.breakpointId,resolvedLocationsCount=result.locations.length}store.locationIndex.set(locationKey,{breakpointId,resolvedLocations:resolvedLocationsCount,refCount:1});let probe={id:probeId,kind:options.kind,enabled:!0,urlPattern:options.urlPattern,lineNumber:options.lineNumber,columnNumber:options.columnNumber,condition:options.condition,logExpression:options.logExpression,hitCondition:options.hitCondition,v8BreakpointIds:[breakpointId],resolvedLocations:resolvedLocationsCount,hitCount:0,createdAt:Date.now()};return store.probes.set(probeId,probe),probe}async function removeProbe(storeKey,probeId){let store=STORES.get(storeKey);if(!store)return!1;let probe=store.probes.get(probeId);if(!probe)return!1;let locationKey=_locationKey(probe.urlPattern,probe.lineNumber,_columnForLocationKey(probe.columnNumber)),entry=store.locationIndex.get(locationKey);if(entry&&(entry.refCount--,entry.refCount===0)){try{await store.v8Api.removeBreakpoint(entry.breakpointId)}catch{}store.locationIndex.delete(locationKey)}return store.probes.delete(probeId),!0}function listProbes(storeKey){return Array.from(STORES.get(storeKey)?.probes.values()??[])}async function clearProbes(storeKey){let store=STORES.get(storeKey);if(!store)return 0;let count=store.probes.size;for(let entry of store.locationIndex.values())try{await store.v8Api.removeBreakpoint(entry.breakpointId)}catch{}return store.locationIndex.clear(),store.probes.clear(),count}function getSnapshots(storeKey){return[...STORES.get(storeKey)?.snapshots??[]]}function getSnapshotsByProbe(storeKey,probeId){return(STORES.get(storeKey)?.snapshots??[]).filter(s=>s.probeId===probeId)}function clearSnapshots(storeKey){let store=STORES.get(storeKey);if(!store)return 0;let count=store.snapshots.length;return store.snapshots.length=0,count}function clearSnapshotsByProbe(storeKey,probeId){let store=STORES.get(storeKey);if(!store)return 0;let before=store.snapshots.length;return store.snapshots=store.snapshots.filter(s=>s.probeId!==probeId),before-store.snapshots.length}function getSnapshotStats(storeKey){let store=STORES.get(storeKey);if(!store||store.snapshots.length===0)return{totalSnapshots:0,snapshotsByProbe:{},averageCaptureTimeMs:0};let snapshotsByProbe={},totalCaptureTime=0;for(let s of store.snapshots)snapshotsByProbe[s.probeId]=(snapshotsByProbe[s.probeId]||0)+1,totalCaptureTime+=s.captureTimeMs;return{totalSnapshots:store.snapshots.length,snapshotsByProbe,oldestTimestamp:store.snapshots[0].timestamp,newestTimestamp:store.snapshots[store.snapshots.length-1].timestamp,averageCaptureTimeMs:totalCaptureTime/store.snapshots.length}}function getConsoleMessages(storeKey){return[...STORES.get(storeKey)?.consoleMessages??[]]}function addWatchExpression(storeKey,expression){let store=STORES.get(storeKey);if(!store)throw new Error("Debug store not initialized");let id=_generateId(),watchExpr={id,expression,createdAt:Date.now()};return store.watchExpressions.set(id,watchExpr),watchExpr}function removeWatchExpression(storeKey,watchExpressionId){return STORES.get(storeKey)?.watchExpressions.delete(watchExpressionId)??!1}function listWatchExpressions(storeKey){return Array.from(STORES.get(storeKey)?.watchExpressions.values()??[])}function clearWatchExpressions(storeKey){let store=STORES.get(storeKey);if(!store)return 0;let count=store.watchExpressions.size;return store.watchExpressions.clear(),count}async function loadSourceMaps(storeKey){let store=STORES.get(storeKey);if(!store?.enabled)throw new Error("Debugging is not enabled");let loaded=await store.sourceMapResolver.loadAllSourceMaps(),sources=store.sourceMapResolver.getOriginalSources();return store.sourceMapsLoaded=!0,{loaded,sources}}function hasSourceMaps(storeKey){return STORES.get(storeKey)?.sourceMapResolver.hasSourceMaps()??!1}function getOriginalSources(storeKey){return STORES.get(storeKey)?.sourceMapResolver.getOriginalSources()??[]}function getScripts(storeKey){return STORES.get(storeKey)?.v8Api.getScripts()??[]}function wrapScriptInAsyncIIFE(script){return`(async function() { ${script} })()`}function formatEvaluationException(exc){let parts=[],text=exc.text??exc.description;text&&parts.push(text);let desc=exc.exception?.description;desc&&desc!==text&&parts.push(desc);let className=exc.exception?.className;className&&!parts.some(p=>p.includes(className))&&parts.push(className),parts.length===0&&parts.push(JSON.stringify(exc));let stack=exc.stackTrace?.callFrames?.slice(0,5).map(f=>` at ${f.functionName??"?"} (${f.url??"?"}:${f.lineNumber??0})`).join(`
|
|
16
16
|
`);return stack&&parts.push(`Stack:
|
|
17
|
-
${stack}`),parts.join(" \u2014 ")}async function evaluateInNode(storeKey,expression,timeoutMs=5e3){let store=STORES.get(storeKey);if(!store?.enabled)throw new Error("Not connected to Node.js process or debugging not enabled. Call debug_connect first.");let codeToRun=wrapScriptInAsyncIIFE(expression),result=await store.session.send("Runtime.evaluate",{expression:codeToRun,returnByValue:!0,awaitPromise:!0,timeout:Math.min(timeoutMs,3e4)});if(result.exceptionDetails){let exc=result.exceptionDetails,message=formatEvaluationException(exc);throw new Error(`Node evaluation failed: ${message}`)}return store.v8Api.extractValue(result.result)}export{PORT,TOOL_OUTPUT_SCHEMA_DISABLE,AVAILABLE_TOOL_DOMAINS,PLATFORM,SESSION_IDLE_SECONDS,SESSION_IDLE_CHECK_SECONDS,SESSION_CLOSE_ON_SOCKET_CLOSE,BROWSER_HEADLESS_ENABLE,BROWSER_PERSISTENT_ENABLE,BROWSER_PERSISTENT_USER_DATA_DIR,BROWSER_USE_INSTALLED_ON_SYSTEM,BROWSER_EXECUTABLE_PATH,BROWSER_LOCALE,BROWSER_CONSOLE_MESSAGES_BUFFER_SIZE,BROWSER_HTTP_REQUESTS_BUFFER_SIZE,BROWSER_SERVER_INSTRUCTIONS_ENABLE,BROWSER_POLICY_UI_DEBUGGING_ENABLE,NODE_SERVER_INSTRUCTIONS_ENABLE,NODE_POLICY_DEBUGGING_ENABLE,NODE_CONSOLE_MESSAGES_BUFFER_SIZE,NODE_INSPECTOR_HOST,OTEL_ENABLE,OTEL_SERVICE_NAME,OTEL_SERVICE_VERSION,OTEL_ASSETS_DIR,OTEL_INSTRUMENTATION_USER_INTERACTION_EVENTS,OTEL_EXPORTER_TYPE,OTEL_EXPORTER_HTTP_URL,OTEL_EXPORTER_HTTP_HEADERS,AWS_REGION,AWS_PROFILE,AMAZON_BEDROCK_ENABLE,AMAZON_BEDROCK_IMAGE_EMBED_MODEL_ID,AMAZON_BEDROCK_TEXT_EMBED_MODEL_ID,AMAZON_BEDROCK_VISION_MODEL_ID,FIGMA_ACCESS_TOKEN,FIGMA_API_BASE_URL,DAEMON_PORT,DAEMON_SESSION_IDLE_SECONDS,DAEMON_SESSION_IDLE_CHECK_SECONDS,enable,disable,isDebugEnabled,debug,info,warn,error,toJson,ConsoleMessageLevelName,ConsoleMessageLevel,V8Api,SourceMapResolver,ProbeKind,DEFAULT_NODE_DEBUG_CONFIG,getStoreKey,enableDebugging,disableDebugging,detachDebugging,isDebuggingEnabled,getStore,resolveSourceLocation,listStoreKeys,setExceptionBreakpoint,getExceptionBreakpoint,createProbe,removeProbe,listProbes,clearProbes,getSnapshots,getSnapshotsByProbe,clearSnapshots,clearSnapshotsByProbe,getSnapshotStats,getConsoleMessages,addWatchExpression,removeWatchExpression,listWatchExpressions,clearWatchExpressions,loadSourceMaps,hasSourceMaps,getOriginalSources,getScripts,evaluateInNode};
|
|
17
|
+
${stack}`),parts.join(" \u2014 ")}async function evaluateInNode(storeKey,expression,timeoutMs=5e3){let store=STORES.get(storeKey);if(!store?.enabled)throw new Error("Not connected to Node.js process or debugging not enabled. Call debug_connect first.");let codeToRun=wrapScriptInAsyncIIFE(expression),result=await store.session.send("Runtime.evaluate",{expression:codeToRun,returnByValue:!0,awaitPromise:!0,timeout:Math.min(timeoutMs,3e4)});if(result.exceptionDetails){let exc=result.exceptionDetails,message=formatEvaluationException(exc);throw new Error(`Node evaluation failed: ${message}`)}return store.v8Api.extractValue(result.result)}export{PORT,TOOL_OUTPUT_SCHEMA_DISABLE,AVAILABLE_TOOL_DOMAINS,PLATFORM,SESSION_IDLE_SECONDS,SESSION_IDLE_CHECK_SECONDS,SESSION_CLOSE_ON_SOCKET_CLOSE,BROWSER_HEADLESS_ENABLE,BROWSER_PERSISTENT_ENABLE,BROWSER_PERSISTENT_USER_DATA_DIR,BROWSER_USE_INSTALLED_ON_SYSTEM,BROWSER_EXECUTABLE_PATH,BROWSER_LOCALE,BROWSER_CONSOLE_MESSAGES_BUFFER_SIZE,BROWSER_HTTP_REQUESTS_BUFFER_SIZE,BROWSER_SERVER_INSTRUCTIONS_ENABLE,BROWSER_POLICY_UI_DEBUGGING_ENABLE,BROWSER_CDP_CONNECT_URL,BROWSER_CDP_ENDPOINT_EXPLICIT,BROWSER_CDP_OPEN_INSPECT,NODE_SERVER_INSTRUCTIONS_ENABLE,NODE_POLICY_DEBUGGING_ENABLE,NODE_CONSOLE_MESSAGES_BUFFER_SIZE,NODE_INSPECTOR_HOST,OTEL_ENABLE,OTEL_SERVICE_NAME,OTEL_SERVICE_VERSION,OTEL_ASSETS_DIR,OTEL_INSTRUMENTATION_USER_INTERACTION_EVENTS,OTEL_EXPORTER_TYPE,OTEL_EXPORTER_HTTP_URL,OTEL_EXPORTER_HTTP_HEADERS,AWS_REGION,AWS_PROFILE,AMAZON_BEDROCK_ENABLE,AMAZON_BEDROCK_IMAGE_EMBED_MODEL_ID,AMAZON_BEDROCK_TEXT_EMBED_MODEL_ID,AMAZON_BEDROCK_VISION_MODEL_ID,FIGMA_ACCESS_TOKEN,FIGMA_API_BASE_URL,DAEMON_PORT,DAEMON_SESSION_IDLE_SECONDS,DAEMON_SESSION_IDLE_CHECK_SECONDS,enable,disable,isDebugEnabled,debug,info,warn,error,toJson,ConsoleMessageLevelName,ConsoleMessageLevel,V8Api,SourceMapResolver,ProbeKind,DEFAULT_NODE_DEBUG_CONFIG,getStoreKey,enableDebugging,disableDebugging,detachDebugging,isDebuggingEnabled,getStore,resolveSourceLocation,listStoreKeys,setExceptionBreakpoint,getExceptionBreakpoint,createProbe,removeProbe,listProbes,clearProbes,getSnapshots,getSnapshotsByProbe,clearSnapshots,clearSnapshotsByProbe,getSnapshotStats,getConsoleMessages,addWatchExpression,removeWatchExpression,listWatchExpressions,clearWatchExpressions,loadSourceMaps,hasSourceMaps,getOriginalSources,getScripts,evaluateInNode};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{DEFAULT_NODE_DEBUG_CONFIG,ProbeKind,addWatchExpression,clearProbes,clearSnapshots,clearSnapshotsByProbe,clearWatchExpressions,createProbe,detachDebugging,disableDebugging,enableDebugging,evaluateInNode,getConsoleMessages,getExceptionBreakpoint,getOriginalSources,getScripts,getSnapshotStats,getSnapshots,getSnapshotsByProbe,getStore,getStoreKey,hasSourceMaps,isDebuggingEnabled,listProbes,listStoreKeys,listWatchExpressions,loadSourceMaps,removeProbe,removeWatchExpression,resolveSourceLocation,setExceptionBreakpoint}from"./core-
|
|
1
|
+
import{DEFAULT_NODE_DEBUG_CONFIG,ProbeKind,addWatchExpression,clearProbes,clearSnapshots,clearSnapshotsByProbe,clearWatchExpressions,createProbe,detachDebugging,disableDebugging,enableDebugging,evaluateInNode,getConsoleMessages,getExceptionBreakpoint,getOriginalSources,getScripts,getSnapshotStats,getSnapshots,getSnapshotsByProbe,getStore,getStoreKey,hasSourceMaps,isDebuggingEnabled,listProbes,listStoreKeys,listWatchExpressions,loadSourceMaps,removeProbe,removeWatchExpression,resolveSourceLocation,setExceptionBreakpoint}from"./core-3YBKJFSF.js";export{DEFAULT_NODE_DEBUG_CONFIG,ProbeKind,addWatchExpression,clearProbes,clearSnapshots,clearSnapshotsByProbe,clearWatchExpressions,createProbe,detachDebugging,disableDebugging,enableDebugging,evaluateInNode,getConsoleMessages,getExceptionBreakpoint,getOriginalSources,getScripts,getSnapshotStats,getSnapshots,getSnapshotsByProbe,getStore,getStoreKey,hasSourceMaps,isDebuggingEnabled,listProbes,listStoreKeys,listWatchExpressions,loadSourceMaps,removeProbe,removeWatchExpression,resolveSourceLocation,setExceptionBreakpoint};
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import{AMAZON_BEDROCK_ENABLE,AMAZON_BEDROCK_IMAGE_EMBED_MODEL_ID,AMAZON_BEDROCK_TEXT_EMBED_MODEL_ID,AMAZON_BEDROCK_VISION_MODEL_ID,AWS_PROFILE,AWS_REGION,BROWSER_CONSOLE_MESSAGES_BUFFER_SIZE,BROWSER_EXECUTABLE_PATH,BROWSER_HEADLESS_ENABLE,BROWSER_HTTP_REQUESTS_BUFFER_SIZE,BROWSER_LOCALE,BROWSER_PERSISTENT_ENABLE,BROWSER_PERSISTENT_USER_DATA_DIR,BROWSER_POLICY_UI_DEBUGGING_ENABLE,BROWSER_SERVER_INSTRUCTIONS_ENABLE,BROWSER_USE_INSTALLED_ON_SYSTEM,ConsoleMessageLevel,ConsoleMessageLevelName,FIGMA_ACCESS_TOKEN,FIGMA_API_BASE_URL,NODE_CONSOLE_MESSAGES_BUFFER_SIZE,NODE_INSPECTOR_HOST,NODE_POLICY_DEBUGGING_ENABLE,NODE_SERVER_INSTRUCTIONS_ENABLE,OTEL_ASSETS_DIR,OTEL_ENABLE,OTEL_EXPORTER_HTTP_HEADERS,OTEL_EXPORTER_HTTP_URL,OTEL_EXPORTER_TYPE,OTEL_INSTRUMENTATION_USER_INTERACTION_EVENTS,OTEL_SERVICE_NAME,OTEL_SERVICE_VERSION,PLATFORM,SourceMapResolver,V8Api,addWatchExpression,clearWatchExpressions,createProbe,debug,detachDebugging,enableDebugging,getConsoleMessages,getExceptionBreakpoint,getSnapshotStats,getSnapshots,getSnapshotsByProbe,getStoreKey,hasSourceMaps,isDebuggingEnabled,listProbes,listWatchExpressions,removeProbe,removeWatchExpression,resolveSourceLocation,setExceptionBreakpoint,toJson,warn}from"./core-3M2NFQVF.js";import fs from"node:fs";import{chromium,firefox,webkit}from"playwright";var DEFAULT_BROWSER_TYPE="chromium",browsers=new Map,persistenceBrowserContexts=new Map;function _browserKey(browserOptions){return JSON.stringify(browserOptions)}function _browserLaunchOptions(browserOptions){let launchOptions={headless:browserOptions.headless,executablePath:browserOptions.executablePath,handleSIGINT:!1,handleSIGTERM:!1};if(browserOptions.useInstalledOnSystem)if(browserOptions.browserType==="chromium")launchOptions.channel="chrome",launchOptions.args=["--disable-blink-features=AutomationControlled"],launchOptions.ignoreDefaultArgs=["--disable-extensions"];else throw new Error(`Browser type ${browserOptions.browserType} is not supported to be used from the one installed on the system`);return launchOptions}async function _createBrowser(browserOptions){let browserInstance;switch(browserOptions.browserType){case"firefox":browserInstance=firefox;break;case"webkit":browserInstance=webkit;break;default:browserInstance=chromium;break}return browserInstance.launch(_browserLaunchOptions(browserOptions))}async function _getBrowser(browserOptions){let browserKey=_browserKey(browserOptions),browserInstance=browsers.get(browserKey);if(browserInstance&&!browserInstance.isConnected()){try{await browserInstance.close().catch(()=>{})}catch{}browserInstance=void 0}return browserInstance||(browserInstance=await _createBrowser(browserOptions),browsers.set(browserKey,browserInstance)),browserInstance}function _persistentBrowserContextKey(browserContextOptions){return browserContextOptions.persistent.userDataDir}function _persistentBrowserContextLaunchOptions(browserContextOptions){let browserOptions=browserContextOptions.browserOptions,launchOptions={headless:browserOptions.headless,executablePath:browserOptions.executablePath,bypassCSP:!0,viewport:browserOptions.headless?void 0:null,locale:browserContextOptions.locale};if(browserOptions.useInstalledOnSystem)if(browserOptions.browserType==="chromium")launchOptions.channel="chrome",launchOptions.args=["--disable-blink-features=AutomationControlled"],launchOptions.ignoreDefaultArgs=["--disable-extensions"];else throw new Error(`Browser type ${browserOptions.browserType} is not supported to be used from the one installed on the system`);return launchOptions}async function _createPersistentBrowserContext(browserContextOptions){let browserInstance;switch(browserContextOptions.browserOptions.browserType){case"firefox":browserInstance=firefox;break;case"webkit":browserInstance=webkit;break;default:browserInstance=chromium;break}let userDataDir=browserContextOptions.persistent.userDataDir;fs.mkdirSync(userDataDir,{recursive:!0});let browserContext=await browserInstance.launchPersistentContext(userDataDir,_persistentBrowserContextLaunchOptions(browserContextOptions));for(let p of browserContext.pages())try{await p.close()}catch{}return browserContext}async function _getPersistentBrowserContext(browserContextOptions){let persistentBrowserContextKey=_persistentBrowserContextKey(browserContextOptions),browserContext=persistenceBrowserContexts.get(persistentBrowserContextKey);if(browserContext&&!browserContext.browser()?.isConnected()){try{await browserContext.close().catch(()=>{})}catch{}browserContext=void 0}if(!browserContext)browserContext=await _createPersistentBrowserContext(browserContextOptions),persistenceBrowserContexts.set(persistentBrowserContextKey,browserContext);else throw new Error(`There is already active persistent browser context in the user data directory: ${browserContextOptions.persistent?.userDataDir}`);return browserContext}async function newBrowserContext(browserContextOptions={browserOptions:{browserType:DEFAULT_BROWSER_TYPE,headless:BROWSER_HEADLESS_ENABLE,executablePath:BROWSER_EXECUTABLE_PATH,useInstalledOnSystem:BROWSER_USE_INSTALLED_ON_SYSTEM},persistent:BROWSER_PERSISTENT_ENABLE?{userDataDir:BROWSER_PERSISTENT_USER_DATA_DIR}:void 0,locale:BROWSER_LOCALE}){return browserContextOptions.persistent?{browserContext:await _getPersistentBrowserContext(browserContextOptions)}:{browserContext:await(await _getBrowser(browserContextOptions.browserOptions)).newContext({viewport:browserContextOptions.browserOptions.headless?void 0:null,bypassCSP:!0,locale:browserContextOptions.locale})}}async function newPage(browserContext,pageOptions={}){return await browserContext.newPage()}async function closeBrowserContext(browserContext){await browserContext.close();let deleted=!1;for(let[key,val]of persistenceBrowserContexts.entries())browserContext===val&&(persistenceBrowserContexts.delete(key),deleted=!0);return deleted}function _normalizeBasePath(input){let p=input.trim();return p.startsWith("/")||(p="/"+p),p.endsWith("/")||(p=p+"/"),p}function _normalizeUpstreamBaseUrl(input){let u=input.trim();return u&&(u.endsWith("/")?u.slice(0,-1):u)}function _computeSuffixPath(fullUrl,basePath){try{let pathname=new URL(fullUrl).pathname;if(!pathname.startsWith(basePath))return null;let raw=pathname.slice(basePath.length);return raw?raw.startsWith("/")?raw:"/"+raw:null}catch{return null}}function _appendSuffixToUpstream(upstreamBaseUrl,suffixPath,originalUrl){try{let qs=new URL(originalUrl).search??"";return upstreamBaseUrl+suffixPath+qs}catch{return upstreamBaseUrl+suffixPath}}var OTELProxy=class{config;queue;workers;isRunning;isInstalled;metrics;constructor(config){let maxQueueSize=config.maxQueueSize??200,concurrency=config.concurrency??2,respondNoContent=config.respondNoContent??!0,normalizedLocalPath=_normalizeBasePath(config.localPath),normalizedUpstreamUrl=_normalizeUpstreamBaseUrl(config.upstreamUrl);this.config={...config,localPath:normalizedLocalPath,upstreamUrl:normalizedUpstreamUrl,maxQueueSize,concurrency,respondNoContent},this.queue=[],this.workers=[],this.isRunning=!1,this.isInstalled=!1,this.metrics={routedRequests:0,acceptedBatches:0,droppedBatches:0,forwardedBatches:0,failedBatches:0,inFlight:0,queueSize:0,lastError:null}}getMetrics(){return{...this.metrics,queueSize:this.queue.length}}async install(context){if(this.isInstalled)return;let basePath=this.config.localPath;if(!basePath.startsWith("/"))throw new Error('localPath must start with "/" (e.g. "/__mcp_otel/").');let pattern=`**${basePath}**`;await context.route(pattern,async route=>{await this._handleRoute(route)}),this.isInstalled=!0,this.isRunning||await this.start(),debug(`[otel-proxy] installed route pattern: ${pattern} (basePath=${basePath}, upstreamBase=${this.config.upstreamUrl})`)}async uninstall(context){if(!this.isInstalled)return;let pattern=`**${this.config.localPath}**`;try{await context.unroute(pattern)}catch{}this.isInstalled=!1,await this.stop()}async start(){if(this.isRunning)return;this.isRunning=!0;let workerCount=Math.max(1,this.config.concurrency);for(let i=0;i<workerCount;i++){let w=this._workerLoop(i);this.workers.push(w)}debug(`[otel-proxy] started with concurrency=${workerCount}, maxQueueSize=${this.config.maxQueueSize}`)}async stop(){if(this.isRunning){this.isRunning=!1,this.queue.length=0;try{await Promise.allSettled(this.workers)}finally{this.workers.length=0}debug("[otel-proxy] stopped")}}async _handleRoute(route){let req=route.request();if(this.metrics.routedRequests++,req.method().toUpperCase()==="OPTIONS"){await this._fulfillFast(route);return}if(this.config.shouldForward&&!this.config.shouldForward(req)){await this._fulfillFast(route);return}let requestUrl=req.url(),basePath=this.config.localPath,suffixPath=_computeSuffixPath(requestUrl,basePath);if(!suffixPath){await route.fallback();return}let upstreamFullUrl=_appendSuffixToUpstream(this.config.upstreamUrl,suffixPath,requestUrl),body=await req.postDataBuffer()??Buffer.alloc(0),contentType=req.headers()["content-type"]??"application/x-protobuf",method=req.method(),headers={"content-type":contentType};if(this.config.upstreamHeaders)for(let[k,v]of Object.entries(this.config.upstreamHeaders))headers[k]=v;if(this.queue.length>=this.config.maxQueueSize){this.metrics.droppedBatches++,await this._fulfillFast(route),warn(`[otel-proxy] dropped batch (queue full: ${this.queue.length}/${this.config.maxQueueSize}) suffix=${suffixPath}`);return}let item={body,contentType,createdAtMs:Date.now(),upstreamUrl:upstreamFullUrl,method,headers};this.queue.push(item),this.metrics.acceptedBatches++,await this._fulfillFast(route)}async _fulfillFast(route){let status=this.config.respondNoContent?204:200;if(status===204){await route.fulfill({status});return}await route.fulfill({status,headers:{"content-type":"text/plain; charset=utf-8"},body:""})}async _workerLoop(workerIndex){for(;this.isRunning;){let item=this.queue.shift();if(!item){await this._sleep(25);continue}this.metrics.inFlight++;try{await this._forwardUpstream(item),this.metrics.forwardedBatches++}catch(e){this.metrics.failedBatches++;let msg=e instanceof Error?e.message:String(e);this.metrics.lastError=msg,warn(`[otel-proxy] worker=${workerIndex} forward failed: ${msg}`)}finally{this.metrics.inFlight--}}}async _forwardUpstream(item){let res=await fetch(item.upstreamUrl,{method:item.method,headers:item.headers,body:new Uint8Array(item.body)});if(res.status<200||res.status>=300){let text=await this._safeReadText(res);throw new Error(`upstream returned ${res.status} for ${item.upstreamUrl}: ${text}`)}}async _safeReadText(res){try{return(await res.text()).slice(0,500)}catch{return""}}async _sleep(ms){await new Promise(resolve=>{setTimeout(()=>resolve(),ms)})}};import*as fs2 from"node:fs";import*as path from"node:path";import{fileURLToPath}from"node:url";var __filename=fileURLToPath(import.meta.url),__dirname=path.dirname(__filename),OTEL_PROXY_LOCAL_PATH="/__mcp_otel/",OTEL_BUNDLE_FILE_NAME="otel-initializer.bundle.js";function _getOtelAssetsDir(){return OTEL_ASSETS_DIR?OTEL_ASSETS_DIR:path.join(__dirname,"platform","browser","otel")}function _getOTELExporterConfig(){if(OTEL_EXPORTER_TYPE==="otlp/http"||OTEL_EXPORTER_HTTP_URL){if(!OTEL_EXPORTER_HTTP_URL)throw new Error('OTEL exporter HTTP url must be set when OTEL exporter type is "otlp/http"');return{type:"otlp/http",url:OTEL_PROXY_LOCAL_PATH,upstreamURL:OTEL_EXPORTER_HTTP_URL,headers:OTEL_EXPORTER_HTTP_HEADERS}}else{if(OTEL_EXPORTER_TYPE==="console")return{type:"console"};if(OTEL_EXPORTER_TYPE==="none")return{type:"none"};throw new Error(`Invalid OTEL exporter type ${OTEL_EXPORTER_TYPE}`)}}function _getOTELInstrumentationConfig(){return{userInteractionEvents:OTEL_INSTRUMENTATION_USER_INTERACTION_EVENTS}}function _getOTELConfig(){return{serviceName:OTEL_SERVICE_NAME,serviceVersion:OTEL_SERVICE_VERSION,exporter:_getOTELExporterConfig(),instrumentation:_getOTELInstrumentationConfig(),debug:!1}}function _readBundleContent(assetDir,bundleFileName){let assetDirAbs=path.isAbsolute(assetDir)?assetDir:path.join(process.cwd(),assetDir),filePath=path.join(assetDirAbs,bundleFileName);if(!fs2.existsSync(filePath))throw new Error(`OTEL bundle not found at: ${filePath}`);return fs2.readFileSync(filePath,"utf-8")}async function _applyConfigToPage(page,cfg){await page.evaluate(nextCfg=>{let g=globalThis;g.__MCP_DEVTOOLS__||(g.__MCP_DEVTOOLS__={}),g.__MCP_TRACE_ID__=nextCfg.traceId,g.__mcpOtel&&typeof g.__mcpOtel.init=="function"?g.__mcpOtel.init(nextCfg):(g.__MCP_DEVTOOLS__.otelInitialized=!1,g.__MCP_DEVTOOLS__.otelInitError="__mcpOtel.init is not available while applying config")},cfg).catch(e=>{let msg=e instanceof Error?e.message:String(e);debug(`[otel-controller] applyConfigToPage failed (ignored): ${msg}`)})}function _installAutoSync(browserContext,getCfg){let perPageHandlers=new WeakMap,attachToPage=page=>{if(perPageHandlers.has(page))return;let onFrameNavigated=async frame=>{frame===page.mainFrame()&&await _applyConfigToPage(page,getCfg())};perPageHandlers.set(page,onFrameNavigated),page.on("framenavigated",onFrameNavigated)};for(let p of browserContext.pages())attachToPage(p);let onNewPage=p=>{attachToPage(p)};browserContext.on("page",onNewPage);let detach=()=>{try{browserContext.off("page",onNewPage)}catch{}for(let p of browserContext.pages()){let h=perPageHandlers.get(p);if(h)try{p.off("framenavigated",h)}catch{}}};return debug("[otel-controller] auto-sync installed (page+framenavigated)"),{detach}}var OTELController=class{browserContext;config;proxy;initialized=!1;autoSyncDetach;constructor(browserContext){this.browserContext=browserContext,this.config=_getOTELConfig()}async init(options){if(this.initialized){debug("[otel-controller] init skipped: BrowserContext already initialized");return}if(!options.traceId||!options.traceId.trim())throw new Error("[otel-controller] init requires a non-empty traceId");this.config.traceId=options.traceId;let assetDir=_getOtelAssetsDir();this.config.exporter.type==="otlp/http"&&(this.proxy=new OTELProxy({localPath:OTEL_PROXY_LOCAL_PATH,upstreamUrl:this.config.exporter.upstreamURL,upstreamHeaders:{...this.config.exporter.headers??{}}}),await this.proxy.install(this.browserContext)),debug(`[otel-controller] exporter=${this.config.exporter.type} localBase=${OTEL_PROXY_LOCAL_PATH}`+(this.config.exporter.type==="otlp/http"?` upstreamBase=${this.config.exporter.upstreamURL}`:""));let bundleContent=_readBundleContent(assetDir,OTEL_BUNDLE_FILE_NAME),sync=_installAutoSync(this.browserContext,()=>this.config);this.autoSyncDetach=sync.detach,await this.browserContext.addInitScript({content:bundleContent}),await this.browserContext.addInitScript(cfg=>{let g=globalThis;g.__MCP_DEVTOOLS__||(g.__MCP_DEVTOOLS__={}),g.__MCP_TRACE_ID__=cfg.traceId,g.__mcpOtel&&typeof g.__mcpOtel.init=="function"?g.__mcpOtel.init(cfg):(g.__MCP_DEVTOOLS__.otelInitialized=!1,g.__MCP_DEVTOOLS__.otelInitError="__mcpOtel.init is not available (initializer bundle did not install)")},this.config),this.initialized=!0,debug("[otel-controller] init installed: bundle + config init scripts + auto-sync")}isOTELRequest(request){return new URL(request.url()).pathname.startsWith(OTEL_PROXY_LOCAL_PATH)}async isInitialized(page){return await page.evaluate(()=>globalThis.__MCP_DEVTOOLS__?.otelInitialized===!0)}async getInitError(page){return await page.evaluate(()=>{let v=globalThis.__MCP_DEVTOOLS__?.otelInitError;if(typeof v=="string"&&v.trim())return v})}async getTraceId(page){return await page.evaluate(()=>{let g=globalThis;if(g.__mcpOtel&&typeof g.__mcpOtel.getTraceId=="function"){let tid=g.__mcpOtel.getTraceId();if(typeof tid=="string"&&tid.trim())return tid}let fallback=g.__MCP_TRACE_ID__;if(typeof fallback=="string"&&fallback.trim())return fallback})}async setTraceId(page,traceId){this.config.traceId=traceId,await page.evaluate(tid=>{let g=globalThis;g.__mcpOtel&&typeof g.__mcpOtel.setTraceId=="function"?g.__mcpOtel.setTraceId(tid):g.__MCP_TRACE_ID__=tid},traceId)}async close(){if(this.autoSyncDetach){try{this.autoSyncDetach()}catch{}this.autoSyncDetach=void 0}this.proxy&&(await this.proxy.uninstall(this.browserContext),this.proxy=void 0)}};var HttpMethod=(HttpMethod3=>(HttpMethod3.GET="GET",HttpMethod3.POST="POST",HttpMethod3.PUT="PUT",HttpMethod3.PATCH="PATCH",HttpMethod3.DELETE="DELETE",HttpMethod3.HEAD="HEAD",HttpMethod3.OPTIONS="OPTIONS",HttpMethod3))(HttpMethod||{}),HttpResourceType=(HttpResourceType2=>(HttpResourceType2.DOCUMENT="document",HttpResourceType2.STYLESHEET="stylesheet",HttpResourceType2.IMAGE="image",HttpResourceType2.MEDIA="media",HttpResourceType2.FONT="font",HttpResourceType2.SCRIPT="script",HttpResourceType2.TEXTTRACK="texttrack",HttpResourceType2.XHR="xhr",HttpResourceType2.FETCH="fetch",HttpResourceType2.EVENTSOURCE="eventsource",HttpResourceType2.WEBSOCKET="websocket",HttpResourceType2.MANIFEST="manifest",HttpResourceType2.OTHER="other",HttpResourceType2))(HttpResourceType||{});import crypto from"node:crypto";function newTraceId(){return crypto.randomBytes(16).toString("hex")}var BrowserToolSessionContext=class _BrowserToolSessionContext{static STATIC_RESOURCE_TYPES=new Set(["image","stylesheet","font","media","script","texttrack","manifest"]);static STATIC_ASSET_EXT=/\.(js|mjs|cjs|map|css|woff2?|ttf|otf|eot|png|jpe?g|gif|webp|svg|ico|mp4|webm|mp3|wav|pdf)(\?|$)/i;_sessionId;options;otelController;consoleMessages=[];httpRequests=[];initialized=!1;closed=!1;traceId;_numOfInFlightRequests=0;_lastNetworkActivityTimestamp=0;_refMap={};browserContext;page;constructor(sessionId,browserContext,page,options){this._sessionId=sessionId,this.browserContext=browserContext,this.page=page,this.options=options,this.otelController=new OTELController(this.browserContext)}async init(){if(this.closed)throw new Error("Session context is already closed");if(this.initialized)throw new Error("Session context is already initialized");let me=this,consoleMessageSequenceNumber=0;this.page.on("console",msg=>{me.consoleMessages.push(me._toConsoleMessage(msg,++consoleMessageSequenceNumber)),me.consoleMessages.length>BROWSER_CONSOLE_MESSAGES_BUFFER_SIZE&&me.consoleMessages.splice(0,me.consoleMessages.length-BROWSER_CONSOLE_MESSAGES_BUFFER_SIZE)}),this.page.on("pageerror",err=>{me.consoleMessages.push(me._errorToConsoleMessage(err,++consoleMessageSequenceNumber)),me.consoleMessages.length>BROWSER_CONSOLE_MESSAGES_BUFFER_SIZE&&me.consoleMessages.splice(0,me.consoleMessages.length-BROWSER_CONSOLE_MESSAGES_BUFFER_SIZE)});let httpRequestSequenceNumber=0;this.page.on("request",async req=>{me.otelController.isOTELRequest(req)||(me._numOfInFlightRequests++,me._lastNetworkActivityTimestamp=Date.now())}),this.page.on("requestfinished",async req=>{me.otelController.isOTELRequest(req)||(me._numOfInFlightRequests--,me._lastNetworkActivityTimestamp=Date.now(),me.httpRequests.push(await me._toHttpRequest(req,++httpRequestSequenceNumber)),me.httpRequests.length>BROWSER_HTTP_REQUESTS_BUFFER_SIZE&&me.httpRequests.splice(0,me.httpRequests.length-BROWSER_HTTP_REQUESTS_BUFFER_SIZE))}),this.page.on("requestfailed",async req=>{me.otelController.isOTELRequest(req)||(me._numOfInFlightRequests--,me._lastNetworkActivityTimestamp=Date.now(),me.httpRequests.push(await me._toHttpRequest(req,++httpRequestSequenceNumber)),me.httpRequests.length>BROWSER_HTTP_REQUESTS_BUFFER_SIZE&&me.httpRequests.splice(0,me.httpRequests.length-BROWSER_HTTP_REQUESTS_BUFFER_SIZE))}),this.options.otelEnable&&(this.traceId=newTraceId(),await this.otelController.init({traceId:this.traceId})),this.initialized=!0}_toConsoleMessageLevelName(type){switch(type){case"assert":case"error":return"error";case"warning":return"warning";case"count":case"dir":case"dirxml":case"info":case"log":case"table":case"time":case"timeEnd":return"info";case"clear":case"debug":case"endGroup":case"profile":case"profileEnd":case"startGroup":case"startGroupCollapsed":case"trace":return"debug";default:return"info"}}_toConsoleMessage(message,sequenceNumber){let timestamp=Date.now(),levelName=this._toConsoleMessageLevelName(message.type()),levelCode=ConsoleMessageLevel[levelName].code;return{type:message.type(),text:message.text(),level:{name:levelName,code:levelCode},location:{url:message.location().url,lineNumber:message.location().lineNumber,columnNumber:message.location().columnNumber},timestamp,sequenceNumber}}_errorToConsoleMessage(error,sequenceNumber){let timestamp=Date.now();return error instanceof Error?{type:"error",text:error.message,level:{name:"error",code:3},timestamp,sequenceNumber}:{type:"error",text:String(error),level:{name:"error",code:3},timestamp,sequenceNumber}}_isStaticResourceUrl(url){try{let pathname=new URL(url).pathname;return _BrowserToolSessionContext.STATIC_ASSET_EXT.test(pathname)}catch{return!1}}_isBodyLikelyPresent(status,method){return!(method==="HEAD"||method==="OPTIONS"||status===204||status===304||status>=300&&status<400)}async _safeReadResponseBody(res){try{let method=res.request().method(),status=res.status();return this._isBodyLikelyPresent(status,method)?(await res.body()).toString("utf-8"):void 0}catch{return}}async _toHttpRequest(req,sequenceNumber){let res=await req.response(),resourceType=req.resourceType(),skipResponseBody=_BrowserToolSessionContext.STATIC_RESOURCE_TYPES.has(resourceType)||this._isStaticResourceUrl(req.url());return{url:req.url(),method:req.method(),headers:req.headers(),body:req.postData()||void 0,resourceType,failure:req.failure()?.errorText,duration:req.timing().responseEnd,response:res?{status:res.status(),statusText:res.statusText(),headers:res.headers(),body:skipResponseBody?void 0:await this._safeReadResponseBody(res)}:void 0,ok:res?res.ok():!1,timestamp:Math.floor(req.timing().startTime),sequenceNumber}}numOfInFlightRequests(){return this._numOfInFlightRequests}lastNetworkActivityTimestamp(){return this._lastNetworkActivityTimestamp}sessionId(){return this._sessionId}async getTraceId(){return this.traceId}async setTraceId(traceId){if(!this.options.otelEnable)throw new Error("OTEL is not enabled");this.traceId=traceId,await this.otelController.setTraceId(this.page,traceId)}getConsoleMessages(){return this.consoleMessages}getHttpRequests(){return this.httpRequests}getRefMap(){return this._refMap}setRefMap(refs){this._refMap=refs}executionContext(){return{page:this.page}}async close(){if(this.closed)return!1;debug(`Closing OTEL controller of the session with id ${this._sessionId} ...`),await this.otelController.close();try{debug(`Closing browser context of the session with id ${this._sessionId} ...`),await closeBrowserContext(this.browserContext)}catch(err){debug(`Error occurred while closing browser context of the session with id ${this._sessionId} ...`,err)}return this.consoleMessages.length=0,this.httpRequests.length=0,this._refMap={},this.closed=!0,!0}};var BrowserToolExecutor=class{async executeTool(context,tool,args){debug(`Executing tool ${tool.name()} with input: ${toJson(args)}`);try{let result=await tool.handle(context,args);return debug(`Executed tool ${tool.name()} and got output: ${toJson(result)}`),result}catch(err){throw debug(`Error occurred while executing ${tool.name()}: ${err}`),err}}};var INTERACTIVE_ROLES=new Set(["button","link","textbox","checkbox","radio","combobox","listbox","menuitem","menuitemcheckbox","menuitemradio","option","searchbox","slider","spinbutton","switch","tab","treeitem"]),CONTENT_ROLES=new Set(["heading","cell","gridcell","columnheader","rowheader","listitem","article","region","main","navigation"]),STRUCTURAL_ROLES=new Set(["generic","group","list","table","row","rowgroup","grid","treegrid","menu","menubar","toolbar","tablist","tree","directory","document","application","presentation","none"]),refCounter=0;function nextRef(){return`e${++refCounter}`}function buildSelectorDescriptor(role,name){if(name!==void 0&&name!==""){let escaped=JSON.stringify(name);return`getByRole('${role}', { name: ${escaped}, exact: true })`}return`getByRole('${role}')`}function createRoleNameTracker(){let counts=new Map,refsByKey=new Map;return{getKey(role,name){return`${role}:${name??""}`},getNextIndex(role,name){let key=this.getKey(role,name),current=counts.get(key)??0;return counts.set(key,current+1),current},trackRef(role,name,ref){let key=this.getKey(role,name),refs=refsByKey.get(key)??[];refs.push(ref),refsByKey.set(key,refs)},getDuplicateKeys(){let duplicates=new Set;for(let[key,refs]of refsByKey)refs.length>1&&duplicates.add(key);return duplicates}}}function removeNthFromNonDuplicates(refs,tracker){let duplicateKeys=tracker.getDuplicateKeys();for(let entry of Object.values(refs)){let key=tracker.getKey(entry.role,entry.name);duplicateKeys.has(key)||delete entry.nth}}function getIndentLevel(line){let m=line.match(/^(\s*)/);return m?Math.floor(m[1].length/2):0}function processLine(line,refs,options,tracker){let depth=getIndentLevel(line);if(options.maxDepth!==void 0&&depth>options.maxDepth)return null;let match=line.match(/^(\s*-\s*)(\w+)(?:\s+"([^"]*)")?(.*)$/);if(!match)return options.interactiveOnly?null:line;let[,prefix,role,name,suffix]=match,roleLower=role.toLowerCase();if(role.startsWith("/"))return line;let isInteractive=INTERACTIVE_ROLES.has(roleLower),isContent=CONTENT_ROLES.has(roleLower),isStructural=STRUCTURAL_ROLES.has(roleLower);if(options.interactiveOnly&&!isInteractive||options.compact&&isStructural&&!name)return null;if(!(isInteractive||isContent&&name))return line;let ref=nextRef(),nth=tracker.getNextIndex(roleLower,name);tracker.trackRef(roleLower,name,ref);let entry={role:roleLower,name:name??void 0,selector:buildSelectorDescriptor(roleLower,name),nth};refs[ref]=entry;let enhanced=`${prefix}${role}`;return name&&(enhanced+=` "${name}"`),enhanced+=` [ref=${ref}]`,nth>0&&(enhanced+=` [nth=${nth}]`),suffix&&suffix.trim()&&(enhanced+=suffix),enhanced}function compactTree(tree){let lines=tree.split(`
|
|
1
|
+
import{AMAZON_BEDROCK_ENABLE,AMAZON_BEDROCK_IMAGE_EMBED_MODEL_ID,AMAZON_BEDROCK_TEXT_EMBED_MODEL_ID,AMAZON_BEDROCK_VISION_MODEL_ID,AWS_PROFILE,AWS_REGION,BROWSER_CDP_CONNECT_URL,BROWSER_CDP_ENDPOINT_EXPLICIT,BROWSER_CDP_OPEN_INSPECT,BROWSER_CONSOLE_MESSAGES_BUFFER_SIZE,BROWSER_EXECUTABLE_PATH,BROWSER_HEADLESS_ENABLE,BROWSER_HTTP_REQUESTS_BUFFER_SIZE,BROWSER_LOCALE,BROWSER_PERSISTENT_ENABLE,BROWSER_PERSISTENT_USER_DATA_DIR,BROWSER_POLICY_UI_DEBUGGING_ENABLE,BROWSER_SERVER_INSTRUCTIONS_ENABLE,BROWSER_USE_INSTALLED_ON_SYSTEM,ConsoleMessageLevel,ConsoleMessageLevelName,FIGMA_ACCESS_TOKEN,FIGMA_API_BASE_URL,NODE_CONSOLE_MESSAGES_BUFFER_SIZE,NODE_INSPECTOR_HOST,NODE_POLICY_DEBUGGING_ENABLE,NODE_SERVER_INSTRUCTIONS_ENABLE,OTEL_ASSETS_DIR,OTEL_ENABLE,OTEL_EXPORTER_HTTP_HEADERS,OTEL_EXPORTER_HTTP_URL,OTEL_EXPORTER_TYPE,OTEL_INSTRUMENTATION_USER_INTERACTION_EVENTS,OTEL_SERVICE_NAME,OTEL_SERVICE_VERSION,PLATFORM,SourceMapResolver,V8Api,addWatchExpression,clearWatchExpressions,createProbe,debug,detachDebugging,enableDebugging,getConsoleMessages,getExceptionBreakpoint,getSnapshotStats,getSnapshots,getSnapshotsByProbe,getStoreKey,hasSourceMaps,isDebuggingEnabled,listProbes,listWatchExpressions,removeProbe,removeWatchExpression,resolveSourceLocation,setExceptionBreakpoint,toJson,warn}from"./core-3YBKJFSF.js";var R_DISCOVERY_REMOTE="Check BROWSER_CDP_ENDPOINT_URL, host reachability, and firewall.",R_CONNECT_GENERIC="Allow the Chrome remote-debugging prompt if shown. If attach still fails, open chrome://inspect/#remote-debugging and confirm debugging is on.",CdpDiscoveryFailedError=class extends Error{constructor(detailLine,loopbackAttach){super(`[CDP discovery] ${detailLine}`);this.detailLine=detailLine;this.loopbackAttach=loopbackAttach;this.name="CdpDiscoveryFailedError"}};function cdpDiscoveryAttachHint(chromeRunning,openedInspectPage){return chromeRunning?`Chrome is running but remote debugging is not active on the probed port(s). ${openedInspectPage?"Opened chrome://inspect/#remote-debugging \u2014 enable remote debugging, then retry.":"Open chrome://inspect/#remote-debugging in Chrome, enable remote debugging, then retry."} Alternatively start Chrome with --remote-debugging-port=9222 or set BROWSER_CDP_ENDPOINT_URL.`:"Google Chrome does not appear to be running. Start Chrome, then enable remote debugging at chrome://inspect/#remote-debugging or launch with --remote-debugging-port=9222, then retry."}function cdpConnectAttachHint(chromeRunning,openedInspectPage){return chromeRunning?openedInspectPage?"If chrome://inspect/#remote-debugging opened in Chrome, enable remote debugging and retry. Approve any remote-debugging prompt.":"Open chrome://inspect/#remote-debugging, enable remote debugging, then retry. Or start Chrome with --remote-debugging-port=9222.":"Google Chrome does not appear to be running. Start Chrome, then enable remote debugging (chrome://inspect/#remote-debugging or --remote-debugging-port=9222), then retry."}function cdpDiscoveryError(detail){return new Error(`[CDP discovery] ${detail}
|
|
2
|
+
\u2192 ${R_DISCOVERY_REMOTE}`)}function cdpConnectError(endpointSummary,cause,chromeRunning,openedInspectPage){let hint=chromeRunning===void 0?R_CONNECT_GENERIC:cdpConnectAttachHint(chromeRunning,openedInspectPage??!1);return new Error(`[CDP connect] ${cause}
|
|
3
|
+
\u2192 endpoint: ${endpointSummary}
|
|
4
|
+
\u2192 ${hint}`)}import net from"node:net";import{execSync}from"node:child_process";function hostPortFromCdpConnectRef(urlStr){let raw=typeof urlStr=="string"?urlStr.trim():"";if(!raw)throw cdpDiscoveryError("Connect URL is empty after endpoint resolution.");let withScheme=raw.startsWith("ws:")||raw.startsWith("wss:")?`http${raw.slice(raw.indexOf(":"))}`:raw.startsWith("http")?raw:`http://${raw}`,u;try{u=new URL(withScheme)}catch{throw cdpDiscoveryError(`Invalid URL "${raw.slice(0,120)}". Expected http://host:port or ws://host:port/...`)}let port=u.port?parseInt(u.port,10):u.protocol==="https:"?443:80;return{host:u.hostname,port}}function isLoopbackHost(host){let h=host.toLowerCase();return h==="localhost"||h==="127.0.0.1"||h==="::1"||h==="[::1]"}function isChromeRunning(){try{return process.platform==="win32"?execSync('tasklist /FI "IMAGENAME eq chrome.exe" /NH',{stdio:"pipe",encoding:"utf8"}).toLowerCase().includes("chrome.exe"):execSync("pgrep -x 'Google Chrome' || pgrep -x chrome || pgrep -x chromium || true",{stdio:"pipe",encoding:"utf8"}).trim().length>0}catch{return!1}}function openChromeRemoteDebuggingPage(){let url="chrome://inspect/#remote-debugging";try{if(process.platform==="darwin")execSync(`open -a "Google Chrome" "${url}"`,{stdio:"pipe",timeout:5e3});else if(process.platform==="win32")execSync(`start chrome "${url}"`,{stdio:"pipe",timeout:5e3});else try{execSync("google-chrome chrome://inspect/#remote-debugging",{stdio:"pipe",timeout:5e3})}catch{execSync("chromium chrome://inspect/#remote-debugging",{stdio:"pipe",timeout:5e3})}return!0}catch{return!1}}var DEFAULT_TIMEOUT_MS=5e3,CDP_PROBE_PORTS=[9222,9229];function _bracketIpv6(host){return host.includes(":")&&!host.startsWith("[")?`[${host}]`:host}function _rewriteWsHost(wsUrl,host,port){try{let u=new URL(wsUrl);return u.hostname=host.includes(":")?_bracketIpv6(host):host,u.port=String(port),u.toString()}catch{return wsUrl}}function _parseHttpCdpRoot(urlStr){try{let u=new URL(urlStr);if(u.protocol!=="http:"&&u.protocol!=="https:")return null;let port=u.port?parseInt(u.port,10):u.protocol==="https:"?443:80;return{host:u.hostname,port}}catch{return null}}async function _fetchText(url,timeoutMs){let ctrl=new AbortController,id=setTimeout(()=>{ctrl.abort()},timeoutMs);try{return await(await fetch(url,{signal:ctrl.signal})).text()}finally{clearTimeout(id)}}function _browserWsUrl(host,port){return`ws://${_bracketIpv6(host)}:${port}/devtools/browser`}function _tcpPortOpen(host,port,timeoutMs){return new Promise(resolve=>{let settled=!1,done=ok=>{settled||(settled=!0,clearTimeout(timer),resolve(ok))},sock=net.createConnection({host,port},()=>{sock.end(),done(!0)});sock.on("error",()=>{done(!1)});let timer=setTimeout(()=>{sock.destroy(),done(!1)},timeoutMs)})}async function _httpDiscoverWsUrl(host,port,timeoutMs){let b=_bracketIpv6(host);try{let body=await _fetchText(`http://${b}:${port}/json/version`,timeoutMs),info=JSON.parse(body);if(info.webSocketDebuggerUrl)return _rewriteWsHost(info.webSocketDebuggerUrl,host,port)}catch{}try{let body=await _fetchText(`http://${b}:${port}/json/list`,timeoutMs),targets=JSON.parse(body),ws=(targets.find(t=>t.type==="browser")??targets[0])?.webSocketDebuggerUrl;if(ws)return _rewriteWsHost(ws,host,port)}catch{}return null}async function _connectUrlForHostPort(host,port,timeoutMs=DEFAULT_TIMEOUT_MS){let fromHttp=await _httpDiscoverWsUrl(host,port,timeoutMs);return fromHttp||_browserWsUrl(host,port)}async function _tryLoopbackProbePorts(timeoutMs=DEFAULT_TIMEOUT_MS){let host="127.0.0.1";for(let port of CDP_PROBE_PORTS){let fromHttp=await _httpDiscoverWsUrl(host,port,timeoutMs);if(fromHttp)return{connectUrl:fromHttp,port}}for(let port of CDP_PROBE_PORTS)if(await _tcpPortOpen(host,port,timeoutMs))return{connectUrl:_browserWsUrl(host,port),port};return null}async function resolveCdpConnectEndpoint(options){let configuredHttpOrWsUrl=options.configuredHttpOrWsUrl;if(!options.explicitEndpointUrl){let found=await _tryLoopbackProbePorts();if(found)return{connectUrl:found.connectUrl,cacheKey:`cdp:127.0.0.1:${found.port}`};throw new CdpDiscoveryFailedError("No CDP on 127.0.0.1:9222 or :9229.",!0)}let url=configuredHttpOrWsUrl;if(url.startsWith("ws://")||url.startsWith("wss://"))return{connectUrl:url,cacheKey:url};let parsed=_parseHttpCdpRoot(url);if(!parsed)throw cdpDiscoveryError(`BROWSER_CDP_ENDPOINT_URL must be http(s)://host:port or ws(s)://\u2026, got: ${configuredHttpOrWsUrl}`);try{return{connectUrl:await _connectUrlForHostPort(parsed.host,parsed.port),cacheKey:url}}catch(e){let detail=e instanceof Error?e.message:String(e),line=`No DevTools at ${parsed.host}:${parsed.port}. ${detail}`;throw isLoopbackHost(parsed.host)?new CdpDiscoveryFailedError(line,!0):cdpDiscoveryError(line)}}import fs from"node:fs";import{chromium,firefox,webkit}from"playwright";var DEFAULT_BROWSER_TYPE="chromium",browsers=new Map,persistenceBrowserContexts=new Map,cdpByEndpoint=new Map;function _browserKey(browserOptions){return JSON.stringify(browserOptions)}function _browserLaunchOptions(browserOptions){let launchOptions={headless:browserOptions.headless,executablePath:browserOptions.executablePath,handleSIGINT:!1,handleSIGTERM:!1};if(browserOptions.useInstalledOnSystem)if(browserOptions.browserType==="chromium")launchOptions.channel="chrome",launchOptions.args=["--disable-blink-features=AutomationControlled"],launchOptions.ignoreDefaultArgs=["--disable-extensions"];else throw new Error(`Browser type ${browserOptions.browserType} is not supported to be used from the one installed on the system`);return launchOptions}async function _createBrowser(browserOptions){let browserInstance;switch(browserOptions.browserType){case"firefox":browserInstance=firefox;break;case"webkit":browserInstance=webkit;break;default:browserInstance=chromium;break}return browserInstance.launch(_browserLaunchOptions(browserOptions))}async function _getBrowser(browserOptions){let browserKey=_browserKey(browserOptions),browserInstance=browsers.get(browserKey);if(browserInstance&&!browserInstance.isConnected()){try{await browserInstance.close().catch(()=>{})}catch{}browserInstance=void 0}return browserInstance||(browserInstance=await _createBrowser(browserOptions),browsers.set(browserKey,browserInstance)),browserInstance}function _persistentBrowserContextKey(browserContextOptions){return browserContextOptions.persistent.userDataDir}function _persistentBrowserContextLaunchOptions(browserContextOptions){let browserOptions=browserContextOptions.browserOptions,launchOptions={headless:browserOptions.headless,executablePath:browserOptions.executablePath,bypassCSP:!0,viewport:browserOptions.headless?void 0:null,locale:browserContextOptions.locale};if(browserOptions.useInstalledOnSystem)if(browserOptions.browserType==="chromium")launchOptions.channel="chrome",launchOptions.args=["--disable-blink-features=AutomationControlled"],launchOptions.ignoreDefaultArgs=["--disable-extensions"];else throw new Error(`Browser type ${browserOptions.browserType} is not supported to be used from the one installed on the system`);return launchOptions}async function _createPersistentBrowserContext(browserContextOptions){let browserInstance;switch(browserContextOptions.browserOptions.browserType){case"firefox":browserInstance=firefox;break;case"webkit":browserInstance=webkit;break;default:browserInstance=chromium;break}let userDataDir=browserContextOptions.persistent.userDataDir;fs.mkdirSync(userDataDir,{recursive:!0});let browserContext=await browserInstance.launchPersistentContext(userDataDir,_persistentBrowserContextLaunchOptions(browserContextOptions));for(let p of browserContext.pages())try{await p.close()}catch{}return browserContext}async function _getPersistentBrowserContext(browserContextOptions){let persistentBrowserContextKey=_persistentBrowserContextKey(browserContextOptions),browserContext=persistenceBrowserContexts.get(persistentBrowserContextKey);if(browserContext&&!browserContext.browser()?.isConnected()){try{await browserContext.close().catch(()=>{})}catch{}browserContext=void 0}if(!browserContext)browserContext=await _createPersistentBrowserContext(browserContextOptions),persistenceBrowserContexts.set(persistentBrowserContextKey,browserContext);else throw new Error(`There is already active persistent browser context in the user data directory: ${browserContextOptions.persistent?.userDataDir}`);return browserContext}function _openInspectIfChromeRunningOnLoopback(endpointHint){if(!BROWSER_CDP_OPEN_INSPECT||!isChromeRunning())return!1;try{let{host}=hostPortFromCdpConnectRef(endpointHint);if(isLoopbackHost(host))return openChromeRemoteDebuggingPage()}catch{return openChromeRemoteDebuggingPage()}return!1}async function _createCdpBrowserContext(connectUrl){let browser=await chromium.connectOverCDP(connectUrl),contexts=browser.contexts();if(contexts.length===0){let opened=_openInspectIfChromeRunningOnLoopback(connectUrl),running=isChromeRunning();throw cdpConnectError(connectUrl,"Attached but browser reported zero contexts (close other debug clients or re-enable remote debugging).",running,opened)}return{browser,context:contexts[0]}}async function _getCdpBrowserContext(){let resolvedRef,resolvedCacheKey;try{let r=await resolveCdpConnectEndpoint({configuredHttpOrWsUrl:BROWSER_CDP_CONNECT_URL,explicitEndpointUrl:BROWSER_CDP_ENDPOINT_EXPLICIT});resolvedRef=r.connectUrl,resolvedCacheKey=r.cacheKey}catch(e){if(e instanceof CdpDiscoveryFailedError&&e.loopbackAttach){let running=isChromeRunning(),openedInspect=BROWSER_CDP_OPEN_INSPECT&&running&&openChromeRemoteDebuggingPage();throw new Error(`${e.message}
|
|
5
|
+
\u2192 ${cdpDiscoveryAttachHint(running,openedInspect)}`)}throw e}let connectUrl=resolvedRef,entry=cdpByEndpoint.get(resolvedCacheKey);if(entry&&!entry.browser.isConnected()&&(cdpByEndpoint.delete(resolvedCacheKey),entry=void 0),!entry){try{entry=await _createCdpBrowserContext(connectUrl)}catch(e){if(e instanceof Error&&e.message.startsWith("[CDP "))throw e;let runningGen=isChromeRunning(),openedGen=_openInspectIfChromeRunningOnLoopback(connectUrl);throw cdpConnectError(connectUrl,e instanceof Error?e.message:String(e),runningGen,openedGen)}cdpByEndpoint.set(resolvedCacheKey,entry)}return{browserContext:entry.context,dontCloseBrowserContext:!0}}async function newBrowserContext(browserContextOptions={browserOptions:{browserType:DEFAULT_BROWSER_TYPE,headless:BROWSER_HEADLESS_ENABLE,executablePath:BROWSER_EXECUTABLE_PATH,useInstalledOnSystem:BROWSER_USE_INSTALLED_ON_SYSTEM},persistent:BROWSER_PERSISTENT_ENABLE?{userDataDir:BROWSER_PERSISTENT_USER_DATA_DIR}:void 0,locale:BROWSER_LOCALE}){if(BROWSER_CDP_CONNECT_URL){if(browserContextOptions.persistent)throw new Error("BROWSER_CDP_ENDPOINT_URL / BROWSER_CDP_ENABLE cannot be used with BROWSER_PERSISTENT_ENABLE.");if(browserContextOptions.browserOptions.browserType!=="chromium")throw new Error("CDP attach mode supports Chromium-based browsers only (Chrome/Edge).");return _getCdpBrowserContext()}return browserContextOptions.persistent?{browserContext:await _getPersistentBrowserContext(browserContextOptions)}:{browserContext:await(await _getBrowser(browserContextOptions.browserOptions)).newContext({viewport:browserContextOptions.browserOptions.headless?void 0:null,bypassCSP:!0,locale:browserContextOptions.locale})}}async function newPage(browserContext,pageOptions={}){return await browserContext.newPage()}async function closeBrowserContext(browserContext){await browserContext.close();let deleted=!1;for(let[key,val]of persistenceBrowserContexts.entries())browserContext===val&&(persistenceBrowserContexts.delete(key),deleted=!0);return deleted}function _normalizeBasePath(input){let p=input.trim();return p.startsWith("/")||(p="/"+p),p.endsWith("/")||(p=p+"/"),p}function _normalizeUpstreamBaseUrl(input){let u=input.trim();return u&&(u.endsWith("/")?u.slice(0,-1):u)}function _computeSuffixPath(fullUrl,basePath){try{let pathname=new URL(fullUrl).pathname;if(!pathname.startsWith(basePath))return null;let raw=pathname.slice(basePath.length);return raw?raw.startsWith("/")?raw:"/"+raw:null}catch{return null}}function _appendSuffixToUpstream(upstreamBaseUrl,suffixPath,originalUrl){try{let qs=new URL(originalUrl).search??"";return upstreamBaseUrl+suffixPath+qs}catch{return upstreamBaseUrl+suffixPath}}var OTELProxy=class{config;queue;workers;isRunning;isInstalled;metrics;constructor(config){let maxQueueSize=config.maxQueueSize??200,concurrency=config.concurrency??2,respondNoContent=config.respondNoContent??!0,normalizedLocalPath=_normalizeBasePath(config.localPath),normalizedUpstreamUrl=_normalizeUpstreamBaseUrl(config.upstreamUrl);this.config={...config,localPath:normalizedLocalPath,upstreamUrl:normalizedUpstreamUrl,maxQueueSize,concurrency,respondNoContent},this.queue=[],this.workers=[],this.isRunning=!1,this.isInstalled=!1,this.metrics={routedRequests:0,acceptedBatches:0,droppedBatches:0,forwardedBatches:0,failedBatches:0,inFlight:0,queueSize:0,lastError:null}}getMetrics(){return{...this.metrics,queueSize:this.queue.length}}async install(context){if(this.isInstalled)return;let basePath=this.config.localPath;if(!basePath.startsWith("/"))throw new Error('localPath must start with "/" (e.g. "/__mcp_otel/").');let pattern=`**${basePath}**`;await context.route(pattern,async route=>{await this._handleRoute(route)}),this.isInstalled=!0,this.isRunning||await this.start(),debug(`[otel-proxy] installed route pattern: ${pattern} (basePath=${basePath}, upstreamBase=${this.config.upstreamUrl})`)}async uninstall(context){if(!this.isInstalled)return;let pattern=`**${this.config.localPath}**`;try{await context.unroute(pattern)}catch{}this.isInstalled=!1,await this.stop()}async start(){if(this.isRunning)return;this.isRunning=!0;let workerCount=Math.max(1,this.config.concurrency);for(let i=0;i<workerCount;i++){let w=this._workerLoop(i);this.workers.push(w)}debug(`[otel-proxy] started with concurrency=${workerCount}, maxQueueSize=${this.config.maxQueueSize}`)}async stop(){if(this.isRunning){this.isRunning=!1,this.queue.length=0;try{await Promise.allSettled(this.workers)}finally{this.workers.length=0}debug("[otel-proxy] stopped")}}async _handleRoute(route){let req=route.request();if(this.metrics.routedRequests++,req.method().toUpperCase()==="OPTIONS"){await this._fulfillFast(route);return}if(this.config.shouldForward&&!this.config.shouldForward(req)){await this._fulfillFast(route);return}let requestUrl=req.url(),basePath=this.config.localPath,suffixPath=_computeSuffixPath(requestUrl,basePath);if(!suffixPath){await route.fallback();return}let upstreamFullUrl=_appendSuffixToUpstream(this.config.upstreamUrl,suffixPath,requestUrl),body=await req.postDataBuffer()??Buffer.alloc(0),contentType=req.headers()["content-type"]??"application/x-protobuf",method=req.method(),headers={"content-type":contentType};if(this.config.upstreamHeaders)for(let[k,v]of Object.entries(this.config.upstreamHeaders))headers[k]=v;if(this.queue.length>=this.config.maxQueueSize){this.metrics.droppedBatches++,await this._fulfillFast(route),warn(`[otel-proxy] dropped batch (queue full: ${this.queue.length}/${this.config.maxQueueSize}) suffix=${suffixPath}`);return}let item={body,contentType,createdAtMs:Date.now(),upstreamUrl:upstreamFullUrl,method,headers};this.queue.push(item),this.metrics.acceptedBatches++,await this._fulfillFast(route)}async _fulfillFast(route){let status=this.config.respondNoContent?204:200;if(status===204){await route.fulfill({status});return}await route.fulfill({status,headers:{"content-type":"text/plain; charset=utf-8"},body:""})}async _workerLoop(workerIndex){for(;this.isRunning;){let item=this.queue.shift();if(!item){await this._sleep(25);continue}this.metrics.inFlight++;try{await this._forwardUpstream(item),this.metrics.forwardedBatches++}catch(e){this.metrics.failedBatches++;let msg=e instanceof Error?e.message:String(e);this.metrics.lastError=msg,warn(`[otel-proxy] worker=${workerIndex} forward failed: ${msg}`)}finally{this.metrics.inFlight--}}}async _forwardUpstream(item){let res=await fetch(item.upstreamUrl,{method:item.method,headers:item.headers,body:new Uint8Array(item.body)});if(res.status<200||res.status>=300){let text=await this._safeReadText(res);throw new Error(`upstream returned ${res.status} for ${item.upstreamUrl}: ${text}`)}}async _safeReadText(res){try{return(await res.text()).slice(0,500)}catch{return""}}async _sleep(ms){await new Promise(resolve=>{setTimeout(()=>resolve(),ms)})}};import*as fs2 from"node:fs";import*as path from"node:path";import{fileURLToPath}from"node:url";var __filename=fileURLToPath(import.meta.url),__dirname=path.dirname(__filename),OTEL_PROXY_LOCAL_PATH="/__mcp_otel/",OTEL_BUNDLE_FILE_NAME="otel-initializer.bundle.js";function _getOtelAssetsDir(){return OTEL_ASSETS_DIR?OTEL_ASSETS_DIR:path.join(__dirname,"platform","browser","otel")}function _getOTELExporterConfig(){if(OTEL_EXPORTER_TYPE==="otlp/http"||OTEL_EXPORTER_HTTP_URL){if(!OTEL_EXPORTER_HTTP_URL)throw new Error('OTEL exporter HTTP url must be set when OTEL exporter type is "otlp/http"');return{type:"otlp/http",url:OTEL_PROXY_LOCAL_PATH,upstreamURL:OTEL_EXPORTER_HTTP_URL,headers:OTEL_EXPORTER_HTTP_HEADERS}}else{if(OTEL_EXPORTER_TYPE==="console")return{type:"console"};if(OTEL_EXPORTER_TYPE==="none")return{type:"none"};throw new Error(`Invalid OTEL exporter type ${OTEL_EXPORTER_TYPE}`)}}function _getOTELInstrumentationConfig(){return{userInteractionEvents:OTEL_INSTRUMENTATION_USER_INTERACTION_EVENTS}}function _getOTELConfig(){return{serviceName:OTEL_SERVICE_NAME,serviceVersion:OTEL_SERVICE_VERSION,exporter:_getOTELExporterConfig(),instrumentation:_getOTELInstrumentationConfig(),debug:!1}}function _readBundleContent(assetDir,bundleFileName){let assetDirAbs=path.isAbsolute(assetDir)?assetDir:path.join(process.cwd(),assetDir),filePath=path.join(assetDirAbs,bundleFileName);if(!fs2.existsSync(filePath))throw new Error(`OTEL bundle not found at: ${filePath}`);return fs2.readFileSync(filePath,"utf-8")}async function _applyConfigToPage(page,cfg){await page.evaluate(nextCfg=>{let g=globalThis;g.__MCP_DEVTOOLS__||(g.__MCP_DEVTOOLS__={}),g.__MCP_TRACE_ID__=nextCfg.traceId,g.__mcpOtel&&typeof g.__mcpOtel.init=="function"?g.__mcpOtel.init(nextCfg):(g.__MCP_DEVTOOLS__.otelInitialized=!1,g.__MCP_DEVTOOLS__.otelInitError="__mcpOtel.init is not available while applying config")},cfg).catch(e=>{let msg=e instanceof Error?e.message:String(e);debug(`[otel-controller] applyConfigToPage failed (ignored): ${msg}`)})}function _installAutoSync(browserContext,getCfg){let perPageHandlers=new WeakMap,attachToPage=page=>{if(perPageHandlers.has(page))return;let onFrameNavigated=async frame=>{frame===page.mainFrame()&&await _applyConfigToPage(page,getCfg())};perPageHandlers.set(page,onFrameNavigated),page.on("framenavigated",onFrameNavigated)};for(let p of browserContext.pages())attachToPage(p);let onNewPage=p=>{attachToPage(p)};browserContext.on("page",onNewPage);let detach=()=>{try{browserContext.off("page",onNewPage)}catch{}for(let p of browserContext.pages()){let h=perPageHandlers.get(p);if(h)try{p.off("framenavigated",h)}catch{}}};return debug("[otel-controller] auto-sync installed (page+framenavigated)"),{detach}}var OTELController=class{browserContext;config;proxy;initialized=!1;autoSyncDetach;constructor(browserContext){this.browserContext=browserContext,this.config=_getOTELConfig()}async init(options){if(this.initialized){debug("[otel-controller] init skipped: BrowserContext already initialized");return}if(!options.traceId||!options.traceId.trim())throw new Error("[otel-controller] init requires a non-empty traceId");this.config.traceId=options.traceId;let assetDir=_getOtelAssetsDir();this.config.exporter.type==="otlp/http"&&(this.proxy=new OTELProxy({localPath:OTEL_PROXY_LOCAL_PATH,upstreamUrl:this.config.exporter.upstreamURL,upstreamHeaders:{...this.config.exporter.headers??{}}}),await this.proxy.install(this.browserContext)),debug(`[otel-controller] exporter=${this.config.exporter.type} localBase=${OTEL_PROXY_LOCAL_PATH}`+(this.config.exporter.type==="otlp/http"?` upstreamBase=${this.config.exporter.upstreamURL}`:""));let bundleContent=_readBundleContent(assetDir,OTEL_BUNDLE_FILE_NAME),sync=_installAutoSync(this.browserContext,()=>this.config);this.autoSyncDetach=sync.detach,await this.browserContext.addInitScript({content:bundleContent}),await this.browserContext.addInitScript(cfg=>{let g=globalThis;g.__MCP_DEVTOOLS__||(g.__MCP_DEVTOOLS__={}),g.__MCP_TRACE_ID__=cfg.traceId,g.__mcpOtel&&typeof g.__mcpOtel.init=="function"?g.__mcpOtel.init(cfg):(g.__MCP_DEVTOOLS__.otelInitialized=!1,g.__MCP_DEVTOOLS__.otelInitError="__mcpOtel.init is not available (initializer bundle did not install)")},this.config),this.initialized=!0,debug("[otel-controller] init installed: bundle + config init scripts + auto-sync")}isOTELRequest(request){return new URL(request.url()).pathname.startsWith(OTEL_PROXY_LOCAL_PATH)}async isInitialized(page){return await page.evaluate(()=>globalThis.__MCP_DEVTOOLS__?.otelInitialized===!0)}async getInitError(page){return await page.evaluate(()=>{let v=globalThis.__MCP_DEVTOOLS__?.otelInitError;if(typeof v=="string"&&v.trim())return v})}async getTraceId(page){return await page.evaluate(()=>{let g=globalThis;if(g.__mcpOtel&&typeof g.__mcpOtel.getTraceId=="function"){let tid=g.__mcpOtel.getTraceId();if(typeof tid=="string"&&tid.trim())return tid}let fallback=g.__MCP_TRACE_ID__;if(typeof fallback=="string"&&fallback.trim())return fallback})}async setTraceId(page,traceId){this.config.traceId=traceId,await page.evaluate(tid=>{let g=globalThis;g.__mcpOtel&&typeof g.__mcpOtel.setTraceId=="function"?g.__mcpOtel.setTraceId(tid):g.__MCP_TRACE_ID__=tid},traceId)}async close(){if(this.autoSyncDetach){try{this.autoSyncDetach()}catch{}this.autoSyncDetach=void 0}this.proxy&&(await this.proxy.uninstall(this.browserContext),this.proxy=void 0)}};var HttpMethod=(HttpMethod3=>(HttpMethod3.GET="GET",HttpMethod3.POST="POST",HttpMethod3.PUT="PUT",HttpMethod3.PATCH="PATCH",HttpMethod3.DELETE="DELETE",HttpMethod3.HEAD="HEAD",HttpMethod3.OPTIONS="OPTIONS",HttpMethod3))(HttpMethod||{}),HttpResourceType=(HttpResourceType2=>(HttpResourceType2.DOCUMENT="document",HttpResourceType2.STYLESHEET="stylesheet",HttpResourceType2.IMAGE="image",HttpResourceType2.MEDIA="media",HttpResourceType2.FONT="font",HttpResourceType2.SCRIPT="script",HttpResourceType2.TEXTTRACK="texttrack",HttpResourceType2.XHR="xhr",HttpResourceType2.FETCH="fetch",HttpResourceType2.EVENTSOURCE="eventsource",HttpResourceType2.WEBSOCKET="websocket",HttpResourceType2.MANIFEST="manifest",HttpResourceType2.OTHER="other",HttpResourceType2))(HttpResourceType||{});import crypto from"node:crypto";function newTraceId(){return crypto.randomBytes(16).toString("hex")}var BrowserToolSessionContext=class _BrowserToolSessionContext{static STATIC_RESOURCE_TYPES=new Set(["image","stylesheet","font","media","script","texttrack","manifest"]);static STATIC_ASSET_EXT=/\.(js|mjs|cjs|map|css|woff2?|ttf|otf|eot|png|jpe?g|gif|webp|svg|ico|mp4|webm|mp3|wav|pdf)(\?|$)/i;_sessionId;options;otelController;consoleMessages=[];httpRequests=[];initialized=!1;closed=!1;traceId;_numOfInFlightRequests=0;_lastNetworkActivityTimestamp=0;_refMap={};_consoleSeq=0;_httpSeq=0;browserContext;_page;get page(){return this._page}constructor(sessionId,browserContext,page,options){this._sessionId=sessionId,this.browserContext=browserContext,this._page=page,this.options=options,this.otelController=new OTELController(this.browserContext)}async ensureSessionPageOpen(){if(this.closed)throw new Error("Session context is already closed");this._page.isClosed()&&(debug(`Session ${this._sessionId}: page closed; opening new tab as session page.`),this._refMap={},this._numOfInFlightRequests=0,this._lastNetworkActivityTimestamp=0,this._page=await this.browserContext.newPage(),this._attachPageListeners(this._page))}_attachPageListeners(page){let me=this;page.on("console",msg=>{me.consoleMessages.push(me._toConsoleMessage(msg,++me._consoleSeq)),me.consoleMessages.length>BROWSER_CONSOLE_MESSAGES_BUFFER_SIZE&&me.consoleMessages.splice(0,me.consoleMessages.length-BROWSER_CONSOLE_MESSAGES_BUFFER_SIZE)}),page.on("pageerror",err=>{me.consoleMessages.push(me._errorToConsoleMessage(err,++me._consoleSeq)),me.consoleMessages.length>BROWSER_CONSOLE_MESSAGES_BUFFER_SIZE&&me.consoleMessages.splice(0,me.consoleMessages.length-BROWSER_CONSOLE_MESSAGES_BUFFER_SIZE)}),page.on("request",async req=>{me.otelController.isOTELRequest(req)||(me._numOfInFlightRequests++,me._lastNetworkActivityTimestamp=Date.now())}),page.on("requestfinished",async req=>{me.otelController.isOTELRequest(req)||(me._numOfInFlightRequests--,me._lastNetworkActivityTimestamp=Date.now(),me.httpRequests.push(await me._toHttpRequest(req,++me._httpSeq)),me.httpRequests.length>BROWSER_HTTP_REQUESTS_BUFFER_SIZE&&me.httpRequests.splice(0,me.httpRequests.length-BROWSER_HTTP_REQUESTS_BUFFER_SIZE))}),page.on("requestfailed",async req=>{me.otelController.isOTELRequest(req)||(me._numOfInFlightRequests--,me._lastNetworkActivityTimestamp=Date.now(),me.httpRequests.push(await me._toHttpRequest(req,++me._httpSeq)),me.httpRequests.length>BROWSER_HTTP_REQUESTS_BUFFER_SIZE&&me.httpRequests.splice(0,me.httpRequests.length-BROWSER_HTTP_REQUESTS_BUFFER_SIZE))})}async init(){if(this.closed)throw new Error("Session context is already closed");if(this.initialized)throw new Error("Session context is already initialized");this._attachPageListeners(this._page),this.options.otelEnable&&(this.traceId=newTraceId(),await this.otelController.init({traceId:this.traceId})),this.initialized=!0}_toConsoleMessageLevelName(type){switch(type){case"assert":case"error":return"error";case"warning":return"warning";case"count":case"dir":case"dirxml":case"info":case"log":case"table":case"time":case"timeEnd":return"info";case"clear":case"debug":case"endGroup":case"profile":case"profileEnd":case"startGroup":case"startGroupCollapsed":case"trace":return"debug";default:return"info"}}_toConsoleMessage(message,sequenceNumber){let timestamp=Date.now(),levelName=this._toConsoleMessageLevelName(message.type()),levelCode=ConsoleMessageLevel[levelName].code;return{type:message.type(),text:message.text(),level:{name:levelName,code:levelCode},location:{url:message.location().url,lineNumber:message.location().lineNumber,columnNumber:message.location().columnNumber},timestamp,sequenceNumber}}_errorToConsoleMessage(error,sequenceNumber){let timestamp=Date.now();return error instanceof Error?{type:"error",text:error.message,level:{name:"error",code:3},timestamp,sequenceNumber}:{type:"error",text:String(error),level:{name:"error",code:3},timestamp,sequenceNumber}}_isStaticResourceUrl(url){try{let pathname=new URL(url).pathname;return _BrowserToolSessionContext.STATIC_ASSET_EXT.test(pathname)}catch{return!1}}_isBodyLikelyPresent(status,method){return!(method==="HEAD"||method==="OPTIONS"||status===204||status===304||status>=300&&status<400)}async _safeReadResponseBody(res){try{let method=res.request().method(),status=res.status();return this._isBodyLikelyPresent(status,method)?(await res.body()).toString("utf-8"):void 0}catch{return}}async _toHttpRequest(req,sequenceNumber){let res=await req.response(),resourceType=req.resourceType(),skipResponseBody=_BrowserToolSessionContext.STATIC_RESOURCE_TYPES.has(resourceType)||this._isStaticResourceUrl(req.url());return{url:req.url(),method:req.method(),headers:req.headers(),body:req.postData()||void 0,resourceType,failure:req.failure()?.errorText,duration:req.timing().responseEnd,response:res?{status:res.status(),statusText:res.statusText(),headers:res.headers(),body:skipResponseBody?void 0:await this._safeReadResponseBody(res)}:void 0,ok:res?res.ok():!1,timestamp:Math.floor(req.timing().startTime),sequenceNumber}}numOfInFlightRequests(){return this._numOfInFlightRequests}lastNetworkActivityTimestamp(){return this._lastNetworkActivityTimestamp}sessionId(){return this._sessionId}async getTraceId(){return this.traceId}async setTraceId(traceId){if(!this.options.otelEnable)throw new Error("OTEL is not enabled");this.traceId=traceId,await this.otelController.setTraceId(this.page,traceId)}getConsoleMessages(){return this.consoleMessages}getHttpRequests(){return this.httpRequests}getRefMap(){return this._refMap}setRefMap(refs){this._refMap=refs}executionContext(){return{page:this.page}}async close(){if(this.closed)return!1;debug(`Closing OTEL controller of the session with id ${this._sessionId} ...`),await this.otelController.close();try{this.options.dontCloseBrowserContext?(debug(`Closing session page only (CDP attach) for session ${this._sessionId} ...`),await this.page.close({runBeforeUnload:!1}).catch(()=>{})):(debug(`Closing browser context of the session with id ${this._sessionId} ...`),await closeBrowserContext(this.browserContext))}catch(err){debug(`Error occurred while closing browser context of the session with id ${this._sessionId} ...`,err)}return this.consoleMessages.length=0,this.httpRequests.length=0,this._refMap={},this.closed=!0,!0}};var BrowserToolExecutor=class{async executeTool(context,tool,args){debug(`Executing tool ${tool.name()} with input: ${toJson(args)}`);try{await context.ensureSessionPageOpen();let result=await tool.handle(context,args);return debug(`Executed tool ${tool.name()} and got output: ${toJson(result)}`),result}catch(err){throw debug(`Error occurred while executing ${tool.name()}: ${err}`),err}}};var INTERACTIVE_ROLES=new Set(["button","link","textbox","checkbox","radio","combobox","listbox","menuitem","menuitemcheckbox","menuitemradio","option","searchbox","slider","spinbutton","switch","tab","treeitem"]),CONTENT_ROLES=new Set(["heading","cell","gridcell","columnheader","rowheader","listitem","article","region","main","navigation"]),STRUCTURAL_ROLES=new Set(["generic","group","list","table","row","rowgroup","grid","treegrid","menu","menubar","toolbar","tablist","tree","directory","document","application","presentation","none"]),refCounter=0;function nextRef(){return`e${++refCounter}`}function buildSelectorDescriptor(role,name){if(name!==void 0&&name!==""){let escaped=JSON.stringify(name);return`getByRole('${role}', { name: ${escaped}, exact: true })`}return`getByRole('${role}')`}function createRoleNameTracker(){let counts=new Map,refsByKey=new Map;return{getKey(role,name){return`${role}:${name??""}`},getNextIndex(role,name){let key=this.getKey(role,name),current=counts.get(key)??0;return counts.set(key,current+1),current},trackRef(role,name,ref){let key=this.getKey(role,name),refs=refsByKey.get(key)??[];refs.push(ref),refsByKey.set(key,refs)},getDuplicateKeys(){let duplicates=new Set;for(let[key,refs]of refsByKey)refs.length>1&&duplicates.add(key);return duplicates}}}function removeNthFromNonDuplicates(refs,tracker){let duplicateKeys=tracker.getDuplicateKeys();for(let entry of Object.values(refs)){let key=tracker.getKey(entry.role,entry.name);duplicateKeys.has(key)||delete entry.nth}}function getIndentLevel(line){let m=line.match(/^(\s*)/);return m?Math.floor(m[1].length/2):0}function processLine(line,refs,options,tracker){let depth=getIndentLevel(line);if(options.maxDepth!==void 0&&depth>options.maxDepth)return null;let match=line.match(/^(\s*-\s*)(\w+)(?:\s+"([^"]*)")?(.*)$/);if(!match)return options.interactiveOnly?null:line;let[,prefix,role,name,suffix]=match,roleLower=role.toLowerCase();if(role.startsWith("/"))return line;let isInteractive=INTERACTIVE_ROLES.has(roleLower),isContent=CONTENT_ROLES.has(roleLower),isStructural=STRUCTURAL_ROLES.has(roleLower);if(options.interactiveOnly&&!isInteractive||options.compact&&isStructural&&!name)return null;if(!(isInteractive||isContent&&name))return line;let ref=nextRef(),nth=tracker.getNextIndex(roleLower,name);tracker.trackRef(roleLower,name,ref);let entry={role:roleLower,name:name??void 0,selector:buildSelectorDescriptor(roleLower,name),nth};refs[ref]=entry;let enhanced=`${prefix}${role}`;return name&&(enhanced+=` "${name}"`),enhanced+=` [ref=${ref}]`,nth>0&&(enhanced+=` [nth=${nth}]`),suffix&&suffix.trim()&&(enhanced+=suffix),enhanced}function compactTree(tree){let lines=tree.split(`
|
|
2
6
|
`),result=[];for(let i=0;i<lines.length;i++){let line=lines[i];if(line.includes("[ref=")){result.push(line);continue}if(line.includes(":")&&!line.endsWith(":")){result.push(line);continue}let currentIndent=getIndentLevel(line),hasRelevantChildren=!1;for(let j=i+1;j<lines.length&&!(getIndentLevel(lines[j])<=currentIndent);j++)if(lines[j].includes("[ref=")){hasRelevantChildren=!0;break}hasRelevantChildren&&result.push(line)}return result.join(`
|
|
3
7
|
`)}function processAriaTreeWithRefs(ariaTree,options={}){refCounter=0;let refs={};if(!ariaTree||!ariaTree.trim())return{tree:"(empty)",refs:{}};let lines=ariaTree.split(`
|
|
4
8
|
`),result=[],tracker=createRoleNameTracker();if(options.interactiveOnly){for(let line of lines){let m=line.match(/^(\s*-\s*)(\w+)(?:\s+"([^"]*)")?(.*)$/);if(!m)continue;let[,,role,name,suffix]=m,roleLower=role.toLowerCase();if(!INTERACTIVE_ROLES.has(roleLower))continue;let ref=nextRef(),nth=tracker.getNextIndex(roleLower,name);tracker.trackRef(roleLower,name,ref),refs[ref]={role:roleLower,name:name??void 0,selector:buildSelectorDescriptor(roleLower,name),nth};let enhanced=`- ${role}`;name&&(enhanced+=` "${name}"`),enhanced+=` [ref=${ref}]`,nth>0&&(enhanced+=` [nth=${nth}]`),suffix&&suffix.includes("[")&&(enhanced+=suffix),result.push(enhanced)}return removeNthFromNonDuplicates(refs,tracker),{tree:result.length?result.join(`
|
|
@@ -406,7 +410,7 @@ How to use it effectively:
|
|
|
406
410
|
- Notes explain which signals were used or skipped; skipped signals usually mean missing cloud configuration (e.g. AWS_REGION, inference profile, etc).
|
|
407
411
|
|
|
408
412
|
This tool is designed for UI regression checks, design parity checks, and "does this page still match the intended layout?" validation.
|
|
409
|
-
`.trim()}isEnabled(){return!!FIGMA_ACCESS_TOKEN?.trim()}inputSchema(){return{figmaFileKey:z19.string().min(1).describe("Figma file key (from URL: part after /file/)."),figmaNodeId:z19.string().min(1).describe("Figma node id (frame/component, e.g. 12:34)."),selector:z19.string().optional().describe("Compare only this region; omit for full page."),fullPage:z19.boolean().optional().default(DEFAULT_FULL_PAGE).describe("Full scrollable page; ignored when selector set."),figmaScale:z19.number().int().positive().optional().describe("Scale for Figma raster (e.g. 1, 2)."),figmaFormat:z19.enum(["png","jpg"]).optional(),weights:z19.object({mssim:z19.number().positive().optional(),imageEmbedding:z19.number().positive().optional(),textEmbedding:z19.number().positive().optional()}).optional().describe("Weights for combining signals."),mssimMode:z19.enum(["raw","semantic"]).optional().default(DEFAULT_MSSIM_MODE).describe("semantic = more robust for real data vs design."),maxDim:z19.number().int().positive().optional().describe("Max dimension for comparison."),jpegQuality:z19.number().int().min(50).max(100).optional().describe("JPEG quality 50\u2013100.")}}outputSchema(){return{score:z19.number().describe("Combined similarity score in the range [0..1]. Higher means more similar."),notes:z19.array(z19.string()).describe("Human-readable notes explaining which signals were used and their individual scores."),meta:z19.object({pageUrl:z19.string().describe("URL of the page that was compared."),pageTitle:z19.string().describe("Title of the page that was compared."),figmaFileKey:z19.string().describe("Figma file key used for the design snapshot."),figmaNodeId:z19.string().describe("Figma node id used for the design snapshot."),selector:z19.string().nullable().describe("Selector used for page screenshot, if any. Null means full page."),fullPage:z19.boolean().describe("Whether the page screenshot was full-page."),pageImageType:z19.enum(["png","jpeg"]).describe("Image type of the captured page screenshot."),figmaImageType:z19.enum(["png","jpeg"]).describe("Image type of the captured Figma snapshot.")}).describe("Metadata about what was compared.")}}async handle(context,args){let pageUrl=String(context.page.url()),pageTitle=String(await context.page.title()),figmaFormat=args.figmaFormat??"png",figmaScale=typeof args.figmaScale=="number"?args.figmaScale:void 0,figmaSnapshot=await getFigmaDesignScreenshot({fileKey:args.figmaFileKey,nodeId:args.figmaNodeId,format:figmaFormat,scale:figmaScale}),pagePng;if(typeof args.selector=="string"&&args.selector.trim()){let selector=args.selector.trim(),locator=context.page.locator(selector);if(await locator.count()===0)throw new Error(`Element not found for selector: ${selector}`);pagePng=await locator.first().screenshot({type:DEFAULT_SCREENSHOT_TYPE2})}else{let fullPage=args.fullPage!==!1;pagePng=await context.page.screenshot({type:DEFAULT_SCREENSHOT_TYPE2,fullPage})}let pageSs={image:pagePng,type:"png",name:"page"},figmaSs={image:figmaSnapshot.image,type:figmaSnapshot.type==="jpeg"?"jpeg":"png",name:"figma"},result=await compareWithNotes(pageSs,figmaSs,{weights:args.weights?{mssim:args.weights.mssim,vectorEmbedding:args.weights.imageEmbedding,textEmbedding:args.weights.textEmbedding}:void 0,mssim:{mode:args.mssimMode??DEFAULT_MSSIM_MODE},imageEmbedding:{maxDim:args.maxDim,jpegQuality:typeof args.jpegQuality=="number"?args.jpegQuality:void 0},textEmbedding:{maxDim:args.maxDim,jpegQuality:typeof args.jpegQuality=="number"?args.jpegQuality:void 0}});return{score:result.score,notes:result.notes,meta:{pageUrl,pageTitle,figmaFileKey:args.figmaFileKey,figmaNodeId:args.figmaNodeId,selector:typeof args.selector=="string"&&args.selector.trim()?args.selector.trim():null,fullPage:typeof args.selector=="string"&&args.selector.trim()?!1:args.fullPage!==!1,pageImageType:"png",figmaImageType:figmaSnapshot.type}}}};var tools4=[new ComparePageWithDesign];async function waitForNavigationAndNetworkIdle(context,navPromise,triggerPromise,options){let timeoutMs=options.timeoutMs,deadlineMs=Date.now()+timeoutMs;try{await Promise.all([navPromise,triggerPromise])}catch(err){let message=err instanceof Error?err.message:String(err);if(/timeout|Timeout|TIMEOUT/.test(message)||message.includes("Navigation")){let hint=options.timeoutHint??"Ensure the action triggers a page load (e.g. link or submit).";throw new Error(`Navigation timed out (${timeoutMs}ms). ${hint} Original: ${message}`)}throw err}let stabilizationMs=Math.min(150,Math.max(0,deadlineMs-Date.now()));stabilizationMs>0&&await new Promise(resolve=>{setTimeout(resolve,stabilizationMs)});let lastNotIdleMs=Date.now();for(;;){let nowMs=Date.now();if(context.numOfInFlightRequests()>0&&(lastNotIdleMs=nowMs),nowMs-lastNotIdleMs>=500||nowMs>=deadlineMs)break;await new Promise(resolve=>{setTimeout(resolve,50)})}}async function waitForNetworkIdle(context,options){let timeoutMs=options.timeoutMs,deadlineMs=Date.now()+timeoutMs,stabilizationMs=Math.min(150,Math.max(0,deadlineMs-Date.now()));stabilizationMs>0&&await new Promise(resolve=>{setTimeout(resolve,stabilizationMs)});let lastNotIdleMs=Date.now();for(;;){let nowMs=Date.now();if(context.numOfInFlightRequests()>0&&(lastNotIdleMs=nowMs),nowMs-lastNotIdleMs>=500||nowMs>=deadlineMs)break;await new Promise(resolve=>{setTimeout(resolve,50)})}}import{z as z20}from"zod";var DEFAULT_SELECTOR_TIMEOUT_MS=1e4,DEFAULT_WAIT_FOR_TIMEOUT_MS=3e4,Click=class{name(){return"interaction_click"}description(){return"Clicks an element. Accepts selector or ref (e.g. e1, @e1). Set waitForNavigation: true when the click opens a new page \u2014 waits for navigation then for network idle so snapshot/screenshot see full content."}inputSchema(){return{selector:z20.string().describe("CSS selector or ref from a11y snapshot (e.g. e1, @e1)."),timeoutMs:z20.number().int().positive().optional().describe("Wait for element, ms. Default 10000."),waitForNavigation:z20.boolean().optional().default(!1).describe("Wait for navigation triggered by click (parallel with click). Use when click opens a new page."),waitForTimeoutMs:z20.number().int().min(0).optional().default(DEFAULT_WAIT_FOR_TIMEOUT_MS).describe("Timeout for navigation and for network idle wait (ms). Only when waitForNavigation is true. Default 30000.")}}outputSchema(){return{}}async handle(context,args){assertRefRole(context,args.selector,REF_ROLES_INTERACTIVE,"click","Use a ref for an interactive element (button, link, etc.) from the latest a11y_take-aria-snapshot. Refs are page-specific; re-snapshot after navigation.");let timeout=args.timeoutMs??DEFAULT_SELECTOR_TIMEOUT_MS,locator=resolveSelectorOrRef(context,args.selector);if(args.waitForNavigation){let waitForTimeoutMs=args.waitForTimeoutMs??DEFAULT_WAIT_FOR_TIMEOUT_MS,navPromise=context.page.waitForNavigation({waitUntil:"load",timeout:waitForTimeoutMs}),triggerPromise=locator.click({timeout});await waitForNavigationAndNetworkIdle(context,navPromise,triggerPromise,{timeoutMs:waitForTimeoutMs,timeoutHint:"Ensure the click triggers a page load (e.g. link or submit)."})}else await locator.click({timeout});return{}}};import{z as z21}from"zod";var DEFAULT_SELECTOR_TIMEOUT_MS2=1e4,Drag=class{name(){return"interaction_drag"}description(){return"Drags an element to a target location. Accepts CSS selectors or refs (e.g. e1, @e1) from the last ARIA snapshot."}inputSchema(){return{sourceSelector:z21.string().describe("CSS selector or ref for the element to drag."),targetSelector:z21.string().describe("CSS selector or ref for the drop target."),timeoutMs:z21.number().int().positive().optional().describe("Wait for elements, ms. Default 10000.")}}outputSchema(){return{}}async handle(context,args){assertRefRole(context,args.sourceSelector,REF_ROLES_INTERACTIVE,"drag (source)","Use a ref for a draggable element from the latest a11y_take-aria-snapshot. Refs are page-specific; re-snapshot after navigation.");let timeout=args.timeoutMs??DEFAULT_SELECTOR_TIMEOUT_MS2,sourceLocator=resolveSelectorOrRef(context,args.sourceSelector),targetLocator=resolveSelectorOrRef(context,args.targetSelector),sourceBound=await sourceLocator.boundingBox({timeout}),targetBound=await targetLocator.boundingBox({timeout});if(!sourceBound||!targetBound)throw new Error("Could not get element positions for drag operation");return await context.page.mouse.move(sourceBound.x+sourceBound.width/2,sourceBound.y+sourceBound.height/2),await context.page.mouse.down(),await context.page.mouse.move(targetBound.x+targetBound.width/2,targetBound.y+targetBound.height/2),await context.page.mouse.up(),{}}};import{z as z22}from"zod";var DEFAULT_SELECTOR_TIMEOUT_MS3=1e4,FILLABLE_ROLES=new Set(["textbox","searchbox","spinbutton","combobox"]),Fill=class{name(){return"interaction_fill"}description(){return"Fills out an input field. Accepts a CSS selector or a ref from the last ARIA snapshot (e.g. e1, @e1)."}inputSchema(){return{selector:z22.string().describe("CSS selector or ref from a11y snapshot for the input."),value:z22.string(),timeoutMs:z22.number().int().positive().optional().describe("Wait for element, ms. Default 10000.")}}outputSchema(){return{}}async handle(context,args){assertRefRole(context,args.selector,FILLABLE_ROLES,"fill","Use a ref for a textbox, searchbox, or input (e.g. from the latest a11y_take-aria-snapshot on this page). Refs are page-specific; re-snapshot after navigation.");let timeout=args.timeoutMs??DEFAULT_SELECTOR_TIMEOUT_MS3;return await resolveSelectorOrRef(context,args.selector).fill(args.value,{timeout}),{}}};import{z as z23}from"zod";var DEFAULT_SELECTOR_TIMEOUT_MS4=1e4,Hover=class{name(){return"interaction_hover"}description(){return"Hovers an element on the page. Accepts a CSS selector or a ref from the last ARIA snapshot (e.g. e1, @e1)."}inputSchema(){return{selector:z23.string().describe("CSS selector or ref from a11y snapshot."),timeoutMs:z23.number().int().positive().optional().describe("Wait for element, ms. Default 10000.")}}outputSchema(){return{}}async handle(context,args){assertRefRole(context,args.selector,REF_ROLES_INTERACTIVE,"hover","Use a ref for an interactive element from the latest a11y_take-aria-snapshot. Refs are page-specific; re-snapshot after navigation.");let timeout=args.timeoutMs??DEFAULT_SELECTOR_TIMEOUT_MS4;return await resolveSelectorOrRef(context,args.selector).hover({timeout}),{}}};import{z as z24}from"zod";var DEFAULT_REPEAT_INTERVAL_MS=50,MIN_REPEAT_INTERVAL_MS=10,DEFAULT_SELECTOR_TIMEOUT_MS5=1e4,PressKey=class{name(){return"interaction_press-key"}description(){return`
|
|
413
|
+
`.trim()}isEnabled(){return!!FIGMA_ACCESS_TOKEN?.trim()}inputSchema(){return{figmaFileKey:z19.string().min(1).describe("Figma file key (from URL: part after /file/)."),figmaNodeId:z19.string().min(1).describe("Figma node id (frame/component, e.g. 12:34)."),selector:z19.string().optional().describe("Compare only this region; omit for full page."),fullPage:z19.boolean().optional().default(DEFAULT_FULL_PAGE).describe("Full scrollable page; ignored when selector set."),figmaScale:z19.number().int().positive().optional().describe("Scale for Figma raster (e.g. 1, 2)."),figmaFormat:z19.enum(["png","jpg"]).optional(),weights:z19.object({mssim:z19.number().positive().optional(),imageEmbedding:z19.number().positive().optional(),textEmbedding:z19.number().positive().optional()}).optional().describe("Weights for combining signals."),mssimMode:z19.enum(["raw","semantic"]).optional().default(DEFAULT_MSSIM_MODE).describe("semantic = more robust for real data vs design."),maxDim:z19.number().int().positive().optional().describe("Max dimension for comparison."),jpegQuality:z19.number().int().min(50).max(100).optional().describe("JPEG quality 50\u2013100.")}}outputSchema(){return{score:z19.number().describe("Combined similarity score in the range [0..1]. Higher means more similar."),notes:z19.array(z19.string()).describe("Human-readable notes explaining which signals were used and their individual scores."),meta:z19.object({pageUrl:z19.string().describe("URL of the page that was compared."),pageTitle:z19.string().describe("Title of the page that was compared."),figmaFileKey:z19.string().describe("Figma file key used for the design snapshot."),figmaNodeId:z19.string().describe("Figma node id used for the design snapshot."),selector:z19.string().nullable().describe("Selector used for page screenshot, if any. Null means full page."),fullPage:z19.boolean().describe("Whether the page screenshot was full-page."),pageImageType:z19.enum(["png","jpeg"]).describe("Image type of the captured page screenshot."),figmaImageType:z19.enum(["png","jpeg"]).describe("Image type of the captured Figma snapshot.")}).describe("Metadata about what was compared.")}}async handle(context,args){let pageUrl=String(context.page.url()),pageTitle=String(await context.page.title()),figmaFormat=args.figmaFormat??"png",figmaScale=typeof args.figmaScale=="number"?args.figmaScale:void 0,figmaSnapshot=await getFigmaDesignScreenshot({fileKey:args.figmaFileKey,nodeId:args.figmaNodeId,format:figmaFormat,scale:figmaScale}),pagePng;if(typeof args.selector=="string"&&args.selector.trim()){let selector=args.selector.trim(),locator=context.page.locator(selector);if(await locator.count()===0)throw new Error(`Element not found for selector: ${selector}`);pagePng=await locator.first().screenshot({type:DEFAULT_SCREENSHOT_TYPE2})}else{let fullPage=args.fullPage!==!1;pagePng=await context.page.screenshot({type:DEFAULT_SCREENSHOT_TYPE2,fullPage})}let pageSs={image:pagePng,type:"png",name:"page"},figmaSs={image:figmaSnapshot.image,type:figmaSnapshot.type==="jpeg"?"jpeg":"png",name:"figma"},result=await compareWithNotes(pageSs,figmaSs,{weights:args.weights?{mssim:args.weights.mssim,vectorEmbedding:args.weights.imageEmbedding,textEmbedding:args.weights.textEmbedding}:void 0,mssim:{mode:args.mssimMode??DEFAULT_MSSIM_MODE},imageEmbedding:{maxDim:args.maxDim,jpegQuality:typeof args.jpegQuality=="number"?args.jpegQuality:void 0},textEmbedding:{maxDim:args.maxDim,jpegQuality:typeof args.jpegQuality=="number"?args.jpegQuality:void 0}});return{score:result.score,notes:result.notes,meta:{pageUrl,pageTitle,figmaFileKey:args.figmaFileKey,figmaNodeId:args.figmaNodeId,selector:typeof args.selector=="string"&&args.selector.trim()?args.selector.trim():null,fullPage:typeof args.selector=="string"&&args.selector.trim()?!1:args.fullPage!==!1,pageImageType:"png",figmaImageType:figmaSnapshot.type}}}};var tools4=[new ComparePageWithDesign];async function waitForNavigationAndNetworkIdle(context,navPromise,triggerPromise,options){let timeoutMs=options.timeoutMs,deadlineMs=Date.now()+timeoutMs;try{await Promise.all([navPromise,triggerPromise])}catch(err){let message=err instanceof Error?err.message:String(err);if(/timeout|Timeout|TIMEOUT/.test(message)||message.includes("Navigation")){let hint=options.timeoutHint??"Ensure the action triggers a page load (e.g. link or submit).";throw new Error(`Navigation timed out (${timeoutMs}ms). ${hint} Original: ${message}`)}throw err}let stabilizationMs=Math.min(150,Math.max(0,deadlineMs-Date.now()));stabilizationMs>0&&await new Promise(resolve=>{setTimeout(resolve,stabilizationMs)});let lastNotIdleMs=Date.now();for(;;){let nowMs=Date.now();if(context.numOfInFlightRequests()>0&&(lastNotIdleMs=nowMs),nowMs-lastNotIdleMs>=500||nowMs>=deadlineMs)break;await new Promise(resolve=>{setTimeout(resolve,50)})}}async function waitForNetworkIdle(context,options){let timeoutMs=options.timeoutMs,deadlineMs=Date.now()+timeoutMs,stabilizationMs=Math.min(150,Math.max(0,deadlineMs-Date.now()));stabilizationMs>0&&await new Promise(resolve=>{setTimeout(resolve,stabilizationMs)});let lastNotIdleMs=Date.now();for(;;){let nowMs=Date.now();if(context.numOfInFlightRequests()>0&&(lastNotIdleMs=nowMs),nowMs-lastNotIdleMs>=500)break;let latestNetworkActivity=context.lastNetworkActivityTimestamp();if(nowMs-latestNetworkActivity>5e3||nowMs>=deadlineMs)break;await new Promise(resolve=>{setTimeout(resolve,50)})}}import{z as z20}from"zod";var DEFAULT_SELECTOR_TIMEOUT_MS=1e4,DEFAULT_WAIT_FOR_TIMEOUT_MS=3e4,Click=class{name(){return"interaction_click"}description(){return"Clicks an element. Accepts selector or ref (e.g. e1, @e1). Set waitForNavigation: true when the click opens a new page \u2014 waits for navigation then for network idle so snapshot/screenshot see full content."}inputSchema(){return{selector:z20.string().describe("CSS selector or ref from a11y snapshot (e.g. e1, @e1)."),timeoutMs:z20.number().int().positive().optional().describe("Wait for element, ms. Default 10000."),waitForNavigation:z20.boolean().optional().default(!1).describe("Wait for navigation triggered by click (parallel with click). Use when click opens a new page."),waitForTimeoutMs:z20.number().int().min(0).optional().default(DEFAULT_WAIT_FOR_TIMEOUT_MS).describe("Timeout for navigation and for network idle wait (ms). Only when waitForNavigation is true. Default 30000.")}}outputSchema(){return{}}async handle(context,args){assertRefRole(context,args.selector,REF_ROLES_INTERACTIVE,"click","Use a ref for an interactive element (button, link, etc.) from the latest a11y_take-aria-snapshot. Refs are page-specific; re-snapshot after navigation.");let timeout=args.timeoutMs??DEFAULT_SELECTOR_TIMEOUT_MS,locator=resolveSelectorOrRef(context,args.selector);if(args.waitForNavigation){let waitForTimeoutMs=args.waitForTimeoutMs??DEFAULT_WAIT_FOR_TIMEOUT_MS,navPromise=context.page.waitForNavigation({waitUntil:"load",timeout:waitForTimeoutMs}),triggerPromise=locator.click({timeout});await waitForNavigationAndNetworkIdle(context,navPromise,triggerPromise,{timeoutMs:waitForTimeoutMs,timeoutHint:"Ensure the click triggers a page load (e.g. link or submit)."})}else await locator.click({timeout});return{}}};import{z as z21}from"zod";var DEFAULT_SELECTOR_TIMEOUT_MS2=1e4,Drag=class{name(){return"interaction_drag"}description(){return"Drags an element to a target location. Accepts CSS selectors or refs (e.g. e1, @e1) from the last ARIA snapshot."}inputSchema(){return{sourceSelector:z21.string().describe("CSS selector or ref for the element to drag."),targetSelector:z21.string().describe("CSS selector or ref for the drop target."),timeoutMs:z21.number().int().positive().optional().describe("Wait for elements, ms. Default 10000.")}}outputSchema(){return{}}async handle(context,args){assertRefRole(context,args.sourceSelector,REF_ROLES_INTERACTIVE,"drag (source)","Use a ref for a draggable element from the latest a11y_take-aria-snapshot. Refs are page-specific; re-snapshot after navigation.");let timeout=args.timeoutMs??DEFAULT_SELECTOR_TIMEOUT_MS2,sourceLocator=resolveSelectorOrRef(context,args.sourceSelector),targetLocator=resolveSelectorOrRef(context,args.targetSelector),sourceBound=await sourceLocator.boundingBox({timeout}),targetBound=await targetLocator.boundingBox({timeout});if(!sourceBound||!targetBound)throw new Error("Could not get element positions for drag operation");return await context.page.mouse.move(sourceBound.x+sourceBound.width/2,sourceBound.y+sourceBound.height/2),await context.page.mouse.down(),await context.page.mouse.move(targetBound.x+targetBound.width/2,targetBound.y+targetBound.height/2),await context.page.mouse.up(),{}}};import{z as z22}from"zod";var DEFAULT_SELECTOR_TIMEOUT_MS3=1e4,FILLABLE_ROLES=new Set(["textbox","searchbox","spinbutton","combobox"]),Fill=class{name(){return"interaction_fill"}description(){return"Fills out an input field. Accepts a CSS selector or a ref from the last ARIA snapshot (e.g. e1, @e1)."}inputSchema(){return{selector:z22.string().describe("CSS selector or ref from a11y snapshot for the input."),value:z22.string(),timeoutMs:z22.number().int().positive().optional().describe("Wait for element, ms. Default 10000.")}}outputSchema(){return{}}async handle(context,args){assertRefRole(context,args.selector,FILLABLE_ROLES,"fill","Use a ref for a textbox, searchbox, or input (e.g. from the latest a11y_take-aria-snapshot on this page). Refs are page-specific; re-snapshot after navigation.");let timeout=args.timeoutMs??DEFAULT_SELECTOR_TIMEOUT_MS3;return await resolveSelectorOrRef(context,args.selector).fill(args.value,{timeout}),{}}};import{z as z23}from"zod";var DEFAULT_SELECTOR_TIMEOUT_MS4=1e4,Hover=class{name(){return"interaction_hover"}description(){return"Hovers an element on the page. Accepts a CSS selector or a ref from the last ARIA snapshot (e.g. e1, @e1)."}inputSchema(){return{selector:z23.string().describe("CSS selector or ref from a11y snapshot."),timeoutMs:z23.number().int().positive().optional().describe("Wait for element, ms. Default 10000.")}}outputSchema(){return{}}async handle(context,args){assertRefRole(context,args.selector,REF_ROLES_INTERACTIVE,"hover","Use a ref for an interactive element from the latest a11y_take-aria-snapshot. Refs are page-specific; re-snapshot after navigation.");let timeout=args.timeoutMs??DEFAULT_SELECTOR_TIMEOUT_MS4;return await resolveSelectorOrRef(context,args.selector).hover({timeout}),{}}};import{z as z24}from"zod";var DEFAULT_REPEAT_INTERVAL_MS=50,MIN_REPEAT_INTERVAL_MS=10,DEFAULT_SELECTOR_TIMEOUT_MS5=1e4,PressKey=class{name(){return"interaction_press-key"}description(){return`
|
|
410
414
|
Presses a keyboard key with optional "hold" and auto-repeat behavior.
|
|
411
415
|
|
|
412
416
|
Key facts:
|
|
@@ -459,7 +463,7 @@ Use this tool to:
|
|
|
459
463
|
- Jump to the top/bottom without knowing exact positions
|
|
460
464
|
- Bring elements into view before clicking
|
|
461
465
|
- Inspect lazy-loaded content that appears on scroll
|
|
462
|
-
`.trim()}inputSchema(){return{mode:z28.enum(["by","to","top","bottom","left","right"]).optional().default(DEFAULT_MODE).describe("by=dx,dy; to=x,y; or edge (top/bottom/left/right)."),selector:z28.string().optional().describe("Scroll this container; omit for viewport."),dx:z28.number().optional(),dy:z28.number().optional(),x:z28.number().optional(),y:z28.number().optional(),behavior:z28.enum(["auto","smooth"]).optional().default(DEFAULT_BEHAVIOR)}}outputSchema(){return{mode:z28.enum(["by","to","top","bottom","left","right"]).describe("The scroll mode used."),selector:z28.string().nullable().describe("The selector of the scroll container if provided; otherwise null (document viewport)."),behavior:z28.enum(["auto","smooth"]).describe("The scroll behavior used."),before:z28.object({x:z28.number().describe("ScrollLeft before scrolling."),y:z28.number().describe("ScrollTop before scrolling."),scrollWidth:z28.number().describe("Total scrollable width before scrolling."),scrollHeight:z28.number().describe("Total scrollable height before scrolling."),clientWidth:z28.number().describe("Viewport/container client width before scrolling."),clientHeight:z28.number().describe("Viewport/container client height before scrolling.")}).describe("Scroll metrics before the scroll action."),after:z28.object({x:z28.number().describe("ScrollLeft after scrolling."),y:z28.number().describe("ScrollTop after scrolling."),scrollWidth:z28.number().describe("Total scrollable width after scrolling."),scrollHeight:z28.number().describe("Total scrollable height after scrolling."),clientWidth:z28.number().describe("Viewport/container client width after scrolling."),clientHeight:z28.number().describe("Viewport/container client height after scrolling.")}).describe("Scroll metrics after the scroll action."),canScrollX:z28.boolean().describe("Whether horizontal scrolling is possible (scrollWidth > clientWidth)."),canScrollY:z28.boolean().describe("Whether vertical scrolling is possible (scrollHeight > clientHeight)."),maxScrollX:z28.number().describe("Maximum horizontal scrollLeft (scrollWidth - clientWidth)."),maxScrollY:z28.number().describe("Maximum vertical scrollTop (scrollHeight - clientHeight)."),isAtLeft:z28.boolean().describe("Whether the scroll position is at the far left."),isAtRight:z28.boolean().describe("Whether the scroll position is at the far right."),isAtTop:z28.boolean().describe("Whether the scroll position is at the very top."),isAtBottom:z28.boolean().describe("Whether the scroll position is at the very bottom.")}}async handle(context,args){let mode=args.mode??DEFAULT_MODE,selector=args.selector,behavior=args.behavior??DEFAULT_BEHAVIOR,dx=args.dx??0,dy=args.dy??0,x=args.x,y=args.y;if(mode==="to"&&typeof x!="number"&&typeof y!="number")throw new Error('mode="to" requires at least one of x or y.');if(mode==="by"&&dx===0&&dy===0)throw new Error('mode="by" requires dx and/or dy to be non-zero.');let params={modeEval:mode,selectorEval:selector,dxEval:dx,dyEval:dy,xEval:x,yEval:y,behaviorEval:behavior},result;return selector?result=await resolveSelectorOrRef(context,selector).evaluate((el,p)=>{let before={x:el.scrollLeft,y:el.scrollTop,scrollWidth:el.scrollWidth,scrollHeight:el.scrollHeight,clientWidth:el.clientWidth,clientHeight:el.clientHeight},readMetrics=elem2=>({x:elem2.scrollLeft,y:elem2.scrollTop,scrollWidth:elem2.scrollWidth,scrollHeight:elem2.scrollHeight,clientWidth:elem2.clientWidth,clientHeight:elem2.clientHeight}),clamp=(v,min,max)=>Math.max(min,Math.min(max,v)),elem=el,maxX=Math.max(0,elem.scrollWidth-elem.clientWidth),maxY=Math.max(0,elem.scrollHeight-elem.clientHeight);p.modeEval==="by"?elem.scrollTo({left:clamp(elem.scrollLeft+p.dxEval,0,maxX),top:clamp(elem.scrollTop+p.dyEval,0,maxY),behavior:p.behaviorEval}):p.modeEval==="to"?elem.scrollTo({left:typeof p.xEval=="number"?clamp(p.xEval,0,maxX):elem.scrollLeft,top:typeof p.yEval=="number"?clamp(p.yEval,0,maxY):elem.scrollTop,behavior:p.behaviorEval}):p.modeEval==="top"?elem.scrollTo({top:0,left:elem.scrollLeft,behavior:p.behaviorEval}):p.modeEval==="bottom"?elem.scrollTo({top:maxY,left:elem.scrollLeft,behavior:p.behaviorEval}):p.modeEval==="left"?elem.scrollTo({left:0,top:elem.scrollTop,behavior:p.behaviorEval}):p.modeEval==="right"&&elem.scrollTo({left:maxX,top:elem.scrollTop,behavior:p.behaviorEval});let after=readMetrics(elem);return{before,after,canScrollX:after.scrollWidth>after.clientWidth,canScrollY:after.scrollHeight>after.clientHeight,maxScrollX:Math.max(0,after.scrollWidth-after.clientWidth),maxScrollY:Math.max(0,after.scrollHeight-after.clientHeight),isAtLeft:after.x<=1,isAtRight:after.x>=Math.max(0,after.scrollWidth-after.clientWidth)-1,isAtTop:after.y<=1,isAtBottom:after.y>=Math.max(0,after.scrollHeight-after.clientHeight)-1}},params):result=await context.page.evaluate(params2=>{let modeEval=params2.modeEval,selectorEval=params2.selectorEval,dxEval=params2.dxEval,dyEval=params2.dyEval,xEval=params2.xEval,yEval=params2.yEval,behaviorEval=params2.behaviorEval,getTarget=()=>{if(selectorEval){let el=document.querySelector(selectorEval);if(!el)throw new Error(`Element with selector "${selectorEval}" not found`);return el}let scrolling=document.scrollingElement||document.documentElement||document.body;if(!scrolling)throw new Error("No scrolling element available.");return scrolling},readMetrics=el=>({x:el.scrollLeft,y:el.scrollTop,scrollWidth:el.scrollWidth,scrollHeight:el.scrollHeight,clientWidth:el.clientWidth,clientHeight:el.clientHeight}),clamp=(v,min,max)=>v<min?min:v>max?max:v,doScroll=el=>{let maxX=Math.max(0,el.scrollWidth-el.clientWidth),maxY=Math.max(0,el.scrollHeight-el.clientHeight);if(modeEval==="by"){let nextX=clamp(el.scrollLeft+dxEval,0,maxX),nextY=clamp(el.scrollTop+dyEval,0,maxY);el.scrollTo({left:nextX,top:nextY,behavior:behaviorEval});return}if(modeEval==="to"){let nextX=typeof xEval=="number"?clamp(xEval,0,maxX):el.scrollLeft,nextY=typeof yEval=="number"?clamp(yEval,0,maxY):el.scrollTop;el.scrollTo({left:nextX,top:nextY,behavior:behaviorEval});return}if(modeEval==="top"){el.scrollTo({top:0,left:el.scrollLeft,behavior:behaviorEval});return}if(modeEval==="bottom"){el.scrollTo({top:maxY,left:el.scrollLeft,behavior:behaviorEval});return}if(modeEval==="left"){el.scrollTo({left:0,top:el.scrollTop,behavior:behaviorEval});return}if(modeEval==="right"){el.scrollTo({left:maxX,top:el.scrollTop,behavior:behaviorEval});return}},target=getTarget(),before=readMetrics(target);doScroll(target);let after=readMetrics(target),maxScrollX=Math.max(0,after.scrollWidth-after.clientWidth),maxScrollY=Math.max(0,after.scrollHeight-after.clientHeight),canScrollX=after.scrollWidth>after.clientWidth,canScrollY=after.scrollHeight>after.clientHeight,eps=1,isAtLeft=after.x<=eps,isAtRight=after.x>=maxScrollX-eps,isAtTop=after.y<=eps,isAtBottom=after.y>=maxScrollY-eps;return{before,after,canScrollX,canScrollY,maxScrollX,maxScrollY,isAtLeft,isAtRight,isAtTop,isAtBottom}},params),{mode,selector:selector??null,behavior,before:result.before,after:result.after,canScrollX:result.canScrollX,canScrollY:result.canScrollY,maxScrollX:result.maxScrollX,maxScrollY:result.maxScrollY,isAtLeft:result.isAtLeft,isAtRight:result.isAtRight,isAtTop:result.isAtTop,isAtBottom:result.isAtBottom}}};var tools5=[new Click,new Drag,new Fill,new Hover,new PressKey,new ResizeViewport,new ResizeWindow,new Select,new Scroll];import fs4 from"node:fs/promises";import os3 from"node:os";import path4 from"node:path";import{z as z29}from"zod";var DEFAULT_SCREENSHOT_NAME2="screenshot",DEFAULT_TIMEOUT_MS=0,DEFAULT_WAIT_FOR_TIMEOUT_MS2=3e4,DEFAULT_WAIT_UNTIL="load",DIRECTION_VALUES=["back","forward"],refMapEntrySchema2=z29.object({role:z29.string(),name:z29.string().optional(),selector:z29.string(),nth:z29.number().optional()}),GoBackOrForward=class{name(){return"navigation_go-back-or-forward"}description(){return'\nNavigates to the previous or next page in history.\n- `direction: "back"` \u2014 previous page in history.\n- `direction: "forward"` \u2014 next page in history.\n\nIn case of multiple redirects, the navigation will resolve with the response of the last redirect.\nIf cannot go back/forward, returns empty response.\n\nBy default (includeSnapshot: true), an ARIA snapshot with refs is returned. Use `snapshotOptions` for `interactiveOnly` (default false) and `cursorInteractive` (default false), same as a11y_take-aria-snapshot.\n\nWhen `includeScreenshot: true`, the screenshot is always saved to disk; `screenshotFilePath` is returned. By default `outputPath` is the OS temp dir and `name` is "screenshot" (same as `content_take-screenshot`). Use `screenshotOptions.includeBase64: true` only when the file cannot be read from the returned path (e.g. remote, container).\n '}inputSchema(){return{direction:z29.enum(DIRECTION_VALUES),timeout:z29.number().int().nonnegative().optional().default(DEFAULT_TIMEOUT_MS).describe("Max wait ms. 0=no timeout."),waitUntil:z29.enum(["load","domcontentloaded","networkidle","commit"]).optional().default(DEFAULT_WAIT_UNTIL).describe("Resolve when this load event fires."),waitForNavigation:z29.boolean().optional().default(!0).describe("Wait for navigation then for network idle before snapshot/screenshot. Default true."),waitForTimeoutMs:z29.number().int().min(0).optional().default(DEFAULT_WAIT_FOR_TIMEOUT_MS2).describe("Timeout for navigation and network idle wait (ms). Only when waitForNavigation is true. Default 30000."),includeSnapshot:z29.boolean().optional().default(!0).describe("Return ARIA snapshot with refs."),snapshotOptions:z29.object({interactiveOnly:z29.boolean().optional().default(!1).describe("Only interactive elements get refs. Default false (content roles like headings also included)."),cursorInteractive:z29.boolean().optional().default(!1).describe("Include cursor:pointer / onclick elements. Default false.")}).optional().describe("Options when includeSnapshot is true. Same as a11y_take-aria-snapshot."),includeScreenshot:z29.boolean().optional().default(!1).describe("Take a screenshot after navigation; saved to disk (default: OS temp dir). Use includeBase64 only when file cannot be read from path."),screenshotOptions:z29.object({outputPath:z29.string().optional().default(os3.tmpdir()).describe("Directory to save the screenshot. Default: OS temp dir."),name:z29.string().optional().default(DEFAULT_SCREENSHOT_NAME2).describe('Base name for the screenshot file. Default: "screenshot".'),fullPage:z29.boolean().optional().default(!0).describe("Capture full scrollable page. Default: true."),type:z29.enum(["png","jpeg"]).optional().default("png").describe("Image format. Default: png."),annotate:z29.boolean().optional().default(!0).describe("Overlay ARIA ref labels on the screenshot. Default: true."),includeBase64:z29.boolean().optional().default(!1).describe("Include base64 image in response; use only when file cannot be read from path (e.g. remote, container).")}).optional().describe('Options when includeScreenshot is true. outputPath/name default to tmp and "screenshot".')}}outputSchema(){return{url:z29.string().describe("Contains the URL of the navigated page.").optional(),status:z29.number().int().positive().describe("Contains the status code of the navigated page (e.g., 200 for a success).").optional(),statusText:z29.string().describe('Contains the status text of the navigated page (e.g. usually an "OK" for a success).').optional(),ok:z29.boolean().describe("Contains a boolean stating whether the navigated page was successful (status in the range 200-299) or not.").optional(),screenshotFilePath:z29.string().optional().describe("When includeScreenshot is true: full path of the saved screenshot file."),output:z29.string().optional(),refs:z29.record(z29.string(),refMapEntrySchema2).optional(),image:z29.object({data:z29.any().describe("Base64-encoded image data."),mimeType:z29.string().describe("MIME type of the image.")}).optional().describe("When includeScreenshot and screenshotOptions.includeBase64 are true: image sent as separate image content part.")}}async handle(context,args){let response=args.direction==="back"?await context.page.goBack({timeout:args.timeout,waitUntil:args.waitUntil}):await context.page.goForward({timeout:args.timeout,waitUntil:args.waitUntil});if(args.waitForNavigation!==!1){let waitForTimeoutMs=args.waitForTimeoutMs??DEFAULT_WAIT_FOR_TIMEOUT_MS2;await waitForNetworkIdle(context,{timeoutMs:waitForTimeoutMs})}let base={url:response?.url(),status:response?.status(),statusText:response?.statusText(),ok:response?.ok()},output=base;if(args.includeSnapshot!==!1)try{let snapOpts=args.snapshotOptions??{},snapshot=await takeAriaSnapshotWithRefs(context,{interactiveOnly:snapOpts.interactiveOnly??!1,cursorInteractive:snapOpts.cursorInteractive??!1});output={...base,output:snapshot.output,refs:snapshot.refs}}catch{}let screenshotFilePath,image;if(args.includeScreenshot===!0){let opts=args.screenshotOptions??{},outputPath=opts.outputPath??os3.tmpdir(),name=opts.name??DEFAULT_SCREENSHOT_NAME2;try{let screenshotType=(opts.type??"png")==="jpeg"?"jpeg":"png",shot=await captureScreenshot(context,{fullPage:opts.fullPage??!0,type:screenshotType,annotate:opts.annotate??!0}),filename=`${name}-${formattedTimeForFilename()}.${shot.screenshotType}`,filePath=path4.resolve(outputPath,filename);await fs4.writeFile(filePath,shot.rawBuffer),screenshotFilePath=filePath,opts.includeBase64===!0&&(image=shot.image)}catch{}}return{url:output.url,status:output.status,statusText:output.statusText,ok:output.ok,...screenshotFilePath!==void 0&&{screenshotFilePath},...output.output!==void 0&&{output:output.output},...output.refs!==void 0&&{refs:output.refs},...image!==void 0&&{image}}}};import fs5 from"node:fs/promises";import os4 from"node:os";import path5 from"node:path";import{z as z30}from"zod";var DEFAULT_SCREENSHOT_NAME3="screenshot",DEFAULT_TIMEOUT_MS2=0,DEFAULT_WAIT_FOR_TIMEOUT_MS3=3e4,DEFAULT_WAIT_UNTIL2="load",refMapEntrySchema3=z30.object({role:z30.string(),name:z30.string().optional(),selector:z30.string(),nth:z30.number().optional()}),GoTo=class{name(){return"navigation_go-to"}description(){return'\nNavigates to the given URL.\n**NOTE**: The tool either throws an error or returns a main resource response. \nThe only exceptions are navigation to `about:blank` or navigation to the same URL with a different hash, \nwhich would succeed and return empty response.\n\n**By default** (`includeSnapshot: true`), an ARIA snapshot with refs is taken after navigation and returned in `output` and `refs`; you can use refs (e1, e2, ...) in interaction tools without calling `a11y_take-aria-snapshot` separately. Use `snapshotOptions` for `interactiveOnly` (default false) and `cursorInteractive` (default false). Set `includeSnapshot: false` to get only url/status/ok.\n\nWhen `includeScreenshot: true`, the screenshot is always saved to disk; `screenshotFilePath` is returned. By default `outputPath` is the OS temp dir and `name` is "screenshot" (same as `content_take-screenshot`). Use `screenshotOptions.includeBase64: true` only when the file cannot be read from the returned path (e.g. remote, container).\n '}inputSchema(){return{url:z30.string(),timeout:z30.number().int().nonnegative().optional().default(DEFAULT_TIMEOUT_MS2).describe("Max wait ms. 0=no timeout."),waitUntil:z30.enum(["load","domcontentloaded","networkidle","commit"]).optional().default(DEFAULT_WAIT_UNTIL2).describe("Resolve when this load event fires."),waitForNavigation:z30.boolean().optional().default(!0).describe("Wait for navigation then for network idle before snapshot/screenshot. Default true."),waitForTimeoutMs:z30.number().int().min(0).optional().default(DEFAULT_WAIT_FOR_TIMEOUT_MS3).describe("Timeout for navigation and network idle wait (ms). Only when waitForNavigation is true. Default 30000."),includeSnapshot:z30.boolean().optional().default(!0).describe("Return ARIA snapshot with refs after nav."),snapshotOptions:z30.object({interactiveOnly:z30.boolean().optional().default(!1).describe("Only interactive elements get refs. Default false (content roles like headings also included)."),cursorInteractive:z30.boolean().optional().default(!1).describe("Include cursor:pointer / onclick elements. Default false.")}).optional().describe("Options when includeSnapshot is true. Same as a11y_take-aria-snapshot."),includeScreenshot:z30.boolean().optional().default(!1).describe("Take a screenshot after navigation; saved to disk (default: OS temp dir). Use includeBase64 only when file cannot be read from path."),screenshotOptions:z30.object({outputPath:z30.string().optional().default(os4.tmpdir()).describe("Directory to save the screenshot. Default: OS temp dir."),name:z30.string().optional().default(DEFAULT_SCREENSHOT_NAME3).describe('Base name for the screenshot file. Default: "screenshot".'),fullPage:z30.boolean().optional().default(!0).describe("Capture full scrollable page. Default: true."),type:z30.enum(["png","jpeg"]).optional().default("png").describe("Image format. Default: png."),annotate:z30.boolean().optional().default(!0).describe("Overlay ARIA ref labels on the screenshot. Default: true."),includeBase64:z30.boolean().optional().default(!1).describe("Include base64 image in response; use only when file cannot be read from path (e.g. remote, container).")}).optional().describe('Options when includeScreenshot is true. outputPath/name default to tmp and "screenshot".')}}outputSchema(){return{url:z30.string().describe("Contains the URL of the navigated page.").optional(),status:z30.number().int().positive().describe("Contains the status code of the navigated page (e.g., 200 for a success).").optional(),statusText:z30.string().describe('Contains the status text of the navigated page (e.g. usually an "OK" for a success).').optional(),ok:z30.boolean().describe("Contains a boolean stating whether the navigated page was successful (status in the range 200-299) or not.").optional(),screenshotFilePath:z30.string().optional().describe("When includeScreenshot is true: full path of the saved screenshot file."),output:z30.string().optional().describe("When includeSnapshot is true: page URL, title, and ARIA tree with refs."),refs:z30.record(z30.string(),refMapEntrySchema3).optional().describe("When includeSnapshot is true: map of ref id (e1, e2, ...) to role/name/selector for use in interaction tools."),image:z30.object({data:z30.any().describe("Base64-encoded image data."),mimeType:z30.string().describe("MIME type of the image.")}).optional().describe("When includeScreenshot and screenshotOptions.includeBase64 are true: image sent as separate image content part.")}}async handle(context,args){let response=await context.page.goto(args.url,{timeout:args.timeout,waitUntil:args.waitUntil});if(args.waitForNavigation!==!1){let waitForTimeoutMs=args.waitForTimeoutMs??DEFAULT_WAIT_FOR_TIMEOUT_MS3;await waitForNetworkIdle(context,{timeoutMs:waitForTimeoutMs})}let base={url:response?.url(),status:response?.status(),statusText:response?.statusText(),ok:response?.ok()},output=base;if(args.includeSnapshot!==!1)try{let snapOpts=args.snapshotOptions??{},snapshot=await takeAriaSnapshotWithRefs(context,{interactiveOnly:snapOpts.interactiveOnly??!1,cursorInteractive:snapOpts.cursorInteractive??!1});output={...base,output:snapshot.output,refs:snapshot.refs}}catch{}let screenshotFilePath,image;if(args.includeScreenshot===!0){let opts=args.screenshotOptions??{},outputPath=opts.outputPath??os4.tmpdir(),name=opts.name??DEFAULT_SCREENSHOT_NAME3;try{let screenshotType=(opts.type??"png")==="jpeg"?"jpeg":"png",shot=await captureScreenshot(context,{fullPage:opts.fullPage??!0,type:screenshotType,annotate:opts.annotate??!0}),filename=`${name}-${formattedTimeForFilename()}.${shot.screenshotType}`,filePath=path5.resolve(outputPath,filename);await fs5.writeFile(filePath,shot.rawBuffer),screenshotFilePath=filePath,opts.includeBase64===!0&&(image=shot.image)}catch{}}return{url:output.url,status:output.status,statusText:output.statusText,ok:output.ok,...screenshotFilePath!==void 0&&{screenshotFilePath},...output.output!==void 0&&{output:output.output},...output.refs!==void 0&&{refs:output.refs},...image!==void 0&&{image}}}};import fs6 from"node:fs/promises";import os5 from"node:os";import path6 from"node:path";import{z as z31}from"zod";var DEFAULT_SCREENSHOT_NAME4="screenshot",DEFAULT_TIMEOUT_MS3=0,DEFAULT_WAIT_FOR_TIMEOUT_MS4=3e4,DEFAULT_WAIT_UNTIL3="load",refMapEntrySchema4=z31.object({role:z31.string(),name:z31.string().optional(),selector:z31.string(),nth:z31.number().optional()}),Reload=class{name(){return"navigation_reload"}description(){return'\nReloads the current page.\nIn case of multiple redirects, the navigation resolves with the response of the last redirect.\nIf the reload does not produce a response, returns empty response.\n\nBy default (includeSnapshot: true), an ARIA snapshot with refs is returned. Use `snapshotOptions` for `interactiveOnly` (default false) and `cursorInteractive` (default false), same as a11y_take-aria-snapshot.\n\nWhen `includeScreenshot: true`, the screenshot is saved to disk; `screenshotFilePath` is returned. Default path/name: OS temp dir and "screenshot". Use `screenshotOptions.includeBase64: true` only when the file cannot be read from the path.\n '}inputSchema(){return{timeout:z31.number().int().nonnegative().optional().default(DEFAULT_TIMEOUT_MS3).describe("Max wait ms."),waitUntil:z31.enum(["load","domcontentloaded","networkidle","commit"]).optional().default(DEFAULT_WAIT_UNTIL3).describe("Resolve when this load event fires."),waitForNavigation:z31.boolean().optional().default(!0).describe("Wait for reload then for network idle before snapshot/screenshot. Default true."),waitForTimeoutMs:z31.number().int().min(0).optional().default(DEFAULT_WAIT_FOR_TIMEOUT_MS4).describe("Timeout for reload and network idle wait (ms). Only when waitForNavigation is true. Default 30000."),includeSnapshot:z31.boolean().optional().default(!0).describe("Return ARIA snapshot with refs."),snapshotOptions:z31.object({interactiveOnly:z31.boolean().optional().default(!1).describe("Only interactive elements get refs. Default false (content roles like headings also included)."),cursorInteractive:z31.boolean().optional().default(!1).describe("Include cursor:pointer / onclick elements. Default false.")}).optional().describe("Options when includeSnapshot is true. Same as a11y_take-aria-snapshot."),includeScreenshot:z31.boolean().optional().default(!1).describe("Take a screenshot after reload; saved to disk (default: OS temp dir)."),screenshotOptions:z31.object({outputPath:z31.string().optional().default(os5.tmpdir()).describe("Directory to save the screenshot. Default: OS temp dir."),name:z31.string().optional().default(DEFAULT_SCREENSHOT_NAME4).describe('Base name for the screenshot file. Default: "screenshot".'),fullPage:z31.boolean().optional().default(!0).describe("Capture full scrollable page. Default: true."),type:z31.enum(["png","jpeg"]).optional().default("png").describe("Image format. Default: png."),annotate:z31.boolean().optional().default(!0).describe("Overlay ARIA ref labels on the screenshot. Default: true."),includeBase64:z31.boolean().optional().default(!1).describe("Include base64 image in response; use only when file cannot be read from path (e.g. remote, container).")}).optional().describe('Options when includeScreenshot is true. outputPath/name default to tmp and "screenshot".')}}outputSchema(){return{url:z31.string().describe("Contains the URL of the reloaded page.").optional(),status:z31.number().int().positive().describe("Contains the status code of the reloaded page (e.g., 200 for a success).").optional(),statusText:z31.string().describe('Contains the status text of the reloaded page (e.g. usually an "OK" for a success).').optional(),ok:z31.boolean().describe("Contains a boolean stating whether the reloaded page was successful (status in the range 200-299) or not.").optional(),screenshotFilePath:z31.string().optional().describe("When includeScreenshot is true: full path of the saved screenshot file."),output:z31.string().optional(),refs:z31.record(z31.string(),refMapEntrySchema4).optional(),image:z31.object({data:z31.any().describe("Base64-encoded image data."),mimeType:z31.string().describe("MIME type of the image.")}).optional().describe("When includeScreenshot and screenshotOptions.includeBase64 are true: image sent as separate image content part.")}}async handle(context,args){let response=await context.page.reload({timeout:args.timeout,waitUntil:args.waitUntil});if(args.waitForNavigation!==!1){let waitForTimeoutMs=args.waitForTimeoutMs??DEFAULT_WAIT_FOR_TIMEOUT_MS4;await waitForNetworkIdle(context,{timeoutMs:waitForTimeoutMs})}let base={url:response?.url(),status:response?.status(),statusText:response?.statusText(),ok:response?.ok()},output=base;if(args.includeSnapshot!==!1)try{let snapOpts=args.snapshotOptions??{},snapshot=await takeAriaSnapshotWithRefs(context,{interactiveOnly:snapOpts.interactiveOnly??!1,cursorInteractive:snapOpts.cursorInteractive??!1});output={...base,output:snapshot.output,refs:snapshot.refs}}catch{}let screenshotFilePath,image;if(args.includeScreenshot===!0){let opts=args.screenshotOptions??{},outputPath=opts.outputPath??os5.tmpdir(),name=opts.name??DEFAULT_SCREENSHOT_NAME4;try{let screenshotType=(opts.type??"png")==="jpeg"?"jpeg":"png",shot=await captureScreenshot(context,{fullPage:opts.fullPage??!0,type:screenshotType,annotate:opts.annotate??!0}),filename=`${name}-${formattedTimeForFilename()}.${shot.screenshotType}`,filePath=path6.resolve(outputPath,filename);await fs6.writeFile(filePath,shot.rawBuffer),screenshotFilePath=filePath,opts.includeBase64===!0&&(image=shot.image)}catch{}}return{url:output.url,status:output.status,statusText:output.statusText,ok:output.ok,...screenshotFilePath!==void 0&&{screenshotFilePath},...output.output!==void 0&&{output:output.output},...output.refs!==void 0&&{refs:output.refs},...image!==void 0&&{image}}}};var tools6=[new GoBackOrForward,new GoTo,new Reload];import{z as z32}from"zod";var GetConsoleMessages=class{name(){return"o11y_get-console-messages"}description(){return"Retrieves console messages/logs from the browser with filtering options."}inputSchema(){return{type:z32.enum(getEnumKeyTuples(ConsoleMessageLevelName)).transform(createEnumTransformer(ConsoleMessageLevelName)).optional().describe("Filter by level (this level or higher)."),search:z32.string().optional().describe("Filter by message text."),timestamp:z32.number().int().nonnegative().optional().describe("Only messages at or after this Unix ms."),sequenceNumber:z32.number().int().nonnegative().optional().describe("Incremental: only messages with sequence > this."),limit:z32.object({count:z32.number().int().nonnegative().default(100),from:z32.enum(["start","end"]).default("end").describe("Keep from start or end when truncating.")}).default({count:100,from:"end"}).optional()}}outputSchema(){return{messages:z32.array(z32.object({type:z32.string().describe("Type of the console message."),text:z32.string().describe("Text of the console message."),location:z32.object({url:z32.string().describe("URL of the resource."),lineNumber:z32.number().nonnegative().describe("0-based line number in the resource."),columnNumber:z32.number().nonnegative().describe("0-based column number in the resource.")}).describe("Location of the console message in the resource.").optional(),timestamp:z32.number().int().nonnegative().describe("Unix epoch timestamp (in milliseconds) of the console message."),sequenceNumber:z32.number().int().nonnegative().describe(`
|
|
466
|
+
`.trim()}inputSchema(){return{mode:z28.enum(["by","to","top","bottom","left","right"]).optional().default(DEFAULT_MODE).describe("by=dx,dy; to=x,y; or edge (top/bottom/left/right)."),selector:z28.string().optional().describe("Scroll this container; omit for viewport."),dx:z28.number().optional(),dy:z28.number().optional(),x:z28.number().optional(),y:z28.number().optional(),behavior:z28.enum(["auto","smooth"]).optional().default(DEFAULT_BEHAVIOR)}}outputSchema(){return{mode:z28.enum(["by","to","top","bottom","left","right"]).describe("The scroll mode used."),selector:z28.string().nullable().describe("The selector of the scroll container if provided; otherwise null (document viewport)."),behavior:z28.enum(["auto","smooth"]).describe("The scroll behavior used."),before:z28.object({x:z28.number().describe("ScrollLeft before scrolling."),y:z28.number().describe("ScrollTop before scrolling."),scrollWidth:z28.number().describe("Total scrollable width before scrolling."),scrollHeight:z28.number().describe("Total scrollable height before scrolling."),clientWidth:z28.number().describe("Viewport/container client width before scrolling."),clientHeight:z28.number().describe("Viewport/container client height before scrolling.")}).describe("Scroll metrics before the scroll action."),after:z28.object({x:z28.number().describe("ScrollLeft after scrolling."),y:z28.number().describe("ScrollTop after scrolling."),scrollWidth:z28.number().describe("Total scrollable width after scrolling."),scrollHeight:z28.number().describe("Total scrollable height after scrolling."),clientWidth:z28.number().describe("Viewport/container client width after scrolling."),clientHeight:z28.number().describe("Viewport/container client height after scrolling.")}).describe("Scroll metrics after the scroll action."),canScrollX:z28.boolean().describe("Whether horizontal scrolling is possible (scrollWidth > clientWidth)."),canScrollY:z28.boolean().describe("Whether vertical scrolling is possible (scrollHeight > clientHeight)."),maxScrollX:z28.number().describe("Maximum horizontal scrollLeft (scrollWidth - clientWidth)."),maxScrollY:z28.number().describe("Maximum vertical scrollTop (scrollHeight - clientHeight)."),isAtLeft:z28.boolean().describe("Whether the scroll position is at the far left."),isAtRight:z28.boolean().describe("Whether the scroll position is at the far right."),isAtTop:z28.boolean().describe("Whether the scroll position is at the very top."),isAtBottom:z28.boolean().describe("Whether the scroll position is at the very bottom.")}}async handle(context,args){let mode=args.mode??DEFAULT_MODE,selector=args.selector,behavior=args.behavior??DEFAULT_BEHAVIOR,dx=args.dx??0,dy=args.dy??0,x=args.x,y=args.y;if(mode==="to"&&typeof x!="number"&&typeof y!="number")throw new Error('mode="to" requires at least one of x or y.');if(mode==="by"&&dx===0&&dy===0)throw new Error('mode="by" requires dx and/or dy to be non-zero.');let params={modeEval:mode,selectorEval:selector,dxEval:dx,dyEval:dy,xEval:x,yEval:y,behaviorEval:behavior},result;return selector?result=await resolveSelectorOrRef(context,selector).evaluate((el,p)=>{let before={x:el.scrollLeft,y:el.scrollTop,scrollWidth:el.scrollWidth,scrollHeight:el.scrollHeight,clientWidth:el.clientWidth,clientHeight:el.clientHeight},readMetrics=elem2=>({x:elem2.scrollLeft,y:elem2.scrollTop,scrollWidth:elem2.scrollWidth,scrollHeight:elem2.scrollHeight,clientWidth:elem2.clientWidth,clientHeight:elem2.clientHeight}),clamp=(v,min,max)=>Math.max(min,Math.min(max,v)),elem=el,maxX=Math.max(0,elem.scrollWidth-elem.clientWidth),maxY=Math.max(0,elem.scrollHeight-elem.clientHeight);p.modeEval==="by"?elem.scrollTo({left:clamp(elem.scrollLeft+p.dxEval,0,maxX),top:clamp(elem.scrollTop+p.dyEval,0,maxY),behavior:p.behaviorEval}):p.modeEval==="to"?elem.scrollTo({left:typeof p.xEval=="number"?clamp(p.xEval,0,maxX):elem.scrollLeft,top:typeof p.yEval=="number"?clamp(p.yEval,0,maxY):elem.scrollTop,behavior:p.behaviorEval}):p.modeEval==="top"?elem.scrollTo({top:0,left:elem.scrollLeft,behavior:p.behaviorEval}):p.modeEval==="bottom"?elem.scrollTo({top:maxY,left:elem.scrollLeft,behavior:p.behaviorEval}):p.modeEval==="left"?elem.scrollTo({left:0,top:elem.scrollTop,behavior:p.behaviorEval}):p.modeEval==="right"&&elem.scrollTo({left:maxX,top:elem.scrollTop,behavior:p.behaviorEval});let after=readMetrics(elem);return{before,after,canScrollX:after.scrollWidth>after.clientWidth,canScrollY:after.scrollHeight>after.clientHeight,maxScrollX:Math.max(0,after.scrollWidth-after.clientWidth),maxScrollY:Math.max(0,after.scrollHeight-after.clientHeight),isAtLeft:after.x<=1,isAtRight:after.x>=Math.max(0,after.scrollWidth-after.clientWidth)-1,isAtTop:after.y<=1,isAtBottom:after.y>=Math.max(0,after.scrollHeight-after.clientHeight)-1}},params):result=await context.page.evaluate(params2=>{let modeEval=params2.modeEval,selectorEval=params2.selectorEval,dxEval=params2.dxEval,dyEval=params2.dyEval,xEval=params2.xEval,yEval=params2.yEval,behaviorEval=params2.behaviorEval,getTarget=()=>{if(selectorEval){let el=document.querySelector(selectorEval);if(!el)throw new Error(`Element with selector "${selectorEval}" not found`);return el}let scrolling=document.scrollingElement||document.documentElement||document.body;if(!scrolling)throw new Error("No scrolling element available.");return scrolling},readMetrics=el=>({x:el.scrollLeft,y:el.scrollTop,scrollWidth:el.scrollWidth,scrollHeight:el.scrollHeight,clientWidth:el.clientWidth,clientHeight:el.clientHeight}),clamp=(v,min,max)=>v<min?min:v>max?max:v,doScroll=el=>{let maxX=Math.max(0,el.scrollWidth-el.clientWidth),maxY=Math.max(0,el.scrollHeight-el.clientHeight);if(modeEval==="by"){let nextX=clamp(el.scrollLeft+dxEval,0,maxX),nextY=clamp(el.scrollTop+dyEval,0,maxY);el.scrollTo({left:nextX,top:nextY,behavior:behaviorEval});return}if(modeEval==="to"){let nextX=typeof xEval=="number"?clamp(xEval,0,maxX):el.scrollLeft,nextY=typeof yEval=="number"?clamp(yEval,0,maxY):el.scrollTop;el.scrollTo({left:nextX,top:nextY,behavior:behaviorEval});return}if(modeEval==="top"){el.scrollTo({top:0,left:el.scrollLeft,behavior:behaviorEval});return}if(modeEval==="bottom"){el.scrollTo({top:maxY,left:el.scrollLeft,behavior:behaviorEval});return}if(modeEval==="left"){el.scrollTo({left:0,top:el.scrollTop,behavior:behaviorEval});return}if(modeEval==="right"){el.scrollTo({left:maxX,top:el.scrollTop,behavior:behaviorEval});return}},target=getTarget(),before=readMetrics(target);doScroll(target);let after=readMetrics(target),maxScrollX=Math.max(0,after.scrollWidth-after.clientWidth),maxScrollY=Math.max(0,after.scrollHeight-after.clientHeight),canScrollX=after.scrollWidth>after.clientWidth,canScrollY=after.scrollHeight>after.clientHeight,eps=1,isAtLeft=after.x<=eps,isAtRight=after.x>=maxScrollX-eps,isAtTop=after.y<=eps,isAtBottom=after.y>=maxScrollY-eps;return{before,after,canScrollX,canScrollY,maxScrollX,maxScrollY,isAtLeft,isAtRight,isAtTop,isAtBottom}},params),{mode,selector:selector??null,behavior,before:result.before,after:result.after,canScrollX:result.canScrollX,canScrollY:result.canScrollY,maxScrollX:result.maxScrollX,maxScrollY:result.maxScrollY,isAtLeft:result.isAtLeft,isAtRight:result.isAtRight,isAtTop:result.isAtTop,isAtBottom:result.isAtBottom}}};var tools5=[new Click,new Drag,new Fill,new Hover,new PressKey,new ResizeViewport,new ResizeWindow,new Select,new Scroll];import fs4 from"node:fs/promises";import os3 from"node:os";import path4 from"node:path";import{z as z29}from"zod";var DEFAULT_SCREENSHOT_NAME2="screenshot",DEFAULT_TIMEOUT_MS2=0,DEFAULT_WAIT_FOR_TIMEOUT_MS2=3e4,DEFAULT_WAIT_UNTIL="load",DIRECTION_VALUES=["back","forward"],refMapEntrySchema2=z29.object({role:z29.string(),name:z29.string().optional(),selector:z29.string(),nth:z29.number().optional()}),GoBackOrForward=class{name(){return"navigation_go-back-or-forward"}description(){return'\nNavigates to the previous or next page in history.\n- `direction: "back"` \u2014 previous page in history.\n- `direction: "forward"` \u2014 next page in history.\n\nIn case of multiple redirects, the navigation will resolve with the response of the last redirect.\nIf cannot go back/forward, returns empty response.\n\nBy default (includeSnapshot: true), an ARIA snapshot with refs is returned. Use `snapshotOptions` for `interactiveOnly` (default false) and `cursorInteractive` (default false), same as a11y_take-aria-snapshot.\n\nWhen `includeScreenshot: true`, the screenshot is always saved to disk; `screenshotFilePath` is returned. By default `outputPath` is the OS temp dir and `name` is "screenshot" (same as `content_take-screenshot`). Use `screenshotOptions.includeBase64: true` only when the file cannot be read from the returned path (e.g. remote, container).\n '}inputSchema(){return{direction:z29.enum(DIRECTION_VALUES),timeout:z29.number().int().nonnegative().optional().default(DEFAULT_TIMEOUT_MS2).describe("Max wait ms. 0=no timeout."),waitUntil:z29.enum(["load","domcontentloaded","commit"]).optional().default(DEFAULT_WAIT_UNTIL).describe("Playwright navigation lifecycle: when the main frame reaches this state. Does not use Playwright networkidle; use waitForNavigation for session network-idle after history navigation."),waitForNavigation:z29.boolean().optional().default(!0).describe("Wait for navigation then for network idle before snapshot/screenshot. Default true."),waitForTimeoutMs:z29.number().int().min(0).optional().default(DEFAULT_WAIT_FOR_TIMEOUT_MS2).describe("Timeout for navigation and network idle wait (ms). Only when waitForNavigation is true. Default 30000."),includeSnapshot:z29.boolean().optional().default(!0).describe("Return ARIA snapshot with refs."),snapshotOptions:z29.object({interactiveOnly:z29.boolean().optional().default(!1).describe("Only interactive elements get refs. Default false (content roles like headings also included)."),cursorInteractive:z29.boolean().optional().default(!1).describe("Include cursor:pointer / onclick elements. Default false.")}).optional().describe("Options when includeSnapshot is true. Same as a11y_take-aria-snapshot."),includeScreenshot:z29.boolean().optional().default(!1).describe("Take a screenshot after navigation; saved to disk (default: OS temp dir). Use includeBase64 only when file cannot be read from path."),screenshotOptions:z29.object({outputPath:z29.string().optional().default(os3.tmpdir()).describe("Directory to save the screenshot. Default: OS temp dir."),name:z29.string().optional().default(DEFAULT_SCREENSHOT_NAME2).describe('Base name for the screenshot file. Default: "screenshot".'),fullPage:z29.boolean().optional().default(!0).describe("Capture full scrollable page. Default: true."),type:z29.enum(["png","jpeg"]).optional().default("png").describe("Image format. Default: png."),annotate:z29.boolean().optional().default(!0).describe("Overlay ARIA ref labels on the screenshot. Default: true."),includeBase64:z29.boolean().optional().default(!1).describe("Include base64 image in response; use only when file cannot be read from path (e.g. remote, container).")}).optional().describe('Options when includeScreenshot is true. outputPath/name default to tmp and "screenshot".')}}outputSchema(){return{url:z29.string().describe("Contains the URL of the navigated page.").optional(),status:z29.number().int().positive().describe("Contains the status code of the navigated page (e.g., 200 for a success).").optional(),statusText:z29.string().describe('Contains the status text of the navigated page (e.g. usually an "OK" for a success).').optional(),ok:z29.boolean().describe("Contains a boolean stating whether the navigated page was successful (status in the range 200-299) or not.").optional(),screenshotFilePath:z29.string().optional().describe("When includeScreenshot is true: full path of the saved screenshot file."),output:z29.string().optional(),refs:z29.record(z29.string(),refMapEntrySchema2).optional(),image:z29.object({data:z29.any().describe("Base64-encoded image data."),mimeType:z29.string().describe("MIME type of the image.")}).optional().describe("When includeScreenshot and screenshotOptions.includeBase64 are true: image sent as separate image content part.")}}async handle(context,args){let response=args.direction==="back"?await context.page.goBack({timeout:args.timeout,waitUntil:args.waitUntil}):await context.page.goForward({timeout:args.timeout,waitUntil:args.waitUntil});if(args.waitForNavigation!==!1){let waitForTimeoutMs=args.waitForTimeoutMs??DEFAULT_WAIT_FOR_TIMEOUT_MS2;await waitForNetworkIdle(context,{timeoutMs:waitForTimeoutMs})}let base={url:response?.url(),status:response?.status(),statusText:response?.statusText(),ok:response?.ok()},output=base;if(args.includeSnapshot!==!1)try{let snapOpts=args.snapshotOptions??{},snapshot=await takeAriaSnapshotWithRefs(context,{interactiveOnly:snapOpts.interactiveOnly??!1,cursorInteractive:snapOpts.cursorInteractive??!1});output={...base,output:snapshot.output,refs:snapshot.refs}}catch{}let screenshotFilePath,image;if(args.includeScreenshot===!0){let opts=args.screenshotOptions??{},outputPath=opts.outputPath??os3.tmpdir(),name=opts.name??DEFAULT_SCREENSHOT_NAME2;try{let screenshotType=(opts.type??"png")==="jpeg"?"jpeg":"png",shot=await captureScreenshot(context,{fullPage:opts.fullPage??!0,type:screenshotType,annotate:opts.annotate??!0}),filename=`${name}-${formattedTimeForFilename()}.${shot.screenshotType}`,filePath=path4.resolve(outputPath,filename);await fs4.writeFile(filePath,shot.rawBuffer),screenshotFilePath=filePath,opts.includeBase64===!0&&(image=shot.image)}catch{}}return{url:output.url,status:output.status,statusText:output.statusText,ok:output.ok,...screenshotFilePath!==void 0&&{screenshotFilePath},...output.output!==void 0&&{output:output.output},...output.refs!==void 0&&{refs:output.refs},...image!==void 0&&{image}}}};import fs5 from"node:fs/promises";import os4 from"node:os";import path5 from"node:path";import{z as z30}from"zod";var DEFAULT_SCREENSHOT_NAME3="screenshot",DEFAULT_TIMEOUT_MS3=0,DEFAULT_WAIT_FOR_TIMEOUT_MS3=3e4,DEFAULT_WAIT_UNTIL2="load",refMapEntrySchema3=z30.object({role:z30.string(),name:z30.string().optional(),selector:z30.string(),nth:z30.number().optional()}),GoTo=class{name(){return"navigation_go-to"}description(){return'\nNavigates to the given URL.\n**NOTE**: The tool either throws an error or returns a main resource response. \nThe only exceptions are navigation to `about:blank` or navigation to the same URL with a different hash, \nwhich would succeed and return empty response.\n\n**By default** (`includeSnapshot: true`), an ARIA snapshot with refs is taken after navigation and returned in `output` and `refs`; you can use refs (e1, e2, ...) in interaction tools without calling `a11y_take-aria-snapshot` separately. Use `snapshotOptions` for `interactiveOnly` (default false) and `cursorInteractive` (default false). Set `includeSnapshot: false` to get only url/status/ok.\n\nWhen `includeScreenshot: true`, the screenshot is always saved to disk; `screenshotFilePath` is returned. By default `outputPath` is the OS temp dir and `name` is "screenshot" (same as `content_take-screenshot`). Use `screenshotOptions.includeBase64: true` only when the file cannot be read from the returned path (e.g. remote, container).\n '}inputSchema(){return{url:z30.string(),timeout:z30.number().int().nonnegative().optional().default(DEFAULT_TIMEOUT_MS3).describe("Max wait ms. 0=no timeout."),waitUntil:z30.enum(["load","domcontentloaded","commit"]).optional().default(DEFAULT_WAIT_UNTIL2).describe("Playwright navigation lifecycle: when the main frame reaches this state. Does not use Playwright networkidle; use waitForNavigation for session network-idle after navigation."),waitForNavigation:z30.boolean().optional().default(!0).describe("Wait for navigation then for network idle before snapshot/screenshot. Default true."),waitForTimeoutMs:z30.number().int().min(0).optional().default(DEFAULT_WAIT_FOR_TIMEOUT_MS3).describe("Timeout for navigation and network idle wait (ms). Only when waitForNavigation is true. Default 30000."),includeSnapshot:z30.boolean().optional().default(!0).describe("Return ARIA snapshot with refs after nav."),snapshotOptions:z30.object({interactiveOnly:z30.boolean().optional().default(!1).describe("Only interactive elements get refs. Default false (content roles like headings also included)."),cursorInteractive:z30.boolean().optional().default(!1).describe("Include cursor:pointer / onclick elements. Default false.")}).optional().describe("Options when includeSnapshot is true. Same as a11y_take-aria-snapshot."),includeScreenshot:z30.boolean().optional().default(!1).describe("Take a screenshot after navigation; saved to disk (default: OS temp dir). Use includeBase64 only when file cannot be read from path."),screenshotOptions:z30.object({outputPath:z30.string().optional().default(os4.tmpdir()).describe("Directory to save the screenshot. Default: OS temp dir."),name:z30.string().optional().default(DEFAULT_SCREENSHOT_NAME3).describe('Base name for the screenshot file. Default: "screenshot".'),fullPage:z30.boolean().optional().default(!0).describe("Capture full scrollable page. Default: true."),type:z30.enum(["png","jpeg"]).optional().default("png").describe("Image format. Default: png."),annotate:z30.boolean().optional().default(!0).describe("Overlay ARIA ref labels on the screenshot. Default: true."),includeBase64:z30.boolean().optional().default(!1).describe("Include base64 image in response; use only when file cannot be read from path (e.g. remote, container).")}).optional().describe('Options when includeScreenshot is true. outputPath/name default to tmp and "screenshot".')}}outputSchema(){return{url:z30.string().describe("Contains the URL of the navigated page.").optional(),status:z30.number().int().positive().describe("Contains the status code of the navigated page (e.g., 200 for a success).").optional(),statusText:z30.string().describe('Contains the status text of the navigated page (e.g. usually an "OK" for a success).').optional(),ok:z30.boolean().describe("Contains a boolean stating whether the navigated page was successful (status in the range 200-299) or not.").optional(),screenshotFilePath:z30.string().optional().describe("When includeScreenshot is true: full path of the saved screenshot file."),output:z30.string().optional().describe("When includeSnapshot is true: page URL, title, and ARIA tree with refs."),refs:z30.record(z30.string(),refMapEntrySchema3).optional().describe("When includeSnapshot is true: map of ref id (e1, e2, ...) to role/name/selector for use in interaction tools."),image:z30.object({data:z30.any().describe("Base64-encoded image data."),mimeType:z30.string().describe("MIME type of the image.")}).optional().describe("When includeScreenshot and screenshotOptions.includeBase64 are true: image sent as separate image content part.")}}async handle(context,args){let response=await context.page.goto(args.url,{timeout:args.timeout,waitUntil:args.waitUntil});if(args.waitForNavigation!==!1){let waitForTimeoutMs=args.waitForTimeoutMs??DEFAULT_WAIT_FOR_TIMEOUT_MS3;await waitForNetworkIdle(context,{timeoutMs:waitForTimeoutMs})}let base={url:response?.url(),status:response?.status(),statusText:response?.statusText(),ok:response?.ok()},output=base;if(args.includeSnapshot!==!1)try{let snapOpts=args.snapshotOptions??{},snapshot=await takeAriaSnapshotWithRefs(context,{interactiveOnly:snapOpts.interactiveOnly??!1,cursorInteractive:snapOpts.cursorInteractive??!1});output={...base,output:snapshot.output,refs:snapshot.refs}}catch{}let screenshotFilePath,image;if(args.includeScreenshot===!0){let opts=args.screenshotOptions??{},outputPath=opts.outputPath??os4.tmpdir(),name=opts.name??DEFAULT_SCREENSHOT_NAME3;try{let screenshotType=(opts.type??"png")==="jpeg"?"jpeg":"png",shot=await captureScreenshot(context,{fullPage:opts.fullPage??!0,type:screenshotType,annotate:opts.annotate??!0}),filename=`${name}-${formattedTimeForFilename()}.${shot.screenshotType}`,filePath=path5.resolve(outputPath,filename);await fs5.writeFile(filePath,shot.rawBuffer),screenshotFilePath=filePath,opts.includeBase64===!0&&(image=shot.image)}catch{}}return{url:output.url,status:output.status,statusText:output.statusText,ok:output.ok,...screenshotFilePath!==void 0&&{screenshotFilePath},...output.output!==void 0&&{output:output.output},...output.refs!==void 0&&{refs:output.refs},...image!==void 0&&{image}}}};import fs6 from"node:fs/promises";import os5 from"node:os";import path6 from"node:path";import{z as z31}from"zod";var DEFAULT_SCREENSHOT_NAME4="screenshot",DEFAULT_TIMEOUT_MS4=0,DEFAULT_WAIT_FOR_TIMEOUT_MS4=3e4,DEFAULT_WAIT_UNTIL3="load",refMapEntrySchema4=z31.object({role:z31.string(),name:z31.string().optional(),selector:z31.string(),nth:z31.number().optional()}),Reload=class{name(){return"navigation_reload"}description(){return'\nReloads the current page.\nIn case of multiple redirects, the navigation resolves with the response of the last redirect.\nIf the reload does not produce a response, returns empty response.\n\nBy default (includeSnapshot: true), an ARIA snapshot with refs is returned. Use `snapshotOptions` for `interactiveOnly` (default false) and `cursorInteractive` (default false), same as a11y_take-aria-snapshot.\n\nWhen `includeScreenshot: true`, the screenshot is saved to disk; `screenshotFilePath` is returned. Default path/name: OS temp dir and "screenshot". Use `screenshotOptions.includeBase64: true` only when the file cannot be read from the path.\n '}inputSchema(){return{timeout:z31.number().int().nonnegative().optional().default(DEFAULT_TIMEOUT_MS4).describe("Max wait ms."),waitUntil:z31.enum(["load","domcontentloaded","commit"]).optional().default(DEFAULT_WAIT_UNTIL3).describe("Playwright navigation lifecycle: when the main frame reaches this state. Does not use Playwright networkidle; use waitForNavigation for session network-idle after reload."),waitForNavigation:z31.boolean().optional().default(!0).describe("Wait for reload then for network idle before snapshot/screenshot. Default true."),waitForTimeoutMs:z31.number().int().min(0).optional().default(DEFAULT_WAIT_FOR_TIMEOUT_MS4).describe("Timeout for reload and network idle wait (ms). Only when waitForNavigation is true. Default 30000."),includeSnapshot:z31.boolean().optional().default(!0).describe("Return ARIA snapshot with refs."),snapshotOptions:z31.object({interactiveOnly:z31.boolean().optional().default(!1).describe("Only interactive elements get refs. Default false (content roles like headings also included)."),cursorInteractive:z31.boolean().optional().default(!1).describe("Include cursor:pointer / onclick elements. Default false.")}).optional().describe("Options when includeSnapshot is true. Same as a11y_take-aria-snapshot."),includeScreenshot:z31.boolean().optional().default(!1).describe("Take a screenshot after reload; saved to disk (default: OS temp dir)."),screenshotOptions:z31.object({outputPath:z31.string().optional().default(os5.tmpdir()).describe("Directory to save the screenshot. Default: OS temp dir."),name:z31.string().optional().default(DEFAULT_SCREENSHOT_NAME4).describe('Base name for the screenshot file. Default: "screenshot".'),fullPage:z31.boolean().optional().default(!0).describe("Capture full scrollable page. Default: true."),type:z31.enum(["png","jpeg"]).optional().default("png").describe("Image format. Default: png."),annotate:z31.boolean().optional().default(!0).describe("Overlay ARIA ref labels on the screenshot. Default: true."),includeBase64:z31.boolean().optional().default(!1).describe("Include base64 image in response; use only when file cannot be read from path (e.g. remote, container).")}).optional().describe('Options when includeScreenshot is true. outputPath/name default to tmp and "screenshot".')}}outputSchema(){return{url:z31.string().describe("Contains the URL of the reloaded page.").optional(),status:z31.number().int().positive().describe("Contains the status code of the reloaded page (e.g., 200 for a success).").optional(),statusText:z31.string().describe('Contains the status text of the reloaded page (e.g. usually an "OK" for a success).').optional(),ok:z31.boolean().describe("Contains a boolean stating whether the reloaded page was successful (status in the range 200-299) or not.").optional(),screenshotFilePath:z31.string().optional().describe("When includeScreenshot is true: full path of the saved screenshot file."),output:z31.string().optional(),refs:z31.record(z31.string(),refMapEntrySchema4).optional(),image:z31.object({data:z31.any().describe("Base64-encoded image data."),mimeType:z31.string().describe("MIME type of the image.")}).optional().describe("When includeScreenshot and screenshotOptions.includeBase64 are true: image sent as separate image content part.")}}async handle(context,args){let response=await context.page.reload({timeout:args.timeout,waitUntil:args.waitUntil});if(args.waitForNavigation!==!1){let waitForTimeoutMs=args.waitForTimeoutMs??DEFAULT_WAIT_FOR_TIMEOUT_MS4;await waitForNetworkIdle(context,{timeoutMs:waitForTimeoutMs})}let base={url:response?.url(),status:response?.status(),statusText:response?.statusText(),ok:response?.ok()},output=base;if(args.includeSnapshot!==!1)try{let snapOpts=args.snapshotOptions??{},snapshot=await takeAriaSnapshotWithRefs(context,{interactiveOnly:snapOpts.interactiveOnly??!1,cursorInteractive:snapOpts.cursorInteractive??!1});output={...base,output:snapshot.output,refs:snapshot.refs}}catch{}let screenshotFilePath,image;if(args.includeScreenshot===!0){let opts=args.screenshotOptions??{},outputPath=opts.outputPath??os5.tmpdir(),name=opts.name??DEFAULT_SCREENSHOT_NAME4;try{let screenshotType=(opts.type??"png")==="jpeg"?"jpeg":"png",shot=await captureScreenshot(context,{fullPage:opts.fullPage??!0,type:screenshotType,annotate:opts.annotate??!0}),filename=`${name}-${formattedTimeForFilename()}.${shot.screenshotType}`,filePath=path6.resolve(outputPath,filename);await fs6.writeFile(filePath,shot.rawBuffer),screenshotFilePath=filePath,opts.includeBase64===!0&&(image=shot.image)}catch{}}return{url:output.url,status:output.status,statusText:output.statusText,ok:output.ok,...screenshotFilePath!==void 0&&{screenshotFilePath},...output.output!==void 0&&{output:output.output},...output.refs!==void 0&&{refs:output.refs},...image!==void 0&&{image}}}};var tools6=[new GoBackOrForward,new GoTo,new Reload];import{z as z32}from"zod";var GetConsoleMessages=class{name(){return"o11y_get-console-messages"}description(){return"Retrieves console messages/logs from the browser with filtering options."}inputSchema(){return{type:z32.enum(getEnumKeyTuples(ConsoleMessageLevelName)).transform(createEnumTransformer(ConsoleMessageLevelName)).optional().describe("Filter by level (this level or higher)."),search:z32.string().optional().describe("Filter by message text."),timestamp:z32.number().int().nonnegative().optional().describe("Only messages at or after this Unix ms."),sequenceNumber:z32.number().int().nonnegative().optional().describe("Incremental: only messages with sequence > this."),limit:z32.object({count:z32.number().int().nonnegative().default(100),from:z32.enum(["start","end"]).default("end").describe("Keep from start or end when truncating.")}).default({count:100,from:"end"}).optional()}}outputSchema(){return{messages:z32.array(z32.object({type:z32.string().describe("Type of the console message."),text:z32.string().describe("Text of the console message."),location:z32.object({url:z32.string().describe("URL of the resource."),lineNumber:z32.number().nonnegative().describe("0-based line number in the resource."),columnNumber:z32.number().nonnegative().describe("0-based column number in the resource.")}).describe("Location of the console message in the resource.").optional(),timestamp:z32.number().int().nonnegative().describe("Unix epoch timestamp (in milliseconds) of the console message."),sequenceNumber:z32.number().int().nonnegative().describe(`
|
|
463
467
|
A monotonically increasing sequence number assigned to each console message.
|
|
464
468
|
It reflects the order in which messages were captured and can be used by clients
|
|
465
469
|
to retrieve messages incrementally by requesting only those with a higher sequence
|
|
@@ -524,10 +528,10 @@ Notes:
|
|
|
524
528
|
- pattern is a glob matched against the full request URL.
|
|
525
529
|
- stubs are evaluated in insertion order; first match wins.
|
|
526
530
|
- times limits how many times the stub applies (-1 means infinite).
|
|
527
|
-
`.trim()}inputSchema(){return{pattern:z43.string().describe("URL glob."),response:z43.object({action:z43.enum(["fulfill","abort"]).optional().default("fulfill"),status:z43.number().int().min(100).max(599).optional(),headers:z43.record(z43.string(),z43.string()).optional(),body:z43.union([z43.string(),z43.record(z43.string(),z43.any()),z43.array(z43.any())]).optional(),abortErrorCode:z43.string().optional().describe("Error code when action=abort.")}).describe("Response."),delayMs:z43.number().int().nonnegative().optional(),times:z43.number().int().optional().describe("Max times to apply; -1=infinite."),chance:z43.number().min(0).max(1).optional().describe("Probability 0\u20131; omit=always.")}}outputSchema(){return{stubId:z43.string().describe("Unique id of the installed stub (use it to clear later)."),kind:z43.literal("mock_http_response").describe("Stub kind."),pattern:z43.string().describe("Glob pattern."),enabled:z43.boolean().describe("Whether the stub is enabled."),delayMs:z43.number().int().describe("Applied artificial delay in milliseconds."),times:z43.number().int().describe("Max applications (-1 means infinite)."),chance:z43.number().optional().describe("Apply probability (omit means always)."),action:z43.enum(["fulfill","abort"]).describe("Applied action."),status:z43.number().int().optional().describe("HTTP status (present when action=fulfill).")}}async handle(context,args){await ensureRoutingInstalled(context.browserContext);let action=args.response.action??"fulfill",delayMs=normalizeDelayMs(args.delayMs),times=normalizeTimesPublic(args.times),chance=normalizeChance(args.chance),status=args.response.status,headers=normalizeHeaders(args.response.headers),body=normalizeBody(args.response.body),abortErrorCode=normalizeAbortCode(args.response.abortErrorCode),stub=addMockHttpResponseStub(context.browserContext,{enabled:!0,pattern:args.pattern,action,status,headers,body,abortErrorCode,delayMs,times,chance}),out={stubId:stub.id,kind:"mock_http_response",pattern:stub.pattern,enabled:stub.enabled,delayMs:stub.delayMs,times:stub.times,action:stub.action};return typeof stub.chance=="number"&&(out.chance=stub.chance),stub.action==="fulfill"&&(out.status=typeof stub.status=="number"?stub.status:200),out}};var tools9=[new Clear,new InterceptHttpRequest,new List,new MockHttpResponse];import{z as z44}from"zod";var
|
|
531
|
+
`.trim()}inputSchema(){return{pattern:z43.string().describe("URL glob."),response:z43.object({action:z43.enum(["fulfill","abort"]).optional().default("fulfill"),status:z43.number().int().min(100).max(599).optional(),headers:z43.record(z43.string(),z43.string()).optional(),body:z43.union([z43.string(),z43.record(z43.string(),z43.any()),z43.array(z43.any())]).optional(),abortErrorCode:z43.string().optional().describe("Error code when action=abort.")}).describe("Response."),delayMs:z43.number().int().nonnegative().optional(),times:z43.number().int().optional().describe("Max times to apply; -1=infinite."),chance:z43.number().min(0).max(1).optional().describe("Probability 0\u20131; omit=always.")}}outputSchema(){return{stubId:z43.string().describe("Unique id of the installed stub (use it to clear later)."),kind:z43.literal("mock_http_response").describe("Stub kind."),pattern:z43.string().describe("Glob pattern."),enabled:z43.boolean().describe("Whether the stub is enabled."),delayMs:z43.number().int().describe("Applied artificial delay in milliseconds."),times:z43.number().int().describe("Max applications (-1 means infinite)."),chance:z43.number().optional().describe("Apply probability (omit means always)."),action:z43.enum(["fulfill","abort"]).describe("Applied action."),status:z43.number().int().optional().describe("HTTP status (present when action=fulfill).")}}async handle(context,args){await ensureRoutingInstalled(context.browserContext);let action=args.response.action??"fulfill",delayMs=normalizeDelayMs(args.delayMs),times=normalizeTimesPublic(args.times),chance=normalizeChance(args.chance),status=args.response.status,headers=normalizeHeaders(args.response.headers),body=normalizeBody(args.response.body),abortErrorCode=normalizeAbortCode(args.response.abortErrorCode),stub=addMockHttpResponseStub(context.browserContext,{enabled:!0,pattern:args.pattern,action,status,headers,body,abortErrorCode,delayMs,times,chance}),out={stubId:stub.id,kind:"mock_http_response",pattern:stub.pattern,enabled:stub.enabled,delayMs:stub.delayMs,times:stub.times,action:stub.action};return typeof stub.chance=="number"&&(out.chance=stub.chance),stub.action==="fulfill"&&(out.status=typeof stub.status=="number"?stub.status:200),out}};var tools9=[new Clear,new InterceptHttpRequest,new List,new MockHttpResponse];import{z as z44}from"zod";var DEFAULT_TIMEOUT_MS5=3e4,DEFAULT_IDLE_TIME_MS=500,DEFAULT_MAX_CONNECTIONS=0,DEFAULT_POLL_INTERVAL_MS=50,WaitForNetworkIdle=class{name(){return"sync_wait-for-network-idle"}description(){return`
|
|
528
532
|
Waits until the page is network-idle: in-flight requests <= maxConnections for at least idleTimeMs (server-side tracking, no page globals).
|
|
529
533
|
Use before SPAs, screenshots, or AX snapshots for stable results. With long-polling, increase maxConnections or use shorter idleTimeMs.
|
|
530
|
-
`.trim()}inputSchema(){return{timeoutMs:z44.number().int().min(0).optional().default(
|
|
534
|
+
`.trim()}inputSchema(){return{timeoutMs:z44.number().int().min(0).optional().default(DEFAULT_TIMEOUT_MS5).describe("Max wait ms before failing."),idleTimeMs:z44.number().int().min(0).optional().default(DEFAULT_IDLE_TIME_MS).describe("Network must stay idle for this many ms to resolve."),maxConnections:z44.number().int().min(0).optional().default(DEFAULT_MAX_CONNECTIONS).describe("Idle when in-flight requests <= this."),pollIntervalMs:z44.number().int().min(10).optional().default(DEFAULT_POLL_INTERVAL_MS).describe("Polling interval ms.")}}outputSchema(){return{waitedMs:z44.number().int().nonnegative().describe("Total time waited until the network became idle or the tool timed out (milliseconds)."),idleTimeMs:z44.number().int().nonnegative().describe("Idle duration required for success (milliseconds)."),timeoutMs:z44.number().int().nonnegative().describe("Maximum allowed wait time (milliseconds)."),maxConnections:z44.number().int().nonnegative().describe("Idle threshold used: in-flight requests must be <= this value."),pollIntervalMs:z44.number().int().nonnegative().describe("Polling interval used to sample the in-flight request count (milliseconds)."),finalInFlightRequests:z44.number().int().nonnegative().describe("The last observed number of in-flight requests at the moment the tool returned."),observedIdleMs:z44.number().int().nonnegative().describe("How long the in-flight request count stayed <= maxConnections right before returning (milliseconds).")}}async handle(context,args){let timeoutMs=args.timeoutMs??DEFAULT_TIMEOUT_MS5,idleTimeMs=args.idleTimeMs??DEFAULT_IDLE_TIME_MS,maxConnections=args.maxConnections??DEFAULT_MAX_CONNECTIONS,pollIntervalMs=args.pollIntervalMs??DEFAULT_POLL_INTERVAL_MS,startMs=Date.now(),deadlineMs=startMs+timeoutMs,lastNotIdleMs=startMs,lastInFlight=0;for(;;){let nowMs=Date.now();lastInFlight=context.numOfInFlightRequests(),lastInFlight>maxConnections&&(lastNotIdleMs=nowMs);let observedIdleMs=nowMs-lastNotIdleMs;if(observedIdleMs>=idleTimeMs)return{waitedMs:nowMs-startMs,idleTimeMs,timeoutMs,maxConnections,pollIntervalMs,finalInFlightRequests:lastInFlight,observedIdleMs};if(nowMs>=deadlineMs){let waitedMs=nowMs-startMs;throw new Error(`Timed out after ${waitedMs}ms waiting for network idle (idleTimeMs=${idleTimeMs}, maxConnections=${maxConnections}, inFlight=${lastInFlight}).`)}await this.sleep(pollIntervalMs)}}async sleep(ms){await new Promise(resolve=>{setTimeout(()=>resolve(),ms)})}};var tools10=[new WaitForNetworkIdle];var tools11=[...tools,...tools2,...tools3,...tools4,...tools5,...tools6,...tools7,...tools8,...tools9,...tools10];async function createToolSessionContext(sessionIdProvider){let sessionId=sessionIdProvider(),browserContextInfo=await newBrowserContext(),page=await newPage(browserContextInfo.browserContext),context=new BrowserToolSessionContext(sessionId,browserContextInfo.browserContext,page,{otelEnable:OTEL_ENABLE,dontCloseBrowserContext:browserContextInfo.dontCloseBrowserContext??!1});return await context.init(),debug(`Created session context for the session with id ${context.sessionId()}`),context}function createToolExecutor(){return new BrowserToolExecutor}import{Option}from"commander";var BrowserCliProvider=class{platform="browser";cliName="browser-devtools-cli";tools=tools11;sessionDescription="Manage browser sessions";cliExamples=["browser-devtools-cli --no-headless interactive","browser-devtools-cli --persistent --no-headless interactive"];bashCompletionOptions="--headless --no-headless --persistent --no-persistent --user-data-dir --use-system-browser --browser-path";bashCompletionCommands="daemon session tools config completion interactive navigation content interaction a11y accessibility o11y react run stub sync figma debug";zshCompletionOptions=` '--headless[Run in headless mode]' \\
|
|
531
535
|
'--no-headless[Run in headful mode]' \\
|
|
532
536
|
'--persistent[Use persistent context]' \\
|
|
533
537
|
'--no-persistent[Use ephemeral context]' \\
|
|
@@ -744,10 +748,10 @@ When asked to check for UI problems, layout issues, or visual bugs, ALWAYS follo
|
|
|
744
748
|
// Or with page.evaluate: await page.evaluate(() => document.querySelector('button').click());
|
|
745
749
|
await callTool('a11y_take-aria-snapshot', {}, true);
|
|
746
750
|
await callTool('content_take-screenshot', {}, true);
|
|
747
|
-
`.trim(),platformInfo={serverInfo:{instructions:BROWSER_SERVER_INSTRUCTIONS_ENABLE?SERVER_INSTRUCTIONS:void 0,policies:BROWSER_POLICY_UI_DEBUGGING_ENABLE?[UI_DEBUGGING_POLICY]:[]},toolsInfo:{tools:tools11,createToolSessionContext,createToolExecutor,executeImportantDescription:EXECUTE_IMPORTANT_DESCRIPTION_BROWSER,executeDescription:EXECUTE_DESCRIPTION_BROWSER},cliInfo:{cliProvider}};import{EventEmitter}from"node:events";import{WebSocket}from"ws";var NodeCDPSession=class extends EventEmitter{ws=null;reqId=0;pending=new Map;connected=!1;wsUrl;connectOptions;constructor(wsUrl,connectOptions){super(),this.wsUrl=wsUrl,this.connectOptions=connectOptions}async connect(){if(this.connected)return;let wsOptions=this.connectOptions?.headers?{headers:this.connectOptions.headers}:void 0;return new Promise((resolve,reject)=>{this.ws=new WebSocket(this.wsUrl,wsOptions);let timeout=setTimeout(()=>{reject(new Error(`Connection timeout to ${this.wsUrl}`)),this.ws?.close()},1e4);this.ws.on("open",()=>{clearTimeout(timeout),this.connected=!0,resolve()}),this.ws.on("error",err=>{clearTimeout(timeout),this.connected||reject(err)}),this.ws.on("close",()=>{this.connected=!1;for(let[id,handler]of this.pending)handler.reject(new Error("CDP session closed"));this.pending.clear()}),this.ws.on("message",data=>{try{let msg=JSON.parse(data.toString());this._handleMessage(msg)}catch{}})})}async send(method,params){if(!this.ws||!this.connected)throw new Error("CDP session is not connected");let id=++this.reqId;return new Promise((resolve,reject)=>{this.pending.set(id,{resolve,reject});let message=JSON.stringify({id,method,params:params||{}});this.ws.send(message,err=>{err&&(this.pending.delete(id),reject(err))}),setTimeout(()=>{this.pending.has(id)&&(this.pending.delete(id),reject(new Error(`CDP command timeout: ${method}`)))},3e4)})}async detach(){if(this.ws){this.connected=!1,this.ws.close(),this.ws=null;for(let[,handler]of this.pending)handler.reject(new Error("CDP session detached"));this.pending.clear()}}isConnected(){return this.connected}_handleMessage(msg){if(msg.id!==void 0){let handler=this.pending.get(msg.id);handler&&(this.pending.delete(msg.id),msg.error?handler.reject(new Error(`CDP error: ${msg.error.message||JSON.stringify(msg.error)}`)):handler.resolve(msg.result||{}))}else msg.method&&this.emit(msg.method,msg.params)}};async function createNodeCDPSession(wsUrl,connectOptions){let session=new NodeCDPSession(wsUrl,connectOptions);return await session.connect(),session}import{execSync}from"node:child_process";import*as http from"node:http";var DEFAULT_HOST="127.0.0.1",DEFAULT_PORT=9229,
|
|
748
|
-
`)){let trimmed=line.trim();if(trimmed)if(platform==="win32"){let parts=trimmed.split(",");if(parts.length>=3){let pid=parseInt(parts[parts.length-1],10),command=parts.slice(1,-1).join(",");!isNaN(pid)&&pid>0&&processes.push({pid,command})}}else{let parts=trimmed.split(/\s+/);if(parts.length>=11){let pid=parseInt(parts[1],10),command=parts.slice(10).join(" ");!isNaN(pid)&&pid>0&&processes.push({pid,command})}}}if(namePattern){let regex=new RegExp(namePattern,"i");return processes.filter(p=>regex.test(p.command))}return processes}catch{return[]}}async function activateInspector(pid,host=DEFAULT_HOST,timeoutMs=
|
|
749
|
-
`).filter(Boolean);return ids.length===0?null:(ids.length>1&&debug(`Multiple containers match "${containerName}", using first: ${ids[0]}`),ids[0])}catch(err){if(/Cannot connect to the Docker daemon|docker: command not found/i.test(err.message??""))throw new Error(`${err.message} When MCP runs in a container, mount /var/run/docker.sock and ensure the docker CLI is available.`);return null}}async function activateInspectorInContainer(containerId,host=DEFAULT_HOST,port=DEFAULT_PORT,timeoutMs=
|
|
750
|
-
`);if(lines.length<2)throw new Error(`No Node.js process found inside container ${containerId}`);for(let i=1;i<lines.length;i++){let line=lines[i];if(!/node/i.test(line))continue;let parts=line.trim().split(/\s+/);if(parts.length>=2&&/^\d+$/.test(parts[1])){hostPid=parts[1];break}}if(!hostPid)throw new Error(`Could not parse Node PID from container ${containerId}`)}catch(err){if(err.message?.includes("No Node.js process"))throw err;let dockerHint=/Cannot connect to the Docker daemon|docker: command not found/i.test(err.message??"")?" When MCP runs in a container, mount /var/run/docker.sock and ensure the docker CLI is available.":"";throw new Error(`Cannot find Node process in container ${containerId}: ${err.message}. Ensure Docker is running and the container has a Node.js process.${dockerHint}`)}debug(`Sending SIGUSR1 to Node process (host PID ${hostPid}) for container ${containerId}...`);try{
|
|
751
|
+
`.trim(),platformInfo={serverInfo:{instructions:BROWSER_SERVER_INSTRUCTIONS_ENABLE?SERVER_INSTRUCTIONS:void 0,policies:BROWSER_POLICY_UI_DEBUGGING_ENABLE?[UI_DEBUGGING_POLICY]:[]},toolsInfo:{tools:tools11,createToolSessionContext,createToolExecutor,executeImportantDescription:EXECUTE_IMPORTANT_DESCRIPTION_BROWSER,executeDescription:EXECUTE_DESCRIPTION_BROWSER},cliInfo:{cliProvider}};import{EventEmitter}from"node:events";import{WebSocket}from"ws";var NodeCDPSession=class extends EventEmitter{ws=null;reqId=0;pending=new Map;connected=!1;wsUrl;connectOptions;constructor(wsUrl,connectOptions){super(),this.wsUrl=wsUrl,this.connectOptions=connectOptions}async connect(){if(this.connected)return;let wsOptions=this.connectOptions?.headers?{headers:this.connectOptions.headers}:void 0;return new Promise((resolve,reject)=>{this.ws=new WebSocket(this.wsUrl,wsOptions);let timeout=setTimeout(()=>{reject(new Error(`Connection timeout to ${this.wsUrl}`)),this.ws?.close()},1e4);this.ws.on("open",()=>{clearTimeout(timeout),this.connected=!0,resolve()}),this.ws.on("error",err=>{clearTimeout(timeout),this.connected||reject(err)}),this.ws.on("close",()=>{this.connected=!1;for(let[id,handler]of this.pending)handler.reject(new Error("CDP session closed"));this.pending.clear()}),this.ws.on("message",data=>{try{let msg=JSON.parse(data.toString());this._handleMessage(msg)}catch{}})})}async send(method,params){if(!this.ws||!this.connected)throw new Error("CDP session is not connected");let id=++this.reqId;return new Promise((resolve,reject)=>{this.pending.set(id,{resolve,reject});let message=JSON.stringify({id,method,params:params||{}});this.ws.send(message,err=>{err&&(this.pending.delete(id),reject(err))}),setTimeout(()=>{this.pending.has(id)&&(this.pending.delete(id),reject(new Error(`CDP command timeout: ${method}`)))},3e4)})}async detach(){if(this.ws){this.connected=!1,this.ws.close(),this.ws=null;for(let[,handler]of this.pending)handler.reject(new Error("CDP session detached"));this.pending.clear()}}isConnected(){return this.connected}_handleMessage(msg){if(msg.id!==void 0){let handler=this.pending.get(msg.id);handler&&(this.pending.delete(msg.id),msg.error?handler.reject(new Error(`CDP error: ${msg.error.message||JSON.stringify(msg.error)}`)):handler.resolve(msg.result||{}))}else msg.method&&this.emit(msg.method,msg.params)}};async function createNodeCDPSession(wsUrl,connectOptions){let session=new NodeCDPSession(wsUrl,connectOptions);return await session.connect(),session}import{execSync as execSync2}from"node:child_process";import*as http from"node:http";var DEFAULT_HOST="127.0.0.1",DEFAULT_PORT=9229,DEFAULT_TIMEOUT_MS6=1e4,ACTIVATION_POLL_INTERVAL_MS=200,MAX_INSPECTOR_SCAN_PORTS=20;function buildInspectorWebSocketUrl(host,port,targetWebSocketDebuggerUrl){let path7=new URL(targetWebSocketDebuggerUrl).pathname;return`ws://${host}:${port}${path7}`}var DISCOVERY_HOST_HEADER="127.0.0.1:9229";function inspectorWsConnectOptions(wsUrl){try{let hostname=new URL(wsUrl).hostname.toLowerCase();return hostname==="127.0.0.1"||hostname==="localhost"?void 0:{headers:{Host:DISCOVERY_HOST_HEADER}}}catch{return}}async function discoverInspectorTargets(host=DEFAULT_HOST,port=DEFAULT_PORT,timeoutMs=5e3){return new Promise((resolve,reject)=>{let timeout=setTimeout(()=>{reject(new Error(`Inspector discovery timeout on ${host}:${port}`))},timeoutMs),req=http.get({host,port,path:"/json/list",...host!==DEFAULT_HOST&&host!=="localhost"&&{headers:{Host:DISCOVERY_HOST_HEADER}}},res=>{let data="";res.on("data",chunk=>{data+=chunk}),res.on("end",()=>{clearTimeout(timeout);try{let targets=JSON.parse(data);resolve(targets)}catch{reject(new Error(`Invalid response from inspector at ${host}:${port}`))}})});req.on("error",err=>{clearTimeout(timeout),reject(new Error(`Cannot reach inspector at ${host}:${port}: ${err.message}`))}),req.end()})}async function scanForInspector(host=DEFAULT_HOST,startPort=DEFAULT_PORT,maxPorts=MAX_INSPECTOR_SCAN_PORTS){for(let port=startPort;port<startPort+maxPorts;port++)try{let targets=await discoverInspectorTargets(host,port,1e3);if(targets.length>0)return{port,targets}}catch{}return null}function findNodeProcesses(namePattern){try{let platform=process.platform,output;platform==="win32"?output=execSync2(`wmic process where "name like '%node%'" get ProcessId,CommandLine /format:csv`,{encoding:"utf-8",timeout:5e3}):output=execSync2('ps aux | grep -E "[n]ode|[t]s-node|[n]px"',{encoding:"utf-8",timeout:5e3});let processes=[];for(let line of output.split(`
|
|
752
|
+
`)){let trimmed=line.trim();if(trimmed)if(platform==="win32"){let parts=trimmed.split(",");if(parts.length>=3){let pid=parseInt(parts[parts.length-1],10),command=parts.slice(1,-1).join(",");!isNaN(pid)&&pid>0&&processes.push({pid,command})}}else{let parts=trimmed.split(/\s+/);if(parts.length>=11){let pid=parseInt(parts[1],10),command=parts.slice(10).join(" ");!isNaN(pid)&&pid>0&&processes.push({pid,command})}}}if(namePattern){let regex=new RegExp(namePattern,"i");return processes.filter(p=>regex.test(p.command))}return processes}catch{return[]}}async function activateInspector(pid,host=DEFAULT_HOST,timeoutMs=DEFAULT_TIMEOUT_MS6){if(process.platform==="win32")throw new Error("SIGUSR1 activation is not supported on Windows. Start the Node.js process with --inspect flag or NODE_OPTIONS=--inspect instead.");try{process.kill(pid,0)}catch{throw new Error(`Process ${pid} does not exist or is not accessible`)}debug(`Sending SIGUSR1 to process ${pid} to activate V8 Inspector...`),process.kill(pid,"SIGUSR1");let startTime=Date.now();for(;Date.now()-startTime<timeoutMs;){let result=await scanForInspector(host,DEFAULT_PORT,10);if(result)return debug(`Inspector activated on port ${result.port} for process ${pid}`),result;await new Promise(resolve=>setTimeout(resolve,ACTIVATION_POLL_INTERVAL_MS))}throw new Error(`Timeout waiting for inspector to activate on process ${pid}`)}function resolveContainerId(containerName){try{let ids=execSync2(`docker ps -q -f name=${containerName}`,{encoding:"utf-8",timeout:5e3}).trim().split(`
|
|
753
|
+
`).filter(Boolean);return ids.length===0?null:(ids.length>1&&debug(`Multiple containers match "${containerName}", using first: ${ids[0]}`),ids[0])}catch(err){if(/Cannot connect to the Docker daemon|docker: command not found/i.test(err.message??""))throw new Error(`${err.message} When MCP runs in a container, mount /var/run/docker.sock and ensure the docker CLI is available.`);return null}}async function activateInspectorInContainer(containerId,host=DEFAULT_HOST,port=DEFAULT_PORT,timeoutMs=DEFAULT_TIMEOUT_MS6){if(process.platform==="win32")throw new Error("Docker container activation may have limitations on Windows. Ensure the container has port 9229 exposed and Node is started with --inspect.");let hostPid="";try{let lines=execSync2(`docker top ${containerId}`,{encoding:"utf-8",timeout:5e3}).trim().split(`
|
|
754
|
+
`);if(lines.length<2)throw new Error(`No Node.js process found inside container ${containerId}`);for(let i=1;i<lines.length;i++){let line=lines[i];if(!/node/i.test(line))continue;let parts=line.trim().split(/\s+/);if(parts.length>=2&&/^\d+$/.test(parts[1])){hostPid=parts[1];break}}if(!hostPid)throw new Error(`Could not parse Node PID from container ${containerId}`)}catch(err){if(err.message?.includes("No Node.js process"))throw err;let dockerHint=/Cannot connect to the Docker daemon|docker: command not found/i.test(err.message??"")?" When MCP runs in a container, mount /var/run/docker.sock and ensure the docker CLI is available.":"";throw new Error(`Cannot find Node process in container ${containerId}: ${err.message}. Ensure Docker is running and the container has a Node.js process.${dockerHint}`)}debug(`Sending SIGUSR1 to Node process (host PID ${hostPid}) for container ${containerId}...`);try{execSync2(`docker run --rm --pid=host alpine kill -USR1 ${hostPid}`,{encoding:"utf-8",timeout:1e4})}catch(err){let dockerHint=/Cannot connect to the Docker daemon|docker: command not found/i.test(err.message??"")?" When MCP runs in a container, mount /var/run/docker.sock and ensure the docker CLI is available.":"";throw new Error(`Failed to send SIGUSR1 to process in container ${containerId}: ${err.message}. (Uses docker run --rm --pid=host alpine kill -USR1 <hostPid>; ensure alpine image is available.)${dockerHint}`)}let startTime=Date.now();for(;Date.now()-startTime<timeoutMs;){try{let targets=await discoverInspectorTargets(host,port,2e3);if(targets.length>0)return debug(`Inspector active at ${host}:${port} for container ${containerId}`),{port,targets}}catch(err){debug(`Inspector not yet ready at ${host}:${port}: ${err instanceof Error?err.message:String(err)}`)}await new Promise(resolve=>setTimeout(resolve,ACTIVATION_POLL_INTERVAL_MS))}throw new Error(`Timeout waiting for inspector from container ${containerId} at ${host}:${port}. Ensure: (1) the container has NODE_OPTIONS=--inspect-port=0.0.0.0:9229 so inspector accepts external connections when activated via SIGUSR1; (2) the container exposes port 9229 to the host (e.g. -p 30019:9229); (3) when MCP runs in a container, host is reachable (e.g. NODE_INSPECTOR_HOST=host.docker.internal and on Linux add extra_hosts: host.docker.internal:host-gateway).`)}async function connectToNodeProcess(options){let host=options.host??NODE_INSPECTOR_HOST??DEFAULT_HOST,timeoutMs=options.timeoutMs||DEFAULT_TIMEOUT_MS6,activateIfNeeded=options.activateIfNeeded!==!1;if(options.wsUrl)return debug(`Connecting to Node.js inspector via WebSocket: ${options.wsUrl}`),{session:await createNodeCDPSession(options.wsUrl,inspectorWsConnectOptions(options.wsUrl)),processInfo:{pid:0,inspectorActive:!0,webSocketUrl:options.wsUrl},target:{id:"direct",type:"node",title:"Direct WebSocket connection",url:"",webSocketDebuggerUrl:options.wsUrl}};let containerId=options.containerId??null;if(!containerId&&options.containerName&&(containerId=resolveContainerId(options.containerName),!containerId))throw new Error(`No running container found matching name: ${options.containerName}`);if(containerId){let port2=options.port||DEFAULT_PORT,targets2=[];try{targets2=await discoverInspectorTargets(host,port2,3e3)}catch{}if(targets2.length===0&&activateIfNeeded&&(debug(`Inspector not active. Activating via SIGUSR1 in container ${containerId}...`),targets2=(await activateInspectorInContainer(containerId,host,port2,timeoutMs)).targets),targets2.length===0)throw new Error(`Cannot connect to Node.js in container ${containerId}. Inspector not active. Ensure port 9229 is exposed (e.g. -p 9229:9229) and use activateIfNeeded or start Node with --inspect.`);let target=targets2[0],wsUrl=buildInspectorWebSocketUrl(host,port2,target.webSocketDebuggerUrl);return{session:await createNodeCDPSession(wsUrl,inspectorWsConnectOptions(wsUrl)),processInfo:{pid:0,inspectorActive:!0,inspectorHost:host,inspectorPort:port2,webSocketUrl:wsUrl,containerId},target}}let pid=options.pid,processCommand;if(!pid&&options.processName){let processes=findNodeProcesses(options.processName);if(processes.length===0)throw new Error(`No Node.js process found matching: ${options.processName}`);if(processes.length>1){let procList=processes.map(p=>` PID ${p.pid}: ${p.command}`).join(`
|
|
751
755
|
`);throw new Error(`Multiple Node.js processes match "${options.processName}":
|
|
752
756
|
${procList}
|
|
753
757
|
Please specify a PID directly.`)}pid=processes[0].pid,processCommand=processes[0].command,debug(`Found Node.js process: PID ${pid} - ${processCommand}`)}let port=options.port||DEFAULT_PORT,targets=[];try{targets=await discoverInspectorTargets(host,port,3e3)}catch{}if(targets.length>0){let target=targets[0];debug(`Inspector already active at ${host}:${port}, connecting...`);let wsUrl=buildInspectorWebSocketUrl(host,port,target.webSocketDebuggerUrl);return{session:await createNodeCDPSession(wsUrl,inspectorWsConnectOptions(wsUrl)),processInfo:{pid:pid||0,command:processCommand,inspectorActive:!0,inspectorHost:host,inspectorPort:port,webSocketUrl:wsUrl},target}}if(pid&&activateIfNeeded){debug(`Inspector not active on ${host}:${port}. Activating via SIGUSR1 on PID ${pid}...`);let{port:activePort,targets:activeTargets}=await activateInspector(pid,host,timeoutMs);if(activeTargets.length===0)throw new Error(`Inspector activated on port ${activePort} but no targets found`);let target=activeTargets[0],wsUrl=buildInspectorWebSocketUrl(host,activePort,target.webSocketDebuggerUrl);return{session:await createNodeCDPSession(wsUrl,inspectorWsConnectOptions(wsUrl)),processInfo:{pid,command:processCommand,inspectorActive:!0,inspectorHost:host,inspectorPort:activePort,webSocketUrl:wsUrl},target}}throw pid?new Error(`Cannot connect to process ${pid}. Inspector is not active and activation failed. Try starting the process with NODE_OPTIONS=--inspect or --inspect flag.`):new Error("No target specified. Provide a PID, process name, port, or WebSocket URL. If the process is not started with --inspect, provide a PID to activate via SIGUSR1.")}var NodeToolSessionContext=class{_sessionId;_connection=null;_storeKey=null;closed=!1;constructor(sessionId){this._sessionId=sessionId}sessionId(){return this._sessionId}executionContext(){return{}}async connect(options){if(this._connection)throw new Error("Already connected to a Node.js process. Disconnect first.");debug(`Connecting to Node.js process with options: ${JSON.stringify(options)}`);let result=await connectToNodeProcess(options);return this._connection=result,this._storeKey=getStoreKey(result.processInfo.pid,result.processInfo.webSocketUrl),await enableDebugging(this._storeKey,result.session,{pid:result.processInfo.pid,command:result.processInfo.command}),debug(`Connected to Node.js process: PID=${result.processInfo.pid}, store=${this._storeKey}`),{processInfo:result.processInfo,target:result.target}}async disconnect(){!this._connection||!this._storeKey||(debug(`Disconnecting from Node.js process: ${this._storeKey}`),await detachDebugging(this._storeKey),await this._connection.session.detach(),this._connection=null,this._storeKey=null)}get storeKey(){if(!this._storeKey)throw new Error("Not connected to any Node.js process. Call connect first.");return this._storeKey}get isConnected(){return this._connection!==null&&this._connection.session.isConnected()}get processInfo(){return this._connection?.processInfo??null}async close(){return this.closed?!1:(await this.disconnect(),this.closed=!0,!0)}};var NodeToolExecutor=class{async executeTool(context,tool,args){debug(`Executing Node.js tool ${tool.name()} with input: ${toJson(args)}`);try{let result=await tool.handle(context,args);return debug(`Executed Node.js tool ${tool.name()} successfully`),result}catch(err){throw debug(`Error executing Node.js tool ${tool.name()}: ${err}`),err}}};import{z as z45}from"zod";var Connect=class{name(){return"debug_connect"}description(){return`
|
|
@@ -760,7 +764,7 @@ Retrieves snapshots captured by tracepoints, logpoints, and/or exceptionpoints.
|
|
|
760
764
|
Optional \`types\`: array of \`tracepoint\`, \`logpoint\`, \`exceptionpoint\`. If omitted or empty, returns all.
|
|
761
765
|
Response: tracepointSnapshots, logpointSnapshots, exceptionpointSnapshots. Optional probeId, fromSequence, limit apply per type.
|
|
762
766
|
Output trimming: by default only the top ${DEFAULT_TRIM_OPTIONS.maxCallStackDepth} call stack frames are returned, only \`${NODE_DEFAULT_INCLUDE_SCOPES.join(", ")}\` scope(s) are included, and variables per scope are capped at ${DEFAULT_TRIM_OPTIONS.maxVariablesPerScope}. Override with maxCallStackDepth, includeScopes, maxVariablesPerScope.
|
|
763
|
-
`.trim()}inputSchema(){return{types:z47.array(z47.enum(GET_SNAPSHOT_TYPES)).optional().describe("Which snapshot types to return. If omitted or empty, all are returned."),probeId:z47.string().optional().describe("Filter tracepoint or logpoint snapshots by this probe ID"),fromSequence:z47.number().int().nonnegative().optional().describe("Return snapshots with sequence number > fromSequence (per type)."),limit:z47.number().int().positive().optional().describe("Maximum number of snapshots per type."),maxCallStackDepth:z47.number().int().positive().optional().default(DEFAULT_TRIM_OPTIONS.maxCallStackDepth).describe(`Max call stack frames per snapshot. Default ${DEFAULT_TRIM_OPTIONS.maxCallStackDepth}.`),includeScopes:z47.array(z47.enum(SCOPE_TYPE_VALUES2)).optional().default([...NODE_DEFAULT_INCLUDE_SCOPES]).describe(`Scope types to include. Default [${NODE_DEFAULT_INCLUDE_SCOPES.join(", ")}] (local only to keep payload small).`),maxVariablesPerScope:z47.number().int().positive().optional().default(DEFAULT_TRIM_OPTIONS.maxVariablesPerScope).describe(`Max variables per scope. Default ${DEFAULT_TRIM_OPTIONS.maxVariablesPerScope}.`)}}outputSchema(){return{tracepointSnapshots:z47.array(z47.any()).describe("Tracepoint snapshots"),logpointSnapshots:z47.array(z47.any()).describe("Logpoint snapshots"),exceptionpointSnapshots:z47.array(z47.any()).describe("Exceptionpoint snapshots")}}async handle(context,args){let getTracepoint=!args.types?.length||args.types.includes("tracepoint"),getLogpoint=!args.types?.length||args.types.includes("logpoint"),getExceptionpoint=!args.types?.length||args.types.includes("exceptionpoint"),trimOpts={maxCallStackDepth:args.maxCallStackDepth,includeScopes:args.includeScopes,maxVariablesPerScope:args.maxVariablesPerScope},tracepointSnapshots=[],logpointSnapshots=[],exceptionpointSnapshots=[],allSnapshots=getSnapshots(context.storeKey),probes=listProbes(context.storeKey);if(args.probeId&&(getTracepoint||getLogpoint)){let raw=getSnapshotsByProbe(context.storeKey,args.probeId),filtered=applySnapshotFilters(raw,args.fromSequence,args.limit),probe=probes.find(p=>p.id===args.probeId);probe?.kind==="tracepoint"&&getTracepoint?tracepointSnapshots=filtered:probe?.kind==="logpoint"&&getLogpoint&&(logpointSnapshots=filtered)}else{if(getTracepoint){let tracepointIds=new Set(probes.filter(p=>p.kind==="tracepoint").map(p=>p.id)),raw=allSnapshots.filter(s=>tracepointIds.has(s.probeId));tracepointSnapshots=applySnapshotFilters(raw,args.fromSequence,args.limit)}if(getLogpoint){let logpointIds=new Set(probes.filter(p=>p.kind==="logpoint").map(p=>p.id)),raw=allSnapshots.filter(s=>logpointIds.has(s.probeId));logpointSnapshots=applySnapshotFilters(raw,args.fromSequence,args.limit)}}if(getExceptionpoint){let raw=allSnapshots.filter(s=>s.probeId==="__exception__");exceptionpointSnapshots=applySnapshotFilters(raw,args.fromSequence,args.limit)}return{tracepointSnapshots:trimSnapshots(tracepointSnapshots,trimOpts),logpointSnapshots:trimSnapshots(logpointSnapshots,trimOpts),exceptionpointSnapshots:trimSnapshots(exceptionpointSnapshots,trimOpts)}}},SNAPSHOT_TYPES3=["tracepoint","logpoint","exceptionpoint"],ClearProbeSnapshots2=class{name(){return"debug_clear-probe-snapshots"}description(){return"\nClears snapshots captured by tracepoints, logpoints, and/or exceptionpoints.\nOptional `types`: array of `tracepoint`, `logpoint`, `exceptionpoint`. If omitted or empty, clears all.\nOptional `probeId`: clear only snapshots for this probe (tracepoint/logpoint).\n ".trim()}inputSchema(){return{types:z47.array(z47.enum(SNAPSHOT_TYPES3)).optional().describe("Which snapshot types to clear. If omitted or empty, all are cleared."),probeId:z47.string().optional().describe("Clear only snapshots for this probe ID (tracepoint or logpoint). Ignored for exceptionpoint.")}}outputSchema(){return{tracepointCleared:z47.number().describe("Tracepoint snapshots cleared"),logpointCleared:z47.number().describe("Logpoint snapshots cleared"),exceptionpointCleared:z47.number().describe("Exceptionpoint snapshots cleared"),message:z47.string().describe("Status message")}}async handle(context,args){let{clearSnapshotsByProbe:clearSnapshotsByProbe2,listProbes:listProbes3}=await import("./core-FPD4CWST.js"),clearTracepoint=!args.types?.length||args.types.includes("tracepoint"),clearLogpoint=!args.types?.length||args.types.includes("logpoint"),clearExceptionpoint=!args.types?.length||args.types.includes("exceptionpoint"),tracepointCleared=0,logpointCleared=0,exceptionpointCleared=0;if(args.probeId&&(clearTracepoint||clearLogpoint)){let n=clearSnapshotsByProbe2(context.storeKey,args.probeId),p=listProbes3(context.storeKey).find(x=>x.id===args.probeId);clearTracepoint&&clearLogpoint?p?.kind==="tracepoint"?tracepointCleared=n:p?.kind==="logpoint"&&(logpointCleared=n):clearTracepoint?tracepointCleared=n:logpointCleared=n}else{if(clearTracepoint)for(let p of listProbes3(context.storeKey).filter(x=>x.kind==="tracepoint"))tracepointCleared+=clearSnapshotsByProbe2(context.storeKey,p.id);if(clearLogpoint)for(let p of listProbes3(context.storeKey).filter(x=>x.kind==="logpoint"))logpointCleared+=clearSnapshotsByProbe2(context.storeKey,p.id);clearExceptionpoint&&(exceptionpointCleared=clearSnapshotsByProbe2(context.storeKey,"__exception__"))}let parts=[tracepointCleared&&`${tracepointCleared} tracepoint snapshot(s)`,logpointCleared&&`${logpointCleared} logpoint snapshot(s)`,exceptionpointCleared&&`${exceptionpointCleared} exceptionpoint snapshot(s)`].filter(Boolean),message=parts.length>0?`Cleared: ${parts.join(", ")}`:"Nothing to clear";return{tracepointCleared,logpointCleared,exceptionpointCleared,message}}};import{z as z48}from"zod";var GetLogs=class{name(){return"debug_get-logs"}description(){return"Retrieves console messages/logs from the Node.js process with filtering options."}inputSchema(){return{type:z48.enum(getEnumKeyTuples(ConsoleMessageLevelName)).transform(createEnumTransformer(ConsoleMessageLevelName)).optional().describe("Filter by level (this level or higher)."),search:z48.string().optional().describe("Filter by message text."),timestamp:z48.number().int().nonnegative().optional().describe("Only messages at or after this Unix ms."),sequenceNumber:z48.number().int().nonnegative().optional().describe("Incremental: only messages with sequence > this."),limit:z48.object({count:z48.number().int().nonnegative().default(0).describe("0 = no limit."),from:z48.enum(["start","end"]).default("end").describe("Keep from start or end when truncating.")}).optional()}}outputSchema(){return{messages:z48.array(z48.object({type:z48.string().describe("Type of the console message."),text:z48.string().describe("Text of the console message."),location:z48.object({url:z48.string().describe("URL of the resource."),lineNumber:z48.number().nonnegative().describe("0-based line number."),columnNumber:z48.number().nonnegative().describe("0-based column number.")}).describe("Location of the console message.").optional(),timestamp:z48.number().int().nonnegative().describe("Unix epoch timestamp (ms)."),sequenceNumber:z48.number().int().nonnegative().describe("Monotonic sequence number.")})).describe("Retrieved console messages.")}}async handle(context,args){let levelCodeThreshold=args.type?ConsoleMessageLevel[args.type]?.code:void 0,filtered=getConsoleMessages(context.storeKey).filter(msg=>!(levelCodeThreshold!==void 0&&msg.level.code<levelCodeThreshold||args.timestamp&&msg.timestamp<args.timestamp||args.sequenceNumber&&msg.sequenceNumber<=args.sequenceNumber||args.search&&!msg.text.includes(args.search)));return{messages:(args.limit?.count?args.limit.from==="start"?filtered.slice(0,args.limit.count):filtered.slice(-args.limit.count):filtered).map(msg=>({type:msg.type,text:msg.text,location:msg.location?{url:msg.location.url,lineNumber:msg.location.lineNumber,columnNumber:msg.location.columnNumber}:void 0,timestamp:msg.timestamp,sequenceNumber:msg.sequenceNumber}))}}};import{z as z49}from"zod";var REMOVE_TYPES2=["tracepoint","logpoint","watch"],RemoveProbe2=class{name(){return"debug_remove-probe"}description(){return"Removes a tracepoint, logpoint, or watch expression by ID. `type`: tracepoint, logpoint, or watch. `id`: the probe or watch ID."}inputSchema(){return{type:z49.enum(REMOVE_TYPES2).describe("Kind of probe to remove: tracepoint, logpoint, or watch"),id:z49.string().describe("Probe or watch expression ID to remove")}}outputSchema(){return{removed:z49.boolean().describe("Whether the probe or watch was removed"),message:z49.string().describe("Status message")}}async handle(context,args){if(args.type==="watch"){let removed2=removeWatchExpression(context.storeKey,args.id);return{removed:removed2,message:removed2?`Watch expression ${args.id} removed`:`Watch expression ${args.id} not found`}}let removed=await removeProbe(context.storeKey,args.id),label=args.type==="tracepoint"?"Tracepoint":"Logpoint";return{removed,message:removed?`${label} ${args.id} removed`:`Failed to remove ${args.type}`}}},LIST_TYPES2=["tracepoint","logpoint","watch"],ListProbes2=class{name(){return"debug_list-probes"}description(){return"Lists tracepoints, logpoints, and/or watch expressions. Optional `types`: array of `tracepoint`, `logpoint`, `watch`. If omitted or empty, returns all."}inputSchema(){return{types:z49.array(z49.enum(LIST_TYPES2)).optional().describe("Which probe types to list. If omitted or empty, all are returned.")}}outputSchema(){return{tracepoints:z49.array(z49.any()).describe("Tracepoints"),logpoints:z49.array(z49.any()).describe("Logpoints"),watches:z49.array(z49.any()).describe("Watch expressions")}}async handle(context,args){let listTracepoints=!args.types?.length||args.types.includes("tracepoint"),listLogpoints=!args.types?.length||args.types.includes("logpoint"),listWatches=!args.types?.length||args.types.includes("watch"),probes=listProbes(context.storeKey),tracepoints=listTracepoints?probes.filter(p=>p.kind==="tracepoint").map(p=>({id:p.id,urlPattern:p.urlPattern,lineNumber:p.lineNumber,columnNumber:p.columnNumber,condition:p.condition,hitCondition:p.hitCondition,hitCount:p.hitCount,resolvedLocations:p.resolvedLocations,enabled:p.enabled})):[],logpoints=listLogpoints?probes.filter(p=>p.kind==="logpoint").map(p=>({id:p.id,urlPattern:p.urlPattern,lineNumber:p.lineNumber,logExpression:p.logExpression,condition:p.condition,hitCondition:p.hitCondition,hitCount:p.hitCount,resolvedLocations:p.resolvedLocations,enabled:p.enabled})):[],watches=listWatches?listWatchExpressions(context.storeKey):[];return{tracepoints,logpoints,watches}}},PROBE_TYPES2=["tracepoint","logpoint","watches"],ClearProbes2=class{name(){return"debug_clear-probes"}description(){return"Removes tracepoints, logpoints, and/or watch expressions. Optional `types`: array of `tracepoint`, `logpoint`, `watches`. If omitted or empty, clears all."}inputSchema(){return{types:z49.array(z49.enum(PROBE_TYPES2)).optional().describe("Which probe types to clear. If omitted or empty, all are cleared.")}}outputSchema(){return{tracepointsCleared:z49.number().describe("Number of tracepoints cleared"),logpointsCleared:z49.number().describe("Number of logpoints cleared"),watchesCleared:z49.number().describe("Number of watch expressions cleared"),message:z49.string().describe("Status message")}}async handle(context,args){let clearTracepoints=!args.types?.length||args.types.includes("tracepoint"),clearLogpoints=!args.types?.length||args.types.includes("logpoint"),clearWatches=!args.types?.length||args.types.includes("watches"),tracepointsCleared=0,logpointsCleared=0,watchesCleared=0;if(clearTracepoints||clearLogpoints){let probes=listProbes(context.storeKey);for(let p of probes)p.kind==="tracepoint"&&clearTracepoints?await removeProbe(context.storeKey,p.id)&&tracepointsCleared++:p.kind==="logpoint"&&clearLogpoints&&await removeProbe(context.storeKey,p.id)&&logpointsCleared++}clearWatches&&(watchesCleared=clearWatchExpressions(context.storeKey));let parts=[];tracepointsCleared&&parts.push(`${tracepointsCleared} tracepoint(s)`),logpointsCleared&&parts.push(`${logpointsCleared} logpoint(s)`),watchesCleared&&parts.push(`${watchesCleared} watch(es)`);let message=parts.length>0?`Cleared: ${parts.join(", ")}`:"Nothing to clear";return{tracepointsCleared,logpointsCleared,watchesCleared,message}}};import{z as z50}from"zod";var PutExceptionpoint2=class{name(){return"debug_put-exceptionpoint"}description(){return`
|
|
767
|
+
`.trim()}inputSchema(){return{types:z47.array(z47.enum(GET_SNAPSHOT_TYPES)).optional().describe("Which snapshot types to return. If omitted or empty, all are returned."),probeId:z47.string().optional().describe("Filter tracepoint or logpoint snapshots by this probe ID"),fromSequence:z47.number().int().nonnegative().optional().describe("Return snapshots with sequence number > fromSequence (per type)."),limit:z47.number().int().positive().optional().describe("Maximum number of snapshots per type."),maxCallStackDepth:z47.number().int().positive().optional().default(DEFAULT_TRIM_OPTIONS.maxCallStackDepth).describe(`Max call stack frames per snapshot. Default ${DEFAULT_TRIM_OPTIONS.maxCallStackDepth}.`),includeScopes:z47.array(z47.enum(SCOPE_TYPE_VALUES2)).optional().default([...NODE_DEFAULT_INCLUDE_SCOPES]).describe(`Scope types to include. Default [${NODE_DEFAULT_INCLUDE_SCOPES.join(", ")}] (local only to keep payload small).`),maxVariablesPerScope:z47.number().int().positive().optional().default(DEFAULT_TRIM_OPTIONS.maxVariablesPerScope).describe(`Max variables per scope. Default ${DEFAULT_TRIM_OPTIONS.maxVariablesPerScope}.`)}}outputSchema(){return{tracepointSnapshots:z47.array(z47.any()).describe("Tracepoint snapshots"),logpointSnapshots:z47.array(z47.any()).describe("Logpoint snapshots"),exceptionpointSnapshots:z47.array(z47.any()).describe("Exceptionpoint snapshots")}}async handle(context,args){let getTracepoint=!args.types?.length||args.types.includes("tracepoint"),getLogpoint=!args.types?.length||args.types.includes("logpoint"),getExceptionpoint=!args.types?.length||args.types.includes("exceptionpoint"),trimOpts={maxCallStackDepth:args.maxCallStackDepth,includeScopes:args.includeScopes,maxVariablesPerScope:args.maxVariablesPerScope},tracepointSnapshots=[],logpointSnapshots=[],exceptionpointSnapshots=[],allSnapshots=getSnapshots(context.storeKey),probes=listProbes(context.storeKey);if(args.probeId&&(getTracepoint||getLogpoint)){let raw=getSnapshotsByProbe(context.storeKey,args.probeId),filtered=applySnapshotFilters(raw,args.fromSequence,args.limit),probe=probes.find(p=>p.id===args.probeId);probe?.kind==="tracepoint"&&getTracepoint?tracepointSnapshots=filtered:probe?.kind==="logpoint"&&getLogpoint&&(logpointSnapshots=filtered)}else{if(getTracepoint){let tracepointIds=new Set(probes.filter(p=>p.kind==="tracepoint").map(p=>p.id)),raw=allSnapshots.filter(s=>tracepointIds.has(s.probeId));tracepointSnapshots=applySnapshotFilters(raw,args.fromSequence,args.limit)}if(getLogpoint){let logpointIds=new Set(probes.filter(p=>p.kind==="logpoint").map(p=>p.id)),raw=allSnapshots.filter(s=>logpointIds.has(s.probeId));logpointSnapshots=applySnapshotFilters(raw,args.fromSequence,args.limit)}}if(getExceptionpoint){let raw=allSnapshots.filter(s=>s.probeId==="__exception__");exceptionpointSnapshots=applySnapshotFilters(raw,args.fromSequence,args.limit)}return{tracepointSnapshots:trimSnapshots(tracepointSnapshots,trimOpts),logpointSnapshots:trimSnapshots(logpointSnapshots,trimOpts),exceptionpointSnapshots:trimSnapshots(exceptionpointSnapshots,trimOpts)}}},SNAPSHOT_TYPES3=["tracepoint","logpoint","exceptionpoint"],ClearProbeSnapshots2=class{name(){return"debug_clear-probe-snapshots"}description(){return"\nClears snapshots captured by tracepoints, logpoints, and/or exceptionpoints.\nOptional `types`: array of `tracepoint`, `logpoint`, `exceptionpoint`. If omitted or empty, clears all.\nOptional `probeId`: clear only snapshots for this probe (tracepoint/logpoint).\n ".trim()}inputSchema(){return{types:z47.array(z47.enum(SNAPSHOT_TYPES3)).optional().describe("Which snapshot types to clear. If omitted or empty, all are cleared."),probeId:z47.string().optional().describe("Clear only snapshots for this probe ID (tracepoint or logpoint). Ignored for exceptionpoint.")}}outputSchema(){return{tracepointCleared:z47.number().describe("Tracepoint snapshots cleared"),logpointCleared:z47.number().describe("Logpoint snapshots cleared"),exceptionpointCleared:z47.number().describe("Exceptionpoint snapshots cleared"),message:z47.string().describe("Status message")}}async handle(context,args){let{clearSnapshotsByProbe:clearSnapshotsByProbe2,listProbes:listProbes3}=await import("./core-5DYSM5YY.js"),clearTracepoint=!args.types?.length||args.types.includes("tracepoint"),clearLogpoint=!args.types?.length||args.types.includes("logpoint"),clearExceptionpoint=!args.types?.length||args.types.includes("exceptionpoint"),tracepointCleared=0,logpointCleared=0,exceptionpointCleared=0;if(args.probeId&&(clearTracepoint||clearLogpoint)){let n=clearSnapshotsByProbe2(context.storeKey,args.probeId),p=listProbes3(context.storeKey).find(x=>x.id===args.probeId);clearTracepoint&&clearLogpoint?p?.kind==="tracepoint"?tracepointCleared=n:p?.kind==="logpoint"&&(logpointCleared=n):clearTracepoint?tracepointCleared=n:logpointCleared=n}else{if(clearTracepoint)for(let p of listProbes3(context.storeKey).filter(x=>x.kind==="tracepoint"))tracepointCleared+=clearSnapshotsByProbe2(context.storeKey,p.id);if(clearLogpoint)for(let p of listProbes3(context.storeKey).filter(x=>x.kind==="logpoint"))logpointCleared+=clearSnapshotsByProbe2(context.storeKey,p.id);clearExceptionpoint&&(exceptionpointCleared=clearSnapshotsByProbe2(context.storeKey,"__exception__"))}let parts=[tracepointCleared&&`${tracepointCleared} tracepoint snapshot(s)`,logpointCleared&&`${logpointCleared} logpoint snapshot(s)`,exceptionpointCleared&&`${exceptionpointCleared} exceptionpoint snapshot(s)`].filter(Boolean),message=parts.length>0?`Cleared: ${parts.join(", ")}`:"Nothing to clear";return{tracepointCleared,logpointCleared,exceptionpointCleared,message}}};import{z as z48}from"zod";var GetLogs=class{name(){return"debug_get-logs"}description(){return"Retrieves console messages/logs from the Node.js process with filtering options."}inputSchema(){return{type:z48.enum(getEnumKeyTuples(ConsoleMessageLevelName)).transform(createEnumTransformer(ConsoleMessageLevelName)).optional().describe("Filter by level (this level or higher)."),search:z48.string().optional().describe("Filter by message text."),timestamp:z48.number().int().nonnegative().optional().describe("Only messages at or after this Unix ms."),sequenceNumber:z48.number().int().nonnegative().optional().describe("Incremental: only messages with sequence > this."),limit:z48.object({count:z48.number().int().nonnegative().default(0).describe("0 = no limit."),from:z48.enum(["start","end"]).default("end").describe("Keep from start or end when truncating.")}).optional()}}outputSchema(){return{messages:z48.array(z48.object({type:z48.string().describe("Type of the console message."),text:z48.string().describe("Text of the console message."),location:z48.object({url:z48.string().describe("URL of the resource."),lineNumber:z48.number().nonnegative().describe("0-based line number."),columnNumber:z48.number().nonnegative().describe("0-based column number.")}).describe("Location of the console message.").optional(),timestamp:z48.number().int().nonnegative().describe("Unix epoch timestamp (ms)."),sequenceNumber:z48.number().int().nonnegative().describe("Monotonic sequence number.")})).describe("Retrieved console messages.")}}async handle(context,args){let levelCodeThreshold=args.type?ConsoleMessageLevel[args.type]?.code:void 0,filtered=getConsoleMessages(context.storeKey).filter(msg=>!(levelCodeThreshold!==void 0&&msg.level.code<levelCodeThreshold||args.timestamp&&msg.timestamp<args.timestamp||args.sequenceNumber&&msg.sequenceNumber<=args.sequenceNumber||args.search&&!msg.text.includes(args.search)));return{messages:(args.limit?.count?args.limit.from==="start"?filtered.slice(0,args.limit.count):filtered.slice(-args.limit.count):filtered).map(msg=>({type:msg.type,text:msg.text,location:msg.location?{url:msg.location.url,lineNumber:msg.location.lineNumber,columnNumber:msg.location.columnNumber}:void 0,timestamp:msg.timestamp,sequenceNumber:msg.sequenceNumber}))}}};import{z as z49}from"zod";var REMOVE_TYPES2=["tracepoint","logpoint","watch"],RemoveProbe2=class{name(){return"debug_remove-probe"}description(){return"Removes a tracepoint, logpoint, or watch expression by ID. `type`: tracepoint, logpoint, or watch. `id`: the probe or watch ID."}inputSchema(){return{type:z49.enum(REMOVE_TYPES2).describe("Kind of probe to remove: tracepoint, logpoint, or watch"),id:z49.string().describe("Probe or watch expression ID to remove")}}outputSchema(){return{removed:z49.boolean().describe("Whether the probe or watch was removed"),message:z49.string().describe("Status message")}}async handle(context,args){if(args.type==="watch"){let removed2=removeWatchExpression(context.storeKey,args.id);return{removed:removed2,message:removed2?`Watch expression ${args.id} removed`:`Watch expression ${args.id} not found`}}let removed=await removeProbe(context.storeKey,args.id),label=args.type==="tracepoint"?"Tracepoint":"Logpoint";return{removed,message:removed?`${label} ${args.id} removed`:`Failed to remove ${args.type}`}}},LIST_TYPES2=["tracepoint","logpoint","watch"],ListProbes2=class{name(){return"debug_list-probes"}description(){return"Lists tracepoints, logpoints, and/or watch expressions. Optional `types`: array of `tracepoint`, `logpoint`, `watch`. If omitted or empty, returns all."}inputSchema(){return{types:z49.array(z49.enum(LIST_TYPES2)).optional().describe("Which probe types to list. If omitted or empty, all are returned.")}}outputSchema(){return{tracepoints:z49.array(z49.any()).describe("Tracepoints"),logpoints:z49.array(z49.any()).describe("Logpoints"),watches:z49.array(z49.any()).describe("Watch expressions")}}async handle(context,args){let listTracepoints=!args.types?.length||args.types.includes("tracepoint"),listLogpoints=!args.types?.length||args.types.includes("logpoint"),listWatches=!args.types?.length||args.types.includes("watch"),probes=listProbes(context.storeKey),tracepoints=listTracepoints?probes.filter(p=>p.kind==="tracepoint").map(p=>({id:p.id,urlPattern:p.urlPattern,lineNumber:p.lineNumber,columnNumber:p.columnNumber,condition:p.condition,hitCondition:p.hitCondition,hitCount:p.hitCount,resolvedLocations:p.resolvedLocations,enabled:p.enabled})):[],logpoints=listLogpoints?probes.filter(p=>p.kind==="logpoint").map(p=>({id:p.id,urlPattern:p.urlPattern,lineNumber:p.lineNumber,logExpression:p.logExpression,condition:p.condition,hitCondition:p.hitCondition,hitCount:p.hitCount,resolvedLocations:p.resolvedLocations,enabled:p.enabled})):[],watches=listWatches?listWatchExpressions(context.storeKey):[];return{tracepoints,logpoints,watches}}},PROBE_TYPES2=["tracepoint","logpoint","watches"],ClearProbes2=class{name(){return"debug_clear-probes"}description(){return"Removes tracepoints, logpoints, and/or watch expressions. Optional `types`: array of `tracepoint`, `logpoint`, `watches`. If omitted or empty, clears all."}inputSchema(){return{types:z49.array(z49.enum(PROBE_TYPES2)).optional().describe("Which probe types to clear. If omitted or empty, all are cleared.")}}outputSchema(){return{tracepointsCleared:z49.number().describe("Number of tracepoints cleared"),logpointsCleared:z49.number().describe("Number of logpoints cleared"),watchesCleared:z49.number().describe("Number of watch expressions cleared"),message:z49.string().describe("Status message")}}async handle(context,args){let clearTracepoints=!args.types?.length||args.types.includes("tracepoint"),clearLogpoints=!args.types?.length||args.types.includes("logpoint"),clearWatches=!args.types?.length||args.types.includes("watches"),tracepointsCleared=0,logpointsCleared=0,watchesCleared=0;if(clearTracepoints||clearLogpoints){let probes=listProbes(context.storeKey);for(let p of probes)p.kind==="tracepoint"&&clearTracepoints?await removeProbe(context.storeKey,p.id)&&tracepointsCleared++:p.kind==="logpoint"&&clearLogpoints&&await removeProbe(context.storeKey,p.id)&&logpointsCleared++}clearWatches&&(watchesCleared=clearWatchExpressions(context.storeKey));let parts=[];tracepointsCleared&&parts.push(`${tracepointsCleared} tracepoint(s)`),logpointsCleared&&parts.push(`${logpointsCleared} logpoint(s)`),watchesCleared&&parts.push(`${watchesCleared} watch(es)`);let message=parts.length>0?`Cleared: ${parts.join(", ")}`:"Nothing to clear";return{tracepointsCleared,logpointsCleared,watchesCleared,message}}};import{z as z50}from"zod";var PutExceptionpoint2=class{name(){return"debug_put-exceptionpoint"}description(){return`
|
|
764
768
|
Sets the exception tracepoint state for a Node.js process:
|
|
765
769
|
- "none": Don't capture on exceptions
|
|
766
770
|
- "uncaught": Capture only on uncaught exceptions
|
package/dist/daemon-server.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Execute,init,shutdown,trackToolCalled}from"./core-UNMLWWDL.js";import{ToolRegistry,isToolEnabled,platformInfo}from"./core-
|
|
1
|
+
import{Execute,init,shutdown,trackToolCalled}from"./core-UNMLWWDL.js";import{ToolRegistry,isToolEnabled,platformInfo}from"./core-RE2PLB4I.js";import{AVAILABLE_TOOL_DOMAINS,DAEMON_PORT,DAEMON_SESSION_IDLE_CHECK_SECONDS,DAEMON_SESSION_IDLE_SECONDS,debug,enable,error,info,isDebugEnabled}from"./core-3YBKJFSF.js";import{createRequire}from"node:module";import{Command,Option,InvalidOptionArgumentError}from"commander";import{serve}from"@hono/node-server";import{Hono}from"hono";import{cors}from"hono/cors";import{z}from"zod";var require2=createRequire(import.meta.url),daemonStartTime=0,daemonPort=0,app=new Hono,sessions=new Map,DEFAULT_SESSION_ID="#default",ERRORS={get sessionNotFound(){return _buildErrorResponse(404,"Session Not Found")},get toolNotFound(){return _buildErrorResponse(404,"Tool Not Found")},get internalServerError(){return _buildErrorResponse(500,"Internal Server Error")}};function _buildErrorResponse(code,message){return{error:{code,message}}}async function _closeSession(session){session.closed=!0;try{await session.context.close(),debug("Closed MCP session context")}catch(err){error("Error occurred while closing MCP session context",err)}sessions.delete(session.id)}async function _createSession(ctx,sessionId){let now=Date.now(),session={id:sessionId,context:await platformInfo.toolsInfo.createToolSessionContext(()=>sessionId),toolExecutor:platformInfo.toolsInfo.createToolExecutor(),closed:!1,createdAt:now,lastActiveAt:now};return debug(`Created session with id ${sessionId}`),session}function _getSessionInfo(session){let now=Date.now();return{id:session.id,createdAt:session.createdAt,lastActiveAt:session.lastActiveAt,idleSeconds:Math.floor((now-session.lastActiveAt)/1e3)}}async function _getSession(ctx){let sessionId=ctx.req.header("session-id")||DEFAULT_SESSION_ID;return sessions.get(sessionId)}async function _getOrCreateSession(ctx){let sessionId=ctx.req.header("session-id")||DEFAULT_SESSION_ID,session=sessions.get(sessionId);return session?debug(`Reusing session with id ${sessionId}`):(debug(`No session could be found with id ${sessionId}`),session=await _createSession(ctx,sessionId),sessions.set(sessionId,session)),session}function _scheduleIdleSessionCheck(){let noActiveSession=!1;setInterval(()=>{let currentTime=Date.now();noActiveSession&&sessions.size===0&&(info("No active session found, so terminating daemon server"),process.exit(0));for(let[sessionId,session]of sessions)session.closed||(debug(`Checking whether session with id ${sessionId} is idle or not ...`),currentTime-session.lastActiveAt>DAEMON_SESSION_IDLE_SECONDS*1e3&&(debug(`Session with id ${sessionId} is idle, so it will be closing ...`),_closeSession(session).then(()=>{debug(`Session with id ${sessionId} was idle, so it has been closed`)}).catch(err=>{error(`Unable to delete idle session with id ${sessionId}`,err)})));noActiveSession=sessions.size===0},DAEMON_SESSION_IDLE_CHECK_SECONDS*1e3)}async function _logRequest(ctx){let reqClone=ctx.req.raw.clone();debug(`Got request: ${await reqClone.json()}`)}async function startDaemonHTTPServer(port){init({source:"cli"});let allowedDomains=AVAILABLE_TOOL_DOMAINS,allTools=platformInfo.toolsInfo.tools.filter(tool=>isToolEnabled(tool)),toolsToExpose=allowedDomains===void 0?allTools:allTools.filter(tool=>{let domain=tool.name().split("_")[0]?.toLowerCase()??"";return allowedDomains.has(domain)}),toolRegistry=new ToolRegistry;for(let tool of toolsToExpose)toolRegistry.addTool(tool);let executeTool=new Execute(toolRegistry,platformInfo.toolsInfo.executeImportantDescription,platformInfo.toolsInfo.executeDescription),toolMap=Object.fromEntries(toolsToExpose.map(tool=>[tool.name(),tool]));toolMap[executeTool.name()]=executeTool,app.use("*",cors({origin:"*",allowMethods:["GET","POST","DELETE","OPTIONS"],allowHeaders:["Content-Type","Authorization","session-id"]})),daemonPort=port,daemonStartTime=Date.now();let gracefulShutdown=async signal=>{info(`Received ${signal}, initiating graceful shutdown...`);let closePromises=[];for(let session of sessions.values())closePromises.push(_closeSession(session));await Promise.allSettled(closePromises),await shutdown(),info("All sessions closed, exiting..."),process.exit(0)};process.on("SIGTERM",()=>gracefulShutdown("SIGTERM")),process.on("SIGINT",()=>gracefulShutdown("SIGINT")),process.on("uncaughtException",err=>{error("Uncaught exception",err)}),process.on("unhandledRejection",reason=>{error("Unhandled rejection",reason)}),app.get("/health",ctx=>ctx.json({status:"ok"})),app.get("/info",ctx=>{let info2={version:require2("../package.json").version,uptime:Math.floor((Date.now()-daemonStartTime)/1e3),sessionCount:sessions.size,port:daemonPort};return ctx.json(info2)}),app.get("/sessions",ctx=>{let sessionList=[];for(let session of sessions.values())sessionList.push(_getSessionInfo(session));return ctx.json({sessions:sessionList})}),app.get("/session",async ctx=>{let session=await _getSession(ctx);return session?ctx.json(_getSessionInfo(session)):ctx.json(ERRORS.sessionNotFound,404)}),app.post("/shutdown",async ctx=>{info("Shutdown request received, closing all sessions...");let closePromises=[];for(let session of sessions.values())closePromises.push(_closeSession(session));return await Promise.allSettled(closePromises),await shutdown(),info("All sessions closed, shutting down daemon server..."),setTimeout(()=>{process.exit(0)},500),ctx.json({status:"shutting_down"},200)}),app.post("/call",async ctx=>{try{isDebugEnabled()&&await _logRequest(ctx);let session=await _getOrCreateSession(ctx);session.lastActiveAt=Date.now();let toolCallRequest=await ctx.req.json(),tool=toolMap[toolCallRequest.toolName];if(!tool)return ctx.json(ERRORS.toolNotFound,404);let toolInput;try{toolInput=z.object(tool.inputSchema()).parse(toolCallRequest.toolInput)}catch(err){let errorMessage=err.errors&&Array.isArray(err.errors)?err.errors.map(e=>`${e.path?.join(".")||"input"}: ${e.message}`).join("; "):"Invalid tool input";return ctx.json(_buildErrorResponse(400,`Invalid Tool Request: ${errorMessage}`),400)}let toolStartTime=Date.now();try{let toolOutput=await session.toolExecutor.executeTool(session.context,tool,toolInput);trackToolCalled({toolName:tool.name(),durationMs:Date.now()-toolStartTime,success:!0,sessionId:session.id});let toolCallResponse={toolOutput};return ctx.json(toolCallResponse,200)}catch(err){trackToolCalled({toolName:toolCallRequest.toolName,durationMs:Date.now()-toolStartTime,success:!1,sessionId:session.id,error:err});let toolCallResponse={toolError:{code:err.code,message:err.message}};return ctx.json(toolCallResponse,500)}}catch(err){return error("Error occurred while handling tool call request",err),ctx.json(ERRORS.internalServerError,500)}}),app.delete("/session",async ctx=>{try{let session=await _getSession(ctx);return session?(await _closeSession(session),ctx.json({ok:!0},200)):ctx.json(ERRORS.sessionNotFound,404)}catch(err){return error("Error occurred while deleting session",err),ctx.json(ERRORS.internalServerError,500)}}),app.onError((err,ctx)=>(error("Unhandled error in request handler",err),ctx.json({error:{code:500,message:"Internal Server Error"}},500))),app.notFound(ctx=>ctx.json({error:"Not Found",status:404},404)),serve({fetch:app.fetch,port},()=>info(`Listening on port ${port}`)),_scheduleIdleSessionCheck()}var isMainModule=import.meta.url===`file://${process.argv[1]}`||import.meta.url===`file://${process.argv[1]}.mjs`||process.argv[1]?.endsWith("daemon-server.js")||process.argv[1]?.endsWith("daemon-server.mjs");if(isMainModule){let parsePort=function(value){let n=Number(value);if(!Number.isInteger(n)||n<1||n>65535)throw new InvalidOptionArgumentError("port must be an integer between 1 and 65535");return n};parsePort2=parsePort;let options=new Command().addOption(new Option("--port <number>","port for daemon HTTP server").argParser(parsePort).default(DAEMON_PORT)).allowUnknownOption().parse(process.argv).opts();enable(),info("Starting daemon HTTP server..."),startDaemonHTTPServer(options.port).then(()=>{info("Daemon HTTP server started")}).catch(err=>{error("Failed to start daemon HTTP server",err),process.exit(1)})}var parsePort2;export{startDaemonHTTPServer};
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{Execute,init,shutdown,trackToolCalled}from"./core-UNMLWWDL.js";import{ToolRegistry,isToolEnabled,platformInfo}from"./core-
|
|
2
|
+
import{Execute,init,shutdown,trackToolCalled}from"./core-UNMLWWDL.js";import{ToolRegistry,isToolEnabled,platformInfo}from"./core-RE2PLB4I.js";import{AVAILABLE_TOOL_DOMAINS,PORT,SESSION_CLOSE_ON_SOCKET_CLOSE,SESSION_IDLE_CHECK_SECONDS,SESSION_IDLE_SECONDS,TOOL_OUTPUT_SCHEMA_DISABLE,debug,disable,enable,error,info,isDebugEnabled}from"./core-3YBKJFSF.js";import{createRequire}from"node:module";var require2=createRequire(import.meta.url),SERVER_NAME="browser-devtools-mcp",SERVER_VERSION=require2("../package.json").version;function getServerInstructions(){if(!platformInfo.serverInfo.instructions)return;let parts=[];return parts.push(platformInfo.serverInfo.instructions),parts.join(`
|
|
3
3
|
|
|
4
4
|
`).trim()}function getServerPolicies(){return platformInfo.serverInfo.policies}import crypto from"node:crypto";import{StreamableHTTPTransport}from"@hono/mcp";import{serve}from"@hono/node-server";import{McpServer}from"@modelcontextprotocol/sdk/server/mcp.js";import{StdioServerTransport}from"@modelcontextprotocol/sdk/server/stdio.js";import{Hono}from"hono";import{cors}from"hono/cors";var MCP_TEMPLATE={jsonrpc:"2.0",error:{code:0,message:"N/A"},id:null},MCP_ERRORS={get sessionNotFound(){return _buildMCPErrorResponse(-32001,"Session Not Found")},get unauthorized(){return _buildMCPErrorResponse(-32001,"Unauthorized")},get internalServerError(){return _buildMCPErrorResponse(-32603,"Internal Server Error")}},sessions=new Map;function _buildMCPErrorResponse(code,message){let result={...MCP_TEMPLATE};return result.error.code=code,result.error.message=message,result}function _getImage(response){if("image"in response&&response.image!==null&&typeof response.image=="object"&&"data"in response.image&&"mimeType"in response.image&&Buffer.isBuffer(response.image.data)&&typeof response.image.mimeType=="string"){let image=response.image;return delete response.image,image}}function _toResponse(response){let image=_getImage(response),contents=[];return contents.push({type:"text",text:JSON.stringify(response,null,2)}),image&&(image.mimeType==="image/svg+xml"?contents.push({type:"text",text:image.data.toString(),mimeType:image.mimeType}):contents.push({type:"image",data:image.data.toString("base64"),mimeType:image.mimeType})),{content:contents,structuredContent:response,isError:!1}}function _createServer(opts){let server=new McpServer({name:SERVER_NAME,version:SERVER_VERSION},{capabilities:{resources:{},tools:{}},instructions:getServerInstructions()}),messages=[],policies=getServerPolicies();if(policies)for(let policy of policies)messages.push({role:"user",content:{type:"text",text:policy}});server.registerPrompt("default_system",{title:"Default System Prompt",description:"General behavior for the AI assistant"},async()=>({description:"Defines the assistant's general reasoning and tool usage rules.",messages}));let toolExecutor=platformInfo.toolsInfo.createToolExecutor(),fallbackSessionId=crypto.randomUUID(),toolSessionContext,createToolCallback=tool=>async args=>{let startTime=Date.now(),session=opts.sessionProvider?opts.sessionProvider():void 0,sessionId=session?.id||fallbackSessionId,clientName=server.server.getClientVersion()?.name;try{toolSessionContext||(session&&(toolSessionContext=session.context),toolSessionContext||(toolSessionContext=await platformInfo.toolsInfo.createToolSessionContext(()=>sessionId),session&&(session.context=toolSessionContext)));let response=await toolExecutor.executeTool(toolSessionContext,tool,args);return trackToolCalled({toolName:tool.name(),durationMs:Date.now()-startTime,success:!0,sessionId,clientName}),_toResponse(response)}catch(error2){return trackToolCalled({toolName:tool.name(),durationMs:Date.now()-startTime,success:!1,error:error2,sessionId,clientName}),{content:[{type:"text",text:`Error: ${error2.message}`}],isError:!0}}},includeOutputSchema=!TOOL_OUTPUT_SCHEMA_DISABLE,allowedDomains=AVAILABLE_TOOL_DOMAINS,toolRegistry=new ToolRegistry;platformInfo.toolsInfo.tools.forEach(t=>{if(!isToolEnabled(t)){debug(`Skipping tool ${t.name()} (isEnabled returned false)`);return}let domain=t.name().split("_")[0]?.toLowerCase()??"";if(allowedDomains&&!allowedDomains.has(domain)){debug(`Skipping tool ${t.name()} (domain ${domain} not in AVAILABLE_TOOL_DOMAINS)`);return}debug(`Registering tool ${t.name()} ...`);let toolOptions=includeOutputSchema?{description:t.description(),inputSchema:t.inputSchema(),outputSchema:t.outputSchema()}:{description:t.description(),inputSchema:t.inputSchema()};server.registerTool(t.name(),toolOptions,createToolCallback(t)),toolRegistry.addTool(t)});let executeTool=new Execute(toolRegistry,platformInfo.toolsInfo.executeImportantDescription,platformInfo.toolsInfo.executeDescription),executeToolOptions=includeOutputSchema?{description:executeTool.description(),inputSchema:executeTool.inputSchema(),outputSchema:executeTool.outputSchema()}:{description:executeTool.description(),inputSchema:executeTool.inputSchema()};return server.registerTool(executeTool.name(),executeToolOptions,createToolCallback(executeTool)),server}async function _createAndConnectServer(transport,opts){let server=_createServer({config:opts.config,sessionProvider:()=>sessions.get(transport.sessionId),transportType:opts.transportType});return await server.connect(transport),server}function _getConfig(){return{}}function _createSession(id,ctx,transport,server,closeOnTransportClose=!0){let session={id,transport,server,closed:!1,lastActiveAt:Date.now()},socket=ctx.env.incoming.socket;return socket._mcpRegistered||(socket._mcpRegistered=!0,socket.on("close",async()=>{debug(`Socket, which is for MCP session with id ${transport.sessionId}, has been closed`),SESSION_CLOSE_ON_SOCKET_CLOSE&&await transport.close()})),_registerMCPSessionClose(transport,session.server,closeOnTransportClose),debug(`Created MCP server session with id ${transport.sessionId}`),session}function _createOrUpdateSession(id,ctx,transport,server,closeOnTransportClose=!0){let session=sessions.get(id);return session?(session.transport=transport,session.server=server,session.closed=!1,session.lastActiveAt=Date.now(),session):_createSession(id,ctx,transport,server,closeOnTransportClose)}async function _createTransport(ctx){let serverConfig=_getConfig(),holder={},useSessionId=ctx.req.header("mcp-use-session-id"),transport=new StreamableHTTPTransport({enableJsonResponse:!0,sessionIdGenerator:()=>useSessionId||crypto.randomUUID(),onsessioninitialized:async sessionId=>{let session=_createOrUpdateSession(sessionId,ctx,transport,holder.server);sessions.set(sessionId,session),debug(`MCP session initialized with id ${sessionId}`)},onsessionclosed:async sessionId=>{debug(`Closing MCP session closed with id ${sessionId} ...`),await transport.close(),debug(`MCP session closed with id ${sessionId}`)}});return holder.server=await _createAndConnectServer(transport,{config:serverConfig,transportType:"streamable-http"}),transport}function _getSessionId(ctx){return ctx.req.header("mcp-session-id")}async function _getTransport(ctx){let sessionId=_getSessionId(ctx);if(sessionId){let session=sessions.get(sessionId);if(session)return debug(`Reusing MCP session with id ${sessionId}`),session.transport}}async function _getOrCreateTransport(ctx){let sessionId=_getSessionId(ctx);if(sessionId){let session=sessions.get(sessionId);if(session)return debug(`Reusing MCP session with id ${sessionId}`),session.transport;debug(`No MCP session could be found with id ${sessionId}`);return}return await _createTransport(ctx)}function _registerMCPSessionClose(transport,mcpServer,closeSession=!0){let closed=!1;transport.onclose=async()=>{if(debug(`Closing MCP session with id ${transport.sessionId} ...`),closed){debug(`MCP session with id ${transport.sessionId} has already been closed`);return}closed=!0;try{await mcpServer.close(),debug("Closed MCP server")}catch(err){error("Error occurred while closing MCP server",err)}if(closeSession&&transport.sessionId){let session=sessions.get(transport.sessionId);if(session&&(session.closed=!0,session.context))try{await session.context.close(),debug("Closed MCP session context")}catch(err){error("Error occurred while closing MCP session context",err)}sessions.delete(transport.sessionId),debug(`Closing MCP session with id ${transport.sessionId} ...`)}}}function _scheduleIdleSessionCheck(){setInterval(()=>{let currentTime=Date.now();for(let[sessionId,session]of sessions)debug(`Checking whether session with id ${sessionId} is idle or not ...`),currentTime-session.lastActiveAt>SESSION_IDLE_SECONDS*1e3&&(debug(`Session with id ${sessionId} is idle, so it will be closing ...`),session.transport.close().then(()=>{debug(`Session with id ${sessionId} was idle, so it has been closed`)}).catch(err=>{error(`Unable to delete idle session with id ${sessionId}`,err)}))},SESSION_IDLE_CHECK_SECONDS*1e3)}async function _logRequest(ctx){let reqClone=ctx.req.raw.clone();debug(`Got request: ${await reqClone.text()}`)}function _markSessionAsActive(ctx){let sessionId=_getSessionId(ctx);if(sessionId){let session=sessions.get(sessionId);session&&(session.lastActiveAt=Date.now())}}async function startStdioServer(){init({source:"mcp"});let transport=new StdioServerTransport;await _createAndConnectServer(transport,{config:_getConfig(),transportType:"stdio"})}var app=new Hono;async function startStreamableHTTPServer(port){init({source:"mcp"}),app.use("*",cors({origin:"*",allowMethods:["GET","POST","OPTIONS"],allowHeaders:["Content-Type","Authorization","MCP-Protocol-Version"]})),app.get("/health",ctx=>ctx.json({status:"ok"})),app.get("/ping",ctx=>ctx.json({status:"ok",message:"pong"})),app.get("/mcp",ctx=>ctx.json({status:"ok",protocol:"model-context-protocol",version:"1.0"})),app.post("/mcp",async ctx=>{try{isDebugEnabled()&&await _logRequest(ctx);let transport=await _getOrCreateTransport(ctx);return transport?(_markSessionAsActive(ctx),await transport.handleRequest(ctx)):ctx.json(MCP_ERRORS.sessionNotFound,400)}catch(err){return error("Error occurred while handling MCP request",err),ctx.json(MCP_ERRORS.internalServerError,500)}}),app.delete("/mcp",async ctx=>{try{let transport=await _getTransport(ctx);return transport?(await transport.close(),ctx.json({ok:!0},200)):ctx.json(MCP_ERRORS.sessionNotFound,400)}catch(err){return error("Error occurred while deleting MCP session",err),ctx.json(MCP_ERRORS.internalServerError,500)}}),app.notFound(ctx=>ctx.json({error:"Not Found",status:404},404)),serve({fetch:app.fetch,port},()=>info(`Listening on port ${port}`)),_scheduleIdleSessionCheck()}import{Command,Option,InvalidOptionArgumentError}from"commander";function _parsePort(value){let n=Number(value);if(!Number.isInteger(n)||n<1||n>65535)throw new InvalidOptionArgumentError("port must be an integer between 1 and 65535");return n}function _getOptions(){return new Command().addOption(new Option("--transport <type>","transport type").choices(["stdio","streamable-http"]).default("stdio")).addOption(new Option("--port <number>","port for Streamable HTTP transport").argParser(_parsePort).default(PORT)).allowUnknownOption().allowExcessArguments().parse(process.argv).opts()}async function _shutdown(){await shutdown()}async function main(){let options=_getOptions();for(let signal of["SIGTERM","SIGINT"])process.on(signal,async()=>{await _shutdown(),process.exit(0)});options.transport==="stdio"?(disable(),await startStdioServer()):options.transport==="streamable-http"?(info("Starting MCP server..."),await startStreamableHTTPServer(options.port),info("Started MCP Server")):(error(`Invalid transport: ${options.transport}`),process.exit(1))}main().catch(async err=>{enable(),error("MCP server error",err),await _shutdown(),process.exit(1)});
|