Skip to content

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

TypeScript

fix(mcp-host): set up chart fonts when a chart is drawn, not at import

marcoteb97a4de60b53a
diff --git a/mcp-host/src/workflow/__tests__/internalTools.import.test.ts b/mcp-host/src/workflow/__tests__/internalTools.import.test.ts
new file mode 100644
index 000000000..b59cfa633
--- /dev/null
+++ b/mcp-host/src/workflow/__tests__/internalTools.import.test.ts
@@ -0,0 +1,38 @@
+/**
+ * Importing the generators changes nothing process-wide: Chart.js keeps its
+ * defaults until a chart is drawn.
+ */
+import { afterEach, beforeEach, describe, expect, it } from 'vitest'
+import { Chart } from 'chart.js'
+import * as fs from 'fs'
+import * as os from 'os'
+import * as path from 'path'
+import { CHART_FONT_STACK } from '../fonts'
+import { INTERNAL_TOOLS } from '../internalTools'
+
+let outputDir: string
+
+beforeEach(() => {
+ outputDir = fs.mkdtempSync(path.join(os.tmpdir(), 'clerum-import-'))
+})
+
+afterEach(() => {
+ fs.rmSync(outputDir, { recursive: true, force: true })
+})
+
+describe('importing the generators', () => {
+ it('leaves the Chart.js defaults as they were until a chart is drawn', async () => {
+ const { family } = Chart.defaults.font
+ const { color } = Chart.defaults
+ expect(family).not.toBe(CHART_FONT_STACK)
+
+ const chart = INTERNAL_TOOLS.find(t => t.name === 'clerum__generate_chart')!
+ const r = await chart.execute(
+ { filename: 'c.png', type: 'bar', data: { labels: ['a'], datasets: [{ data: [1] }] } },
+ outputDir
+ )
+ expect(r.success, r.error).toBe(true)
+ expect(Chart.defaults.font.family).toBe(CHART_FONT_STACK)
+ expect(Chart.defaults.color).not.toBe(color)
+ })
+})
diff --git a/mcp-host/src/workflow/internalTools.ts b/mcp-host/src/workflow/internalTools.ts
index b39f6c289..9db1f5554 100644
--- a/mcp-host/src/workflow/internalTools.ts
+++ b/mcp-host/src/workflow/internalTools.ts
@@ -127,11 +127,16 @@ const PdfPrinter = require('pdfmake')
// chart types we expose (line, bar, pie, doughnut, etc.) plus axes/legend/title.
Chart.register(...registerables)
-// Charts use the stack ./fonts registers (Roboto first), so labels render the
-// same on every image, including one without system fonts.
-ensureFontsReady()
-Chart.defaults.font.family = CHART_FONT_STACK
-Chart.defaults.color = '#0f172a'
+/**
+ * Charts use the stack ./fonts registers (Roboto first), so labels render the
+ * same on every image, including one without system fonts. Set when a chart is
+ * drawn, so importing this module leaves the fonts and Chart.js as they were.
+ */
+function prepareChartFonts(): void {
+ ensureFontsReady()
+ Chart.defaults.font.family = CHART_FONT_STACK
+ Chart.defaults.color = '#0f172a'
+}
// Accessor to the current Host CRD, injected by main (avoids a circular import).
// Re-read on every getOutputDir() call because `currentHost` is hydrated async
@@ -949,7 +954,7 @@ const generateChart: InternalToolDefinition = {
async execute(args: Record<string, unknown>, outputDir: string): Promise<InternalToolResult> {
let chart: Chart | undefined
try {
- ensureFontsReady()
+ prepareChartFonts()
const filename = outputFilename(args.filename, 'png', 'chart')
const chartTypeRaw = String(args.type ?? 'bar')