Skip to content

Commit 15d679c

Browse files
steipeteclaude
andcommitted
feat(chrome-devtools): route autoConnect through OpenClaw extension relay
When the OpenClaw Chrome-extension relay is paired and live on this host, rewrite chrome-devtools-mcp --autoConnect to --wsEndpoint/--wsHeaders against the relay's loopback CDP endpoint. Same signed-in Chrome, but no per-session "Allow remote debugging?" dialog (Chrome M144+). Best-effort: missing secret, dead relay, or unpaired extension falls back to plain autoConnect. Opt out with MCPORTER_DISABLE_CHROME_DEVTOOLS_RELAY=1; override the loopback relay URL with MCPORTER_CHROME_DEVTOOLS_RELAY_URL. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 7259c8c commit 15d679c

5 files changed

Lines changed: 261 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### CLI
66

7+
- Route `chrome-devtools-mcp --autoConnect` through a paired OpenClaw Chrome-extension relay when one is live on this host, eliminating Chrome's per-session "Allow remote debugging?" dialog; falls back to plain auto-connect otherwise (`MCPORTER_DISABLE_CHROME_DEVTOOLS_RELAY=1` to opt out).
78
- Correct the documented call timeout to 60 seconds and distinguish its `MCPORTER_CALL_TIMEOUT` and `--timeout` overrides from the 30-second list timeout. (PR #230, thanks @KrasimirKralev)
89
- Isolate long-lived SSE receive streams from ordinary HTTP requests so byte-idle servers cannot stall tool listing and calls. (PR #234, thanks @umutkeltek)
910

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ What MCPorter handles for you:
413413
- Stdio commands inherit the directory of the file that defined them (imports or local config).
414414
- Import precedence matches the array order; omit `imports` to use the default `["cursor", "claude-code", "claude-desktop", "codex", "windsurf", "opencode", "vscode"]`.
415415
- `chrome-devtools-mcp --autoConnect` receives a small compatibility patch while upstream auto-connect can hang on busy Chrome profiles; set `MCPORTER_DISABLE_CHROME_DEVTOOLS_COMPAT=1` to opt out.
416+
- When the [OpenClaw Chrome extension relay](https://docs.openclaw.ai/tools/chrome-extension/) is paired on this host, `chrome-devtools-mcp --autoConnect` is transparently rewritten to the relay's loopback CDP endpoint — driving the same signed-in Chrome with no "Allow remote debugging?" dialog at all. mcporter probes `http://127.0.0.1:18799` (override with `MCPORTER_CHROME_DEVTOOLS_RELAY_URL`, loopback only) and falls back to plain `--autoConnect` when the relay is down or unpaired; set `MCPORTER_DISABLE_CHROME_DEVTOOLS_RELAY=1` to opt out.
416417

417418
#### OAuth-protected servers
418419

‎src/chrome-devtools-relay.ts‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import fs from 'node:fs';
2+
import os from 'node:os';
3+
import path from 'node:path';
4+
5+
/**
6+
* OpenClaw browser-extension relay rewrite for chrome-devtools-mcp.
7+
*
8+
* `--autoConnect` attaches through Chrome's M144+ remote-debugging handshake,
9+
* which pops a per-session "Allow remote debugging?" dialog. When the OpenClaw
10+
* Chrome extension relay is paired on this host, the same real profile is
11+
* reachable over a loopback CDP endpoint with no dialog at all, so mcporter
12+
* rewrites the server args to connect there instead. Everything is best-effort:
13+
* if the relay secret is missing, the relay is down, or the extension is not
14+
* paired, the original --autoConnect args are kept unchanged.
15+
*/
16+
17+
const AUTO_CONNECT_FLAGS = new Set(['--autoConnect', '--auto-connect']);
18+
const RELAY_PROBE_TIMEOUT_MS = 750;
19+
const DEFAULT_RELAY_URL = 'http://127.0.0.1:18799';
20+
21+
export interface ChromeDevtoolsRelayRewrite {
22+
readonly args: readonly string[];
23+
readonly applied: boolean;
24+
readonly endpoint?: string;
25+
}
26+
27+
export interface ChromeDevtoolsRelayProbeOptions {
28+
readonly readToken?: () => string | undefined;
29+
readonly probe?: (versionUrl: string, token: string) => Promise<boolean>;
30+
}
31+
32+
function isChromeDevtoolsToken(token: string): boolean {
33+
return (
34+
token === 'chrome-devtools-mcp' ||
35+
token.startsWith('chrome-devtools-mcp@') ||
36+
token.includes('/chrome-devtools-mcp')
37+
);
38+
}
39+
40+
function relayBaseUrl(env: NodeJS.ProcessEnv): URL | undefined {
41+
const raw = env.MCPORTER_CHROME_DEVTOOLS_RELAY_URL?.trim() || DEFAULT_RELAY_URL;
42+
let url: URL;
43+
try {
44+
url = new URL(raw);
45+
} catch {
46+
return undefined;
47+
}
48+
// The relay is loopback-only and the Bearer token rides plain HTTP headers.
49+
if (url.protocol !== 'http:' || !['127.0.0.1', 'localhost', '[::1]', '::1'].includes(url.hostname)) {
50+
return undefined;
51+
}
52+
return url;
53+
}
54+
55+
function defaultReadToken(env: NodeJS.ProcessEnv): string | undefined {
56+
const stateDir = env.OPENCLAW_STATE_DIR?.trim() || path.join(os.homedir(), '.openclaw');
57+
const secretPath = path.join(stateDir, 'credentials', 'browser-extension-relay.secret');
58+
let raw: string;
59+
try {
60+
raw = fs.readFileSync(secretPath, 'utf8');
61+
} catch {
62+
return undefined;
63+
}
64+
const token = raw.trim();
65+
return /^[0-9a-f]{64}$/.test(token) ? token : undefined;
66+
}
67+
68+
async function defaultProbe(versionUrl: string, token: string): Promise<boolean> {
69+
const controller = new AbortController();
70+
const timer = setTimeout(() => controller.abort(), RELAY_PROBE_TIMEOUT_MS);
71+
timer.unref?.();
72+
try {
73+
const response = await fetch(versionUrl, {
74+
headers: { Authorization: `Bearer ${token}` },
75+
signal: controller.signal,
76+
});
77+
// 200 requires a paired, connected extension; 503 means relay up but
78+
// unpaired, where --autoConnect (with its dialog) still beats failing.
79+
return response.status === 200;
80+
} catch {
81+
return false;
82+
} finally {
83+
clearTimeout(timer);
84+
}
85+
}
86+
87+
export function shouldAttemptChromeDevtoolsRelay(
88+
command: string,
89+
args: readonly string[],
90+
env: NodeJS.ProcessEnv = process.env
91+
): boolean {
92+
if (env.MCPORTER_DISABLE_CHROME_DEVTOOLS_RELAY === '1') {
93+
return false;
94+
}
95+
const tokens = [command, ...args];
96+
return tokens.some(isChromeDevtoolsToken) && args.some((arg) => AUTO_CONNECT_FLAGS.has(arg));
97+
}
98+
99+
/**
100+
* Rewrite `--autoConnect` to `--wsEndpoint/--wsHeaders` against a live, paired
101+
* OpenClaw extension relay. Returns the original args when the relay is not
102+
* usable for any reason.
103+
*/
104+
export async function rewriteChromeDevtoolsArgsForRelay(
105+
command: string,
106+
args: readonly string[],
107+
env: NodeJS.ProcessEnv = process.env,
108+
options: ChromeDevtoolsRelayProbeOptions = {}
109+
): Promise<ChromeDevtoolsRelayRewrite> {
110+
if (!shouldAttemptChromeDevtoolsRelay(command, args, env)) {
111+
return { args, applied: false };
112+
}
113+
const base = relayBaseUrl(env);
114+
if (!base) {
115+
return { args, applied: false };
116+
}
117+
const token = (options.readToken ?? (() => defaultReadToken(env)))();
118+
if (!token) {
119+
return { args, applied: false };
120+
}
121+
const versionUrl = new URL('/json/version', base).toString();
122+
const alive = await (options.probe ?? defaultProbe)(versionUrl, token);
123+
if (!alive) {
124+
return { args, applied: false };
125+
}
126+
const wsEndpoint = `ws://${base.host}/cdp`;
127+
const rewritten = args.filter((arg) => !AUTO_CONNECT_FLAGS.has(arg));
128+
rewritten.push('--wsEndpoint', wsEndpoint, '--wsHeaders', JSON.stringify({ Authorization: `Bearer ${token}` }));
129+
return { args: rewritten, applied: true, endpoint: wsEndpoint };
130+
}

‎src/runtime/transport.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
44
import { StreamableHTTPClientTransport, StreamableHTTPError } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
55
import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
66
import { applyChromeDevtoolsCompat } from '../chrome-devtools-compat.js';
7+
import { rewriteChromeDevtoolsArgsForRelay } from '../chrome-devtools-relay.js';
78
import type { ServerDefinition } from '../config.js';
89
import { resolveEnvValue, withEnvOverrides } from '../env.js';
910
import { analyzeConnectionError } from '../error-classifier.js';
@@ -320,7 +321,12 @@ async function createStdioClientContext(
320321
? { ...process.env, ...resolvedEnvOverrides }
321322
: { ...process.env };
322323
const command = resolveCommandArgument(definition.command.command);
323-
const commandArgs = resolveCommandArguments(definition.command.args);
324+
const resolvedArgs = resolveCommandArguments(definition.command.args);
325+
const relay = await rewriteChromeDevtoolsArgsForRelay(command, resolvedArgs, mergedEnv as NodeJS.ProcessEnv);
326+
if (relay.applied) {
327+
logger.info(`Routing chrome-devtools-mcp through the OpenClaw extension relay at ${relay.endpoint} (no dialog).`);
328+
}
329+
const commandArgs = [...relay.args];
324330
const compat = applyChromeDevtoolsCompat(mergedEnv as Record<string, string>, command, commandArgs);
325331
if (compat.applied) {
326332
logger.info(`Injecting chrome-devtools-mcp --autoConnect compatibility patch from ${compat.patchPath}.`);
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { afterEach, describe, expect, it } from 'vitest';
2+
import { rewriteChromeDevtoolsArgsForRelay, shouldAttemptChromeDevtoolsRelay } from '../src/chrome-devtools-relay.js';
3+
4+
const TOKEN = 'a'.repeat(64);
5+
const AUTO_ARGS = ['-y', 'chrome-devtools-mcp@latest', '--autoConnect'];
6+
7+
describe('chrome-devtools OpenClaw relay rewrite', () => {
8+
afterEach(() => {
9+
delete process.env.MCPORTER_DISABLE_CHROME_DEVTOOLS_RELAY;
10+
delete process.env.MCPORTER_CHROME_DEVTOOLS_RELAY_URL;
11+
});
12+
13+
it('only considers autoConnect chrome-devtools commands', () => {
14+
expect(shouldAttemptChromeDevtoolsRelay('npx', AUTO_ARGS)).toBe(true);
15+
expect(shouldAttemptChromeDevtoolsRelay('npx', ['-y', 'chrome-devtools-mcp@latest'])).toBe(false);
16+
expect(shouldAttemptChromeDevtoolsRelay('npx', ['-y', 'other-mcp', '--autoConnect'])).toBe(false);
17+
});
18+
19+
it('allows opting out via env', () => {
20+
expect(shouldAttemptChromeDevtoolsRelay('npx', AUTO_ARGS, { MCPORTER_DISABLE_CHROME_DEVTOOLS_RELAY: '1' })).toBe(
21+
false
22+
);
23+
});
24+
25+
it('rewrites autoConnect to wsEndpoint against a live paired relay', async () => {
26+
const probed: string[] = [];
27+
const result = await rewriteChromeDevtoolsArgsForRelay(
28+
'npx',
29+
AUTO_ARGS,
30+
{},
31+
{
32+
readToken: () => TOKEN,
33+
probe: async (url, token) => {
34+
probed.push(url, token);
35+
return true;
36+
},
37+
}
38+
);
39+
40+
expect(result.applied).toBe(true);
41+
expect(probed).toEqual(['http://127.0.0.1:18799/json/version', TOKEN]);
42+
expect(result.args).toEqual([
43+
'-y',
44+
'chrome-devtools-mcp@latest',
45+
'--wsEndpoint',
46+
'ws://127.0.0.1:18799/cdp',
47+
'--wsHeaders',
48+
JSON.stringify({ Authorization: `Bearer ${TOKEN}` }),
49+
]);
50+
expect(result.endpoint).toBe('ws://127.0.0.1:18799/cdp');
51+
});
52+
53+
it('honors a custom loopback relay URL', async () => {
54+
const result = await rewriteChromeDevtoolsArgsForRelay(
55+
'npx',
56+
AUTO_ARGS,
57+
{ MCPORTER_CHROME_DEVTOOLS_RELAY_URL: 'http://localhost:18798' },
58+
{ readToken: () => TOKEN, probe: async () => true }
59+
);
60+
61+
expect(result.applied).toBe(true);
62+
expect(result.args).toContain('ws://localhost:18798/cdp');
63+
});
64+
65+
it('rejects non-loopback relay URLs', async () => {
66+
const result = await rewriteChromeDevtoolsArgsForRelay(
67+
'npx',
68+
AUTO_ARGS,
69+
{ MCPORTER_CHROME_DEVTOOLS_RELAY_URL: 'http://relay.example.com:18799' },
70+
{ readToken: () => TOKEN, probe: async () => true }
71+
);
72+
73+
expect(result).toEqual({ args: AUTO_ARGS, applied: false });
74+
});
75+
76+
it('keeps autoConnect when the relay secret is missing', async () => {
77+
const result = await rewriteChromeDevtoolsArgsForRelay(
78+
'npx',
79+
AUTO_ARGS,
80+
{},
81+
{
82+
readToken: () => undefined,
83+
probe: async () => true,
84+
}
85+
);
86+
87+
expect(result).toEqual({ args: AUTO_ARGS, applied: false });
88+
});
89+
90+
it('keeps autoConnect when the relay is down or unpaired', async () => {
91+
const result = await rewriteChromeDevtoolsArgsForRelay(
92+
'npx',
93+
AUTO_ARGS,
94+
{},
95+
{
96+
readToken: () => TOKEN,
97+
probe: async () => false,
98+
}
99+
);
100+
101+
expect(result).toEqual({ args: AUTO_ARGS, applied: false });
102+
});
103+
104+
it('leaves unrelated commands untouched without probing', async () => {
105+
let probes = 0;
106+
const result = await rewriteChromeDevtoolsArgsForRelay(
107+
'npx',
108+
['-y', 'some-mcp'],
109+
{},
110+
{
111+
readToken: () => TOKEN,
112+
probe: async () => {
113+
probes += 1;
114+
return true;
115+
},
116+
}
117+
);
118+
119+
expect(result).toEqual({ args: ['-y', 'some-mcp'], applied: false });
120+
expect(probes).toBe(0);
121+
});
122+
});

0 commit comments

Comments
 (0)