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
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path to the PDF file |
Implementation Reference
- src/pdf_reader_mcp/server.py:434-465 (handler)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)}")
- src/pdf_reader_mcp/server.py:351-361 (registration)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"], }, ),
- src/pdf_reader_mcp/server.py:354-360 (schema)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"], },
- src/pdf_reader_mcp/server.py:15-17 (helper)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