Skip to main content
Glama
ziux

Playwright Server MCP

by ziux

playwright_new_session

Create and manage new browser sessions to open windows and optionally navigate to specified URLs using browser automation capabilities.

Instructions

创建新的浏览器会话,打开一个浏览器窗口并可选择性地访问指定网址

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
urlNo需要访问的初始网址,可选参数,不填则只打开浏览器

Implementation Reference

  • NewSessionToolHandler class implements the core logic for the 'playwright_new_session' tool, launching a new Chromium browser instance, creating a page, managing sessions, and optionally navigating to a URL.
    class NewSessionToolHandler(ToolHandler):
        name = "playwright_new_session"
        description = "创建新的浏览器会话,打开一个浏览器窗口并可选择性地访问指定网址"
        inputSchema = [
            Property(name="url", typ="string", description="需要访问的初始网址,可选参数,不填则只打开浏览器", required=False)
        ]
        
        async def handle(self, name: str, arguments: dict | None) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]:
            logger.info("开始创建新的浏览器会话")
            try:
                self._playwright = await async_playwright().start()
                logger.debug("Playwright 实例已创建")
                
                browser = await self._playwright.chromium.launch(headless=False)
                logger.debug("浏览器已启动")
                
                page = await browser.new_page()
                logger.debug("新页面已创建")
                
                session_id = str(uuid.uuid4())
                self._sessions[session_id] = {"browser": browser, "page": page}
                logger.info(f"会话已创建,ID: {session_id}")
                
                url = arguments.get("url")
                if url:
                    if not url.startswith("http://") and not url.startswith("https://"):
                        url = "https://" + url
                    logger.info(f"正在导航到 URL: {url}")
                    await page.goto(url)
                    logger.debug(f"导航完成, 当前URL: {page.url}")
                return [types.TextContent(type="text", text="succ")]
            except Exception as e:
                logger.error(f"创建会话失败: {str(e)}", exc_info=True)
                return [types.TextContent(type="text", text=f"创建会话失败: {str(e)}")]
  • Defines the input schema for the tool, with an optional 'url' parameter of type string.
    inputSchema = [
        Property(name="url", typ="string", description="需要访问的初始网址,可选参数,不填则只打开浏览器", required=False)
    ]
  • Registers the NewSessionToolHandler instance in tool_handler_list and creates tool_handlers dictionary mapped by name, used by MCP server for list_tools() and call_tool().
    tool_handler_list = [
        NavigateToolHandler(),
        # ScreenshotToolHandler(),
        EvaluateToolHandler(),
        GetTextContentToolHandler(),
        GetHtmlContentToolHandler(),
        NewSessionToolHandler(),
        ActionToolHandler()
    ]
    
    # 根据每个处理程序的 name 属性创建字典
    tool_handlers = {handler.name: handler for handler in tool_handler_list}
  • Imports the NewSessionToolHandler class required for registration.
    from playwright_server.tools.handles import NavigateToolHandler, ScreenshotToolHandler, EvaluateToolHandler, GetTextContentToolHandler, GetHtmlContentToolHandler, NewSessionToolHandler
  • ToolHandler base class method to_tool() converts the handler's name, description, and inputSchema into MCP Tool object for registration in list_tools().
    @classmethod
    def to_tool(cls) -> types.Tool:
        return types.Tool(
            name=cls.name,
            description=cls.description,
            inputSchema={
                "type": "object",
                "properties": {
                        property.name: {
                        "type": property.typ,
                        "description": property.description
                    }
                    for property in cls.inputSchema
                },
                "required": [property.name for property in cls.inputSchema if property.required]
            }
        )
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/ziux/playwright-plus-python-mcp'

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