Skip to content
Merged
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
10 changes: 10 additions & 0 deletions src/McpResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,16 @@ export class McpResponse implements Response {
includePreservedMessages?: boolean;
};
#devToolsData?: DevToolsData;
#tabId?: string;

attachDevToolsData(data: DevToolsData): void {
this.#devToolsData = data;
}

setTabId(tabId: string): void {
this.#tabId = tabId;
}

setIncludePages(value: boolean): void {
this.#includePages = value;
}
Expand Down Expand Up @@ -398,8 +403,13 @@ Call ${handleDialog.name} to handle it before continuing.`);
const structuredContent: {
snapshot?: object;
snapshotFilePath?: string;
tabId?: string;
} = {};

if (this.#tabId) {
structuredContent.tabId = this.#tabId;
}

if (data.snapshot) {
if (typeof data.snapshot === 'string') {
response.push(`Saved snapshot to ${data.snapshot}.`);
Expand Down
5 changes: 5 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ export const cliOptions = {
'Whether to include all kinds of pages such as webviews or background pages as pages.',
hidden: true,
},
experimentalInteropTools: {
type: 'boolean',
describe: 'Whether to enable interoperability tools',
hidden: true,
},
chromeArg: {
type: 'array',
describe:
Expand Down
6 changes: 6 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ function registerTool(tool: ToolDefinition): void {
) {
return;
}
if (
tool.annotations.conditions?.includes('experimentalInteropTools') &&
!args.experimentalInteropTools
) {
return;
}
server.registerTool(
tool.name,
{
Expand Down
1 change: 1 addition & 0 deletions src/tools/ToolDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export interface Response {
attachConsoleMessage(msgid: number): void;
// Allows re-using DevTools data queried by some tools.
attachDevToolsData(data: DevToolsData): void;
setTabId(tabId: string): void;
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/tools/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,26 @@ export const handleDialog = defineTool({
response.setIncludePages(true);
},
});

export const getTabId = defineTool({
name: 'get_tab_id',
description: `Get the tab ID of the page`,
annotations: {
category: ToolCategory.NAVIGATION,
readOnlyHint: true,
conditions: ['experimentalInteropTools'],
},
schema: {
pageId: zod
.number()
.describe(
`The ID of the page to get the tab ID for. Call ${listPages.name} to get available pages.`,
),
},
handler: async (request, response, context) => {
const page = context.getPageById(request.params.pageId);
// @ts-expect-error _tabId is internal.
const tabId = page._tabId;
response.setTabId(tabId);
},
});
18 changes: 18 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ describe('e2e', () => {
if (maybeTool.annotations?.conditions?.includes('computerVision')) {
continue;
}
if (
maybeTool.annotations?.conditions?.includes(
'experimentalInteropTools',
)
) {
continue;
}
definedNames.push(maybeTool.name);
}
}
Expand All @@ -120,4 +127,15 @@ describe('e2e', () => {
['--experimental-vision'],
);
});

it('has experimental interop tools', async () => {
await withClient(
async client => {
const {tools} = await client.listTools();
const getTabId = tools.find(t => t.name === 'get_tab_id');
assert.ok(getTabId);
},
['--experimental-interop-tools'],
);
});
});
18 changes: 18 additions & 0 deletions tests/tools/pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
navigatePage,
resizePage,
handleDialog,
getTabId,
} from '../../src/tools/pages.js';
import {withMcpContext} from '../utils.js';

Expand Down Expand Up @@ -322,4 +323,21 @@ describe('pages', () => {
});
});
});

describe('get_tab_id', () => {
it('returns the tab id', async () => {
await withMcpContext(async (response, context) => {
const page = context.getSelectedPage();
// @ts-expect-error _tabId is internal.
assert.ok(typeof page._tabId === 'string');
// @ts-expect-error _tabId is internal.
page._tabId = 'test-tab-id';
await getTabId.handler({params: {pageId: 1}}, response, context);
const result = await response.handle('get_tab_id', context);
// @ts-expect-error _tabId is internal.
assert.strictEqual(result.structuredContent.tabId, 'test-tab-id');
assert.deepStrictEqual(response.responseLines, []);
});
});
});
});