fix(browser): distinguish WebMCP registration frame owners (#813)
Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>
This commit is contained in:
@@ -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",
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
+45
@@ -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,
|
||||
|
||||
+15
-10
@@ -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<Record<string, unknown>> => ({
|
||||
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",
|
||||
|
||||
+1
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user