write_docx
Create new Microsoft Word documents with structured content including paragraphs and tables. Specify file path and content to generate docx files for documentation or reports.
Instructions
Create a new docx file with given content.Editing exisiting docx file with this tool is not recomended.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Absolute path to target file. It should be under your current working directory. | |
| content | Yes | Content to write to the file. Two line breaks in content represent new paragraph.Table should starts with [Table], and separated with '|'.Escape line break when you input multiple lines. |
Implementation Reference
- mcp_server_office/office.py:128-150 (handler)The main handler function that implements the write_docx tool logic by creating a new Document, parsing content into paragraphs and tables, and saving to the specified path.async def write_docx(path: str, content: str) -> None: """Create a new docx file with the given content. Args: path: target path to create docx file content: text content to write """ document = Document() # Split content into sections sections = content.split("\n\n") for section in sections: if section.startswith("[Table]"): table = create_table_from_text(section[7:].strip()) # Remove [Table] prefix document.element.body.append(table._element) elif section.startswith("[Image]"): document.add_paragraph("[Image placeholder]") else: document.add_paragraph(section) document.save(path)
- mcp_server_office/tools.py:64-88 (schema)The input schema and metadata definition for the write_docx tool, including parameters for path and content.WRITE_DOCX = types.Tool( name="write_docx", description=( "Create a new docx file with given content." "Editing exisiting docx file with this tool is not recomended." ), inputSchema={ "type": "object", "properties": { "path": { "type": "string", "description": "Absolute path to target file. It should be under your current working directory.", }, "content": { "type": "string", "description": ( "Content to write to the file. Two line breaks in content represent new paragraph." "Table should starts with [Table], and separated with '|'." "Escape line break when you input multiple lines." ), } }, "required": ["path", "content"] } )
- mcp_server_office/office.py:358-360 (registration)The server endpoint that registers and lists available tools, including WRITE_DOCX.@server.list_tools() async def list_tools() -> list[types.Tool]: return [READ_DOCX, EDIT_DOCX_PARAGRAPH, WRITE_DOCX, EDIT_DOCX_INSERT]
- mcp_server_office/office.py:370-372 (registration)The tool dispatcher in call_tool that handles invocation of write_docx.elif name == "write_docx": await write_docx(arguments["path"], arguments["content"]) return [types.TextContent(type="text", text="Document created successfully")]