@kelceyp/chrome-control-mcp 0.1.0 → 0.2.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/package.json +2 -2
- package/server/index.js +148 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kelceyp/chrome-control-mcp",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "MCP server for controlling Google Chrome on macOS via AppleScript",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "MCP server for controlling Google Chrome on macOS via AppleScript with async JavaScript support",
|
|
5
5
|
"main": "server/index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"bin": {
|
package/server/index.js
CHANGED
|
@@ -170,6 +170,21 @@ class ChromeControlServer {
|
|
|
170
170
|
tab_id: { type: 'number', description: 'ID of the tab' }
|
|
171
171
|
}
|
|
172
172
|
}
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
name: 'execute_javascript_async',
|
|
176
|
+
description: 'Execute async JavaScript in the current tab and wait for result',
|
|
177
|
+
inputSchema: {
|
|
178
|
+
type: 'object',
|
|
179
|
+
properties: {
|
|
180
|
+
code: { type: 'string', description: 'JavaScript code to execute (supports await)' },
|
|
181
|
+
tab_id: { type: 'number', description: 'ID of the tab' },
|
|
182
|
+
timeout_ms: { type: 'number', description: 'Timeout in milliseconds', default: 60000 },
|
|
183
|
+
poll_ms: { type: 'number', description: 'Polling interval in milliseconds', default: 200 },
|
|
184
|
+
stringify: { type: 'boolean', description: 'JSON stringify the result', default: true }
|
|
185
|
+
},
|
|
186
|
+
required: ['code']
|
|
187
|
+
}
|
|
173
188
|
}
|
|
174
189
|
]
|
|
175
190
|
}));
|
|
@@ -405,6 +420,139 @@ class ChromeControlServer {
|
|
|
405
420
|
return { content: [{ type: 'text', text: result }] };
|
|
406
421
|
}
|
|
407
422
|
|
|
423
|
+
case 'execute_javascript_async': {
|
|
424
|
+
const {
|
|
425
|
+
code,
|
|
426
|
+
tab_id,
|
|
427
|
+
timeout_ms = 60000,
|
|
428
|
+
poll_ms = 200,
|
|
429
|
+
stringify = true,
|
|
430
|
+
} = args;
|
|
431
|
+
|
|
432
|
+
if (typeof code !== 'string' || !code.trim()) {
|
|
433
|
+
return { content: [{ type: 'text', text: 'Error: code is required (string)' }] };
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// Build the page-side bootstrap that returns a token and completes later
|
|
437
|
+
// Note: we embed `code` as-is inside an async IIFE, so it can use `await`.
|
|
438
|
+
const bootstrapJs = `
|
|
439
|
+
(() => {
|
|
440
|
+
try {
|
|
441
|
+
window.__MCP_ASYNC_RESULTS = window.__MCP_ASYNC_RESULTS || {};
|
|
442
|
+
const token = 'mcp_' + Date.now() + '_' + Math.random().toString(36).slice(2);
|
|
443
|
+
(async () => {
|
|
444
|
+
try {
|
|
445
|
+
const __result__ = await (async () => { ${code} })();
|
|
446
|
+
window.__MCP_ASYNC_RESULTS[token] = {
|
|
447
|
+
status: 'fulfilled',
|
|
448
|
+
value: ${stringify ? 'JSON.stringify(__result__)' : '__result__'}
|
|
449
|
+
};
|
|
450
|
+
} catch (e) {
|
|
451
|
+
window.__MCP_ASYNC_RESULTS[token] = {
|
|
452
|
+
status: 'rejected',
|
|
453
|
+
error: String(e && e.message || e)
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
})();
|
|
457
|
+
return token;
|
|
458
|
+
} catch (e) {
|
|
459
|
+
return 'ERROR_BOOTSTRAP:' + String(e && e.message || e);
|
|
460
|
+
}
|
|
461
|
+
})();
|
|
462
|
+
`.trim();
|
|
463
|
+
|
|
464
|
+
// Escape for AppleScript execute javascript "..."
|
|
465
|
+
const asQuote = (s) =>
|
|
466
|
+
s.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n');
|
|
467
|
+
|
|
468
|
+
const jsBootEsc = asQuote(bootstrapJs);
|
|
469
|
+
const pollSeconds = (Math.max(50, Number(poll_ms)) / 1000).toFixed(3); // >=50ms, to real
|
|
470
|
+
const timeoutMs = Math.max(1000, Number(timeout_ms)); // >=1s
|
|
471
|
+
|
|
472
|
+
const script = tab_id ? `
|
|
473
|
+
set _timeoutMs to ${timeoutMs}
|
|
474
|
+
set _pollSec to ${pollSeconds} as real
|
|
475
|
+
tell application "Google Chrome"
|
|
476
|
+
set _token to ""
|
|
477
|
+
repeat with w in windows
|
|
478
|
+
repeat with t in tabs of w
|
|
479
|
+
if (id of t as string) is "${tab_id}" then
|
|
480
|
+
set _token to execute t javascript "${jsBootEsc}"
|
|
481
|
+
exit repeat
|
|
482
|
+
end if
|
|
483
|
+
end repeat
|
|
484
|
+
if _token is not "" then exit repeat
|
|
485
|
+
end repeat
|
|
486
|
+
if _token is "" then return "Tab not found"
|
|
487
|
+
|
|
488
|
+
set _t0 to (current date)
|
|
489
|
+
repeat
|
|
490
|
+
set _status to execute active tab of front window javascript "(() => { var r = window.__MCP_ASYNC_RESULTS && window.__MCP_ASYNC_RESULTS['\" & _token & \"']; return r ? r.status : 'pending'; })()"
|
|
491
|
+
if _status is "fulfilled" then
|
|
492
|
+
set _val to execute active tab of front window javascript "window.__MCP_ASYNC_RESULTS['\" & _token & \"'].value"
|
|
493
|
+
execute active tab of front window javascript "delete window.__MCP_ASYNC_RESULTS['\" & _token & \"']"
|
|
494
|
+
return _val
|
|
495
|
+
else if _status is "rejected" then
|
|
496
|
+
set _err to execute active tab of front window javascript "window.__MCP_ASYNC_RESULTS['\" & _token & \"'].error"
|
|
497
|
+
execute active tab of front window javascript "delete window.__MCP_ASYNC_RESULTS['\" & _token & \"']"
|
|
498
|
+
return "ERROR:" & _err
|
|
499
|
+
end if
|
|
500
|
+
|
|
501
|
+
set _elapsedMs to ((current date) - _t0) * 1000
|
|
502
|
+
if _elapsedMs > _timeoutMs then
|
|
503
|
+
execute active tab of front window javascript "if (window.__MCP_ASYNC_RESULTS) delete window.__MCP_ASYNC_RESULTS['\" & _token & \"']"
|
|
504
|
+
return "TIMEOUT"
|
|
505
|
+
end if
|
|
506
|
+
delay _pollSec
|
|
507
|
+
end repeat
|
|
508
|
+
end tell
|
|
509
|
+
` : `
|
|
510
|
+
set _timeoutMs to ${timeoutMs}
|
|
511
|
+
set _pollSec to ${pollSeconds} as real
|
|
512
|
+
tell application "Google Chrome"
|
|
513
|
+
set _token to execute active tab of front window javascript "${jsBootEsc}"
|
|
514
|
+
if _token starts with "ERROR_BOOTSTRAP:" then return _token
|
|
515
|
+
set _t0 to (current date)
|
|
516
|
+
repeat
|
|
517
|
+
set _status to execute active tab of front window javascript "(() => { var r = window.__MCP_ASYNC_RESULTS && window.__MCP_ASYNC_RESULTS['\" & _token & \"']; return r ? r.status : 'pending'; })()"
|
|
518
|
+
if _status is "fulfilled" then
|
|
519
|
+
set _val to execute active tab of front window javascript "window.__MCP_ASYNC_RESULTS['\" & _token & \"'].value"
|
|
520
|
+
execute active tab of front window javascript "delete window.__MCP_ASYNC_RESULTS['\" & _token & \"']"
|
|
521
|
+
return _val
|
|
522
|
+
else if _status is "rejected" then
|
|
523
|
+
set _err to execute active tab of front window javascript "window.__MCP_ASYNC_RESULTS['\" & _token & \"'].error"
|
|
524
|
+
execute active tab of front window javascript "delete window.__MCP_ASYNC_RESULTS['\" & _token & \"']"
|
|
525
|
+
return "ERROR:" & _err
|
|
526
|
+
end if
|
|
527
|
+
|
|
528
|
+
set _elapsedMs to ((current date) - _t0) * 1000
|
|
529
|
+
if _elapsedMs > _timeoutMs then
|
|
530
|
+
execute active tab of front window javascript "if (window.__MCP_ASYNC_RESULTS) delete window.__MCP_ASYNC_RESULTS['\" & _token & \"']"
|
|
531
|
+
return "TIMEOUT"
|
|
532
|
+
end if
|
|
533
|
+
delay _pollSec
|
|
534
|
+
end repeat
|
|
535
|
+
end tell
|
|
536
|
+
`;
|
|
537
|
+
|
|
538
|
+
const result = await this.executeAppleScript(script);
|
|
539
|
+
|
|
540
|
+
// Normalize response: if we JSON-stringified in page, parse here.
|
|
541
|
+
if (typeof result === 'string' && result.startsWith('ERROR:')) {
|
|
542
|
+
return { content: [{ type: 'text', text: result }] };
|
|
543
|
+
}
|
|
544
|
+
if (stringify && typeof result === 'string') {
|
|
545
|
+
try {
|
|
546
|
+
const parsed = JSON.parse(result);
|
|
547
|
+
return { content: [{ type: 'text', text: typeof parsed === 'string' ? parsed : JSON.stringify(parsed) }] };
|
|
548
|
+
} catch {
|
|
549
|
+
// Not JSON (e.g., primitive string); return as-is
|
|
550
|
+
return { content: [{ type: 'text', text: result }] };
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
return { content: [{ type: 'text', text: result ?? '' }] };
|
|
554
|
+
}
|
|
555
|
+
|
|
408
556
|
default:
|
|
409
557
|
throw new Error(`Unknown tool: ${name}`);
|
|
410
558
|
}
|