Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(chat): an extra argument no longer fails a chat tool call (#16391)

Hazem Adel292e1e1df9ee
diff --git a/packages/server/worker/src/lib/execute/jobs/ee/agent/tools/skill-tools.ts b/packages/server/worker/src/lib/execute/jobs/ee/agent/tools/skill-tools.ts
index 45f114a65ed..3eecf5181e5 100644
--- a/packages/server/worker/src/lib/execute/jobs/ee/agent/tools/skill-tools.ts
+++ b/packages/server/worker/src/lib/execute/jobs/ee/agent/tools/skill-tools.ts
@@ -171,12 +171,22 @@ async function validateToolInput({ target, input }: { target: ToolSet[string], i
const result = await schema.validate(input)
return result.success ? { success: true, value: isObject(result.value) ? result.value : input } : result
}
- const { data: zodSchema } = await tryCatch(async () => z.fromJSONSchema(plainJsonSchema<z.core.JSONSchema.JSONSchema>(await schema.jsonSchema)))
+ const jsonSchema = plainJsonSchema<z.core.JSONSchema.JSONSchema>(await schema.jsonSchema)
+ const declaredInput = withoutUndeclaredKeys({ schema: jsonSchema, input })
+ const { data: zodSchema } = await tryCatch(async () => z.fromJSONSchema(jsonSchema))
if (isNil(zodSchema)) {
- return { success: true, value: input }
+ return { success: true, value: declaredInput }
}
- const result = zodSchema.safeParse(input)
- return result.success ? { success: true, value: input } : { success: false, error: new Error(result.error.message) }
+ const result = zodSchema.safeParse(declaredInput)
+ return result.success ? { success: true, value: declaredInput } : { success: false, error: new Error(result.error.message) }
+}
+
+function withoutUndeclaredKeys({ schema, input }: { schema: unknown, input: Record<string, unknown> }): Record<string, unknown> {
+ if (!isObject(schema) || !isObject(schema['properties']) || schema['additionalProperties'] !== false) {
+ return input
+ }
+ const declared = schema['properties']
+ return Object.fromEntries(Object.entries(input).filter(([key]) => key in declared))
}
async function withOuterLabels({ target, outer, input }: { target: ToolSet[string], outer: Record<string, unknown>, input: Record<string, unknown> }): Promise<Record<string, unknown>> {
diff --git a/packages/server/worker/test/lib/execute/jobs/ee/agent/skill-tools.test.ts b/packages/server/worker/test/lib/execute/jobs/ee/agent/skill-tools.test.ts
index 337799cc15c..e15a75acb34 100644
--- a/packages/server/worker/test/lib/execute/jobs/ee/agent/skill-tools.test.ts
+++ b/packages/server/worker/test/lib/execute/jobs/ee/agent/skill-tools.test.ts
@@ -198,6 +198,40 @@ describe('ap_lazy_tool', () => {
expect(errorMessageOf(rejected)).toContain('Invalid input for "ap_send_email"')
})
+ function surfaceWithMcpTool({ additionalProperties }: { additionalProperties: boolean }) {
+ return buildSkillSurface({
+ tools: {
+ ...stubTools(CHAT_TOOL_NAMES),
+ ap_rename_flow: tool({ description: 'Rename a flow.', inputSchema: jsonSchema<Record<string, unknown>>({ type: 'object', properties: { flowId: { type: 'string' } }, required: ['flowId'], additionalProperties }), execute: async () => ({}) }),
+ },
+ surface: 'CHAT',
+ guides: {},
+ onSkillLoaded: vi.fn(),
+ canAffordPaidTool: () => true,
+ })
+ }
+
+ it('drops a top-level key a strict MCP tool does not declare instead of failing the call', async () => {
+ const checked = await validateWrapped({ surface: surfaceWithMcpTool({ additionalProperties: false }), value: { tool: 'ap_rename_flow', input: { flowId: 'f1', pieceName: '@activepieces/piece-tables' } } })
+
+ expect(checked).toMatchObject({ success: true, value: { input: { flowId: 'f1' } } })
+ expect(checked).not.toMatchObject({ value: { input: { pieceName: expect.anything() } } })
+ })
+
+ it('keeps extra keys for an MCP tool whose schema allows them', async () => {
+ const checked = await validateWrapped({ surface: surfaceWithMcpTool({ additionalProperties: true }), value: { tool: 'ap_rename_flow', input: { flowId: 'f1', note: 'kept' } } })
+
+ expect(checked).toMatchObject({ success: true, value: { input: { flowId: 'f1', note: 'kept' } } })
+ })
+
+ it('still rejects a call that is missing a required key', async () => {
+ const surface = surfaceWithJsonSchemaTool(async () => ({ renamed: true }))
+
+ const rejected = await validateWrapped({ surface, value: { tool: 'ap_rename_flow', input: { pieceName: 'x' } } })
+
+ expect(errorMessageOf(rejected)).toContain('Invalid input for "ap_rename_flow"')
+ })
+
it('hands a JSON-schema tool its input unchanged', async () => {
const execute = vi.fn(async () => ({ renamed: true }))
await executeWrapped({ surface: surfaceWithJsonSchemaTool(execute), toolName: 'ap_rename_flow', input: { flowId: 'f1' } })