Skip to main content
Glama

playwright_click_and_switch_tab

Automate clicking a link and switching to the newly opened tab in a browser using Playwright MCP Server. Requires a CSS selector to identify the link.

Instructions

Click a link and switch to the newly opened tab

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
selectorYesCSS selector for the link to click

Implementation Reference

  • The ClickAndSwitchTabTool class implementing the core logic: clicks the selector, waits for new tab via context.waitForEvent('page'), loads it, switches global page, returns URL.
    export class ClickAndSwitchTabTool extends BrowserToolBase {
      /**
       * Execute the click and switch tab tool
       */
      async execute(args: any, context: ToolContext): Promise<ToolResponse> {
        
        return this.safeExecute(context, async (page) => {
          // Listen for a new tab to open
          const [newPage] = await Promise.all([
            //context.browser.waitForEvent('page'), // Wait for a new page (tab) to open
            page.context().waitForEvent('page'),// Wait for a new page (tab) to open
            page.click(args.selector), // Click the link that opens the new tab
          ]);
    
          // Wait for the new page to load
          await newPage.waitForLoadState('domcontentloaded');
    
          // Switch control to the new tab
          setGlobalPage(newPage);
          //page= newPage; // Update the current page to the new tab
          //context.page = newPage;
          //context.page.bringToFront(); // Bring the new tab to the front
          return createSuccessResponse(`Clicked link and switched to new tab: ${newPage.url()}`);
          //return createSuccessResponse(`Clicked link and switched to new tab: ${context.page.url()}`);
        });
      }
    }
  • Tool schema definition including name, description, and input schema requiring 'selector' string.
      name: "playwright_click_and_switch_tab",
      description: "Click a link and switch to the newly opened tab",
      inputSchema: {
        type: "object",
        properties: {
          selector: { type: "string", description: "CSS selector for the link to click" },
        },
        required: ["selector"],
      },
    },
  • Registration in the main tool handler switch statement dispatching to the tool's execute method.
    case "playwright_click_and_switch_tab":
      return await clickAndSwitchTabTool.execute(args, context);
  • src/tools.ts:450-450 (registration)
    Tool name registered in BROWSER_TOOLS array for browser-requiring tool detection.
    "playwright_click_and_switch_tab"
  • Instantiation of ClickAndSwitchTabTool instance in initializeTools function.
    if (!clickAndSwitchTabTool) clickAndSwitchTabTool = new ClickAndSwitchTabTool(server);
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It mentions the action (click and switch tab) but lacks details on behavioral traits such as error handling (e.g., if no new tab opens), timing (e.g., waits for tab), permissions, or side effects. This is a significant gap for a tool with potential navigation impacts.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core functionality. There is no wasted wording, and it directly communicates the tool's purpose without redundancy or fluff.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (interactive browser action with tab management), lack of annotations, and no output schema, the description is minimally adequate. It states what the tool does but omits important contextual details like return values, error conditions, or prerequisites, leaving gaps for an agent to use it correctly.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, with the parameter 'selector' clearly documented in the schema as a CSS selector. The description does not add any additional meaning beyond this, such as examples or constraints. Baseline 3 is appropriate since the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('click a link') and the subsequent effect ('switch to the newly opened tab'), distinguishing it from sibling tools like 'playwright_click' (which only clicks) and 'playwright_get' (which navigates without clicking). It uses precise verbs and identifies the resource (a link via selector).

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage context (clicking links that open new tabs) but does not explicitly state when not to use it or name alternatives. For example, it doesn't clarify if it should be used over 'playwright_click' for links that open in the same tab or other navigation tools. However, the context is clear enough for basic differentiation.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/BhanuTJ93/MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server