Skip to main content
Glama
pietermyb

mcp-pdf-reader

open-pdf

Open PDF files from specified paths to enable reading and processing of document content within the MCP server environment.

Instructions

Open a PDF file

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesPath to the PDF file

Implementation Reference

  • Handler for the 'open-pdf' tool: validates the PDF path, opens it with PyPDF2.PdfReader, generates a unique PDF ID, stores the reader and path in global dictionaries, notifies clients of resource changes, and returns a success message with PDF details.
    if name == "open-pdf":
        path = arguments.get("path")
        if not path:
            raise ValueError("Missing path")
    
        # Validate that the file exists and is a PDF
        if not os.path.exists(path):
            raise ValueError(f"File not found: {path}")
    
        try:
            # Try to open as PDF
            reader = PyPDF2.PdfReader(path)
    
            # Generate a unique ID
            pdf_id = base64.urlsafe_b64encode(os.path.abspath(path).encode()).decode()[:12]
    
            # Store the reader and path
            pdfs[pdf_id] = reader
            pdf_paths[pdf_id] = path
    
            # Notify clients that resources have changed
            await server.request_context.session.send_resource_list_changed()
    
            return [
                types.TextContent(
                    type="text",
                    text=f"Opened PDF '{os.path.basename(path)}' with {len(reader.pages)} pages. PDF ID: {pdf_id}",
                )
            ]
        except Exception as e:
            raise ValueError(f"Failed to open PDF: {str(e)}")
  • Registration of the 'open-pdf' tool in the handle_list_tools function, defining the tool name, description, and input schema requiring a 'path' parameter.
    types.Tool(
        name="open-pdf",
        description="Open a PDF file",
        inputSchema={
            "type": "object",
            "properties": {
                "path": {"type": "string", "description": "Path to the PDF file"},
            },
            "required": ["path"],
        },
    ),
  • JSON Schema for the 'open-pdf' tool input: requires a 'path' string parameter.
    inputSchema={
        "type": "object",
        "properties": {
            "path": {"type": "string", "description": "Path to the PDF file"},
        },
        "required": ["path"],
    },
  • Global state storage for opened PDFs: dictionaries holding PdfReader objects and file paths by PDF ID, used by the open-pdf handler.
    # Store opened PDF documents
    pdfs: Dict[str, PyPDF2.PdfReader] = {}
    pdf_paths: Dict[str, str] = {}  # Map of PDF IDs to their file paths

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/pietermyb/mcp-pdf-reader'

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