Skip to main content
Glama

merge_pdfs

Combine multiple PDF files from local paths or URLs into a single document. Specify the order of files and save the merged PDF to your chosen location.

Instructions

Merge multiple PDF files into one.

Supports both local file paths and URLs. URLs will be downloaded to temporary
files before merging. Mixed local and URL paths are supported.

Args:
    pdf_paths: List of paths to PDF files to merge (in order) - can include URLs
    output_path: Path where the merged PDF will be saved (must be local path)
    
Returns:
    Success message with merge details or error message

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pdf_pathsYes
output_pathYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • Implementation of the merge_pdfs MCP tool, which resolves input paths (including URLs), validates the output path, and merges PDFs using PyPDF2.
    async def merge_pdfs(pdf_paths: List[str], output_path: str) -> str:
        """Merge multiple PDF files into one.
        
        Supports both local file paths and URLs. URLs will be downloaded to temporary
        files before merging. Mixed local and URL paths are supported.
    
        Args:
            pdf_paths: List of paths to PDF files to merge (in order) - can include URLs
            output_path: Path where the merged PDF will be saved (must be local path)
            
        Returns:
            Success message with merge details or error message
        """
        # Resolve all input paths (download URLs if needed)
        actual_paths = []
        for pdf_path in pdf_paths:
            try:
                actual_path = resolve_path(pdf_path)
                
                # Validate local path if not URL
                if not is_url(pdf_path):
                    is_valid, error_msg = validate_path(pdf_path)
                    if not is_valid:
                        return error_msg
                
                actual_paths.append(actual_path)
                
            except Exception as e:
                return f"Error resolving path '{pdf_path}': {str(e)}"
        
        # Validate output path (must be local)
        if is_url(output_path):
            return "Error: Output path cannot be a URL, must be a local file path"
        
        is_valid, error_msg = validate_path(output_path)
        if not is_valid:
            return error_msg
        
        try:
            pdf_writer = PyPDF2.PdfWriter()
            total_pages_merged = 0
            
            for i, actual_path in enumerate(actual_paths):
                original_path = pdf_paths[i]
                try:
                    with open(actual_path, 'rb') as pdf_file:
                        pdf_reader = PyPDF2.PdfReader(pdf_file)
                        pages_count = len(pdf_reader.pages)
                        
                        for page in pdf_reader.pages:
                            pdf_writer.add_page(page)
                        
                        total_pages_merged += pages_count
                        logging.info(f"Added {pages_count} pages from {original_path}")
                
                except Exception as e:
                    return f"Error reading PDF '{original_path}': {str(e)}"
            
            # Write the merged PDF
            with open(output_path, 'wb') as output_file:
                pdf_writer.write(output_file)
            
            return f"Successfully merged {len(pdf_paths)} PDFs into '{output_path}'\nTotal pages: {total_pages_merged}"
            
        except Exception as e:
            return f"Error merging PDFs: {str(e)}"
Behavior3/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It adds useful context beyond basic function, such as that URLs are downloaded to temporary files and the output must be a local path. However, it lacks details on permissions, error handling, or rate limits, which are important for a tool that processes external resources.

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 well-structured and front-loaded with the core purpose, followed by supporting details and parameter explanations. Each sentence earns its place by providing essential information without redundancy, making it efficient and easy to parse.

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

Completeness4/5

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

Given the tool's moderate complexity (merging files with external inputs), no annotations, and an output schema present, the description is mostly complete. It covers input semantics and behavioral context but could benefit from more details on error cases or performance limits to fully guide the agent.

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?

The schema description coverage is 0%, so the description must fully compensate. It effectively explains both parameters: 'pdf_paths' as a list of paths in order that can include URLs, and 'output_path' as a local path for saving the merged PDF. This adds clear meaning beyond the bare schema.

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 ('merge multiple PDF files into one') and identifies the resource ('PDF files'). It distinguishes this tool from sibling tools like 'extract_pdf_pages' or 'get_pdf_info' by focusing on combining files rather than extracting, reading, or searching content.

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 provides clear context on when to use this tool by specifying it handles 'both local file paths and URLs' and supports 'mixed local and URL paths.' However, it does not explicitly state when not to use it or name alternatives among siblings, such as using 'extract_pdf_pages' for partial operations instead.

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/lockon-n/pdf-tools-mcp'

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