From b3b44d8a2d8b2aa70a8ed8b9cddf365865fcc53f Mon Sep 17 00:00:00 2001 From: Rudy Mizrahi Celekli <47457359+rudycelekli@users.noreply.github.com> Date: Tue, 6 Oct 2026 21:19:49 -0500 Subject: [PATCH] fix(browser): distinguish WebMCP registration frame owners (#813) Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com> --- docs/browser-observation.md | 2 +- src/browser/CdpWebMcpDiscovery.ts | 6 +-- ...cdpBrowserProvider.document_script.test.ts | 48 +++++++++++++++++++ tests/fixtures/fakeCdpBrowserEvents.ts | 45 +++++++++++++++++ tests/fixtures/fakeCdpBrowserResults.ts | 25 ++++++---- tests/fixtures/fakeCdpBrowserTypes.ts | 1 + 6 files changed, 113 insertions(+), 14 deletions(-) diff --git a/docs/browser-observation.md b/docs/browser-observation.md index 1baac522f..bd7b61bf4 100644 --- a/docs/browser-observation.md +++ b/docs/browser-observation.md @@ -12,7 +12,7 @@ REA can attach to a user-owned Chrome-family browser through the Chrome DevTools - `inspect_web_page` / `rea inspect-web-page` captures DOM structure, accessibility nodes, scripts, resources, safe response/DOM metadata, attach-window network and console metadata, WebSocket frame sizes, workers, quota, and optionally selected storage key names, redacted storage-content fingerprints, or script sources. - `analyze_web_bundle` / `rea analyze-web-bundle` parses selected script artifacts without execution and derives chunk edges, route and endpoint candidates, vendor fingerprints, static WebMCP declarations, and optionally source-map/original-source evidence. - `observe_web_session` / `rea observe-web-session` arms an observation window of the requested duration for an external user action and records ordered reload, SPA navigation, redirect, failure, lifecycle, and target-termination metadata. -- `discover_webmcp_tools` / `rea discover-webmcp-tools` uses the experimental CDP WebMCP domain to return every in-scope registration and its complete structural input-schema summary inline. REA never exposes `WebMCP.invokeTool`; malformed protocol payloads fail explicitly instead of returning incomplete tool metadata. +- `discover_webmcp_tools` / `rea discover-webmcp-tools` uses the experimental CDP WebMCP domain to return every in-scope registration and its complete structural input-schema summary inline. Registrations with the same name and URL in distinct observed frames retain separate identities. `tool_key` includes the observed frame ID; it is not a stable identity across navigation or browser sessions. Same-frame registration updates replace that owner only, and removal leaves other owners intact. REA never exposes `WebMCP.invokeTool`; malformed protocol payloads fail explicitly instead of returning incomplete tool metadata. - `compare_web_captures` / `rea compare-web-captures` compares passive captures or exact-step-aligned browser scenarios. Scenario results commit reproducible literal normalization and expose alignment failures plus artifact-level action, screenshot, DOM, accessibility, URL, history, storage, and event differences. Missing or truncated evidence is never treated as equivalence. - `capture_web_screenshot` / `rea capture-web-screenshot` returns a content-addressed visible-viewport PNG. - `compare_web_screenshots` / `rea compare-web-screenshots` performs bounded local PNG pixel comparison without OCR or external services. diff --git a/src/browser/CdpWebMcpDiscovery.ts b/src/browser/CdpWebMcpDiscovery.ts index ddbc6fabf..1acacc8da 100644 --- a/src/browser/CdpWebMcpDiscovery.ts +++ b/src/browser/CdpWebMcpDiscovery.ts @@ -410,7 +410,7 @@ const normalizeTool = ( const description = stringValue(value.description) ?? ""; const annotations = recordValue(value.annotations); return { - tool_key: toolKey(frame.url, name), + tool_key: toolKey(frame.url, frameId, name), name, description, frame_id: frameId, @@ -497,8 +497,8 @@ const buildWebMcpResult = (options: WebMcpResultOptions): WebMcpDiscovery => { }; }; -const toolKey = (frameUrl: string, name: string): string => - `webmcp_${createHash("sha256").update(`${frameUrl}\0${name}`).digest("hex")}`; +const toolKey = (frameUrl: string, frameId: string, name: string): string => + `webmcp_${createHash("sha256").update(`${frameUrl}\0${frameId}\0${name}`).digest("hex")}`; const booleanOrNull = (value: unknown): boolean | null => typeof value === "boolean" ? value : null; diff --git a/tests/boundary/browser/cdpBrowserProvider.document_script.test.ts b/tests/boundary/browser/cdpBrowserProvider.document_script.test.ts index b0e8440fa..e770000b1 100644 --- a/tests/boundary/browser/cdpBrowserProvider.document_script.test.ts +++ b/tests/boundary/browser/cdpBrowserProvider.document_script.test.ts @@ -500,3 +500,51 @@ describeBrowser("CdpBrowserProvider: document script 4", () => { expect(result.value.storage.content_fingerprints).toEqual([]); }); }); + +describeBrowser("CdpBrowserProvider: WebMCP registration owners", () => { + it.each(["retain", "remove-second"] as const)( + "keeps same-URL frame owners separate: %s", + async (mode) => { + const browser = await startFakeCdpBrowser({ + webMcpSameUrlRegistrations: mode, + }); + trackBrowser(browser); + const result = await new CdpBrowserProvider().discoverWebMcpTools( + discoverWebMcpToolsInputSchema.parse({ + cdp_endpoint: browser.endpoint, + allowed_origins: [browser.allowedOrigin], + target_id: "allowed-page", + observation_ms: 0, + }), + ); + if (!result.ok) throw result.error; + const tools = result.value.tools.items; + expect(tools).toHaveLength(mode === "retain" ? 2 : 1); + expect(result.value.tools.total).toBe(tools.length); + expect(tools).toContainEqual( + expect.objectContaining({ + name: "read_item", + frame_id: "webmcp-frame-0", + description: "First owner updated", + }), + ); + if (mode === "retain") { + expect(tools).toContainEqual( + expect.objectContaining({ + name: "read_item", + frame_id: "webmcp-frame-1", + description: "Second owner", + }), + ); + expect(new Set(tools.map((tool) => tool.tool_key)).size).toBe(2); + expect(new Set(tools.map((tool) => tool.frame_url)).size).toBe(1); + } + expect(browser.commands.map(({ method }) => method)).not.toContain( + "WebMCP.invokeTool", + ); + expect(browser.commands.map(({ method }) => method)).not.toContain( + "Runtime.evaluate", + ); + }, + ); +}); diff --git a/tests/fixtures/fakeCdpBrowserEvents.ts b/tests/fixtures/fakeCdpBrowserEvents.ts index b72723e6d..3a2c5bde8 100644 --- a/tests/fixtures/fakeCdpBrowserEvents.ts +++ b/tests/fixtures/fakeCdpBrowserEvents.ts @@ -22,6 +22,13 @@ const emitWebMcpEvents = ( port: number, options: FakeOptions, ): void => { + if ( + command.method === "WebMCP.enable" && + options.webMcpSameUrlRegistrations !== undefined + ) { + emitSameUrlWebMcpRegistrations(socket, command, options); + return; + } if (command.method === "WebMCP.enable" && options.webMcpTools === true) { const inputSchemaProperties = options.webMcpSchemaPropertyCount === undefined @@ -160,6 +167,44 @@ const emitWebMcpEvents = ( } }; +const emitSameUrlWebMcpRegistrations = ( + socket: WebSocket, + command: FakeCdpCommand, + options: FakeOptions, +): void => { + event(socket, "WebMCP.toolsAdded", command.sessionId, { + tools: [ + { + name: "read_item", + description: "First owner", + frameId: "webmcp-frame-0", + }, + ], + }); + event(socket, "WebMCP.toolsAdded", command.sessionId, { + tools: [ + { + name: "read_item", + description: "First owner updated", + frameId: "webmcp-frame-0", + }, + ], + }); + event(socket, "WebMCP.toolsAdded", command.sessionId, { + tools: [ + { + name: "read_item", + description: "Second owner", + frameId: "webmcp-frame-1", + }, + ], + }); + if (options.webMcpSameUrlRegistrations === "remove-second") + event(socket, "WebMCP.toolsRemoved", command.sessionId, { + tools: [{ name: "read_item", frameId: "webmcp-frame-1" }], + }); +}; + const emitSessionTimeline = ( socket: WebSocket, command: FakeCdpCommand, diff --git a/tests/fixtures/fakeCdpBrowserResults.ts b/tests/fixtures/fakeCdpBrowserResults.ts index edb3dff77..d73d15e42 100644 --- a/tests/fixtures/fakeCdpBrowserResults.ts +++ b/tests/fixtures/fakeCdpBrowserResults.ts @@ -66,7 +66,9 @@ const frameTreeResult = ( ? ":" : (options.electronFileUrl ?? options.attachedFrameUrl), options.extraCollections === true, - options.webMcpFrameCount, + options.webMcpSameUrlRegistrations === undefined + ? options.webMcpFrameCount + : "same-url", ); const resourceTreeResult = (port: number, options: FakeOptions) => @@ -236,7 +238,7 @@ const frameTree = ( port: number, overrideUrl?: string, extraCollections = false, - webMcpFrameCount = 0, + webMcpFrameCount: number | "same-url" = 0, ): Readonly> => ({ frameTree: { frame: { @@ -259,14 +261,17 @@ const frameTree = ( }, ] : []), - ...Array.from({ length: webMcpFrameCount }, (_value, index) => ({ - frame: { - id: `webmcp-frame-${String(index)}`, - parentId: "frame-main", - loaderId: `loader-webmcp-${String(index)}`, - url: `http://127.0.0.1:${String(port)}/webmcp-frame/${String(index)}`, - }, - })), + ...Array.from( + { length: webMcpFrameCount === "same-url" ? 2 : webMcpFrameCount }, + (_value, index) => ({ + frame: { + id: `webmcp-frame-${String(index)}`, + parentId: "frame-main", + loaderId: `loader-webmcp-${String(index)}`, + url: `http://127.0.0.1:${String(port)}/webmcp-frame/${webMcpFrameCount === "same-url" ? "shared" : String(index)}`, + }, + }), + ), { frame: { id: "frame-private", diff --git a/tests/fixtures/fakeCdpBrowserTypes.ts b/tests/fixtures/fakeCdpBrowserTypes.ts index 6d9c5d602..fba013130 100644 --- a/tests/fixtures/fakeCdpBrowserTypes.ts +++ b/tests/fixtures/fakeCdpBrowserTypes.ts @@ -62,6 +62,7 @@ export interface FakeOptions { readonly cachedResponseBody?: string; readonly invalidResponseBodyBase64?: boolean; readonly webMcpTools?: boolean; + readonly webMcpSameUrlRegistrations?: "retain" | "remove-second"; readonly webMcpFrameCount?: number; readonly webMcpSchemaPropertyCount?: number; readonly webMcpChildLeavesScope?: boolean;