Skip to main content
Glama
Orchardxyz

Mammoth MCP Server

by Orchardxyz

convert_docx_to_html

Convert Microsoft Word DOCX files to clean HTML while preserving formatting, tables, and lists. This tool transforms document content for web use.

Instructions

Convert a DOCX file to HTML using mammoth. Supports reading from a file path and returns the HTML content.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filePathYesAbsolute path to the DOCX file to convert

Implementation Reference

  • The handler function that implements the core logic of the 'convert_docx_to_html' tool. It reads the DOCX file using mammoth, converts it to HTML, formats the output with markdown including the HTML code block and any messages, and handles errors.
    async ({ filePath }) => {
      try {
        const absolutePath = path.resolve(filePath);
        await fs.access(absolutePath);
    
        const result = await mammoth.convertToHtml({ path: absolutePath });
    
        let output = `# Conversion Result\n\n`;
        output += `**File**: ${absolutePath}\n\n`;
        output += `## HTML Output:\n\n\`\`\`html\n${result.value}\n\`\`\`\n\n`;
    
        if (result.messages.length > 0) {
          output += `## Messages:\n\n`;
          result.messages.forEach((msg: any) => {
            output += `- ${msg.type}: ${msg.message}\n`;
          });
        }
    
        return {
          content: [
            {
              type: 'text',
              text: output,
            },
          ],
        };
      } catch (error) {
        return {
          content: [
            {
              type: 'text',
              text: `Error converting DOCX file: ${error instanceof Error ? error.message : String(error)}`,
            },
          ],
          isError: true,
        };
      }
    }
  • src/index.ts:15-62 (registration)
    The registration of the 'convert_docx_to_html' tool on the MCP server, specifying the tool name, description, input schema, and handler function.
    server.registerTool(
      'convert_docx_to_html',
      {
        description:
          'Convert a DOCX file to HTML using mammoth. Supports reading from a file path and returns the HTML content.',
        inputSchema: {
          filePath: z.string().describe('Absolute path to the DOCX file to convert'),
        },
      },
      async ({ filePath }) => {
        try {
          const absolutePath = path.resolve(filePath);
          await fs.access(absolutePath);
    
          const result = await mammoth.convertToHtml({ path: absolutePath });
    
          let output = `# Conversion Result\n\n`;
          output += `**File**: ${absolutePath}\n\n`;
          output += `## HTML Output:\n\n\`\`\`html\n${result.value}\n\`\`\`\n\n`;
    
          if (result.messages.length > 0) {
            output += `## Messages:\n\n`;
            result.messages.forEach((msg: any) => {
              output += `- ${msg.type}: ${msg.message}\n`;
            });
          }
    
          return {
            content: [
              {
                type: 'text',
                text: output,
              },
            ],
          };
        } catch (error) {
          return {
            content: [
              {
                type: 'text',
                text: `Error converting DOCX file: ${error instanceof Error ? error.message : String(error)}`,
              },
            ],
            isError: true,
          };
        }
      }
    );
  • The input schema for the tool, defining 'filePath' as a required string using Zod validation.
    inputSchema: {
      filePath: z.string().describe('Absolute path to the DOCX file to convert'),
    },

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/Orchardxyz/mammoth-mcp'

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