Skip to content

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

TypeScript

fix(mcp-host): keep decoded images by size, and hold each call's until it ends

marcoteb1073a2e64071
diff --git a/mcp-host/src/workflow/__tests__/embeddedImages.test.ts b/mcp-host/src/workflow/__tests__/embeddedImages.test.ts
index 0bfc21a2b..ef258971e 100644
--- a/mcp-host/src/workflow/__tests__/embeddedImages.test.ts
+++ b/mcp-host/src/workflow/__tests__/embeddedImages.test.ts
@@ -344,3 +344,33 @@ describe('loadEmbeddableImage', () => {
)
})
})
+
+describe('images decoded for a call', () => {
+ /** A WebP of noise, whose decoded PNG is a few megabytes. */
+ async function writeNoise(name: string, seed: number) {
+ const canvas = createCanvas(1000, 1000)
+ const ctx = canvas.getContext('2d')
+ const image = ctx.createImageData(1000, 1000)
+ let s = seed
+ for (let i = 0; i < image.data.length; i++) {
+ s = (s * 1103515245 + 12345) % 2147483648
+ image.data[i] = i % 4 === 3 ? 255 : s & 255
+ }
+ ctx.putImageData(image, 0, 0)
+ fs.writeFileSync(path.join(outputDir, name), await canvas.encode('webp', 100))
+ }
+
+ it('stay readable while another call decodes more than the cache holds', async () => {
+ await writeImage('held.webp', 'webp')
+ const releaseHeld = await predecodeImages(['held.webp'], outputDir)
+ const others = Array.from({ length: 34 }, (_, i) => `noise-${i}.webp`)
+ for (const [i, name] of others.entries()) await writeNoise(name, i + 1)
+ const releaseOthers = await predecodeImages(others, outputDir)
+ releaseOthers()
+
+ const warnings: string[] = []
+ expect(loadEmbeddableImage('held.webp', outputDir, warnings)).toBeDefined()
+ expect(warnings).toEqual([])
+ releaseHeld()
+ }, 60_000)
+})
diff --git a/mcp-host/src/workflow/embeddedImages.ts b/mcp-host/src/workflow/embeddedImages.ts
index 07c12cc02..c04972aca 100644
--- a/mcp-host/src/workflow/embeddedImages.ts
+++ b/mcp-host/src/workflow/embeddedImages.ts
@@ -507,11 +507,30 @@ function decodable(raw: Buffer): Buffer | undefined {
const decoded = new Map<string, Buffer>()
/** Why an image named in the arguments was not converted, by file and version. */
const refused = new Map<string, string>()
-const MAX_DECODED = 32
+const MAX_REFUSALS = 32
+/** Decoded bytes kept between calls; an image a call still runs on is never dropped. */
+const MAX_DECODED_BYTES = 64 * 1024 * 1024
+/** How many running calls use each decoded image. */
+const inUse = new Map<string, number>()
+let decodedBytes = 0
+
+function refuse(key: string, reason: string): void {
+ refused.set(key, reason)
+ while (refused.size > MAX_REFUSALS) refused.delete(refused.keys().next().value as string)
+}
-function remember<T>(map: Map<string, T>, key: string, value: T): void {
- map.set(key, value)
- while (map.size > MAX_DECODED) map.delete(map.keys().next().value as string)
+/** Keep `bytes` for `key`, dropping the oldest images no running call uses past the budget. */
+function keepDecoded(key: string, bytes: Buffer): void {
+ decodedBytes -= decoded.get(key)?.length ?? 0
+ decoded.delete(key)
+ decoded.set(key, bytes)
+ decodedBytes += bytes.length
+ for (const [k, v] of decoded) {
+ if (decodedBytes <= MAX_DECODED_BYTES) break
+ if (inUse.has(k)) continue
+ decoded.delete(k)
+ decodedBytes -= v.length
+ }
}
function decodedKey(file: string): string | undefined {
@@ -553,10 +572,17 @@ function collectImageNames(value: unknown, into: Set<string>, depth = 0): void {
* loadEmbeddableImage can convert it. Never throws: whatever cannot be decoded
* here is reported by loadEmbeddableImage when a generator asks for it.
*/
-export async function predecodeImages(args: unknown, outputDir: string): Promise<void> {
+export async function predecodeImages(args: unknown, outputDir: string): Promise<() => void> {
const names = new Set<string>()
collectImageNames(args, names)
const root = path.resolve(outputDir)
+ // Every image this call decodes or finds decoded stays until it releases them,
+ // so a call running beside it cannot push them out before the generator reads them.
+ const held: string[] = []
+ const hold = (key: string) => {
+ held.push(key)
+ inUse.set(key, (inUse.get(key) ?? 0) + 1)
+ }
for (const name of names) {
try {
const resolved = path.resolve(root, name)
@@ -566,7 +592,11 @@ export async function predecodeImages(args: unknown, outputDir: string): Promise
? path.join(root, path.basename(name))
: undefined
const key = file && decodedKey(file)
- if (!file || !key || decoded.has(key) || !insideRealFolder(file, root)) continue
+ if (!file || !key || !insideRealFolder(file, root)) continue
+ if (decoded.has(key)) {
+ hold(key)
+ continue
+ }
const raw = fs.readFileSync(file)
if (isPng(raw) || (isJpeg(raw) && jpegOrientation(raw) <= 1)) continue
const input = decodable(raw)
@@ -574,17 +604,16 @@ export async function predecodeImages(args: unknown, outputDir: string): Promise
if (isSvg(raw)) {
const refusal = svgRefusal(input.toString('utf8'))
if (refusal) {
- remember(refused, key, refusal)
+ refuse(key, refusal)
continue
}
const png = await renderSvg(input)
- if (png) remember(decoded, key, png)
- else
- remember(
- refused,
- key,
- `took longer than ${SVG_RENDER_MS / 1000} s to draw, or could not be drawn`
- )
+ if (png) {
+ hold(key)
+ keepDecoded(key, png)
+ } else {
+ refuse(key, `took longer than ${SVG_RENDER_MS / 1000} s to draw, or could not be drawn`)
+ }
continue
}
const image = await loadImage(input)
@@ -593,8 +622,8 @@ export async function predecodeImages(args: unknown, outputDir: string): Promise
canvas.getContext('2d').drawImage(image, 0, 0)
// The decoder applies the EXIF rotation; re-encoding drops it, so every
// format shows the photo the same way up.
- remember(
- decoded,
+ hold(key)
+ keepDecoded(
key,
isJpeg(raw) ? canvas.toBuffer('image/jpeg', 92) : canvas.toBuffer('image/png')
)
@@ -602,6 +631,16 @@ export async function predecodeImages(args: unknown, outputDir: string): Promise
// Left for loadEmbeddableImage to report against the argument that named it.
}
}
+ let released = false
+ return () => {
+ if (released) return
+ released = true
+ for (const key of held) {
+ const count = (inUse.get(key) ?? 1) - 1
+ if (count > 0) inUse.set(key, count)
+ else inUse.delete(key)
+ }
+ }
}
/**
diff --git a/mcp-host/src/workflow/internalTools.ts b/mcp-host/src/workflow/internalTools.ts
index df47fb05a..296e0d046 100644
--- a/mcp-host/src/workflow/internalTools.ts
+++ b/mcp-host/src/workflow/internalTools.ts
@@ -1772,18 +1772,24 @@ function prepared(tool: InternalToolDefinition): InternalToolDefinition {
...tool,
execute: async (args, outputDir) => {
let clean: Record<string, unknown>
+ let release: () => void
try {
clean = withoutUnsetNulls(tool.parameters, cleanToolArgs(args ?? {})) as Record<
string,
unknown
>
const { predecodeImages } = await import('./embeddedImages')
- await predecodeImages(clean, outputDir)
+ release = await predecodeImages(clean, outputDir)
} catch (err) {
return { success: false, error: err instanceof Error ? err.message : String(err) }
}
const unknown = watchUnknownArguments(tool.parameters, clean)
- const result = await tool.execute(clean, outputDir)
+ let result: InternalToolResult
+ try {
+ result = await tool.execute(clean, outputDir)
+ } finally {
+ release()
+ }
const ignored = unknown.ignored()
if (!result.success || ignored.length === 0) return result
const listed = ignored.slice(0, 10).map(name => `'${name}'`)