Skip to main content
Glama
zeph-gh

DOCX MCP Server

by zeph-gh

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
NameRequiredDescriptionDefault
file_pathYesPath to the .docx file
include_stylesNoInclude inline styles (default: true)

Implementation Reference

  • 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, } } }
  • 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, } } } )
  • Import of the 'mammoth' library used by the tool for DOCX to HTML conversion.
    const mammoth = require('mammoth')

Other Tools

Related Tools

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/zeph-gh/Docx-Mcp-Server'

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