Skip to main content
Glama
dragons96

MCP-Undetected-Chromedriver

by dragons96

browser_save_as_pdf

Convert and save the current browser page as a PDF with customizable options like format, margin, and filename. Ideal for automation, testing, or generating printable content from advanced websites.

Instructions

Save the current page as a PDF

Args:
    outputPath: The path to save the PDF to - required
    filename: The name of the PDF file - optional, default is "page.pdf"
    format: The format of the PDF - optional, default is "A4" (e.g. "A4", "LETTER", "LEGAL", "TABLOID")
    printBackground: Whether to print the background - optional, default is True
    margin: The margin of the PDF - optional, default is None (e.g. {"top": "1cm", "right": "1cm", "bottom": "1cm", "left": "1cm"})

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filenameNopage.pdf
formatNoA4
marginNo
outputPathYes
printBackgroundNo

Implementation Reference

  • Implementation of the browser_save_as_pdf tool handler. This async function handles saving the current browser page as a PDF file. It uses Chrome's print_page functionality with customizable options for format, margins, and background printing. The @mcp.tool() decorator registers it as an MCP tool.
    @mcp.tool()
    async def browser_save_as_pdf(
            outputPath: str,
            filename: str = "page.pdf",
            format: str = 'A4',
            printBackground: bool = True,
            margin: dict = None,
    ):
        """Save the current page as a PDF
    
        Args:
            outputPath: The path to save the PDF to - required
            filename: The name of the PDF file - optional, default is "page.pdf"
            format: The format of the PDF - optional, default is "A4" (e.g. "A4", "LETTER", "LEGAL", "TABLOID")
            printBackground: Whether to print the background - optional, default is True
            margin: The margin of the PDF - optional, default is None (e.g. {"top": "1cm", "right": "1cm", "bottom": "1cm", "left": "1cm"})
        """
        assert outputPath, "Output path is required"
    
        margin = margin or {"top": 0, "right": 0, "bottom": 0, "left": 0}
    
        async def save_as_pdf_handler(driver: uc.Chrome):
            # 确保输出路径存在
            os.makedirs(outputPath, exist_ok=True)
    
            # 构建完整文件路径
            full_path = os.path.join(outputPath, filename)
    
            # 设置打印选项
            print_options = PrintOptions()
            print_options.orientation  = 'portrait'
            print_options.scale = 1.0
            print_options.background = printBackground
            print_options.margin_left = margin.get('left', 0)
            print_options.margin_right = margin.get('right', 0)
            print_options.margin_top = margin.get('top', 0)
            print_options.margin_bottom = margin.get('bottom', 0)
    
            # 保存PDF文件
            data = driver.print_page(print_options)
            with open(full_path, 'wb') as f:
                f.write(base64.b64decode(data))
    
            return await create_success_response(f"Saved page as PDF to {full_path}")
    
        return await tool.safe_execute(
            ToolContext(webdriver=await ensure_browser()), save_as_pdf_handler
        )
  • The @mcp.tool() decorator registers the browser_save_as_pdf function as an MCP tool.
    @mcp.tool()
  • Docstring defining the input parameters and their descriptions for the browser_save_as_pdf tool.
    """Save the current page as a PDF
    
    Args:
        outputPath: The path to save the PDF to - required
        filename: The name of the PDF file - optional, default is "page.pdf"
        format: The format of the PDF - optional, default is "A4" (e.g. "A4", "LETTER", "LEGAL", "TABLOID")
        printBackground: Whether to print the background - optional, default is True
        margin: The margin of the PDF - optional, default is None (e.g. {"top": "1cm", "right": "1cm", "bottom": "1cm", "left": "1cm"})
    """
Behavior2/5

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

With no annotations provided, the description carries full burden for behavioral disclosure. It states the action ('save') but doesn't mention important behavioral aspects: whether this overwrites existing files, what happens on invalid paths, if it requires specific page states (e.g., fully loaded), or error conditions. The description covers what the tool does but not how it behaves in edge cases.

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

Conciseness4/5

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

Well-structured with purpose statement followed by parameter details. The parameter explanations are efficient with required/optional indicators, defaults, and examples. Minor room for improvement: could be more front-loaded with key behavioral information, but overall sentences earn their place without waste.

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 5 parameters, no annotations, no output schema, and nested objects, the description does a decent job but has gaps. It thoroughly documents parameters but lacks information about return values (success/failure indicators), error handling, or dependencies on browser state. For a file-writing tool with complex parameters, more behavioral context would be helpful.

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

Parameters5/5

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

Schema description coverage is 0%, so the description must fully compensate - and it does excellently. For all 5 parameters, it provides clear semantics: outputPath is 'required', filename default is 'page.pdf', format examples include 'A4', 'LETTER', etc., printBackground default is 'True', and margin examples show object structure. This adds substantial meaning beyond the bare 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 tool's purpose: 'Save the current page as a PDF' - a specific verb ('save') and resource ('current page as PDF'). It distinguishes from siblings like browser_screenshot (image capture) and browser_get_visible_html/text (content extraction), though not explicitly. The purpose is unambiguous but lacks explicit sibling differentiation.

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 on when to use this tool versus alternatives. It doesn't mention when PDF saving is preferable over browser_screenshot for document capture, or prerequisites like needing a page loaded in the browser. The description provides only functional information without contextual usage advice.

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/dragons96/mcp-undetected-chromedriver'

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