Skip to main content
Glama
cablate

Simple Document Processing MCP Server

docx_to_html

Convert DOCX files to HTML format while preserving original document formatting for web display or further processing.

Instructions

Convert DOCX to HTML while preserving formatting

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
inputPathYesPath to the input DOCX file
outputDirYesDirectory where HTML should be saved

Implementation Reference

  • Defines the tool schema for 'docx_to_html', including name, description, and input schema requiring inputPath and outputDir.
    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"], }, };
  • The core handler function that converts DOCX to HTML using mammoth, ensures output directory exists, generates unique filename, and returns success/error.
    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", }; } }
  • Imports DOCX_TO_HTML_TOOL and includes it in the exported 'tools' array used for MCP tool listing.
    import { DOCUMENT_READER_TOOL } from "./documentReader.js"; import { DOCX_TO_HTML_TOOL, DOCX_TO_PDF_TOOL } from "./docxTools.js"; import { EXCEL_READ_TOOL } from "./excelTools.js"; import { FORMAT_CONVERTER_TOOL } from "./formatConverterPlus.js"; import { HTML_CLEAN_TOOL, HTML_EXTRACT_RESOURCES_TOOL, HTML_FORMAT_TOOL, HTML_TO_MARKDOWN_TOOL, HTML_TO_TEXT_TOOL } from "./htmlTools.js"; import { PDF_MERGE_TOOL, PDF_SPLIT_TOOL } from "./pdfTools.js"; import { TEXT_DIFF_TOOL, TEXT_ENCODING_CONVERT_TOOL, TEXT_FORMAT_TOOL, TEXT_SPLIT_TOOL } from "./txtTools.js"; 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)
    MCP server request handler dispatches 'docx_to_html' tool calls to the docxToHtml 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, }; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/cablate/mcp-doc-forge'

If you have feedback or need assistance with the MCP directory API, please join our Discord server