Skip to content

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

TypeScript

fix(mcp-host): report a chart decoration that could not be drawn

marcoteb746c03215400
diff --git a/mcp-host/src/workflow/__tests__/chartPlugins.failures.test.ts b/mcp-host/src/workflow/__tests__/chartPlugins.failures.test.ts
new file mode 100644
index 000000000..2ce6168ee
--- /dev/null
+++ b/mcp-host/src/workflow/__tests__/chartPlugins.failures.test.ts
@@ -0,0 +1,73 @@
+/**
+ * Value labels and the gauge reading are drawn after the chart itself, so a
+ * failure there must not cost the image; it must not go unnoticed either. The
+ * chart is still saved, and the result says what is missing from it.
+ */
+import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
+import * as fs from 'fs'
+import * as os from 'os'
+import * as path from 'path'
+import { INTERNAL_TOOLS } from '../internalTools'
+
+vi.mock('../chartPlugins', async importOriginal => {
+ const actual = await importOriginal<typeof import('../chartPlugins')>()
+ // Options whose field throws when read: the plugin's own drawing code fails,
+ // and its own catch is what runs.
+ const failing = <T extends object>(options: T, field: string): T =>
+ Object.defineProperty({ ...options }, field, {
+ get() {
+ throw new Error('canvas lost')
+ },
+ })
+ return {
+ ...actual,
+ valueLabelsPlugin: (o: Parameters<typeof actual.valueLabelsPlugin>[0]) =>
+ actual.valueLabelsPlugin(failing(o, 'fontScale')),
+ gaugeCenterPlugin: (o: Parameters<typeof actual.gaugeCenterPlugin>[0]) =>
+ actual.gaugeCenterPlugin(failing(o, 'textColor')),
+ }
+})
+
+let chart: (typeof INTERNAL_TOOLS)[number]
+beforeAll(() => {
+ chart = INTERNAL_TOOLS.find(t => t.name === 'clerum__generate_chart')!
+})
+
+let dir: string
+beforeEach(() => {
+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'clerum-chart-fail-'))
+})
+afterEach(() => {
+ fs.rmSync(dir, { recursive: true, force: true })
+})
+
+describe('a decoration that fails to draw', () => {
+ it('keeps the chart and says the value labels are missing', async () => {
+ const result = await chart.execute(
+ {
+ filename: 'c.png',
+ type: 'bar',
+ showValues: true,
+ data: { labels: ['a', 'b'], datasets: [{ label: 'x', data: [1, 2] }] },
+ },
+ dir
+ )
+ expect(result.success, result.error).toBe(true)
+ expect(fs.existsSync(result.artifact!.path)).toBe(true)
+ expect(result.content).toContain('drawn without value labels (canvas lost)')
+ })
+
+ it('keeps the gauge and says its reading is missing', async () => {
+ const result = await chart.execute(
+ {
+ filename: 'g.png',
+ type: 'gauge',
+ gaugeMax: 100,
+ data: { datasets: [{ label: 'value', data: [42] }] },
+ },
+ dir
+ )
+ expect(result.success, result.error).toBe(true)
+ expect(result.content).toContain('drawn without the gauge reading (canvas lost)')
+ })
+})
diff --git a/mcp-host/src/workflow/chartGenerator.ts b/mcp-host/src/workflow/chartGenerator.ts
index d62666b56..56d3758bd 100644
--- a/mcp-host/src/workflow/chartGenerator.ts
+++ b/mcp-host/src/workflow/chartGenerator.ts
@@ -745,7 +745,8 @@ export async function runGenerateChart(
const labelValues = shouldLabelValues(chartTypeRaw, args.showValues)
const plugins: Plugin[] = [backgroundPlugin(theme.backgroundColor)]
const unlabelled = { count: 0 }
- if (labelValues) plugins.push(valueLabelsPlugin({ ...valueOptions, unlabelled }))
+ const failed: string[] = []
+ if (labelValues) plugins.push(valueLabelsPlugin({ ...valueOptions, unlabelled, failed }))
if (chartTypeRaw === 'gauge') {
plugins.push(
gaugeCenterPlugin({
@@ -754,6 +755,7 @@ export async function runGenerateChart(
textColor: theme.textColor,
mutedColor: theme.mutedTextColor,
format: valueOptions,
+ failed,
})
)
}
@@ -908,6 +910,9 @@ export async function runGenerateChart(
'series is too dense; a larger chart or fewer series leaves room.'
)
}
+ for (const what of new Set(failed)) {
+ warnings.push(`The chart was drawn without ${what}.`)
+ }
// The plot area in requested pixels; the layout may run at a smaller scale.
const plotWidth = Math.max(0, Math.round(chart.chartArea.width * layoutScale))
const plotHeight = Math.max(0, Math.round(chart.chartArea.height * layoutScale))
diff --git a/mcp-host/src/workflow/chartPlugins.ts b/mcp-host/src/workflow/chartPlugins.ts
index a91f761fd..0e7eba1c2 100644
--- a/mcp-host/src/workflow/chartPlugins.ts
+++ b/mcp-host/src/workflow/chartPlugins.ts
@@ -9,6 +9,7 @@
* plugin package, so nothing is added to the dependency tree.
*/
import type { Chart, ChartType, Plugin } from 'chart.js'
+import { logger } from '../logger'
import { CHART_FONT_STACK } from './fonts'
export type ValueFormat = 'auto' | 'plain' | 'compact' | 'currency' | 'percent'
@@ -26,6 +27,8 @@ export interface ValueLabelOptions {
fontScale?: number
/** Set on each draw to how many values got no label. */
unlabelled?: { count: number }
+ /** Receives what could not be drawn, so the result can say it is missing. */
+ failed?: string[]
}
/** Below this, money is grouped in full rather than abbreviated. */
@@ -273,8 +276,9 @@ export function valueLabelsPlugin(options: ValueLabelOptions): Plugin {
afterDraw(chart: Chart) {
try {
drawValueLabels(chart, options)
- } catch {
- // A label is an enhancement; never let it cost the whole render.
+ } catch (err) {
+ // A label is an enhancement; it never costs the whole render, but it is reported.
+ decorationFailed(options.failed, 'value labels', err)
}
},
}
@@ -512,6 +516,15 @@ export interface GaugeCenterOptions {
textColor: string
mutedColor: string
format: ValueLabelOptions
+ /** Receives what could not be drawn, so the result can say it is missing. */
+ failed?: string[]
+}
+
+/** Record a decoration that could not be drawn: in the result, and in the log. */
+function decorationFailed(failed: string[] | undefined, what: string, err: unknown): void {
+ const message = err instanceof Error ? err.message : String(err)
+ failed?.push(`${what} (${message})`)
+ logger.warn({ decoration: what, err: message }, 'Chart decoration could not be drawn')
}
/**
@@ -541,8 +554,8 @@ export function gaugeCenterPlugin(o: GaugeCenterOptions): Plugin {
// may be in any language, and this sits inside the same picture.
ctx.fillText(`/ ${formatValue(o.max, o.format)}`, cx, cy + size * 0.62)
ctx.restore()
- } catch {
- // Enhancement only.
+ } catch (err) {
+ decorationFailed(o.failed, 'the gauge reading', err)
}
},
}