convert_to_html
Convert DOCX files to HTML while preserving formatting. Specify the file path and choose to include inline styles for accurate document representation.
Instructions
Convert DOCX file to HTML with formatting preserved
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | Path to the .docx file | |
| include_styles | No | Include inline styles (default: true) |
Implementation Reference
- src/index.ts:78-133 (handler)The handler function for the 'convert_to_html' tool. It resolves the DOCX file path, checks if the file exists, configures mammoth options based on include_styles flag, converts the DOCX to HTML using mammoth.convertToHtml, processes messages into warnings and errors, and returns a structured JSON response or error message.async ({ file_path, include_styles = true }) => { try { const absolutePath = path.resolve(file_path) if (!fs.existsSync(absolutePath)) { throw new Error(`File not found: ${absolutePath}`) } const options = include_styles ? {} : { styleMap: [ "p[style-name='Heading 1'] => h1:fresh", "p[style-name='Heading 2'] => h2:fresh", "p[style-name='Heading 3'] => h3:fresh", "r[style-name='Strong'] => strong", "r[style-name='Emphasis'] => em", ], } const result = await mammoth.convertToHtml( { path: absolutePath }, options ) return { content: [ { type: 'text', text: JSON.stringify( { html: result.value, messages: result.messages, warnings: result.messages.filter( (m: any) => m.type === 'warning' ), errors: result.messages.filter((m: any) => m.type === 'error'), }, null, 2 ), }, ], } } catch (error) { return { content: [ { type: 'text', text: `Error converting to HTML: ${(error as Error).message}`, }, ], isError: true, } } }
- src/index.ts:71-77 (schema)Input schema for the 'convert_to_html' tool using Zod validation. Defines 'file_path' as required string and 'include_styles' as optional boolean.{ file_path: z.string().describe('Path to the .docx file'), include_styles: z .boolean() .optional() .describe('Include inline styles (default: true)'), },
- src/index.ts:68-134 (registration)MCP server registration of the 'convert_to_html' tool, specifying name, description, input schema, and inline handler function.server.tool( 'convert_to_html', 'Convert DOCX file to HTML with formatting preserved', { file_path: z.string().describe('Path to the .docx file'), include_styles: z .boolean() .optional() .describe('Include inline styles (default: true)'), }, async ({ file_path, include_styles = true }) => { try { const absolutePath = path.resolve(file_path) if (!fs.existsSync(absolutePath)) { throw new Error(`File not found: ${absolutePath}`) } const options = include_styles ? {} : { styleMap: [ "p[style-name='Heading 1'] => h1:fresh", "p[style-name='Heading 2'] => h2:fresh", "p[style-name='Heading 3'] => h3:fresh", "r[style-name='Strong'] => strong", "r[style-name='Emphasis'] => em", ], } const result = await mammoth.convertToHtml( { path: absolutePath }, options ) return { content: [ { type: 'text', text: JSON.stringify( { html: result.value, messages: result.messages, warnings: result.messages.filter( (m: any) => m.type === 'warning' ), errors: result.messages.filter((m: any) => m.type === 'error'), }, null, 2 ), }, ], } } catch (error) { return { content: [ { type: 'text', text: `Error converting to HTML: ${(error as Error).message}`, }, ], isError: true, } } } )
- src/index.ts:11-11 (helper)Import of the 'mammoth' library used by the tool for DOCX to HTML conversion.const mammoth = require('mammoth')