Skip to main content
Glama
Lokii0911
by Lokii0911

switch_window

Switches the active browser context to a specific window or tab using its Selenium window handle. Useful for managing multiple browser windows or tabs during automation.

Instructions

Switch to a browser window or tab by Selenium window handle.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
handleYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The core handler: BrowserManager.switch_window() calls Selenium's switch_to.window(handle) with thread safety.
    def switch_window(self, handle: str) -> BrowserState:
        with self._lock:
            self._require_driver().switch_to.window(handle)
            return self.state()
  • The MCP tool registration: switch_window is decorated with @mcp.tool() and delegates to browser.switch_window via _run().
    @mcp.tool()
    def switch_window(handle: str) -> dict[str, Any]:
        """Switch to a browser window or tab by Selenium window handle."""
        return _run("switch_window", browser.switch_window, handle)
  • The _run() helper that executes any browser method and converts the result via _as_dict(), with error handling.
    def _run(action: str, func: Any, *args: Any, **kwargs: Any) -> Any:
        try:
            return _as_dict(func(*args, **kwargs))
        except BrowserError:
            logger.exception("Browser action failed: %s", action)
            raise
        except Exception as exc:
            logger.exception("Unexpected Selenium MCP error during %s", action)
            raise BrowserError(f"{action} failed: {exc}") from exc
  • The _as_dict() helper that serializes Pydantic models (like BrowserState) to dictionaries.
    def _as_dict(value: Any) -> Any:
        if hasattr(value, "model_dump"):
            return value.model_dump()
        return value
  • The BrowserState model returned by switch_window, containing session_id, URL, title, window_handles, and active_window_handle.
    class BrowserState(BaseModel):
        session_id: str | None
        current_url: str | None
        title: str | None
        window_handles: list[str]
        active_window_handle: str | None
Behavior2/5

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

With no annotations, the description must disclose behavioral traits. It only states the action but fails to explain side effects (e.g., does it close previous window?), error conditions (e.g., invalid handle), or return value.

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

Conciseness3/5

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

The description is a single sentence, concise, but it is too short to convey necessary information. It could be expanded with clear behavioral notes without becoming verbose.

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

Completeness2/5

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

Given the complexity (1 param, no annotations), the description is incomplete. It lacks usage context, error handling, and relation to sibling tools like close_window or navigate. The presence of an output schema is not leveraged.

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

Parameters1/5

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

The parameter 'handle' is not described in the description; schema coverage is 0%. The description only mentions 'by Selenium window handle' without specifying format, source, or constraints, adding almost no value beyond the schema.

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

Purpose4/5

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

The description clearly states the action ('Switch') and the resource ('browser window or tab') with a specific method ('by Selenium window handle'), making the purpose straightforward. However, it does not differentiate from sibling tools like close_window or open_new_tab, but those are distinct actions.

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

Usage Guidelines2/5

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

No guidance is provided on when to use this tool versus alternatives (e.g., navigate, open_new_tab). There is no mention of prerequisites, such as the need for a valid window handle, or context for use.

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

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/Lokii0911/SeleniumMCP'

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