🚀 HWPX MCP 服務器 - 增強版
這是一個可與 AI 工具(如 Claude 等)集成,實現自動編輯韓文字處理軟件(HWPX)文檔的 MCP(模型上下文協議)服務器。它基於原項目進行了復刻,並大幅改進了穩定性和功能。
🚀 快速開始
本項目支持所有操作系統!
| 操作系統 |
MCP 服務器 |
HWPX 編輯 |
結果查看 |
| ✅ Windows |
✅ |
✅ |
漢松辦公軟件 |
| ✅ macOS |
✅ |
✅ |
漢松辦公軟件 Mac 版 |
| ✅ Linux |
✅ |
✅ |
漢松辦公軟件 Linux 版 / LibreOffice* |
為什麼能實現跨平臺?
HWPX 文件採用 ZIP + XML 結構,僅使用 Node.js 就能完美讀寫,無需安裝韓文字處理軟件。
*LibreOffice 對 HWPX 的支持有限,為確保完全兼容,建議使用漢松辦公軟件。
✨ 主要特性
本項目對比原項目,解決了以下核心問題:
🔧 關鍵 bug 修復
| 問題 |
原項目狀態 |
改進後 |
| 表格保存失敗 |
單元格修改後保存,更改內容消失 |
✅ 完美保存 |
| 文本重疊現象 |
保存後在韓文字處理軟件中打開,文字重疊顯示 |
✅ 正常顯示 |
| 文件損壞 |
保存時偶爾文件損壞 |
✅ 通過原子寫入確保 100% 安全 |
| 多單元格損壞 |
同一行中多個單元格修改時,XML 損壞 |
✅ 通過索引管理確保安全 |
| 字間距/行間距丟失 |
保存後樣式信息丟失 |
✅ 保留所有樣式 |
🛠 技術改進
- 原子文件寫入
- 臨時文件 → ZIP 驗證 → 原子移動
- 即使保存過程中出現錯誤,也能保護原始文件
- 智能行佈局重置
- 文本修改時自動重置
lineseg
- 韓文字處理軟件打開時自動重新計算換行
- 徹底解決文本重疊現象
- 基於深度的 XML 解析
- 解決了現有惰性正則表達式對嵌套結構的誤識別問題
- 完美支持複雜表格(嵌套表格、子列表等)
- 完整樣式保留
- 100% 保留
charPr、spacing 等原始樣式
- 去除不完整的序列化邏輯,確保數據完整性
- 安全的多單元格更新
- 即使同時修改同一行的多個單元格也很安全
- 通過按行分組和逆序處理,防止索引損壞
📦 安裝指南
1. 安裝 MCP 服務器
git clone https://github.com/Dayoooun/hwpx-mcp.git
cd hwpx-mcp/mcp-server
npm install
npm run build
2. 配置 MCP 客戶端
你可以選擇以下客戶端進行配置:
🖥️ Claude Desktop
配置文件位置:
| 操作系統 |
路徑 |
| Windows |
%APPDATA%\Claude\claude_desktop_config.json |
| macOS |
~/Library/Application Support/Claude/claude_desktop_config.json |
| Linux |
~/.config/Claude/claude_desktop_config.json |
配置內容:
{
"mcpServers": {
"hwpx-mcp": {
"command": "node",
"args": ["C:/path/to/hwpx-mcp/mcp-server/dist/index.js"]
}
}
}
⚠️ 在 Windows 系統中,路徑需使用 \\ 或 /(例如:C:/Users/username/hwpx-mcp/...)
💻 Claude Code (CLI)
方法 1:按項目配置(在項目根目錄創建 .mcp.json 文件)
{
"mcpServers": {
"hwpx-mcp": {
"command": "node",
"args": ["/path/to/hwpx-mcp/mcp-server/dist/index.js"]
}
}
}
方法 2:全局配置(~/.claude/settings.json)
{
"mcpServers": {
"hwpx-mcp": {
"command": "node",
"args": ["/path/to/hwpx-mcp/mcp-server/dist/index.js"]
}
}
}
方法 3:通過 CLI 命令添加
claude mcp add hwpx-mcp node /path/to/hwpx-mcp/mcp-server/dist/index.js
🔷 Cursor
配置文件:~/.cursor/mcp.json(若不存在則創建)
{
"mcpServers": {
"hwpx-mcp": {
"command": "node",
"args": ["/path/to/hwpx-mcp/mcp-server/dist/index.js"]
}
}
}
或者你也可以在 Cursor Settings > MCP 中直接添加。
🆚 VS Code(使用 MCP 擴展時)
配置文件:.vscode/mcp.json(項目文件夾內)
{
"servers": {
"hwpx-mcp": {
"command": "node",
"args": ["${workspaceFolder}/../hwpx-mcp/mcp-server/dist/index.js"]
}
}
}
可以使用 ${workspaceFolder} 變量指定相對路徑。
🛠️ 其他 MCP 客戶端
通用的 MCP 配置格式如下:
{
"mcpServers": {
"hwpx-mcp": {
"command": "node",
"args": ["/absolute/path/to/hwpx-mcp/mcp-server/dist/index.js"]
}
}
}
路徑示例:
| 操作系統 |
路徑示例 |
| Windows |
C:/Users/username/hwpx-mcp/mcp-server/dist/index.js |
| macOS |
/Users/username/hwpx-mcp/mcp-server/dist/index.js |
| Linux |
/home/username/hwpx-mcp/mcp-server/dist/index.js |
3. 安裝驗證
重啟客戶端後,若 MCP 工具列表中顯示 hwpx-mcp 服務器和 77 個工具,則安裝成功!
💻 使用示例
基礎用法
await mcp.open_document({ file_path: "report.hwpx" })
await mcp.update_table_cell({
doc_id: "...",
section_index: 0,
table_index: 0,
row: 0,
col: 1,
text: "修改後的內容"
})
await mcp.insert_nested_table({
doc_id: "...",
section_index: 0,
parent_table_index: 0,
row: 1,
col: 2,
nested_rows: 2,
nested_cols: 2,
data: [["A1", "A2"], ["B1", "B2"]]
})
await mcp.render_mermaid({
doc_id: "...",
mermaid_code: "graph TD; A-->B; B-->C;",
after_index: 0,
theme: "default"
})
await mcp.save_document({ doc_id: "..." })
高級用法
表格單元格搜索/替換工作流程
當同一文本出現在多個位置時,可按以下方法僅修改 特定位置:
const results = await mcp.search_text({
doc_id: "...",
query: "待修改內容",
include_tables: true
})
await mcp.replace_text_in_cell({
doc_id: "...",
section_index: 0,
table_index: 2,
row: 3,
col: 1,
old_text: "待修改內容",
new_text: "新內容"
})
📚 詳細文檔
🔌 MCP 工具(77 個)
📁 文檔管理 - 5 個
| 工具 |
說明 |
主要參數 |
create_document |
創建新的空白 HWPX 文檔 |
title?, creator? |
open_document |
打開 HWPX 文檔 |
file_path |
close_document |
關閉打開的文檔 |
doc_id |
save_document |
保存文檔(支持備份/完整性驗證) |
doc_id, output_path?, create_backup?, verify_integrity? |
list_open_documents |
查看當前打開的文檔列表 |
- |
📄 文檔信息 - 5 個
| 工具 |
說明 |
主要參數 |
get_document_text |
提取文檔的全部文本 |
doc_id |
get_document_structure |
查看文檔結構(章節/段落/表格/圖像數量) |
doc_id |
get_document_metadata |
查看元數據(標題、作者、日期等) |
doc_id |
set_document_metadata |
修改元數據 |
doc_id, title?, creator?, subject?, description? |
get_word_count |
統計字符數/單詞數 |
doc_id |
📝 段落 - 8 個
| 工具 |
說明 |
主要參數 |
get_paragraphs |
查看段落列表(包含文本/樣式) |
doc_id, section_index? |
get_paragraph |
獲取特定段落的詳細信息 |
doc_id, section_index, paragraph_index |
insert_paragraph |
插入新段落 |
doc_id, section_index, after_index, text |
delete_paragraph |
刪除段落 |
doc_id, section_index, paragraph_index |
update_paragraph_text |
修改段落文本內容 |
doc_id, section_index, paragraph_index, text, run_index? |
append_text_to_paragraph |
在現有段落中添加文本 |
doc_id, section_index, paragraph_index, text |
copy_paragraph |
將段落複製到其他位置 |
doc_id, source_section, source_paragraph, target_section, target_after |
move_paragraph |
將段落移動到其他位置 |
doc_id, source_section, source_paragraph, target_section, target_after |
🎨 文本樣式 - 4 個
| 工具 |
說明 |
主要參數 |
get_text_style |
查看字體格式(字體/大小/顏色等) |
doc_id, section_index, paragraph_index, run_index? |
set_text_style |
設置字體格式 |
doc_id, section_index, paragraph_index, bold?, italic?, underline?, strikethrough?, font_name?, font_size?, font_color?, background_color? |
get_paragraph_style |
查看段落格式(對齊方式/行間距/邊距等) |
doc_id, section_index, paragraph_index |
set_paragraph_style |
設置段落格式 |
doc_id, section_index, paragraph_index, align?, line_spacing?, margin_*?, first_line_indent? |
🔍 搜索/替換 - 4 個
| 工具 |
說明 |
主要參數 |
search_text |
在文檔中搜索文本(支持正則表達式,包含表格單元格) |
doc_id, query, case_sensitive?, regex?, include_tables? |
replace_text |
查找並替換文本 |
doc_id, old_text, new_text, case_sensitive?, regex?, replace_all? |
replace_text_in_cell |
替換特定表格單元格內的文本 |
doc_id, section_index, table_index, row, col, old_text, new_text |
batch_replace |
批量替換多個文本 |
doc_id, replacements[](old_text, new_text 對數組) |
📊 表格 - 12 個
| 工具 |
說明 |
主要參數 |
get_tables |
獲取文檔中所有表格的列表 |
doc_id |
get_table |
查看特定表格的全部數據 |
doc_id, section_index, table_index |
get_table_cell |
查看特定單元格的內容 |
doc_id, section_index, table_index, row, col |
update_table_cell |
修改單元格內容(保留樣式) |
doc_id, section_index, table_index, row, col, text, char_shape_id? |
set_cell_properties |
設置單元格屬性(大小/背景顏色/對齊方式) |
doc_id, section_index, table_index, row, col, width?, height?, background_color?, vertical_align? |
insert_table |
插入新表格 |
doc_id, section_index, after_index, rows, cols, width? |
insert_table_row |
在表格中插入行 |
doc_id, section_index, table_index, after_row, cell_texts? |
delete_table_row |
刪除表格中的行 |
doc_id, section_index, table_index, row_index |
insert_table_column |
在表格中插入列 |
doc_id, section_index, table_index, after_col |
delete_table_column |
刪除表格中的列 |
doc_id, section_index, table_index, col_index |
insert_nested_table |
在單元格中插入嵌套表格(表中表) |
doc_id, section_index, parent_table_index, row, col, nested_rows, nested_cols, data? |
get_table_as_csv |
將表格導出為 CSV 格式 |
doc_id, section_index, table_index, delimiter? |
📐 頁面設置 - 2 個
| 工具 |
說明 |
主要參數 |
get_page_settings |
查看頁面設置(紙張大小/邊距) |
doc_id, section_index? |
set_page_settings |
修改頁面設置 |
doc_id, section_index?, width?, height?, margin_*?, orientation? |
🖼️ 圖像 - 5 個
| 工具 |
說明 |
主要參數 |
get_images |
獲取文檔中所有圖像的列表 |
doc_id |
insert_image |
插入圖像文件(自動註冊 BinData) |
doc_id, section_index, after_index, image_path, width?, height? |
update_image_size |
修改現有圖像的大小 |
doc_id, section_index, image_index, width, height |
delete_image |
刪除圖像 |
doc_id, section_index, image_index |
render_mermaid |
將 Mermaid 圖表作為圖像插入 |
doc_id, mermaid_code, after_index, section_index?, width?, height?, theme?, background_color? |
✏️ 圖形 - 3 個
| 工具 |
說明 |
主要參數 |
insert_line |
插入線條圖形 |
doc_id, section_index, after_index, x1, y1, x2, y2, stroke_color?, stroke_width? |
insert_rect |
插入矩形圖形 |
doc_id, section_index, after_index, x, y, width, height, fill_color?, stroke_color? |
insert_ellipse |
插入橢圓圖形 |
doc_id, section_index, after_index, cx, cy, rx, ry, fill_color?, stroke_color? |
📑 頁眉/頁腳 - 4 個
| 工具 |
說明 |
主要參數 |
get_header |
查看頁眉內容 |
doc_id, section_index? |
set_header |
設置頁眉 |
doc_id, text, section_index?, apply_page_type? (both/even/odd) |
get_footer |
查看頁腳內容 |
doc_id, section_index? |
set_footer |
設置頁腳 |
doc_id, text, section_index?, apply_page_type? (both/even/odd) |
📌 腳註/尾註 - 4 個
| 工具 |
說明 |
主要參數 |
get_footnotes |
獲取文檔中所有腳註的列表 |
doc_id |
insert_footnote |
在特定位置插入腳註 |
doc_id, section_index, paragraph_index, text |
get_endnotes |
獲取文檔中所有尾註的列表 |
doc_id |
insert_endnote |
在特定位置插入尾註 |
doc_id, section_index, paragraph_index, text |
🔗 書籤/超鏈接 - 4 個
| 工具 |
說明 |
主要參數 |
get_bookmarks |
獲取文檔中所有書籤的列表 |
doc_id |
insert_bookmark |
在特定位置插入書籤 |
doc_id, section_index, paragraph_index, name |
get_hyperlinks |
獲取文檔中所有超鏈接的列表 |
doc_id |
insert_hyperlink |
插入超鏈接 |
doc_id, section_index, paragraph_index, url, text |
➗ 公式 - 2 個
| 工具 |
說明 |
主要參數 |
get_equations |
獲取文檔中所有公式的列表 |
doc_id |
insert_equation |
插入公式(HWP 公式腳本格式) |
doc_id, section_index, after_index, script |
💬 備註 - 3 個
| 工具 |
說明 |
主要參數 |
get_memos |
獲取文檔中所有備註/註釋的列表 |
doc_id |
insert_memo |
插入備註/註釋 |
doc_id, section_index, paragraph_index, content, author? |
delete_memo |
刪除備註/註釋 |
doc_id, memo_id |
📚 章節 - 5 個
| 工具 |
說明 |
主要參數 |
get_sections |
獲取文檔中所有章節的列表 |
doc_id |
insert_section |
插入新章節 |
doc_id, after_index |
delete_section |
刪除章節 |
doc_id, section_index |
get_section_xml |
查看章節原始 XML(用於 AI 文檔操作) |
doc_id, section_index? |
set_section_xml |
替換章節原始 XML(必須為 HWPML 格式) |
doc_id, xml, section_index?, validate? |
🎭 樣式定義 - 4 個
| 工具 |
說明 |
主要參數 |
get_styles |
獲取文檔中定義的樣式列表 |
doc_id |
get_char_shapes |
獲取字符形狀(CharShape)定義列表 |
doc_id |
get_para_shapes |
獲取段落形狀(ParaShape)定義列表 |
doc_id |
apply_style |
將樣式應用於段落 |
doc_id, section_index, paragraph_index, style_id |
📰 分欄設置 - 2 個
| 工具 |
說明 |
主要參數 |
get_column_def |
查看分欄(Column)設置 |
doc_id, section_index? |
set_column_def |
修改分欄設置(多欄編輯) |
doc_id, count, section_index?, type?, same_size?, gap? |
📤 導出 - 2 個
| 工具 |
說明 |
主要參數 |
export_to_text |
將文檔導出為文本文件 |
doc_id, output_path |
export_to_html |
將文檔導出為 HTML 文件 |
doc_id, output_path |
↩️ 撤銷/重做 - 2 個
| 工具 |
說明 |
主要參數 |
undo |
撤銷最後一次更改 |
doc_id |
redo |
恢復撤銷的更改 |
doc_id |
🔧 技術細節
📋 支持的格式
| 格式 |
擴展名 |
讀取 |
寫入 |
| HWPX |
.hwpx |
✅ |
✅ |
| HWP |
.hwp |
❌ |
❌ |
注意:不支持 HWP(二進制)文件,請先在漢松辦公軟件中將其轉換為 HWPX 格式後再使用。
📝 版本說明
v0.4.0(增強搜索和圖表支持)
- 🆕 新功能:為
search_text 添加 include_tables 選項,可搜索表格單元格內的文本
- 🆕 新功能:
replace_text_in_cell 可精確替換特定表格單元格內的文本
- 🆕 新功能:
render_mermaid 可將 Mermaid 圖表作為圖像插入文檔
- 支持所有 Mermaid 語法,如流程圖、序列圖、類圖等
- 可選擇主題(默認、深色、森林、中性)
- 🆕 新功能:
get_section_xml / set_section_xml 可直接操作章節原始 XML
- 🔧 改進:全面改進
insert_image 功能
- 自動將圖像保存到 BinData 文件夾
- 自動註冊 content.hpf 清單
- 確保插入圖像時文件不會損壞
v0.3.0(嵌套表格支持)
- 🆕 新功能:
insert_nested_table 可在表格單元格中插入嵌套表格
- 在父表格的特定單元格中插入新表格
- 可指定初始數據(二維數組)
- 完全遵循 HWPX 標準結構(
treatAsChar, hp:subList)
- 🔧 改進:改進 charSpacing 解析(處理屬性順序無關)
v0.2.1(關鍵修復)
- 🔥 關鍵修復:徹底解決同一行中多個單元格同時修改時文件損壞的問題
- 通過按行分組更新,防止索引不一致
- 通過逆序處理確保安全的 XML 修改
v0.2.0(增強版)
- 🔥 重大修復:通過在文本修改時自動重置 lineseg,徹底解決文本重疊問題
- 🔧 錯誤修復:修復嵌套表格結構中 XML 元素邊界誤識別問題
- 🛡️ 穩定性提升:通過原子文件寫入防止文件損壞
- 📦 樣式保留:完全保留原始 charPr/spacing 樣式
v0.1.0(原始版本)
- 首次發佈(mjyoo2/hwp-extension)
📄 許可證
本項目採用 MIT 許可證。
🤝 貢獻
如果你發現了 bug 或有功能需求,請在 GitHub Issues 中反饋。