Skip to main content
Glama

copy_document

Duplicate a Word document by specifying source and optional destination filenames. Enables quick replication of content for editing, sharing, or backup purposes within a Word document management system.

Instructions

Create a copy of a Word document.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
destination_filenameNo
source_filenameYes

Implementation Reference

  • MCP tool registration for 'copy_document'. This is the entrypoint where the tool is registered with the FastMCP server using @mcp.tool() decorator. It delegates to the implementation in document_tools.
    async def copy_document(source_filename: str, destination_filename: Optional[str] = None):
        """Create a copy of a Word document."""
        return await document_tools.copy_document(source_filename, destination_filename)
  • The main handler function that executes the copy_document tool logic. Ensures .docx extensions and calls the file_utils helper to perform the copy.
    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}"
  • Supporting utility function that performs the actual file copying using shutil.copy2, generates destination name if needed, and handles errors.
    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
  • Import registration in tools package __init__.py, exposing copy_document for use in main.py.
    from word_document_server.tools.document_tools import (
        create_document, get_document_info, get_document_text, 
        get_document_outline, list_available_documents, 
        copy_document, merge_documents

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/franlealp1/mcp-word'

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