Skip to main content
Glama

extract-pages

Extract specific pages from a PDF file to create a new document containing only selected content.

Instructions

Extract specific pages from a PDF file

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
input_pathYesInput PDF file path
output_pathYesOutput path for new PDF
pagesYesList of page numbers to extract (1-based indexing)

Implementation Reference

  • The main handler logic for the 'extract-pages' tool. It reads the input PDF using PyPDF2.PdfReader, extracts the specified pages (converting from 1-based to 0-based indexing), writes them to a new PDF using PdfWriter, and returns a success or error message.
    elif name == "extract-pages":
        input_path = arguments.get("input_path")
        output_path = arguments.get("output_path")
        pages = arguments.get("pages", [])
        
        if not input_path or not output_path or not pages:
            raise ValueError("Missing required arguments")
    
        try:
            reader = PyPDF2.PdfReader(input_path)
            writer = PyPDF2.PdfWriter()
    
            # Convert 1-based page numbers to 0-based indices
            for page_num in pages:
                if 1 <= page_num <= len(reader.pages):
                    writer.add_page(reader.pages[page_num - 1])
                else:
                    return [types.TextContent(
                        type="text",
                        text=f"Error: Page number {page_num} is out of range"
                    )]
    
            # Write the extracted pages to the output file
            with open(output_path, 'wb') as output_file:
                writer.write(output_file)
    
            return [types.TextContent(
                type="text",
                text=f"Successfully extracted {len(pages)} pages to {output_path}"
            )]
            
        except Exception as e:
            return [types.TextContent(
                type="text",
                text=f"Error extracting pages: {str(e)}"
            )]
  • The input schema definition for the 'extract-pages' tool, specifying the required parameters: input_path (string), output_path (string), and pages (array of integers). This is returned by the list_tools handler.
    types.Tool(
        name="extract-pages",
        description="Extract specific pages from a PDF file",
        inputSchema={
            "type": "object",
            "properties": {
                "input_path": {
                    "type": "string",
                    "description": "Input PDF file path"
                },
                "output_path": {
                    "type": "string",
                    "description": "Output path for new PDF"
                },
                "pages": {
                    "type": "array",
                    "items": {"type": "integer"},
                    "description": "List of page numbers to extract (1-based indexing)"
                }
            },
            "required": ["input_path", "output_path", "pages"]
        }
    ),
  • The tool is registered by being included in the list returned by the handle_list_tools function, decorated with @server.list_tools(). This makes it discoverable by MCP clients.
    @server.list_tools()
    async def handle_list_tools() -> list[types.Tool]:
        """List available PDF manipulation tools."""
        return [
            types.Tool(
                name="merge-pdfs",
                description="Merge multiple PDF files into a single PDF",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "input_paths": {
                            "type": "array",
                            "items": {"type": "string"},
                            "description": "List of input PDF file paths"
                        },
                        "output_path": {
                            "type": "string",
                            "description": "Output path for merged PDF"
                        }
                    },
                    "required": ["input_paths", "output_path"]
                }
            ),
            types.Tool(
                name="extract-pages",
                description="Extract specific pages from a PDF file",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "input_path": {
                            "type": "string",
                            "description": "Input PDF file path"
                        },
                        "output_path": {
                            "type": "string",
                            "description": "Output path for new PDF"
                        },
                        "pages": {
                            "type": "array",
                            "items": {"type": "integer"},
                            "description": "List of page numbers to extract (1-based indexing)"
                        }
                    },
                    "required": ["input_path", "output_path", "pages"]
                }
            ),
            types.Tool(
                name="search-pdfs",
                description="Search for PDF files in a directory with optional pattern matching",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "base_path": {
                            "type": "string",
                            "description": "Base directory to search in"
                        },
                        "pattern": {
                            "type": "string",
                            "description": "Pattern to match against filenames (e.g., 'report*.pdf')"
                        },
                        "recursive": {
                            "type": "boolean",
                            "description": "Whether to search in subdirectories",
                            "default": True
                        }
                    },
                    "required": ["base_path"]
                }
            ),
            types.Tool(
                name="merge-pdfs-ordered",
                description="Merge PDFs in a specific order based on patterns or exact names",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "base_path": {
                            "type": "string",
                            "description": "Base directory containing PDFs"
                        },
                        "patterns": {
                            "type": "array",
                            "items": {"type": "string"},
                            "description": "List of patterns or names in desired order"
                        },
                        "output_path": {
                            "type": "string",
                            "description": "Output path for merged PDF"
                        },
                        "fuzzy_matching": {
                            "type": "boolean",
                            "description": "Use fuzzy matching for filenames",
                            "default": True
                        }
                    },
                    "required": ["base_path", "patterns", "output_path"]
                }
            ),
            types.Tool(
                name="find-related-pdfs",
                description="Find a PDF and then search for related PDFs based on its content, including common substring patterns",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "base_path": {
                            "type": "string",
                            "description": "Base directory to search in"
                        },
                        "target_filename": {
                            "type": "string",
                            "description": "Name of the initial PDF to analyze"
                        },
                        "pattern_matching_only": {
                            "type": "boolean", 
                            "description": "Only search for repeating substring patterns",
                            "default": False
                        },
                        "min_pattern_occurrences": {
                            "type": "integer",
                            "description": "Minimum times a pattern must appear to be considered significant",
                            "default": 2
                        }
                    },
                    "required": ["base_path", "target_filename"]
                }
            )
        ]
Behavior2/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 states the action ('extract') but doesn't mention whether this is a read-only or destructive operation, potential side effects (e.g., file creation at output_path), error conditions, or performance considerations. This leaves significant gaps in understanding how the tool behaves beyond its basic function.

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, direct sentence that efficiently conveys the core function without any wasted words. It is appropriately sized and front-loaded, making it easy for an agent to parse quickly and understand the tool's purpose at a glance.

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 moderate complexity (file manipulation with three parameters) and lack of annotations or output schema, the description is minimally adequate. It covers the basic action but omits critical context like behavioral traits, usage guidelines, and output details, leaving the agent with incomplete information for safe and effective invocation.

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?

The schema description coverage is 100%, with clear documentation for all three parameters (input_path, output_path, pages). The description adds no additional semantic context beyond what the schema provides, such as file format requirements or page numbering specifics, so it meets the baseline for adequate but not enhanced parameter understanding.

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 ('extract specific pages') and resource ('from a PDF file'), making the tool's purpose immediately understandable. However, it doesn't differentiate from sibling tools like 'merge-pdfs' or 'search-pdfs' beyond the basic function, which prevents a perfect score.

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?

The description provides no guidance on when to use this tool versus alternatives like 'merge-pdfs' or 'search-pdfs'. It lacks any context about prerequisites, such as needing an existing PDF file, or exclusions, leaving the agent to infer usage scenarios independently.

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

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