docx_to_html
Transform DOCX documents into HTML while preserving formatting. Provide the input file path and output directory to complete the conversion.
Instructions
Convert DOCX to HTML while preserving formatting
Input 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 docxToHtml async function that handles the actual DOCX-to-HTML conversion using the 'mammoth' library. It reads the DOCX file, converts it to HTML, and saves it to the output directory with a unique ID.
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 DOCX_TO_HTML_TOOL constant defines the tool's name ('docx_to_html'), description, and input schema specifying required inputPath and outputDir string parameters.
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 tool is registered in the 'tools' array exported from _index.ts, making it available to 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 (handler)The call handler in the MCP server that routes 'docx_to_html' requests to the docxToHtml function and returns the result.
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 generateUniqueId used to create unique filenames for the converted HTML output.
function generateUniqueId(): string { return randomBytes(9).toString("hex"); }