Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions packages/playwright-core/src/tools/backend/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ export class Context {
private _recordedActions: string[] | undefined;
private _disposables: Disposable[] = [];

private _webmcpTools: WebMCPToolDefinition[] = [];
private _webmcpToolsSignature = '';

private _runningToolName: string | undefined;
Expand Down Expand Up @@ -328,16 +327,15 @@ export class Context {
}

currentWebMCPTools(): WebMCPToolDefinition[] {
return this._webmcpTools;
// Handlers are bound to a tab and frame, always take the fresh ones.
return this._currentTab?.webmcpTools()?.tools.map(tool => tool.mcpTool) ?? [];
}

maybeNotifyWebMCPToolsChanged() {
const tools = this._currentTab?.webmcpTools()?.tools.map(tool => tool.mcpTool) ?? [];
const signature = JSON.stringify(tools.map(tool => tool.schema));
const signature = JSON.stringify(this.currentWebMCPTools().map(tool => tool.schema));
if (signature === this._webmcpToolsSignature)
return;
this._webmcpToolsSignature = signature;
this._webmcpTools = tools;
this.options.onWebMCPToolsChanged?.();
}

Expand Down
23 changes: 23 additions & 0 deletions tests/mcp/webmcp-dynamic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,29 @@ test('only the current tab contributes tools, switching tabs swaps them', async
expect(await names()).toEqual(['webmcp_add']);
});

test('a tool registered in an iframe can be called after the page is reloaded', async ({ startClient, server, mcpBrowser }) => {
test.skip(mcpBrowser === 'firefox', 'Firefox does not list tools registered in iframes');
server.setRoute('/frame', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`<title>frame</title>${registerScript(kAdd)}`);
});
server.setRoute('/', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`<title>top</title><iframe src="/frame"></iframe>`);
});

const { client } = await startClient({ config: webmcpConfig(mcpBrowser) });
await client.callTool({ name: 'browser_navigate', arguments: { url: server.PREFIX } });
expect(await client.callTool({ name: 'webmcp_add', arguments: { a: 1, b: 2 } })).toHaveResponse({
result: expect.stringContaining('"text": "3"'),
});

await client.callTool({ name: 'browser_navigate', arguments: { url: server.PREFIX } });
expect(await client.callTool({ name: 'webmcp_add', arguments: { a: 2, b: 3 } })).toHaveResponse({
result: expect.stringContaining('"text": "5"'),
});
});

test('--no-webmcp opts out of page tools entirely', async ({ startClient, server, mcpBrowser }) => {
server.setRoute('/', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
Expand Down
Loading