Skip to main content
Glama
andr3medeiros

PDF Manipulation MCP Server

pdf_set_metadata

Set or modify metadata properties in PDF files, such as title, author, keywords, and custom fields, to organize and describe document content.

Instructions

Set metadata for a PDF.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pdf_pathYes
metadataYes

Implementation Reference

  • The handler function for the 'pdf_set_metadata' tool. It validates the PDF file, opens it using PyMuPDF (fitz), iteratively sets each metadata field provided in the input dictionary, generates a timestamped output filename to avoid overwriting the original, saves the modified PDF, and returns a success message with the updated fields and output path.
    @mcp.tool()
    async def pdf_set_metadata(
        pdf_path: str,
        metadata: Dict[str, str]
    ) -> str:
        """Set metadata for a PDF."""
        if not os.path.exists(pdf_path):
            return f"Error: PDF file not found: {pdf_path}"
        
        if not validate_pdf_file(pdf_path):
            return f"Error: Invalid PDF file: {pdf_path}"
        
        try:
            # Open PDF document
            doc = fitz.open(pdf_path)
            
            # Set metadata fields
            updated_fields = []
            for field, value in metadata.items():
                if value:  # Only set non-empty values
                    doc.set_metadata({field: str(value)})
                    updated_fields.append(f"{field}: {value}")
            
            # Generate output filename
            output_path = generate_output_filename(pdf_path)
            
            # Save the modified PDF
            doc.save(output_path)
            doc.close()
            
            if updated_fields:
                return f"Successfully updated metadata. Output saved to: {output_path}\nUpdated fields:\n" + "\n".join(updated_fields)
            else:
                return "No metadata fields to update."
            
        except Exception as e:
            return f"Error setting PDF metadata: {str(e)}"

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/andr3medeiros/pdf-manipulation-mcp-server'

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