Skip to content

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

TypeScript

fix(mcp-host): leave out of a PDF an image too large to decode

marcoteb6a37350c81c9
diff --git a/mcp-host/src/workflow/__tests__/embeddedImages.svg.test.ts b/mcp-host/src/workflow/__tests__/embeddedImages.svg.test.ts
index 521c63a97..348357a3a 100644
--- a/mcp-host/src/workflow/__tests__/embeddedImages.svg.test.ts
+++ b/mcp-host/src/workflow/__tests__/embeddedImages.svg.test.ts
@@ -102,3 +102,29 @@ describe('SVG images', () => {
expect(longestGap).toBeLessThan(1000)
}, 30000)
})
+
+describe('raster images a PDF would decode', () => {
+ it('leaves an image over the pixel limit out of a PDF, saying so, and still embeds it in a DOCX', async () => {
+ const { createCanvas } = await import('@napi-rs/canvas')
+ // 16.4 million pixels in a small file: pdfkit would inflate all of them.
+ fs.writeFileSync(
+ path.join(outputDir, 'huge.png'),
+ createCanvas(4100, 4000).toBuffer('image/png')
+ )
+ const result = await pdf.execute(
+ { filename: 'r.pdf', body: 'Chart:\n\n![c](huge.png)' },
+ outputDir
+ )
+ expect(result.success, result.error).toBe(true)
+ expect(result.content).toMatch(/'huge\.png' is 4100 x 4000 pixels, more than the 16 million/)
+ const bytes = fs.readFileSync(path.join(outputDir, result.artifact!.name)).toString('latin1')
+ expect(bytes).not.toMatch(/\/Subtype\s*\/Image/)
+ const docx = INTERNAL_TOOLS.find(t => t.name === 'clerum__generate_docx')!
+ const doc = await docx.execute(
+ { filename: 'r.docx', body: 'Chart:', images: ['huge.png'] },
+ outputDir
+ )
+ expect(doc.success, doc.error).toBe(true)
+ expect(doc.content ?? '').not.toMatch(/huge\.png/)
+ }, 30000)
+})
diff --git a/mcp-host/src/workflow/embeddedImages.ts b/mcp-host/src/workflow/embeddedImages.ts
index 86471dcdb..07c12cc02 100644
--- a/mcp-host/src/workflow/embeddedImages.ts
+++ b/mcp-host/src/workflow/embeddedImages.ts
@@ -273,7 +273,7 @@ export interface EmbeddableImage {
* bitmap, so every size is checked before decoding: a raster's from its file
* header, an SVG's from its root element, which is refused when unreadable.
*/
-const MAX_DECODE_PIXELS = 16_000_000
+export const MAX_DECODE_PIXELS = 16_000_000
const MAX_SVG_SIDE = 2048
/** Width and height from a GIF, WebP or BMP header, read before the image is decoded. */
diff --git a/mcp-host/src/workflow/pdfGenerator.ts b/mcp-host/src/workflow/pdfGenerator.ts
index 1aa37c091..7291b0519 100644
--- a/mcp-host/src/workflow/pdfGenerator.ts
+++ b/mcp-host/src/workflow/pdfGenerator.ts
@@ -19,7 +19,12 @@ import {
printedBranding,
} from './documentChrome'
import { documentEastAsianScript } from './docxScript'
-import { fitImageSize, imageDataUrl, loadEmbeddableImage } from './embeddedImages'
+import {
+ MAX_DECODE_PIXELS,
+ fitImageSize,
+ imageDataUrl,
+ loadEmbeddableImage,
+} from './embeddedImages'
import { PDF_FONT_FAMILY, PDF_MONO_FAMILY, pdfGlyphSource } from './fonts'
import {
closesFence,
@@ -862,7 +867,18 @@ export async function runGeneratePdf(
// A path outside the output folder still fails the call, but says what to pass instead.
const loadImage = (ref: unknown, label: string) => {
try {
- return loadEmbeddableImage(ref, outputDir, warnings, label)
+ const image = loadEmbeddableImage(ref, outputDir, warnings, label)
+ // pdfkit decodes every pixel of a PNG to embed it, so a small file of a
+ // huge image takes gigabytes; the other formats embed the bytes as they are.
+ if (image && image.pixels.width * image.pixels.height > MAX_DECODE_PIXELS) {
+ warnings.push(
+ `${label} '${path.basename(image.path)}' is ${image.pixels.width} x ${image.pixels.height} ` +
+ `pixels, more than the ${MAX_DECODE_PIXELS / 1e6} million a PDF image may have, so it ` +
+ 'was left out. Scale it down first.'
+ )
+ return undefined
+ }
+ return image
} catch (err) {
const reason = err instanceof Error ? err.message : String(err)
throw new Error(