@devries-cameron20 / composio Mirror · read-only / commits / fff8c8963
Fork 0

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

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 22, 03:15 UTC

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

- Store original Zod schema on CustomToolHandle.inputParams for runtime validation (defaults, coercions, transforms now applied correctly) - Add safeParse in executeLocalTool — returns validation error for bad input - Fix operator precedence: `(input.arguments as T | undefined) ?? {}` - Add tests for Zod defaults and validation error handling Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Dhawal Upadhyay committed Mar 10, 2026 at 15:57 UTC fff8c8963a9071a3ff1824d67ac06ecda6ed626e
5 files changed +63 -13
ts/packages/core/src/models/CustomTool.ts
+1
@@ -113,6 +113,7 @@ export function CustomTool<T extends z.ZodType>(
113 description,
114 toolkit,
115 inputSchema,
116 + inputParams,
117 execute: execute as CustomToolExecuteFn<z.ZodType>,
118 };
119 }
ts/packages/core/src/models/ToolRouterSession.ts
+14 -13
@@ -80,13 +80,11 @@ export class ToolRouterSession<
80 ): Promise<ToolExecuteResponse> => {
81 // Intercept COMPOSIO_MULTI_EXECUTE_TOOL and check if the inner tool is local
82 if (toolSlug === COMPOSIO_MULTI_EXECUTE_TOOL) {
83 - const innerSlug = String((input as Record<string, unknown>).tool_slug ?? '');
83 + const innerSlug = String(input.tool_slug ?? '');
84 const entry = this.findLocalTool(innerSlug);
85 if (entry) {
86 - return this.executeLocalTool(
87 - entry,
88 - (input as Record<string, unknown>).arguments as Record<string, unknown> ?? {}
89 - );
86 + const innerArgs = (input.arguments as Record<string, unknown> | undefined) ?? {};
87 + return this.executeLocalTool(entry, innerArgs);
88 }
89 }
90 // Default: send to backend
@@ -258,13 +256,16 @@ export class ToolRouterSession<
256 ): Promise<ToolExecuteResponse> {
257 const { handle } = entry;
258
261 - // Validate input against the Zod schema
262 - // The handle was created from a Zod schema, but we stored the execute fn.
263 - // We need to re-validate at execution time. The inputParams is on the original options,
264 - // but the handle doesn't store the Zod schema — only the JSON Schema.
265 - // The execute function itself should handle its input. We trust the LLM/caller here
266 - // and let the execute function handle validation. If the user used Zod in their execute,
267 - // it will validate there.
259 + // Validate and transform input using the original Zod schema.
260 + // This applies defaults, coercions, and transforms (e.g. z.string().default('all')).
261 + const parsed = handle.inputParams.safeParse(arguments_);
262 + if (!parsed.success) {
263 + return {
264 + data: {},
265 + error: `Input validation failed: ${parsed.error.message}`,
266 + successful: false,
267 + };
268 + }
269
270 // Build session context
271 const sessionContext = new SessionContextImpl(
@@ -274,7 +275,7 @@ export class ToolRouterSession<
275 );
276
277 try {
277 - const result = await handle.execute(arguments_, sessionContext);
278 + const result = await handle.execute(parsed.data, sessionContext);
279 return {
280 data: result.data,
281 error: result.error,
ts/packages/core/src/types/customTool.types.ts
+2
@@ -124,6 +124,8 @@ export interface CustomToolHandle {
124 readonly description: string;
125 readonly toolkit?: string;
126 readonly inputSchema: Record<string, unknown>;
127 + /** @internal Original Zod schema — used for runtime input validation (defaults, coercions, transforms) */
128 + readonly inputParams: z.ZodType;
129 /** Direct reference to the execute function — useful for testing */
130 readonly execute: CustomToolExecuteFn<z.ZodType>;
131 }
ts/packages/core/test/models/customTool.test.ts
+4
@@ -38,6 +38,7 @@ describe('CustomTool', () => {
38 },
39 required: ['category'],
40 });
41 + expect(handle.inputParams).toBe(baseOptions.inputParams);
42 });
43
44 it('should include toolkit when provided', () => {
@@ -163,6 +164,7 @@ describe('buildLocalToolsMap', () => {
164 name: `Tool ${slug}`,
165 description: `Description for ${slug}`,
166 inputSchema: { type: 'object', properties: {} },
167 + inputParams: z.object({}),
168 execute: vi.fn(),
169 });
170
@@ -223,6 +225,7 @@ describe('serializeLocalTools', () => {
225 description: 'Gets some data',
226 toolkit: 'my_toolkit',
227 inputSchema: { type: 'object', properties: { id: { type: 'string' } } },
228 + inputParams: z.object({ id: z.string() }),
229 execute: vi.fn(),
230 };
231
@@ -245,6 +248,7 @@ describe('serializeLocalTools', () => {
248 name: 'No Toolkit',
249 description: 'No toolkit tool',
250 inputSchema: { type: 'object', properties: {} },
251 + inputParams: z.object({}),
252 execute: vi.fn(),
253 };
254
ts/packages/core/test/models/customToolRouting.test.ts
+42
@@ -304,6 +304,48 @@ describe('ToolRouterSession execution routing', () => {
304 expect(result.data).toEqual({});
305 });
306
307 + it('should apply Zod defaults when input is missing optional fields', async () => {
308 + const defaultExecute = vi.fn().mockImplementation(async (input: any) => ({
309 + data: { category: input.category },
310 + error: null,
311 + successful: true,
312 + }));
313 +
314 + const toolWithDefaults = CustomTool({
315 + slug: 'DEFAULTS_TOOL',
316 + name: 'Defaults',
317 + description: 'Tool with default values',
318 + inputParams: z.object({ category: z.string().default('all') }),
319 + execute: defaultExecute,
320 + });
321 +
322 + const session = createSession(mockClient, [toolWithDefaults]);
323 + const result = await session.execute('DEFAULTS_TOOL', {});
324 +
325 + // Zod default should be applied
326 + expect(defaultExecute).toHaveBeenCalledWith(
327 + { category: 'all' },
328 + expect.anything()
329 + );
330 + expect(result.data).toEqual({ category: 'all' });
331 + });
332 +
333 + it('should return validation error for invalid input', async () => {
334 + const strictTool = CustomTool({
335 + slug: 'STRICT_TOOL',
336 + name: 'Strict',
337 + description: 'Tool with strict input',
338 + inputParams: z.object({ count: z.number() }),
339 + execute: vi.fn(),
340 + });
341 +
342 + const session = createSession(mockClient, [strictTool]);
343 + const result = await session.execute('STRICT_TOOL', { count: 'not-a-number' });
344 +
345 + expect(result.error).toContain('Input validation failed');
346 + expect(result.data).toEqual({});
347 + });
348 +
349 it('should handle non-Error throws gracefully', async () => {
350 const throwingTool = CustomTool({
351 slug: 'STRING_THROW',