A comprehensive Model Context Protocol (MCP) server for processing Microsoft Word (.docx) documents with full formatting support.
This MCP server provides advanced DOCX document processing capabilities using the powerful mammoth library:
- Text Extraction: Extract plain text with word count
- HTML Conversion: Convert to HTML with preserved formatting
- Structure Analysis: Analyze document structure, headings, and formatting elements
- Image Extraction: Extract embedded images (as base64 or save to files)
- Markdown Conversion: Convert to Markdown format
- Rich Formatting Support: Handles bold, italic, lists, headings, and more
Extract plain text content from a DOCX file.
Parameters:
file_path(string): Path to the .docx file
Returns:
- Plain text content
- Processing messages
- Word count
Convert DOCX file to HTML with formatting preserved.
Parameters:
file_path(string): Path to the .docx fileinclude_styles(boolean, optional): Include inline styles (default: true)
Returns:
- HTML content with formatting
- Processing messages
- Warnings and errors
Analyze document structure, headings, and formatting elements.
Parameters:
file_path(string): Path to the .docx file
Returns:
- Document statistics (characters, words, paragraphs, headings)
- Structure analysis (headings with levels)
- Formatting analysis (bold, italic, lists count)
- Processing messages
Extract and list images from a DOCX file.
Parameters:
file_path(string): Path to the .docx fileoutput_dir(string, optional): Directory to save extracted images
Returns:
- Total image count
- Image details (src, alt text, base64 status)
- Output directory information
- Processing messages
Convert DOCX file to Markdown format.
Parameters:
file_path(string): Path to the .docx file
Returns:
- Markdown content
- Word count
- Processing messages
In addition to MCP stdio mode, this server can run as a standalone HTTP server for direct API access.
Set the USE_HTTP environment variable to true:
USE_HTTP=true npm start
# or
USE_HTTP=true node build/index.jsThe server will start on port 3000 (configurable via PORT environment variable).
Note: Uploaded files are temporarily stored in the OS temp directory (e.g., /tmp on Linux/Mac, %TEMP% on Windows) with namespace docx-mcp-temp. Files are kept for the duration of the session and automatically cleaned up when the session closes.
The HTTP server provides MCP-compatible endpoints:
POST /mcp- MCP JSON-RPC endpoint (compatible with MCP specifications)POST /upload- File upload endpoint for convenience
The /mcp endpoint accepts standard MCP JSON-RPC messages. Use this for programmatic access compatible with MCP clients.
Example MCP request:
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "extract_text",
"arguments": {
"file_path": "/path/to/document.docx"
}
}
}For web applications, use the two-step process:
- Upload file:
POST /uploadwith multipart form-data - Call MCP tool: Use the returned
file_pathwith the/mcpendpoint
Example:
# Step 1: Upload file
curl -X POST -F "file=@document.docx" http://localhost:3000/upload
# Returns: {"file_path": "/path/to/temp/file", "original_name": "document.docx"}
# Step 2: Call MCP tool
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "extract_text",
"arguments": {
"file_path": "/path/to/temp/file"
}
}
}'npm install
npm run buildThe server runs on stdio and communicates via JSON-RPC 2.0 protocol.
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "analyze_structure",
"arguments": {
"file_path": "/path/to/document.docx"
}
}
}{
"file_path": "/path/to/document.docx"
}- ✅ Text Extraction: Plain text with word counting
- ✅ Rich Formatting: Bold, italic, underline, strikethrough
- ✅ Document Structure: Headings (H1-H6), paragraphs
- ✅ Lists: Ordered and unordered lists with items
- ✅ Images: Extraction as base64 or file export
- ✅ Tables: Basic table structure (via HTML conversion)
- ✅ Links: Hyperlinks preservation
- ✅ Styles: Custom style mapping support
- ✅ Error Handling: Comprehensive error reporting
- ✅ Multiple Formats: HTML, Markdown, plain text output
The convert_to_html tool supports custom style mapping for better semantic HTML output:
// Example style mappings
"p[style-name='Heading 1'] => h1:fresh"
"r[style-name='Strong'] => strong"
"r[style-name='Emphasis'] => em"- Base64 Embedding: Images can be embedded as base64 data URLs
- File Export: Images can be extracted to a specified directory
- Metadata: Alt text and content type preservation
Provides comprehensive document analysis including:
- Character and word counts
- Paragraph and heading counts
- Formatting element statistics
- Document structure hierarchy
Install dependencies:
npm installBuild the server:
npm run buildFor development with auto-rebuild:
npm run watchTo use with Claude Desktop, add the server config:
On MacOS: ~/Library/Application Support/Claude/claude_desktop_config.json
On Windows: %APPDATA%/Claude/claude_desktop_config.json
{
"mcpServers": {
"docx-format-server": {
"command": "/path/to/docx-format-server/build/index.js"
}
}
}@modelcontextprotocol/sdk: MCP protocol implementationmammoth: Advanced DOCX processing libraryzod: Schema validationtypescript: TypeScript support
All tools include comprehensive error handling with detailed error messages for:
- File not found errors
- Invalid file format
- Processing errors
- Permission issues
Since MCP servers communicate over stdio, debugging can be challenging. We recommend using the MCP Inspector, which is available as a package script:
npm run inspectorThe Inspector will provide a URL to access debugging tools in your browser.
- v0.3.0: Added HTTP server mode with REST API endpoints alongside MCP stdio mode
- v0.2.0: Complete rewrite with mammoth library, added 5 comprehensive tools
- v0.1.0: Basic text extraction with docx-parser (deprecated)
ISC License