convert_docx_to_html
Convert Microsoft Word DOCX files to clean HTML while preserving formatting, tables, and lists for web publishing or content migration.
Instructions
Convert a DOCX file to HTML using mammoth. Supports reading from a file path and returns the HTML content.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Absolute path to the DOCX file to convert |
Implementation Reference
- src/index.ts:24-62 (handler)Handler function that performs the DOCX to HTML conversion using mammoth library, formats the output with markdown including the HTML content and any conversion messages, and handles errors appropriately.async ({ filePath }) => { try { const absolutePath = path.resolve(filePath); await fs.access(absolutePath); const result = await mammoth.convertToHtml({ path: absolutePath }); let output = `# Conversion Result\n\n`; output += `**File**: ${absolutePath}\n\n`; output += `## HTML Output:\n\n\`\`\`html\n${result.value}\n\`\`\`\n\n`; if (result.messages.length > 0) { output += `## Messages:\n\n`; result.messages.forEach((msg: any) => { output += `- ${msg.type}: ${msg.message}\n`; }); } return { content: [ { type: 'text', text: output, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `Error converting DOCX file: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } } );
- src/index.ts:17-23 (schema)Input schema and description for the convert_docx_to_html tool, defining the required filePath parameter using Zod.{ description: 'Convert a DOCX file to HTML using mammoth. Supports reading from a file path and returns the HTML content.', inputSchema: { filePath: z.string().describe('Absolute path to the DOCX file to convert'), }, },
- src/index.ts:15-62 (registration)Registration of the convert_docx_to_html tool on the MCP server, specifying the tool name, schema, and handler function.server.registerTool( 'convert_docx_to_html', { description: 'Convert a DOCX file to HTML using mammoth. Supports reading from a file path and returns the HTML content.', inputSchema: { filePath: z.string().describe('Absolute path to the DOCX file to convert'), }, }, async ({ filePath }) => { try { const absolutePath = path.resolve(filePath); await fs.access(absolutePath); const result = await mammoth.convertToHtml({ path: absolutePath }); let output = `# Conversion Result\n\n`; output += `**File**: ${absolutePath}\n\n`; output += `## HTML Output:\n\n\`\`\`html\n${result.value}\n\`\`\`\n\n`; if (result.messages.length > 0) { output += `## Messages:\n\n`; result.messages.forEach((msg: any) => { output += `- ${msg.type}: ${msg.message}\n`; }); } return { content: [ { type: 'text', text: output, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `Error converting DOCX file: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } } );