歡迎信件給四十位新客戶、每位參與者的證書、每筆訂單的發票:相同的文件,每次使用不同的記錄。使用本機執行的 GroupDocs.Assembly.Mcp (dnx GroupDocs.Assembly.Mcp --yes),Claude Desktop、Cursor 或 Copilot 只需一次工具呼叫 assemble_per_record,即可為 JSON 陣列中的每個元素填入您的 Word 範本,將每個結果儲存為您選擇的欄位命名的 PDF,並回報失敗的記錄,而不是在第一個失敗時就停止:
為
customers.json中的每位客戶使用welcome-letter.docx製作歡迎信 PDF,並以客戶名稱命名每個檔案。
逐步說明(含設定與除錯)請參考文件:How to generate one document per record (mail merge) with an AI agent
為什麼不讓代理程式自行迴圈?
它可以,對於五筆記錄來說沒問題。但四十次工具呼叫的迴圈等於四十次機會讓代理程式失算、在上下文變長時跳過記錄,或「好心」地改寫某封信件。每筆記錄的工具會把迴圈移到伺服器端:一次呼叫、一次範本、一次遍歷陣列,並回報寫入了多少檔案以及哪些記錄未成功。代理程式的工作就是選擇範本、資料檔與命名欄位,然後把報告讀回給您。
代理程式首先檢查什麼?
兩件事:資料長什麼樣子、範本會讀取什麼。在我們的測試中,customers.json 內有五位客戶,且只有一頁的歡迎信:
-> get_document_info {"file":{"filePath":"customers.json"}}
<- { "dataKind": "json",
"data": { "root": "array", "length": 5, "keys": null } }
-> get_template_fields {"template":{"filePath":"welcome-letter.docx"}}
<- { "dataSourceNames": [ "customer" ],
"fields": [ "customer.Contact", "customer.Plan", "customer.Name",
"customer.Manager", "customer.FirstInvoice" ] }
範本呼叫的記錄名稱為 customer;伺服器會自行解析,因此代理程式不必傳遞 dataSourceName。
一次呼叫、五筆記錄、一筆失敗
-> assemble_per_record {"template":{"filePath":"welcome-letter.docx"},
"data":{"filePath":"customers.json"},"fileNameField":"Name",
"format":"pdf"}
<- Assembled 4 of 5 document(s) from 'welcome-letter.docx' with
'customers.json', one per record (each record as 'customer'), as PDF.
Saved to:
.../Contoso Pharma.pdf
.../Fabrikam Logistics.pdf
.../Tailspin Toys.pdf
.../Litware Labs.pdf
1 record(s) failed:
record 4: InvalidOperationException: ... Can not get the value of
member 'FirstInvoice' on type 'System.Data.DataRow'. The template
reads 'FirstInvoice', which the data does not have. ...
第 4 筆記錄(Wingtip Travel)在資料中沒有第一張發票日期。其餘四封信件皆已寫入,且檔名皆以客戶名稱命名,失敗回報會提供記錄編號與缺少的欄位。代理程式會回報為「已完成四封信件;Wingtip Travel 沒有第一張發票日期,您要我將該行留空還是等候日期?」這才是您想要的對話,而不是四十個檔案卻不知道哪一個錯誤。
fan‑out 的運作方式
customers.json [ {Name:"Contoso Pharma",...}, {Name:"Fabrikam ..."},
... 5 records ]
|
assemble_per_record {"template":{"filePath":"welcome-letter.docx"},
"data":{"filePath":"customers.json"},
"fileNameField":"Name","format":"pdf"}
|
+--> Contoso Pharma.pdf
+--> Fabrikam Logistics.pdf ... 2 more
'--> "4 of 5 ... 1 record(s) failed: record 4 ..."
三個參數負責此工作。fileNameField 以記錄本身的欄位為每個輸出命名:此例為 Name,或使用點記法路徑如 order.Number(當每筆記錄內含子物件時)。對於這些平面記錄,customer.Name 會全部失敗(「記錄沒有 ‘customer.Name’ 欄位可用來命名檔案」)。若未提供此參數,檔案會使用遞增編號,且已存在的名稱永不被覆寫:新檔會得到 (1)。recordsPath 用於 JSON 為物件且內含陣列的情況,例如 "customers" 或 "batch.items"。maxDocuments 則是限制上限。
什麼會阻止批次失控?
maxDocuments,預設為 100,最高可設定至 1000。寫入任何檔案前,伺服器會先計算記錄數;若超過上限,便會拒絕。我們將其設為 3,對應我們的五筆客戶:
-> assemble_per_record {..., "maxDocuments":3}
<- Per-record assembly failed for 'welcome-letter.docx': there are 5
records, more than maxDocuments=3. Raise 'maxDocuments' (up to 1000)
or pass fewer records.
沒有任何檔案被寫入。對於實際寄送給真實人的郵件而言,這是正確的預設:若不小心指向整個 CRM 匯出,系統會回報問題,而不是一次產生上萬個 PDF。
我的資料是試算表,而非 JSON
每筆記錄的產生僅支援 JSON:物件陣列或包含單一陣列的物件。當資料來源是 CSV 或工作表時,代理程式會先將列轉換為 JSON(這是代理程式擅長的),並寫入儲存資料夾。如果您想要的是一份列出所有列的文件(例如價目表或登記冊),則使用 assemble_document 搭配範本中的 <<foreach>>,它能直接讀取 CSV 與試算表。
誠實的限制
- 每筆記錄組裝僅支援 JSON;CSV 與試算表資料必須先轉換。
- 評估模式:每個產生的檔案都會以 「Evaluation Only. Created with GroupDocs.Assembly 26.7…」 開頭,且電子郵件範本只保留純文字內容。正式寄送前請先為伺服器授權。
- 缺少欄位會導致該筆記錄失敗。設定
allowMissingMembers: true則會將該行留空;請依需求決定使用哪種方式。 - 日期:請以
2026-12-01T00:00:00格式寫入,並在範本中格式化,如本信使用<<[customer.FirstInvoice]:"d MMMM yyyy">>。在我們的測試中,僅日期字串在 UTC+2 機器上會多一天。 - 伺服器只負責寫入檔案,不會傳送。傳遞是另一個步驟。
常見問題
我可以在不上傳客戶清單的情況下使用 Claude 進行郵件合併嗎?
可以。伺服器在本機執行,會從您的儲存資料夾讀取 customers.json;產生的 PDF 也會寫入同一資料夾。
輸出檔案的命名規則是什麼?
使用您傳入的 fileNameField 欄位命名,此例為客戶名稱,所以會得到 Contoso Pharma.pdf。若未設定,則使用遞增編號。
每份文件可以是電子郵件而不是 PDF 嗎?
可以,只要範本本身是電子郵件:EML 或 MSG 範本會分別儲存為 eml、emlx 或 msg,每筆記錄產生一封訊息。像本信這樣的 Word 範本無法儲存為電子郵件;伺服器會回覆 「a WordProcessing template cannot be saved as ’eml’」。在評估模式下,電子郵件的 HTML 內容會被評估字樣取代,請使用授權來測試電子郵件輸出。
深入了解
- 文件與官方教學:How to generate one document per record (mail merge) with an AI agent
- 文件中心:GroupDocs.Assembly MCP Server
- GroupDocs.Assembly MCP 產品頁面 — 為 VS Code 與 Cursor 提供一鍵安裝,並提供其他客戶端的設定
- 從這裡開始:Let the model fill in the data, not write the document
- 相關文章:What does this template need? Ask it.
- 相關文章:The chart comes from the template, the numbers come from the data
- 相關文章:Your repo already has the data; let the agent make the PDF
- 本地部署與安全模型:3 architectures for AI document processing, and the one that keeps files inside your network
- 問題討論:GroupDocs Assembly forum
- 原始碼:GroupDocs.Assembly.Mcp on GitHub