Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(core): address review findings — Zod validation, operator precedence

Dhawal Upadhyayfff8c8963a90
diff --git a/ts/packages/core/src/models/CustomTool.ts b/ts/packages/core/src/models/CustomTool.ts
index bbc01e53c..0aae19854 100644
--- a/ts/packages/core/src/models/CustomTool.ts
+++ b/ts/packages/core/src/models/CustomTool.ts
@@ -113,6 +113,7 @@ export function CustomTool<T extends z.ZodType>(
description,
toolkit,
inputSchema,
+ inputParams,
execute: execute as CustomToolExecuteFn<z.ZodType>,
};
}
diff --git a/ts/packages/core/src/models/ToolRouterSession.ts b/ts/packages/core/src/models/ToolRouterSession.ts
index ccfb56890..1cc2af510 100644
--- a/ts/packages/core/src/models/ToolRouterSession.ts
+++ b/ts/packages/core/src/models/ToolRouterSession.ts
@@ -80,13 +80,11 @@ export class ToolRouterSession<
): Promise<ToolExecuteResponse> => {
// Intercept COMPOSIO_MULTI_EXECUTE_TOOL and check if the inner tool is local
if (toolSlug === COMPOSIO_MULTI_EXECUTE_TOOL) {
- const innerSlug = String((input as Record<string, unknown>).tool_slug ?? '');
+ const innerSlug = String(input.tool_slug ?? '');
const entry = this.findLocalTool(innerSlug);
if (entry) {
- return this.executeLocalTool(
- entry,
- (input as Record<string, unknown>).arguments as Record<string, unknown> ?? {}
- );
+ const innerArgs = (input.arguments as Record<string, unknown> | undefined) ?? {};
+ return this.executeLocalTool(entry, innerArgs);
}
}
// Default: send to backend
@@ -258,13 +256,16 @@ export class ToolRouterSession<
): Promise<ToolExecuteResponse> {
const { handle } = entry;
- // Validate input against the Zod schema
- // The handle was created from a Zod schema, but we stored the execute fn.
- // We need to re-validate at execution time. The inputParams is on the original options,
- // but the handle doesn't store the Zod schema — only the JSON Schema.
- // The execute function itself should handle its input. We trust the LLM/caller here
- // and let the execute function handle validation. If the user used Zod in their execute,
- // it will validate there.
+ // Validate and transform input using the original Zod schema.
+ // This applies defaults, coercions, and transforms (e.g. z.string().default('all')).
+ const parsed = handle.inputParams.safeParse(arguments_);
+ if (!parsed.success) {
+ return {
+ data: {},
+ error: `Input validation failed: ${parsed.error.message}`,
+ successful: false,
+ };
+ }
// Build session context
const sessionContext = new SessionContextImpl(
@@ -274,7 +275,7 @@ export class ToolRouterSession<
);
try {
- const result = await handle.execute(arguments_, sessionContext);
+ const result = await handle.execute(parsed.data, sessionContext);
return {
data: result.data,
error: result.error,
diff --git a/ts/packages/core/src/types/customTool.types.ts b/ts/packages/core/src/types/customTool.types.ts
index b55a0cb56..ce3e51f8b 100644
--- a/ts/packages/core/src/types/customTool.types.ts
+++ b/ts/packages/core/src/types/customTool.types.ts
@@ -124,6 +124,8 @@ export interface CustomToolHandle {
readonly description: string;
readonly toolkit?: string;
readonly inputSchema: Record<string, unknown>;
+ /** @internal Original Zod schema — used for runtime input validation (defaults, coercions, transforms) */
+ readonly inputParams: z.ZodType;
/** Direct reference to the execute function — useful for testing */
readonly execute: CustomToolExecuteFn<z.ZodType>;
}
diff --git a/ts/packages/core/test/models/customTool.test.ts b/ts/packages/core/test/models/customTool.test.ts
index 575d5f2b9..15ce50fc9 100644
--- a/ts/packages/core/test/models/customTool.test.ts
+++ b/ts/packages/core/test/models/customTool.test.ts
@@ -38,6 +38,7 @@ describe('CustomTool', () => {
},
required: ['category'],
});
+ expect(handle.inputParams).toBe(baseOptions.inputParams);
});
it('should include toolkit when provided', () => {
@@ -163,6 +164,7 @@ describe('buildLocalToolsMap', () => {
name: `Tool ${slug}`,
description: `Description for ${slug}`,
inputSchema: { type: 'object', properties: {} },
+ inputParams: z.object({}),
execute: vi.fn(),
});
@@ -223,6 +225,7 @@ describe('serializeLocalTools', () => {
description: 'Gets some data',
toolkit: 'my_toolkit',
inputSchema: { type: 'object', properties: { id: { type: 'string' } } },
+ inputParams: z.object({ id: z.string() }),
execute: vi.fn(),
};
@@ -245,6 +248,7 @@ describe('serializeLocalTools', () => {
name: 'No Toolkit',
description: 'No toolkit tool',
inputSchema: { type: 'object', properties: {} },
+ inputParams: z.object({}),
execute: vi.fn(),
};
diff --git a/ts/packages/core/test/models/customToolRouting.test.ts b/ts/packages/core/test/models/customToolRouting.test.ts
index 0abee961d..bcdbf2f74 100644
--- a/ts/packages/core/test/models/customToolRouting.test.ts
+++ b/ts/packages/core/test/models/customToolRouting.test.ts
@@ -304,6 +304,48 @@ describe('ToolRouterSession execution routing', () => {
expect(result.data).toEqual({});
});
+ it('should apply Zod defaults when input is missing optional fields', async () => {
+ const defaultExecute = vi.fn().mockImplementation(async (input: any) => ({
+ data: { category: input.category },
+ error: null,
+ successful: true,
+ }));
+
+ const toolWithDefaults = CustomTool({
+ slug: 'DEFAULTS_TOOL',
+ name: 'Defaults',
+ description: 'Tool with default values',
+ inputParams: z.object({ category: z.string().default('all') }),
+ execute: defaultExecute,
+ });
+
+ const session = createSession(mockClient, [toolWithDefaults]);
+ const result = await session.execute('DEFAULTS_TOOL', {});
+
+ // Zod default should be applied
+ expect(defaultExecute).toHaveBeenCalledWith(
+ { category: 'all' },
+ expect.anything()
+ );
+ expect(result.data).toEqual({ category: 'all' });
+ });
+
+ it('should return validation error for invalid input', async () => {
+ const strictTool = CustomTool({
+ slug: 'STRICT_TOOL',
+ name: 'Strict',
+ description: 'Tool with strict input',
+ inputParams: z.object({ count: z.number() }),
+ execute: vi.fn(),
+ });
+
+ const session = createSession(mockClient, [strictTool]);
+ const result = await session.execute('STRICT_TOOL', { count: 'not-a-number' });
+
+ expect(result.error).toContain('Input validation failed');
+ expect(result.data).toEqual({});
+ });
+
it('should handle non-Error throws gracefully', async () => {
const throwingTool = CustomTool({
slug: 'STRING_THROW',