docx-mcp
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@docx-mcp extract fragments from contract.docx"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
docx-mcp
Legal document redlining engine. Takes AI-generated changes (structured JSON)
and applies them as professional tracked changes with comments inside .docx
files. The output is indistinguishable from what a lawyer would produce in
Microsoft Word -- proper w:ins/w:del markup, comment annotations with
justification text, and preserved formatting.
Installation
Requires Python 3.14+.
uv syncRelated MCP server: word-agent-mcp
Quick start
Python API
from docx_mcp import (
ParagraphChange, ParagraphChangeType,
TableChange, TableChangeType,
RedlineConfig, apply_redlines,
)
changes = [
# Modify a body paragraph
ParagraphChange(
kind="paragraph",
fragment_id="3", # ← str (was int in v0.1.0)
change_type=ParagraphChangeType.MODIFY,
new_text="The Company **shall** provide written notice.",
justification="Strengthened obligation language.",
),
# Delete a paragraph
ParagraphChange(
kind="paragraph",
fragment_id="5",
change_type=ParagraphChangeType.DELETE,
justification="Removed redundant clause.",
),
# Append a new paragraph
ParagraphChange(
kind="paragraph",
fragment_id="7",
change_type=ParagraphChangeType.APPEND_AFTER,
new_text="The foregoing shall survive termination.",
justification="Added survival provision.",
),
# Modify a header paragraph
ParagraphChange(
kind="paragraph",
fragment_id="header_1.1",
change_type=ParagraphChangeType.MODIFY,
new_text="CONFIDENTIAL",
justification="Updated header text.",
),
# Modify a table cell
TableChange(
kind="table",
table_id=2,
row=1,
col=1,
change_type=TableChangeType.MODIFY_CELL,
new_text="Updated **cell** content",
justification="Corrected table entry.",
),
# Clear a table cell
TableChange(
kind="table",
table_id=2,
row=3,
col=2,
change_type=TableChangeType.CLEAR_CELL,
justification="Removed obsolete data.",
),
]
doc = apply_redlines("contract.docx", changes)
doc.save("contract_redlined.docx")CLI
# Extract fragment text from a document
docx-mcp convert input.docx
docx-mcp convert input.docx --format json
# Apply changes
docx-mcp apply input.docx changes.json -o output.docx
# Validate a redlined document
docx-mcp validate output.docx
# Audit a document for structural issues
docx-mcp audit input.docx
docx-mcp audit input.docx --format jsonNote: The CLI
convertcommand extracts body content only (no headers, footers, or tables). For full-document extraction, use the MCPextract_fragmentstool or the Pythonfull_to_fragments()function.
MCP server
The library includes an MCP server so that LLM clients (Claude Desktop, Cursor,
etc.) can redline .docx files directly.
# Start the server (stdio transport)
docx-mcp-serverConfigure in Claude Desktop (claude_desktop_config.json):
{
"mcpServers": {
"docx-mcp": {
"command": "uv",
"args": ["run", "--directory", "/path/to/docx-mcp", "docx-mcp-server"]
}
}
}Configure in Cursor (.cursor/mcp.json):
{
"mcpServers": {
"docx-mcp": {
"command": "uv",
"args": ["run", "--directory", "/path/to/docx-mcp", "docx-mcp-server"]
}
}
}Tools
Tool | Description |
| Read a |
| Apply tracked changes from an inline list and save |
| Apply tracked changes from a JSON file on disk |
| Run structural validation checks |
| Compare two |
| Audit a |
Resource
URI | Description |
| Browse paragraph fragments (URL-encode the path) |
Example workflow
An LLM client would typically:
Call
extract_fragmentsto read the document and get fragment IDs.Reason about the content and construct a list of changes.
Call
apply_changeswith the change list to produce a redlined document.Optionally call
diff_fragmentsto compare original vs. redlined output.
Concepts
Fragments
Documents are decomposed into fragments: paragraphs, tables, headers, and footers, all indexed in document order. Each fragment has a string ID.
Fragment IDs:
Pattern | Meaning | Example |
| Body paragraphs / tables |
|
| Header part P, paragraph I |
|
| Footer part P, paragraph I |
|
Tables and body paragraphs share the same ID space (they interleave in document
order). Fragment "3" might be a table and fragment "4" a paragraph.
Use extract_fragments (MCP) or full_to_fragments() (Python) to see the
fragment map for any document:
<f=1>Introduction paragraph.</f=1>
<f=2>**Definitions.** The following terms shall apply.</f=2>
<table=3 rows=2 cols=3>
<cell=3.1.1 span="2">Merged Header</cell=3.1.1>
<cell=3.1.3>Header C</cell=3.1.3>
<cell=3.2.1>Data 1</cell=3.2.1>
<cell=3.2.2>Data 2</cell=3.2.2>
<cell=3.2.3>Data 3</cell=3.2.3>
</table=3>
<f=4>Closing paragraph. See [Section 2](https://example.com).</f=4>
<f=header_1.1>Confidential</f=header_1.1>
<f=footer_1.1>Page 1 of 10</f=footer_1.1>Tables
Simple tables
Simple (rectangular) tables are extracted as <table=N> blocks. Each cell has
a cell_id in "table_id.row.col" format (e.g., "3.1.2").
Merged-cell tables
Tables with horizontally or vertically merged cells (gridSpan / vMerge) are
now supported. Merge spans are shown as attributes:
span="2"— cell spans 2 columns (horizontal merge)vspan="3"— cell spans 3 rows (vertical merge)
Spanned-over cells (positions covered by a merge) are omitted from output.
For example, if cell=3.1.1 has span="2", then cell=3.1.2 does not appear.
When targeting merged cells with changes, always target the originating cell
(the one with the span/vspan attribute). Targeting a spanned-over position
raises a ValueError.
Skipped tables
Tables that cannot be processed (nested tables, malformed merges, tables inside headers/footers) appear as:
<table=5 skipped reason="table 5, cell 2.3 contains nested table"/>Headers and footers
Header and footer paragraphs are extracted with prefixed fragment IDs:
header_1.1, footer_2.1, etc. The first number is the 1-based part index
(usually 1 for the default header/footer), the second is the 1-based
paragraph index within that part.
Header/footer paragraphs can be modified, deleted, and appended to just like body paragraphs. Tables inside headers/footers are not editable and are reported as skipped elements.
Limitation: Comments on header/footer changes are not attached to the output (Word and LibreOffice do not support comment ranges in those parts). They trigger a
UserWarningand are dropped.
Hyperlinks
Hyperlinks are extracted as [link text](url) inline within paragraph text.
Formatting inside links is preserved: [**bold link**](url).
When modifying an existing paragraph, [text] without (url) preserves
the original hyperlink URL. [text](new_url) creates a new link.
When appending new text, [text](url) creates a hyperlink. [text] without
(url) produces plain text — always specify (url) on append if you want a
hyperlink.
Tracked changes policy
Documents with pre-existing tracked changes (<w:ins>, <w:del>,
<w:moveFrom>, <w:moveTo>) are hard-rejected in both extract_fragments
and apply_redlines. Accept or reject all changes in Word before processing.
collapse_empty mode
Optional mode that suppresses empty paragraphs from extraction and redlining. Produces cleaner output for LLM consumption. When enabled, it must be used consistently across extraction and redlining — mismatched values cause fragment ID misalignment.
Change types
Paragraph changes
Type | Description | Requires |
| Word-level diff applied as tracked changes | Yes |
| Entire paragraph marked as deleted | No |
| New paragraph inserted after the referenced fragment | Yes |
Table cell changes
Type | Description | Requires |
| Modify cell content (single or multi-paragraph) | Yes |
| Delete all content in a cell (preserves structure) | No |
Cell modification uses positional alignment: if the cell has multiple
paragraphs, the new text is split on newlines (\n) and each line is applied
to the corresponding paragraph in order. Cell content is marked with tracked
changes and comments just like paragraph modifications.
Blank line management
When appending new paragraphs, you can control surrounding blank lines:
Change(
fragment_id=10,
change_type=ChangeType.APPEND_AFTER,
new_text="New clause text here.",
justification="Added new provision.",
blank_lines_before=1, # Insert 1 blank line before the new paragraph
blank_lines_after=1, # Insert 1 blank line after the new paragraph
)When deleting paragraphs, you can remove trailing blank lines automatically:
Change(
fragment_id=15,
change_type=ChangeType.DELETE,
justification="Removed obsolete clause.",
delete_next_blanks=1, # Also delete the next blank paragraph
)All blank lines are marked as tracked insertions/deletions and will appear in the redlined document.
Pseudo-Markdown
Text content uses a simplified Markdown-like format for inline formatting:
**bold**_italic___underline__
Unicode characters (smart quotes, em dashes, section symbols, non-breaking spaces) are preserved as-is.
Font inheritance: When appending new paragraphs, the font family, size, and color are automatically copied from the reference paragraph's first text-bearing run. Bold, italic, and underline formatting from the pseudo-Markdown is layered on top of the inherited base formatting.
Changes JSON
The CLI accepts a JSON file containing either a bare array or a
{"changes": [...]} wrapper.
Paragraph changes example
[
{
"fragment_id": "1",
"change_type": "modify",
"new_text": "The Seller agrees to deliver within **sixty** days.",
"justification": "Extended delivery window."
},
{
"fragment_id": "3",
"change_type": "delete",
"justification": "Removed governing law clause.",
"delete_next_blanks": 1
},
{
"fragment_id": "5",
"change_type": "append_after",
"new_text": "This Agreement shall be governed by Delaware law.",
"justification": "Added Delaware governing law.",
"blank_lines_before": 1,
"blank_lines_after": 0
},
{
"fragment_id": "header_1.1",
"change_type": "modify",
"new_text": "CONFIDENTIAL",
"justification": "Updated header marking."
}
]Table cell changes example
[
{
"cell_id": "2.1.1",
"change_type": "modify_cell",
"new_text": "Updated **cell** content",
"justification": "Corrected cell value."
},
{
"cell_id": "2.3.2",
"change_type": "clear_cell",
"justification": "Cleared obsolete data."
}
]Cell IDs use the format "table_id.row.col" where rows and columns are 1-based.
Validation
The validate_document() function (and docx-mcp validate CLI) checks:
Annotation ID isolation -- tracked-change and comment IDs don't collide across groups
Comment integrity -- every
<w:comment>has matching range markers in the document body, and vice versaTracked-change attributes -- every
<w:ins>and<w:del>has requiredw:id,w:author, andw:datePackage consistency -- content-type and relationship entries exist for
comments.xml
from docx_mcp import validate_document
result = validate_document(doc)
if not result.ok:
for error in result.errors:
print(error)Architecture
The library manipulates OOXML directly via lxml (not python-docx) because
python-docx has no tracked-change support. Key design decisions:
Word-level diffing via
diff-match-patchwith a word-to-char mapping for high-quality diffsConservative mutation -- only changed paragraphs are touched; everything else passes through byte-identical
Globally unique annotation IDs via a monotonic
IdManagerseeded from the document's existing max IDpython-docxis used only for test fixture generation, not in the library itself
Module map
src/docx_mcp/
__init__.py Public API
cli.py CLI entry point (apply, convert, validate)
models.py Pydantic data models (Change, ChangeType, RedlineConfig, ...)
document.py DocxDocument: ZIP parsing, XML tree access, serialization
converter.py Paragraph & table XML -> pseudo-Markdown conversion
table_utils.py Table inspection utilities (cell access, simplicity checks)
tokenizer.py Word-level tokenization
differ.py Word-level diff engine (diff-match-patch wrapper)
run_ops.py Diff-to-XML-run mapping, run splitting, element building
id_manager.py Monotonic annotation ID allocator
comments.py Comment creation and range marker insertion
redliner.py Main orchestrator: apply_redlines()
table_redliner.py Table cell change application
audit.py Document structural audit (headers, images, tables, etc.)
validator.py Structural validation checks
server.py MCP server (FastMCP 3.x, stdio transport)
handlers/
modify.py Word-level tracked changes on existing paragraphs
delete.py Full paragraph deletion markup
append.py New paragraph insertion markupDevelopment
# Run tests
uv run pytest tests/ -v
# Lint
uvx ruff check src/ tests/
# Auto-fix lint issues
uvx ruff check src/ tests/ --fix
# Type check
uvx ty check src/ tests/431 tests covering all modules, handlers, table operations, headers/footers, hyperlinks, tracked-change rejection, merged-cell tables, section breaks, CLI, validation, and MCP server.
License
MIT
Available Tools
6 toolsapply_changesAIdempotent
Apply tracked changes to a .docx file and save a new redlined document.
Produces Word-compatible tracked changes (``w:ins`` / ``w:del``) with comments.
Always call ``extract_fragments`` first to get fragment IDs.
Paragraph changes (use ``fragment_id``):
- ``modify``: Word-level diff. ``new_text`` required.
- ``delete``: Marks paragraph deleted. Omit ``new_text``.
- ``append_after``: Insert after target. ``new_text`` required.
Table changes (use ``cell_id`` like ``"2.1.3"``):
- ``modify_cell``: Replace cell text. ``new_text`` required. Use ``
for multi-paragraph cells. -clear_cell: Delete cell content. Omit new_text``.
Rules:
- ``delete_next_blanks`` only with ``delete``.
- ``blank_lines_before`` / ``blank_lines_after`` only with ``append_after``.
- Header/footer changes work but comments are silently dropped.
- Documents with pre-existing tracked changes are hard-rejected.
``new_text`` uses pseudo-Markdown: ``**bold**``, ``_italic_``, ``__underline__``.
Example::
changes = [
{
"fragment_id": 1,
"change_type": "modify",
"new_text": "**Updated title**",
"justification": "Fixed typo"
},
{
"cell_id": "2.1.1",
"change_type": "modify_cell",
"new_text": "New header",
"justification": "Clarified"
}
]
Args:
document_path: Absolute path to the input .docx file.
changes: List of change objects (paragraph or table cell changes).
output_path: Where to save the redlined document. Defaults to
``<stem>_redlined.docx`` beside the input file.
author: Author name for tracked changes and comments. Defaults to
"AI Review".
Returns:
Summary string with change counts, output path, and validation result.
Inputs: changes (required), document_path (required), author, output_path. Returns: result
apply_changes_from_fileAIdempotent
Same as apply_changes but reads the change list from a JSON file.
Useful for large change sets that would exceed token limits in a direct tool call, or for reusing a change set across multiple runs.
The JSON file must contain either a bare array of change objects, or an
object with a "changes" key::
[
{"fragment_id": 1, "change_type": "modify", "new_text": "..."},
{"cell_id": "2.1.1", "change_type": "modify_cell", "new_text": "..."}
]See apply_changes for change object schema and rules.
Inputs: changes_file (required), document_path (required), author, output_path. Returns: result
audit_document_toolARead-onlyIdempotent
Audit a .docx file for structural issues and skipped content.
Reports headers, footers, images, tables, section breaks, tracked changes, comments, and unsupported elements (footnotes, endnotes, text boxes).
Inputs: document_path (required), format. Returns: result
diff_fragmentsARead-onlyIdempotent
Compare two .docx files and show paragraph and table-level text differences.
Extracts the pseudo-Markdown text from each paragraph and table cell in both documents, then produces a word-level diff for each fragment position. This is useful for understanding what changed between two versions of a document.
Use this tool:
To compare an original document with its redlined version (see what changes were applied)
To verify that
apply_changesproduced the expected modificationsTo understand differences between two versions of a document
Important Limitations
Fragments are matched by position (fragment 1 vs fragment 1, fragment 2 vs fragment 2, etc.). This tool does not detect fragment reordering or track moved sections. If the documents have very different structures (different fragment counts, major reordering), the output will show extensive changes.
Best used for comparing documents with the same basic structure where you made local edits (word changes, clause deletions, appended sections, table cell modifications).
Output Format
Each fragment is reported with its change status.
Paragraphs::
Fragment 1: unchanged
Fragment 2: modified
- shall deliver
+ must deliver immediately
Fragment 3: unchanged
Fragment 5: deleted (only in original)
- This clause is removed.
Fragment 10: added (only in modified)
+ This is a new clause.Tables::
Table 56: modified
Cell 56.2.2: modified
- Gwendolyn Mahon, M.Sc., Ph.D
+ John H. Smith, Ph.D.
Table 57: unchanged
Table 58: dimensions changed (3x2 → 4x2)Lines starting with - show deleted text, + shows inserted text.
Unchanged fragments are listed but their text is omitted for brevity.
For tables, each modified cell is shown with its cell ID (table_id.row.col) followed by the word-level diff of the cell content.
Difference from extract_fragments
extract_fragmentsshows the plain text of a single document (paragraphs, headers, footers, tables). Pre-existing tracked changes cause a hard rejection.diff_fragmentscompares two separate documents and computes the differences between their plain text (ignoring any tracked changes)
Inputs: modified_path (required), original_path (required). Returns: result
extract_fragmentsARead-onlyIdempotent
Extract text from a .docx file as tagged fragments.
Call this first to get fragment IDs, then use those IDs in ``apply_changes``.
Output format::
<f=1>**Title**</f=1>
<f=2>Body paragraph.</f=2>
<table=3 rows=2 cols=3>
<cell=3.1.1>Header</cell=3.1.1>
<cell=3.1.2 span="2">Merged cell</cell=3.1.2>
</table=3>
Fragment IDs:
- Body: ``"1"``, ``"2"``, ...
- Headers: ``"header_1.1"``, ``"header_1.2"``, ...
- Footers: ``"footer_1.1"``, ``"footer_2.1"``, ...
- Table cells: ``"table_id.row.col"`` (e.g., ``"3.1.2"``)
Formatting: ``**bold**``, ``_italic_``, ``__underline__``. ``
`` for paragraph breaks.
Limitations:
- Images, nested tables, VML text boxes are skipped.
- Pre-existing tracked changes are hard-rejected.
Args:
document_path: Absolute path to the .docx file.
Returns:
Tagged text string with fragment and table markup.
Inputs: document_path (required). Returns: result
validate_document_toolARead-onlyIdempotent
Check a .docx file for structural issues.
Runs validation checks on the document's OOXML structure to ensure it will open correctly in Microsoft Word and that all tracked changes and comments are properly formed.
Use this tool:
After calling
apply_changesto verify the redlined document is valid (automatically enabled by default viavalidate=Trueparameter)When debugging a document that won't open correctly in Word
When verifying that an existing redlined document has proper structure
Validation Checks
Annotation ID isolation: Tracked-change and comment IDs must not collide across groups. Each
<w:ins>,<w:del>, and<w:comment>needs a globally unique ID within the document.Comment integrity: Every
<w:comment>in comments.xml must have matching<w:commentRangeStart>/<w:commentRangeEnd>markers in the document body, and vice versa.Tracked-change attributes: Every
<w:ins>and<w:del>must have required attributes:w:id(unique ID),w:author(author name), andw:date(timestamp).Package consistency: Content-type and relationship entries must be present in the .docx ZIP structure when comments.xml exists.
Example Output
Success case::
"Validation: passed (0 errors, 0 warnings)."Failure case::
"Validation: FAILED (2 error(s), 1 warning(s)).
Error 1: Annotation ID collision: ID 5 used by both tracked change and comment
Error 2: Orphaned comment range: commentRangeStart with id=3 has no matching end
Warning 1: Comment with id=7 is not referenced by any comment range"Inputs: document_path (required). Returns: result
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 6 tools
Most tools have clear, distinct purposes: extract, apply changes, validate, audit, and diff. However, validate_document_tool and audit_document_tool both inspect document structure and could be confused, and apply_changes_from_file is a redundant variant of apply_changes. Overall, the descriptions help clarify boundaries.
Tool names follow a consistent verb_noun pattern with lowercase underscores (extract_fragments, apply_changes, diff_fragments). The '_tool' suffix on validate_document_tool and audit_document_tool is a minor inconsistency, and apply_changes_from_file is long but clear. The naming is generally predictable.
With 6 tools, the server is well-scoped for the domain of .docx extraction, tracked changes, validation, and comparison. Each tool contributes to a focused workflow, and the count is within the ideal 3-15 range.
The tool surface covers the core lifecycle: extraction, applying changes, validation, auditing, and diffing. Minor gaps exist, such as no direct tool for creating documents or accepting/rejecting changes, and the extraction tool skips some elements. However, the primary redlining workflow is well-supported.
Maintenance
Related MCP Connectors
Contract review that keeps your contracts: cited answers, Word redlines, key-date alerts.
Run deterministic SaaS contract redlining, GDPR Art. 28 DPA & EU AI Act compliance audits.
Run deterministic SaaS contract redlining, GDPR Art. 28 DPA & EU AI Act compliance audits.
Convert, create and edit Word, PowerPoint, Excel and PDF documents from any AI agent.
Related MCP Servers
- AlicenseAqualityAmaintenanceA "Virtual DOM" for Microsoft Word enabling AI to safely read, redline, and sanitize DOCX contracts without breaking formatting.11173MIT
- FlicenseAqualityDmaintenanceEnables AI agents to edit Word documents using track changes, allowing lawyers to review each modification via Word's revision interface.5-

BitsBound MCP Serverofficial
AlicenseAqualityDmaintenanceEnables AI-powered contract analysis with partner-level redlines and real OOXML Track Changes for Claude Desktop and Claude.ai.12MIT- AlicenseAqualityBmaintenanceEnables full-featured editing of Word .docx files through natural language, including merge-aware table operations, bulk cell edits, footnotes/endnotes, TOC management, tracked changes, and document comparison.1017AGPL 3.0