copy_document
Duplicate Word documents to create backups, share versions, or preserve original files while editing.
Instructions
Create a copy of a Word document.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| source_filename | Yes | ||
| destination_filename | No |
Implementation Reference
- Main async handler that validates filenames and delegates to the file copy utility.async def copy_document(source_filename: str, destination_filename: Optional[str] = None) -> str: """Create a copy of a Word document. Args: source_filename: Path to the source document destination_filename: Optional path for the copy. If not provided, a default name will be generated. """ source_filename = ensure_docx_extension(source_filename) if destination_filename: destination_filename = ensure_docx_extension(destination_filename) success, message, new_path = create_document_copy(source_filename, destination_filename) if success: return message else: return f"Failed to copy document: {message}"
- word_document_server/main.py:99-102 (registration)Registers the synchronous wrapper for the copy_document tool with the FastMCP server.@mcp.tool() def copy_document(source_filename: str, destination_filename: str = None): """Create a copy of a Word document.""" return document_tools.copy_document(source_filename, destination_filename)
- Core utility function that performs the actual document copying using shutil.copy2.def create_document_copy(source_path: str, dest_path: Optional[str] = None) -> Tuple[bool, str, Optional[str]]: """ Create a copy of a document. Args: source_path: Path to the source document dest_path: Optional path for the new document. If not provided, will use source_path + '_copy.docx' Returns: Tuple of (success, message, new_filepath) """ if not os.path.exists(source_path): return False, f"Source document {source_path} does not exist", None if not dest_path: # Generate a new filename if not provided base, ext = os.path.splitext(source_path) dest_path = f"{base}_copy{ext}" try: # Simple file copy shutil.copy2(source_path, dest_path) return True, f"Document copied to {dest_path}", dest_path except Exception as e: return False, f"Failed to copy document: {str(e)}", None