Skip to main content
Glama
Orchardxyz

Mammoth MCP Server

by Orchardxyz

convert_docx_to_html_with_images

Convert DOCX files to HTML with images embedded as base64 data URIs for web display or content migration.

Instructions

Convert a DOCX file to HTML with embedded images as base64 data URIs

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filePathYesAbsolute path to the DOCX file to convert

Implementation Reference

  • The main handler function for the 'convert_docx_to_html_with_images' tool. It resolves the file path, converts the DOCX to HTML using mammoth with custom image conversion to base64 data URIs, formats the output with markdown, and handles errors.
    async ({ filePath }) => {
      try {
        const absolutePath = path.resolve(filePath);
        await fs.access(absolutePath);
    
        const result = await mammoth.convertToHtml(
          { path: absolutePath },
          {
            convertImage: mammoth.images.imgElement(async (image: any) => {
              const buffer = await image.read();
              const base64 = buffer.toString('base64');
              const contentType = image.contentType || 'image/png';
              return {
                src: `data:${contentType};base64,${base64}`,
              };
            }),
          }
        );
    
        let output = `# Conversion Result (with images)\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 and description for the tool, defining the 'filePath' parameter using Zod.
    {
      description: 'Convert a DOCX file to HTML with embedded images as base64 data URIs',
      inputSchema: {
        filePath: z.string().describe('Absolute path to the DOCX file to convert'),
      },
  • src/index.ts:64-122 (registration)
    The registration of the 'convert_docx_to_html_with_images' tool using server.registerTool, including schema and inline handler.
    server.registerTool(
      'convert_docx_to_html_with_images',
      {
        description: 'Convert a DOCX file to HTML with embedded images as base64 data URIs',
        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 },
            {
              convertImage: mammoth.images.imgElement(async (image: any) => {
                const buffer = await image.read();
                const base64 = buffer.toString('base64');
                const contentType = image.contentType || 'image/png';
                return {
                  src: `data:${contentType};base64,${base64}`,
                };
              }),
            }
          );
    
          let output = `# Conversion Result (with images)\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,
          };
        }
      }
    );

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