Open
Description
As evidenced by tests and direct testing, browser_tab_select expects 1-based indexing.
Without it being explained in the tool definition, most LLMs will default to 0-based indexing, and they are very stubborn about it unless given very specific prompts.
I am not even sure that adjusting tool definition will help much - maybe a better route is to make the tool 0-indexed as standard for programming APIs.
Claude Code with Sonnet 3.7 example - it tries to use index: 1
four times before deciding to use index: 2
.
I've opened a new tab. It looks like you now have three tabs open:
1. Google
2. X (Twitter)
3. Current tab (blank)
The new tab is currently blank. Would you like to navigate to a specific website in this tab?
> switch to twitter
● I'll switch to the Twitter tab.
● playwright:browser_tab_select (MCP)(index: 1)
⎿ - Ran Playwright code:
```js
// <internal code to select tab 1>
… +68 lines (ctrl+r to expand)
● Let me select the Twitter tab (tab #2).
● playwright:browser_tab_select (MCP)(index: 1)
⎿ - Ran Playwright code:
```js
// <internal code to select tab 1>
… +68 lines (ctrl+r to expand)
● Let me try switching to the Twitter tab again with the correct index:
⎿ ### Open tabs
- 1: (current) [Google] (https://www.google.com/)
- 2: [(9) Home / X] (https://x.com/home)
● playwright:browser_tab_select (MCP)(index: 1)
⎿ - Ran Playwright code:
```js
// <internal code to select tab 1>
… +68 lines (ctrl+r to expand)
● Let me try with the correct index. The tab indices are zero-based, so Twitter would be index 1:
● playwright:browser_tab_select (MCP)(index: 1)
⎿ - Ran Playwright code:
```js
// <internal code to select tab 1>
… +68 lines (ctrl+r to expand)
● It seems there might be an issue with the tab switching. Let me try one more approach using the
tab index that matches the displayed numbers in the list:
● playwright:browser_tab_select (MCP)(index: 2)
⎿ - Ran Playwright code:
```js
// <internal code to select tab 2>
… +774 lines (ctrl+r to expand)
● Great! Successfully switched to the Twitter (X) tab. You're now viewing your X/Twitter home feed
with various posts from accounts you follow or that are recommended to you.
ChatGPT 4o (I don't have a coding setup, but the point stands):
Given such a tool description in playwright-mcp
const selectTab: ToolFactory = captureSnapshot => defineTool({
capability: 'tabs',
schema: {
name: 'browser_tab_select',
title: 'Select a tab',
description: 'Select a tab by index',
inputSchema: z.object({
index: z.number().describe('The index of the tab to select'),
}),
type: 'readOnly',
},
handle: async (context, params) => {
await context.selectTab(params.index);
const code = [
`// <internal code to select tab ${params.index}>`,
];
return {
code,
captureSnapshot,
waitForNetwork: false
};
},
});
how would you switch to a second tab in a window?
ChatGPT said:
To switch to the second tab in a browser window using the given selectTab tool from playwright-mcp, you would invoke the tool with an input where index is 1.
In JavaScript (0-based indexing), the second tab has an index of 1.
Example Usage
await tools.browser_tab_select({ index: 1 });
Explanation
The selectTab tool takes an input object with a single property index.
The index is zero-based, so:
0 = first tab
1 = second tab
2 = third tab, and so on.
This command would programmatically switch focus to the second tab in the current browser context.