MCP Word Processor MCP Server

Automate text extraction and replacement in Microsoft Word documents

README.md

mcp-word-processor

このプロジェクトは、Model Context Protocol (MCP) に準拠したWord文書処理サーバーです。 pywin32 ライブラリを使用してMicrosoft Wordアプリケーションを直接操作することで、 Wordファイル(.docx)内のテキストコンテンツの抽出と置換を行います。 python-docx ライブラリは使用していません。

機能

  • Wordファイルのテキストコンテンツ全体(本文、ヘッダー、フッター、表、図形内のテキストなど)を抽出します。
  • Wordファイル内の指定されたテキストを別のテキストに置換し、新しいファイルとして保存します。
  • Windows環境でのみ動作します。

セットアップ

1. 仮想環境の作成と依存関係のインストール

プロジェクトルートディレクトリで、uv を使用して仮想環境を作成し、必要な依存関係をインストールします。

uv venv
# 仮想環境をアクティベート (Windows PowerShellの場合)
.venv\Scripts\Activate.ps1
# 依存関係のインストール
uv pip install mcp pywin32 fastapi uvicorn

pyproject.toml に依存関係が定義されているため、uv pip install の代わりに uv pip sync を使用することもできます。

2. サンプルWordファイルの準備

templates ディレクトリを作成し、テスト用のWordファイル (sample_template.docx) を配置してください。

mkdir templates

例: templates/sample_template.docx

This is a sample Word document.

Here is some text to be replaced: {{OLD_TEXT_1}}
And another one: {{OLD_TEXT_2}}

This is a table:
+-------+-------+
| Header1 | Header2 |
+-------+-------+
| Value1  | Value2  |
+-------+-------+

In the header: Document Title
In the footer: Page Number

サーバーの実行

プロジェクトのルートディレクトリから main.py を実行することでサーバーを起動できます。

python main.py

サーバーは標準入出力を介してMCPリクエストを待ち受けます。

利用可能なツール

このMCPサーバーは以下のツールを提供します。

`get_word_elements`

指定されたWordファイルから全てのテキスト要素を取得します。

  • 説明: Word文書の本文、ヘッダー、フッター、表、図形内のテキストなど、全ての可視テキストを抽出します。
  • 入力スキーマ:
    {
        "type": "object",
        "properties": {
            "file_path": {
                "type": "string",
                "description": "処理するWordファイルの絶対パス。"
            }
        },
        "required": ["file_path"]
    }
    
  • :
    {
        "name": "get_word_elements",
        "arguments": {
            "file_path": "C:\\path\\to\\your\\document.docx"
        }
    }
    

`replace_word_elements`

指定されたWordファイル内のテキストを指定された内容で置換し、新しいファイルに保存します。

  • 説明: 元のWordファイルをコピーし、そのコピー内で複数のテキスト置換を実行し、指定されたパスに新しいファイルとして保存します。
  • 入力スキーマ:
    {
        "type": "object",
        "properties": {
            "file_path": {
                "type": "string",
                "description": "処理するWordファイルの絶対パス。"
            },
            "output_path": {
                "type": "string",
                "description": "変更を保存する新しいWordファイルの絶対パス。"
            },
            "replacements": {
                "type": "object",
                "patternProperties": {
                    "^[a-zA-Z0-9_\\s\\S]+$": {
                        "type": "string"
                    }
                },
                "description": "置換するキー(古いテキスト)と値(新しいテキスト)のペアを含む辞書。"
            }
        },
        "required": ["file_path", "output_path", "replacements"]
    }
    
  • :
    {
        "name": "replace_word_elements",
        "arguments": {
            "file_path": "C:\\path\\to\\your\\templates\\sample_template.docx",
            "output_path": "C:\\path\\to\\your\\output\\new_document.docx",
            "replacements": {
                "{{OLD_TEXT_1}}": "新しいテキスト1",
                "{{OLD_TEXT_2}}": "新しいテキスト2",
                "Header1": "新しいヘッダー1"
            }
        }
    }
    

注意事項

  • このサーバーはWindows環境にMicrosoft Wordがインストールされている必要があります。
  • pywin32 を使用しているため、サーバー実行中はバックグラウンドでWordアプリケーションが起動します。
  • output_path には、元の file_path と異なるパスを指定してください。同じパスを指定すると予期せぬ動作を引き起こす可能性があります。

Tools 2

get_word_elementsExtracts all visible text from a Word document, including body, headers, footers, tables, and shapes.
replace_word_elementsReplaces specified text within a Word document and saves the result as a new file.

Try it

Extract all text from the document located at C:\Documents\report.docx.
Replace the placeholder {{CLIENT_NAME}} with 'Acme Corp' in C:\Documents\template.docx and save it as C:\Documents\final_report.docx.
Find all text content in my Word file at C:\Templates\contract.docx including headers and tables.
Update the document C:\Templates\invoice.docx by replacing '{{DATE}}' with '2023-10-27' and '{{TOTAL}}' with '$500'.

Frequently Asked Questions

What are the key features of MCP Word Processor?

Extracts text from body, headers, footers, tables, and shapes. Performs bulk text replacement within Word documents. Saves modified documents as new files to prevent data loss. Directly automates Microsoft Word via pywin32.

What can I use MCP Word Processor for?

Automating the generation of personalized documents from templates. Extracting data from legacy Word reports for analysis. Batch updating placeholders in multiple Word documents. Integrating Word document manipulation into AI-driven workflows.

How do I install MCP Word Processor?

Install MCP Word Processor by running: uv venv && .venv\Scripts\Activate.ps1 && uv pip install mcp pywin32 fastapi uvicorn

What MCP clients work with MCP Word Processor?

MCP Word Processor works with any MCP-compatible client including Claude Desktop, Claude Code, Cursor, and other editors with MCP support.

Turn this server into reusable context

Keep MCP Word Processor docs, env vars, and workflow notes in Conare so your agent carries them across sessions.

Open Conare