The owner asked for a browser-use MCP of magpie's own, so an agent's browser keeps working when Claude Code switches accounts, and asked that it act in the browser the user uses every day: a fresh Playwright profile is signed in to nothing. Magpie Browser is an MV3 extension (internal/browserext, ID dmlhojidlfnkgamkekjhacgkpanbkeko, pinned by its key) that the user loads once in Chrome, Edge, Arc, Brave or another Chromium browser. It holds a WebSocket to the gateway's /v1/magpie/browser/extension and carries out each tool call with chrome.debugger (the DevTools protocol) in the user's own tabs. The socket is taken only from this computer, with a loopback Host (no DNS rebinding) and the extension's own Origin; a page or another extension that connected in its place would be handed every call. Agents reach it at /mcp/magpie/browser (Streamable HTTP) or through `magpie mcp browser` (stdio), and the Library's MCP market lists it as Magpie Browser, opt-in. Like the image server it is magpie's own, but it is never served to another computer, shared and with a key or not, and never to a web page, since it acts as the user wherever they are signed in. 17 tools: navigate (url, back, forward, reload), snapshot (an accessibility tree with refs, interactive-only on ask), click, type, press, select, hover, drag, upload, scroll, wait (text, gone, seconds), dialog, evaluate, text, screenshot (page, full page, element; JPEG or PNG), console and tabs (list, new, select, close). Each agent session goes on in its own tab; a link that opens a tab is followed, and a dialog a click opens is said in the reply. The Library's sheet for it has Set up the extension…, which writes the extension to ~/.config/magpie/browser-extension pointed at this gateway and shows the folder, the three steps to load it, and whether the browser is connected, polled while the sheet is open. Run: - TestBrowserToolsInARealBrowser: the extension magpie writes, loaded in a real Chrome for Testing 141 (Playwright's chromium-1234, headless=new) with --load-extension, connects, and all 17 tools act in a real tab of a fixture site. Green on mote (macOS 27), on xiaomi-gpu (Windows 11, win-run) and in magpie-desktop (Ubuntu 24.04, wsl-run). Under a test HOME Chrome needs --use-mock-keychain / --password-store=basic, or the extension's storage waits on a keychain and it never connects. - go test ./internal/gateway -run 'Browser|ReadOnly|Image', ./internal/ browserext, ./internal/browsermcp, ./internal/library, ./internal/gui -run 'Library|Browser', ./internal/cli on mote. - GUI: library-browser-setup.test.cjs (new) in Chromium and WebKit, en, zh, zh-TW, ja, de, at 1100 and 420 wide: 22/22. With i18n-keys and every library*.test.cjs: 915 pass, 1 fail, i18n-keys' "Unmarked", red on main since5787d758a(#4524, fixed in the next commit). - gofmt -l, go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./... on mote. - Again at this commit, rebased on2e9df9b1b: gofmt -l, the vet trio, ./internal/gui -run 'Budget|Size', ./internal/proc (exec guard), ./internal/usage, ./internal/browserext, ./internal/browsermcp, and i18n-keys (0 fail with the next commit). The full go test ./... runs in magpie-desktop (Linux) as this is pushed; mote had another session's suite running. Broken by hand, each red, then restored: - browser_click's dispatch in background.js turned into a no-op: the E2E test is red at "clicked Ada". - the extension socket's Origin check, its loopback-Host check and local(): TestBrowserExtensionOnlyFromItsOrigin red for each. - browserMCP's local()/fromBrowser() guard: TestBrowserMCPGuarded red. - a tool dropped from background.js: TestBrowserToolsAreTheExtensions red. - the sheet's steps left shown once connected: the GUI test is red. Not tried: loading it by hand (Load unpacked) in the owner's own daily Chrome or Edge and driving a site they are signed in to; Arc, Brave and Edge themselves (only Chrome for Testing); Firefox and Safari, which have no chrome.debugger and are not supported. Chrome shows its "is debugging this browser" bar while a tab is being driven; that is Chrome's and cannot be hidden.
198 lines
6.7 KiB
JavaScript
198 lines
6.7 KiB
JavaScript
// The DevTools protocol over chrome.debugger, one attachment per tab. A
|
|
// tab stays attached while an agent works in it and is let go after a
|
|
// quiet spell, so Chrome's "is debugging this browser" bar is up only
|
|
// while it is used.
|
|
|
|
const IDLE_MS = 2 * 60 * 1000;
|
|
const LOG_KEPT = 300;
|
|
|
|
// tabId → what is known of the page while attached
|
|
const tabs = new Map();
|
|
|
|
export function state(tabId) {
|
|
let t = tabs.get(tabId);
|
|
if (!t) {
|
|
t = {
|
|
tabId, attached: false, attaching: null, idle: 0,
|
|
refs: new Map(), keys: new Map(), next: 1, doc: "",
|
|
logs: [], dialog: null, answer: null, chooser: null,
|
|
inflight: new Map(), lastNet: 0, loading: false,
|
|
stoppedByUser: false,
|
|
};
|
|
tabs.set(tabId, t);
|
|
}
|
|
return t;
|
|
}
|
|
|
|
export function forget(tabId) {
|
|
const t = tabs.get(tabId);
|
|
if (t) clearTimeout(t.idle);
|
|
tabs.delete(tabId);
|
|
}
|
|
|
|
export async function attach(tabId) {
|
|
const t = state(tabId);
|
|
clearTimeout(t.idle);
|
|
t.idle = setTimeout(() => detach(tabId), IDLE_MS);
|
|
if (t.attached) return t;
|
|
if (!t.attaching) {
|
|
t.attaching = (async () => {
|
|
try {
|
|
await chrome.debugger.attach({ tabId }, "1.3");
|
|
} catch (e) {
|
|
const m = String(e && e.message || e);
|
|
if (!/already attached/i.test(m)) throw new Error(why(m));
|
|
}
|
|
t.attached = true;
|
|
t.stoppedByUser = false;
|
|
await Promise.all([
|
|
send(tabId, "Page.enable"),
|
|
send(tabId, "Runtime.enable"),
|
|
send(tabId, "Network.enable"),
|
|
send(tabId, "DOM.enable"),
|
|
send(tabId, "Accessibility.enable"),
|
|
]);
|
|
await send(tabId, "Page.setInterceptFileChooserDialog", { enabled: false }).catch(() => {});
|
|
})().finally(() => { t.attaching = null; });
|
|
}
|
|
await t.attaching;
|
|
return t;
|
|
}
|
|
|
|
function why(m) {
|
|
if (/chrome:\/\/|chrome-extension:\/\/|Cannot access|Cannot attach/i.test(m)) {
|
|
return "Chrome doesn't let an extension control this page (a chrome:// page, the Web Store or another extension's page): open a web page in the tab first";
|
|
}
|
|
if (/Another debugger is already attached/i.test(m)) {
|
|
return "DevTools or another debugger is attached to this tab: close DevTools in it, or use another tab";
|
|
}
|
|
return m;
|
|
}
|
|
|
|
export async function detach(tabId) {
|
|
const t = tabs.get(tabId);
|
|
if (!t || !t.attached) return;
|
|
t.attached = false;
|
|
try { await chrome.debugger.detach({ tabId }); } catch {}
|
|
}
|
|
|
|
export function send(tabId, method, params = {}) {
|
|
return chrome.debugger.sendCommand({ tabId }, method, params).catch((e) => {
|
|
const m = String(e && e.message || e);
|
|
if (/Debugger is not attached|No tab with given id|detached/i.test(m)) {
|
|
const t = tabs.get(tabId);
|
|
if (t) t.attached = false;
|
|
}
|
|
throw new Error(m.replace(/^\{"code":-?\d+,"message":"(.*)"\}$/, "$1"));
|
|
});
|
|
}
|
|
|
|
// requests that never end on their own: a page with one open is still
|
|
// quiet
|
|
const STREAMING = new Set(["EventSource", "WebSocket", "Ping", "CSPViolationReport", "Preflight"]);
|
|
|
|
chrome.debugger.onEvent.addListener((src, method, p) => {
|
|
const t = tabs.get(src.tabId);
|
|
if (!t) return;
|
|
switch (method) {
|
|
case "Network.requestWillBeSent":
|
|
if (!STREAMING.has(p.type) && !/^data:|^blob:/.test(p.request.url)) t.inflight.set(p.requestId, Date.now());
|
|
t.lastNet = Date.now();
|
|
break;
|
|
case "Network.loadingFinished":
|
|
case "Network.loadingFailed":
|
|
t.inflight.delete(p.requestId);
|
|
t.lastNet = Date.now();
|
|
if (method === "Network.loadingFailed" && !p.canceled && p.type === "Document") log(t, "network", `failed: ${p.errorText}`);
|
|
break;
|
|
case "Network.responseReceived":
|
|
if (p.response.status >= 400) log(t, "network", `${p.response.status} ${p.type} ${p.response.url.slice(0, 300)}`);
|
|
// a streamed answer (server-sent events, a long poll) isn't waited for
|
|
if (/text\/event-stream/i.test(p.response.mimeType || "")) t.inflight.delete(p.requestId);
|
|
break;
|
|
case "Page.frameStartedLoading":
|
|
if (!p.frameId || p.frameId === t.mainFrame) t.loading = true;
|
|
break;
|
|
case "Page.loadEventFired":
|
|
t.loading = false;
|
|
break;
|
|
case "Page.frameNavigated":
|
|
if (!p.frame.parentId) {
|
|
t.mainFrame = p.frame.id;
|
|
t.inflight.clear();
|
|
}
|
|
break;
|
|
case "Runtime.consoleAPICalled": {
|
|
const text = (p.args || []).map((a) => a.value !== undefined ? String(a.value) : (a.description || a.type)).join(" ");
|
|
log(t, p.type === "warning" ? "warn" : p.type, text);
|
|
break;
|
|
}
|
|
case "Runtime.exceptionThrown": {
|
|
const d = p.exceptionDetails || {};
|
|
log(t, "error", (d.exception && d.exception.description) || d.text || "exception");
|
|
break;
|
|
}
|
|
case "Page.javascriptDialogOpening":
|
|
if (t.answer) {
|
|
const a = t.answer;
|
|
t.answer = null;
|
|
send(src.tabId, "Page.handleJavaScriptDialog", { accept: a.accept, promptText: a.promptText }).catch(() => {});
|
|
t.answered = { type: p.type, message: p.message, accept: a.accept };
|
|
} else {
|
|
t.dialog = { type: p.type, message: p.message, defaultPrompt: p.defaultPrompt };
|
|
}
|
|
break;
|
|
case "Page.javascriptDialogClosed":
|
|
t.dialog = null;
|
|
break;
|
|
case "Page.fileChooserOpened":
|
|
if (t.chooser) t.chooser(p);
|
|
break;
|
|
}
|
|
});
|
|
|
|
chrome.debugger.onDetach.addListener((src, reason) => {
|
|
const t = tabs.get(src.tabId);
|
|
if (!t) return;
|
|
t.attached = false;
|
|
// the user pressed Cancel on Chrome's bar: they want it stopped
|
|
if (reason === "canceled_by_user") t.stoppedByUser = true;
|
|
});
|
|
|
|
function log(t, level, text) {
|
|
t.logs.push({ at: Date.now(), level, text: String(text).slice(0, 2000) });
|
|
if (t.logs.length > LOG_KEPT) t.logs.splice(0, t.logs.length - LOG_KEPT);
|
|
}
|
|
|
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
export { sleep };
|
|
|
|
// settle waits for the page to go quiet after an action: no load under
|
|
// way and no request open for a short spell, or until the bound.
|
|
export async function settle(t, bound = 2500, quiet = 400) {
|
|
const end = Date.now() + bound;
|
|
await sleep(60);
|
|
while (Date.now() < end) {
|
|
const now = Date.now();
|
|
for (const [id, at] of t.inflight) if (now - at > 10000) t.inflight.delete(id); // a long poll
|
|
if (!t.loading && t.inflight.size === 0 && now - t.lastNet >= quiet) return;
|
|
await sleep(80);
|
|
}
|
|
}
|
|
|
|
// waitLoad waits for the main frame to finish loading after a navigation.
|
|
export async function waitLoad(t, bound = 30000) {
|
|
const end = Date.now() + bound;
|
|
await sleep(100);
|
|
while (Date.now() < end) {
|
|
if (!t.loading) {
|
|
try {
|
|
const r = await send(t.tabId, "Runtime.evaluate", { expression: "document.readyState", returnByValue: true });
|
|
if (r.result.value === "complete" || r.result.value === "interactive") return true;
|
|
} catch {}
|
|
}
|
|
await sleep(150);
|
|
}
|
|
return false;
|
|
}
|