create_document
Create new Microsoft Word documents with customizable metadata like filename, title, and author for structured document management.
Instructions
Create a new Word document with optional metadata.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes | ||
| title | No | ||
| author | No |
Implementation Reference
- Core handler function that creates a new Word document (.docx) file, sets optional title and author metadata, ensures required styles are present, and handles file writability checks and errors.async def create_document(filename: str, title: Optional[str] = None, author: Optional[str] = None) -> str: """Create a new Word document with optional metadata. Args: filename: Name of the document to create (with or without .docx extension) title: Optional title for the document metadata author: Optional author for the document metadata """ filename = ensure_docx_extension(filename) # Check if file is writeable is_writeable, error_message = check_file_writeable(filename) if not is_writeable: return f"Cannot create document: {error_message}" try: doc = Document() # Set properties if provided if title: doc.core_properties.title = title if author: doc.core_properties.author = author # Ensure necessary styles exist ensure_heading_style(doc) ensure_table_style(doc) # Save the document doc.save(filename) return f"Document {filename} created successfully" except Exception as e: return f"Failed to create document: {str(e)}"
- word_document_server/main.py:94-97 (registration)MCP tool registration using FastMCP @mcp.tool() decorator. This thin wrapper registers the 'create_document' tool and delegates execution to the core handler in document_tools.@mcp.tool() def create_document(filename: str, title: str = None, author: str = None): """Create a new Word document with optional metadata.""" return document_tools.create_document(filename, title, author)