Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(mcp-host): give each image in a DOCX its own drawing id

marcoteba4b8f9c82e29
diff --git a/mcp-host/src/workflow/__tests__/internalTools.images.test.ts b/mcp-host/src/workflow/__tests__/internalTools.images.test.ts
index f3e2f1702..d05ef8487 100644
--- a/mcp-host/src/workflow/__tests__/internalTools.images.test.ts
+++ b/mcp-host/src/workflow/__tests__/internalTools.images.test.ts
@@ -90,6 +90,27 @@ describe('imageDisplaySize', () => {
})
})
+describe('DOCX images', () => {
+ it('gives every drawing its own id, as Office Open XML requires', async () => {
+ await makeChart('c.png', 400, 200)
+ const r = await findTool('clerum__generate_docx').execute(
+ {
+ filename: 'd.docx',
+ body: 'One\n\n![a](c.png)\n\nTwo',
+ images: ['c.png', 'missing.png', 'c.png'],
+ branding: { logoPath: 'c.png' },
+ },
+ outputDir
+ )
+ expect(r.success, r.error).toBe(true)
+ const ids = [
+ ...zipEntryText(r.artifact!.path, 'word/document.xml').matchAll(/<wp:docPr id="(\d+)"/g),
+ ].map(m => m[1])
+ expect(ids).toHaveLength(4)
+ expect(new Set(ids).size).toBe(4)
+ })
+})
+
describe('fitImageBox', () => {
it('keeps the aspect ratio when neither side is given', async () => {
const file = await makeChart('wide.png', 800, 400)
diff --git a/mcp-host/src/workflow/docxGenerator.ts b/mcp-host/src/workflow/docxGenerator.ts
index 6ee7370e1..66446be14 100644
--- a/mcp-host/src/workflow/docxGenerator.ts
+++ b/mcp-host/src/workflow/docxGenerator.ts
@@ -105,7 +105,9 @@ export async function runGenerateDocx(
box: { width: number; height: number },
placement: DocxImagePlacement
): Paragraph | undefined => {
- const paragraph = docxImageParagraph(ref, outputDir, box, placement, warnings, label)
+ // Word needs each drawing's id unique in the document; the next one is unused.
+ const id = embedded + 1
+ const paragraph = docxImageParagraph(ref, outputDir, box, placement, warnings, label, id)
if (paragraph) embedded++
else imageLeftOut = true
return paragraph
diff --git a/mcp-host/src/workflow/docxImages.ts b/mcp-host/src/workflow/docxImages.ts
index 2d12c4ee2..0af46ae71 100644
--- a/mcp-host/src/workflow/docxImages.ts
+++ b/mcp-host/src/workflow/docxImages.ts
@@ -22,7 +22,8 @@ function alignmentOf(value: unknown) {
* it cannot be embedded; the reason is added to `warnings`, prefixed with the
* argument `label` so the logo and an image of the same name can be told apart.
* A path that leaves the output folder still fails the call, with an error
- * naming the argument and the fix.
+ * naming the argument and the fix. `id` must be unique among the document's
+ * images, as Office Open XML requires; the docx library numbers every one 1.
*/
export function docxImageParagraph(
ref: unknown,
@@ -30,7 +31,8 @@ export function docxImageParagraph(
box: ImageSize,
placement: DocxImagePlacement,
warnings: string[],
- label: string
+ label: string,
+ id: number
): Paragraph | undefined {
const notes: string[] = []
let image: ReturnType<typeof loadEmbeddableImage>
@@ -57,6 +59,7 @@ export function docxImageParagraph(
data: image.data,
transformation: size,
altText: {
+ id: String(id),
name: path.basename(image.path),
...(placement.altText ? { description: placement.altText } : {}),
},