docx_to_html
Convert DOCX files to HTML format while maintaining original structure and layout using Simple Document Processing MCP Server.
Instructions
Convert DOCX to HTML while preserving formatting
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| inputPath | Yes | Path to the input DOCX file | |
| outputDir | Yes | Directory where HTML should be saved |
Implementation Reference
- src/tools/docxTools.ts:59-98 (handler)The core handler function that reads the DOCX file, converts it to HTML using the mammoth library, saves it with a unique filename in the output directory, and returns success/error info.export async function docxToHtml(inputPath: string, outputDir: string) { try { console.error(`Starting DOCX to HTML conversion...`); console.error(`Input file: ${inputPath}`); console.error(`Output directory: ${outputDir}`); // 確保輸出目錄存在 try { await fs.access(outputDir); console.error(`Output directory exists: ${outputDir}`); } catch { console.error(`Creating output directory: ${outputDir}`); await fs.mkdir(outputDir, { recursive: true }); console.error(`Created output directory: ${outputDir}`); } const uniqueId = generateUniqueId(); const buffer = await fs.readFile(inputPath); const result = await mammoth.convertToHtml({ buffer }); console.error( `Conversion completed with ${result.messages.length} messages` ); const outputPath = path.join(outputDir, `converted_${uniqueId}.html`); await fs.writeFile(outputPath, result.value); console.error(`Written HTML to ${outputPath}`); return { success: true, data: `Successfully converted DOCX to HTML: ${outputPath}`, }; } catch (error) { console.error(`Error in docxToHtml:`, error); return { success: false, error: error instanceof Error ? error.message : "Unknown error", }; } }
- src/tools/docxTools.ts:15-32 (schema)The tool definition including name, description, and input schema for validation.export const DOCX_TO_HTML_TOOL: Tool = { name: "docx_to_html", description: "Convert DOCX to HTML while preserving formatting", inputSchema: { type: "object", properties: { inputPath: { type: "string", description: "Path to the input DOCX file", }, outputDir: { type: "string", description: "Directory where HTML should be saved", }, }, required: ["inputPath", "outputDir"], }, };
- src/tools/_index.ts:9-9 (registration)The DOCX_TO_HTML_TOOL is imported and included in the central tools array for registration in the MCP server.export const tools = [DOCUMENT_READER_TOOL, PDF_MERGE_TOOL, PDF_SPLIT_TOOL, DOCX_TO_PDF_TOOL, DOCX_TO_HTML_TOOL, HTML_CLEAN_TOOL, HTML_TO_TEXT_TOOL, HTML_TO_MARKDOWN_TOOL, HTML_EXTRACT_RESOURCES_TOOL, HTML_FORMAT_TOOL, TEXT_DIFF_TOOL, TEXT_SPLIT_TOOL, TEXT_FORMAT_TOOL, TEXT_ENCODING_CONVERT_TOOL, EXCEL_READ_TOOL, FORMAT_CONVERTER_TOOL];
- src/index.ts:132-148 (registration)Server request handler dispatches 'docx_to_html' tool calls by extracting arguments and invoking the docxToHtml handler function.if (name === "docx_to_html") { const { inputPath, outputDir } = args as { inputPath: string; outputDir: string; }; const result = await docxToHtml(inputPath, outputDir); if (!result.success) { return { content: [{ type: "text", text: `Error: ${result.error}` }], isError: true, }; } return { content: [{ type: "text", text: fileOperationResponse(result.data) }], isError: false, }; }
- src/tools/docxTools.ts:10-12 (helper)Helper function to generate unique ID for output filenames.function generateUniqueId(): string { return randomBytes(9).toString("hex"); }