playwright-mcp/src/tools/tabs.ts

102 lines
2.5 KiB
TypeScript
Raw Normal View History

2025-04-03 19:24:17 -07:00
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { z } from 'zod';
import { defineTool } from './tool.js';
2025-04-03 19:24:17 -07:00
const listTabs = defineTool({
capability: 'core-tabs',
2025-04-03 19:24:17 -07:00
schema: {
name: 'browser_tab_list',
title: 'List tabs',
2025-04-03 19:24:17 -07:00
description: 'List browser tabs',
inputSchema: z.object({}),
type: 'readOnly',
2025-04-03 19:24:17 -07:00
},
2025-07-22 16:36:21 -07:00
handle: async (context, params, response) => {
2025-04-17 00:58:02 -07:00
await context.ensureTab();
2025-07-22 16:36:21 -07:00
response.setIncludeTabs();
2025-04-03 19:24:17 -07:00
},
});
const selectTab = defineTool({
capability: 'core-tabs',
2025-04-03 19:24:17 -07:00
schema: {
name: 'browser_tab_select',
title: 'Select a tab',
2025-04-03 19:24:17 -07:00
description: 'Select a tab by index',
inputSchema: z.object({
index: z.number().describe('The index of the tab to select'),
}),
type: 'readOnly',
2025-04-03 19:24:17 -07:00
},
2025-07-22 16:36:21 -07:00
handle: async (context, params, response) => {
await context.selectTab(params.index);
2025-07-22 16:36:21 -07:00
response.setIncludeSnapshot();
2025-04-03 19:24:17 -07:00
},
});
const newTab = defineTool({
capability: 'core-tabs',
2025-04-03 19:24:17 -07:00
schema: {
name: 'browser_tab_new',
title: 'Open a new tab',
2025-04-03 19:24:17 -07:00
description: 'Open a new tab',
inputSchema: z.object({
url: z.string().optional().describe('The URL to navigate to in the new tab. If not provided, the new tab will be blank.'),
}),
type: 'readOnly',
2025-04-03 19:24:17 -07:00
},
2025-07-22 16:36:21 -07:00
handle: async (context, params, response) => {
const tab = await context.newTab();
if (params.url)
await tab.navigate(params.url);
2025-07-22 16:36:21 -07:00
response.setIncludeSnapshot();
2025-04-03 19:24:17 -07:00
},
});
2025-04-03 19:24:17 -07:00
const closeTab = defineTool({
capability: 'core-tabs',
2025-04-03 19:24:17 -07:00
schema: {
name: 'browser_tab_close',
title: 'Close a tab',
2025-04-03 19:24:17 -07:00
description: 'Close a tab',
inputSchema: z.object({
index: z.number().optional().describe('The index of the tab to close. Closes current tab if not provided.'),
}),
type: 'destructive',
2025-04-03 19:24:17 -07:00
},
2025-07-22 16:36:21 -07:00
handle: async (context, params, response) => {
await context.closeTab(params.index);
response.setIncludeSnapshot();
2025-04-03 19:24:17 -07:00
},
});
export default [
listTabs,
newTab,
selectTab,
closeTab,
];