word-engine-mcp
A local MCP server that uses Word COM automation to perform tasks python-docx cannot, such as computing field/TOC values, counting real pages, rendering high-fidelity PDFs, converting/repairing legacy formats, producing redline comparisons, and running native mail merge.
README
word-engine-mcp
A local MCP server that drives the real Word desktop application (COM automation) to do the things python-docx fundamentally cannot: compute field/TOC values, count real pages, render true-fidelity PDF, convert/repair legacy formats, produce redline comparisons, and run native mail merge.
Design philosophy: this server complements library workflows instead of replacing them. Editing text, tables and styles is faster with python-docx — but a document built with python-docx has
?where TOC page numbers should be, and no library has a concept of "pages" (that requires a layout engine). This MCP handles only the engine-exclusive part, keeping its tool surface tiny (8 tools).
Requirements
- Windows 10+ with a logged-in interactive desktop session (Word has no true headless mode)
- Microsoft Office (Word) installed and licensed — verified on Office 2016+ (Word 16.0)
- Python 3.10+ — verified on 3.12
- Claude Code or any MCP client
Install
git clone https://github.com/Feynman520/d01-p04-word-engine-mcp.git
cd d01-p04-word-engine-mcp
py -3.12 -m venv .venv # or: python -m venv .venv
.\.venv\Scripts\python.exe -m pip install -r requirements.txt
Register with Claude Code
Run this in the cloned folder (uses absolute paths, so it works from anywhere afterwards):
claude mcp add word-automation --scope user -- "$PWD\.venv\Scripts\python.exe" "$PWD\server.py"
--scope user makes it available in every project. Use --scope project to limit it to one project.
Verify
$py = ".\.venv\Scripts\python.exe"; $env:PYTHONUTF8 = "1"
& $py tests\smoke_com.py # field update / page stats / PDF / convert / compare / mail merge
& $py tests\server_tools.py # MCP tool registration (does not launch Word)
Tools (6 core + 2 diagnostics)
| # | Tool | Input → Output | Why engine-only |
|---|---|---|---|
| ① | word_update_fields |
src_path, out_path → {out_path,fields_updated} |
Computes displayed values of TOC, cross-references, PAGE/SEQ, index (python-docx stores field codes only — shown as ?) |
| ② | word_read_layout |
path, update_fields?, include_readability? → {pages,words,lines,characters,...} |
Real page count and layout statistics (libraries have no page concept) |
| ③ | word_export_pdf |
src_path, out_path, update_fields?, pdf_a?, from_page?, to_page?, create_bookmarks? → {out_path} |
Word render engine PDF (PDF/A, heading bookmarks, page ranges) |
| ④ | word_convert |
src_path, out_path, repair? → {out_path,format,repaired} |
.doc/.rtf/.odt/.html/.txt ↔ .docx conversion and corrupt-file repair (python-docx is .docx-only) |
| ⑤ | word_compare |
original_path, revised_path, out_path, author?, granularity? → {out_path,revisions} |
Native CompareDocuments redline (no library equivalent) |
| ⑥ | word_mail_merge |
template_path, data_path, out_path → {out_path,records} |
Native mail merge (per-record documents) |
| — | word_health |
→ {alive, word_version} |
Session check (launches Word on first call) |
| — | word_restart |
→ {alive} |
Recovery from COM errors |
Typical flow: build a .docx with python-docx → word_update_fields to bake TOC/numbers →
word_read_layout to check the real page count → word_export_pdf for the final PDF.
Originals are never modified; results are always written to out_path.
Mail merge data source
The most reliable source is a .docx containing a table with one header row + data rows
(no database driver involved, fully unattended). .csv/.xlsx are also accepted, but some
environments show Word's SQL confirmation prompt (depends on an HKCU setting).
Architecture notes
- Single STA worker thread (
engine/session.py): every Word call is serialized onto one dedicated thread (win32com COM objects are thread-bound; FastMCP may hop threads). - Lazy session: Word starts on the first tool call, is reused, and closes with the server.
DispatchEx+ early binding (gencache.EnsureDispatch): a dedicated instance, and argument-heavy methods likeExportAsFixedFormat/CompareDocumentsare marshalled correctly via the type library. The one-time makepy output is redirected away from stdout (the JSON-RPC channel).Visible=False, macros blocked on open viaAutomationSecurity=ForceDisable.- Originals preserved: inputs open read-only, results are written to new paths, then
Close(SaveChanges=False). - Zombie prevention: Word gives no stable window handle, so the dedicated instance PID is
identified by diffing the
WINWORD.EXEprocess list before/afterDispatchEx, and force-killed at shutdown if it survivesQuit(). - RPC-rejection retry:
RPC_E_CALL_REJECTEDright after startup is retried with backoff.
Limitations
- Not suitable for unattended/service sessions (needs an interactive desktop).
- Text/table/style editing is faster with python-docx — that is by design.
License
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。