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 no schema in the shared argument validator

marcotebfffb985abd62
diff --git a/mcp-host/src/workflow/__tests__/argumentGate.test.ts b/mcp-host/src/workflow/__tests__/argumentGate.test.ts
new file mode 100644
index 000000000..5c43e7f83
--- /dev/null
+++ b/mcp-host/src/workflow/__tests__/argumentGate.test.ts
@@ -0,0 +1,44 @@
+import { describe, expect, it } from 'vitest'
+import { checkToolArguments } from '../argumentGate'
+import type { InternalToolDefinition } from '../types'
+
+/** A tool whose schema is a new object on every build, as the GFS and capability tools are. */
+function rebuilt(): InternalToolDefinition {
+ return {
+ name: 'clerum__rebuilt',
+ description: 'test',
+ parameters: {
+ $id: 'https://clerum.test/rebuilt',
+ type: 'object',
+ required: ['count'],
+ properties: { count: { type: 'number' }, mode: { type: 'string', default: 'fast' } },
+ },
+ execute: async () => ({ success: true }),
+ }
+}
+
+describe('the argument check shared by chat and workflow steps', () => {
+ it('validates a schema built anew for each conversation, without keeping the old ones', () => {
+ // A schema Ajv kept would make the next one with the same $id fail to
+ // compile, and its calls would go through unchecked.
+ for (let i = 0; i < 3; i++) {
+ expect(checkToolArguments(rebuilt(), { count: 'one' })).toEqual({
+ ok: false,
+ error: 'Invalid arguments: data/count must be number',
+ })
+ expect(checkToolArguments(rebuilt(), { count: 1 })).toEqual({
+ ok: true,
+ input: { count: 1, mode: 'fast' },
+ })
+ }
+ })
+
+ it('reads a null sent for an unset optional argument as unset, and leaves the arguments as sent', () => {
+ const args = { count: 2, mode: null }
+ expect(checkToolArguments(rebuilt(), args)).toEqual({
+ ok: true,
+ input: { count: 2, mode: 'fast' },
+ })
+ expect(args).toEqual({ count: 2, mode: null })
+ })
+})
diff --git a/mcp-host/src/workflow/argumentGate.ts b/mcp-host/src/workflow/argumentGate.ts
index 4f26c7ca1..48442081c 100644
--- a/mcp-host/src/workflow/argumentGate.ts
+++ b/mcp-host/src/workflow/argumentGate.ts
@@ -10,6 +10,11 @@ import { withoutUnsetNulls } from './schemaArguments'
import type { InternalToolDefinition } from './types'
const ajv = new Ajv({ allErrors: true, strict: false, useDefaults: true })
+/**
+ * One validator per schema object, kept as long as the schema is. Tools built
+ * for one conversation or one step (GFS, capabilities, the catalog bridge)
+ * come with schema objects of their own, so the validators go with them.
+ */
const compiled = new WeakMap<object, ValidateFunction | null>()
/**
@@ -33,6 +38,11 @@ export function argumentValidator(tool: InternalToolDefinition): ValidateFunctio
} catch (err) {
// Surface the failure so a schema regression does not pass silently.
logger.warn({ err, toolName: tool.name }, 'Internal tool schema compilation failed')
+ } finally {
+ // Ajv keeps every schema it compiled, for good; the validator above is
+ // whole without that entry, so dropping it keeps the process from growing
+ // with every conversation's tools.
+ ajv.removeSchema(schema)
}
compiled.set(schema, validator)
return validator