focus_tab
Switch the active tab to the one identified by its index (from list_tabs) or by a URL substring.
Instructions
Switch the active tab by index (from list_tabs) or by URL substring.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| index | No | ||
| urlContains | No |
Implementation Reference
- src/index.ts:208-224 (handler)Handler function for focus_tab. Accepts optional 'index' (tab number from list_tabs) or 'urlContains' (URL substring). Iterates all contexts/pages, matches by index or URL substring, brings the matching tab to front, and sets it as the active page. Throws if no match found.
async function focusTab(args: { index?: number; urlContains?: string }) { if (!browser) throw new Error('Not attached.'); let i = 0; for (const ctx of browser.contexts()) { for (const p of ctx.pages()) { const matchByIndex = args.index !== undefined && i === args.index; const matchByUrl = args.urlContains && p.url().includes(args.urlContains); if (matchByIndex || matchByUrl) { page = p; await p.bringToFront().catch(() => {}); return { url: p.url(), title: await p.title().catch(() => '') }; } i++; } } throw new Error('No matching tab. Try list_tabs first.'); } - src/index.ts:358-368 (schema)Schema definition for the focus_tab tool. Describes the tool and its input schema with two optional properties: 'index' (number) and 'urlContains' (string).
{ name: 'focus_tab', description: 'Switch the active tab by index (from list_tabs) or by URL substring.', inputSchema: { type: 'object', properties: { index: { type: 'number' }, urlContains: { type: 'string' }, }, }, }, - src/index.ts:463-463 (registration)Registration/dispatch of focus_tab in the CallToolRequestSchema handler. Routes incoming 'focus_tab' calls to the focusTab function with proper type casting.
case 'focus_tab': result = await focusTab(args as { index?: number; urlContains?: string }); break;