Skip to content

Commit 3df2d67

Browse files
author
root
committed
docs: switch remote debugging example to MCP SDK
1 parent ff90213 commit 3df2d67

1 file changed

Lines changed: 29 additions & 47 deletions

File tree

examples/remote-debugging-9222.mjs

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// Example: connect to a Chrome instance that was started with a remote debugging port.
1+
// Example: connect to a Chrome instance that was started with a remote debugging port,
2+
// using the official MCP TypeScript SDK.
23
//
34
// 1) Start Chrome manually with remote debugging enabled, e.g.:
45
// macOS:
@@ -9,59 +10,40 @@
910
// 2) Run this script:
1011
// node examples/remote-debugging-9222.mjs
1112
//
12-
// This spawns the MCP server and connects it to http://127.0.0.1:9222 via --browserUrl.
13+
// This starts chrome-devtools-mcp with --browserUrl and connects through MCP stdio.
1314
//
14-
// Note: This is intentionally a small template you can adapt for your own MCP client.
15+
// For SDK details, see:
16+
// https://modelcontextprotocol.io/docs/develop/build-client#typescript
1517

16-
import { spawn } from 'node:child_process';
18+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
19+
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
1720

1821
const DEBUG_URL = process.env.CHROME_DEBUG_URL ?? 'http://127.0.0.1:9222';
1922

20-
const child = spawn(
21-
'npx',
22-
['--yes', 'chrome-devtools-mcp@latest', '--browserUrl', DEBUG_URL],
23-
{
24-
stdio: ['pipe', 'pipe', 'pipe'],
25-
env: {
26-
...process.env,
27-
CHROME_DEVTOOLS_MCP_NO_USAGE_STATISTICS: 'true',
28-
},
23+
const transport = new StdioClientTransport({
24+
command: 'npx',
25+
args: ['--yes', 'chrome-devtools-mcp@latest', '--browserUrl', DEBUG_URL],
26+
env: {
27+
...process.env,
28+
CHROME_DEVTOOLS_MCP_NO_USAGE_STATISTICS: 'true',
2929
},
30+
stderr: 'inherit',
31+
});
32+
33+
const client = new Client(
34+
{ name: 'remote-debugging-9222', version: '0.0.0' },
35+
{ capabilities: {} },
3036
);
3137

32-
child.stderr.on('data', (d) => process.stderr.write(d));
38+
try {
39+
await client.connect(transport);
3340

34-
let buf = '';
35-
child.stdout.on('data', (d) => {
36-
buf += d.toString('utf8');
37-
let idx;
38-
while ((idx = buf.indexOf('\n')) !== -1) {
39-
const line = buf.slice(0, idx).trim();
40-
buf = buf.slice(idx + 1);
41-
if (!line) continue;
42-
try {
43-
const msg = JSON.parse(line);
44-
console.log('←', msg);
45-
// Exit after initialize.
46-
if (msg.id === 1) {
47-
child.kill();
48-
process.exit(0);
49-
}
50-
} catch {
51-
// ignore non-JSON lines
52-
}
53-
}
54-
});
41+
const { tools } = await client.listTools();
42+
console.log(`Connected to ${DEBUG_URL}. Found ${tools.length} tools.`);
5543

56-
const init = {
57-
jsonrpc: '2.0',
58-
id: 1,
59-
method: 'initialize',
60-
params: {
61-
protocolVersion: '2024-11-05',
62-
capabilities: {},
63-
clientInfo: { name: 'remote-debugging-9222', version: '0.0.0' },
64-
},
65-
};
66-
67-
child.stdin.write(JSON.stringify(init) + '\n');
44+
for (const tool of tools.slice(0, 10)) {
45+
console.log(`- ${tool.name}`);
46+
}
47+
} finally {
48+
await transport.close();
49+
}

0 commit comments

Comments
 (0)