pdf_set_metadata
Set custom metadata for PDF documents including title, author, subject, and keywords to organize and identify files efficiently.
Instructions
Set metadata for a PDF.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pdf_path | Yes | ||
| metadata | Yes |
Implementation Reference
- The pdf_set_metadata tool handler function, registered via @mcp.tool() decorator. Validates input PDF path, opens with PyMuPDF (fitz), iteratively sets each metadata field if non-empty, generates timestamped output filename using helper, saves modified PDF, and returns success message with updated fields list or error.@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)}"