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.

Feat add strict mode in tool fetching (#32)

Musthaq Ahamad208e320b2732
diff --git a/.changeset/wild-items-argue.md b/.changeset/wild-items-argue.md
new file mode 100644
index 000000000..88a2d3651
--- /dev/null
+++ b/.changeset/wild-items-argue.md
@@ -0,0 +1,13 @@
+---
+'@composio/vercel': patch
+'@composio/core': patch
+'@composio/json-schema-to-zod': patch
+'@composio/anthropic': patch
+'@composio/cloudflare': patch
+'@composio/google': patch
+'@composio/langchain': patch
+'@composio/mastra': patch
+'@composio/openai': patch
+---
+
+Update json schema transformations issues related to strict mode
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 5ef1c5fda..7894007aa 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -48,10 +48,11 @@ jobs:
commit: 'Release: update version'
title: 'Release: update version'
- - name: Format published packages for Slack
- id: format_packages
+ - name: Set Version Info
+ if: steps.changesets.outputs.published == 'true'
run: |
- echo "SLACK_VERSIONS=$(echo '${{ steps.changesets.outputs.publishedPackages }}' | jq -r '.[] | \"⚡️ \\(.name) -> \\(.version)\"' | paste -sd '\\n' -)" >> $GITHUB_ENV
+ VERSIONS=$(pnpm changeset status --json | jq -r '.releases[] | "⚡️ \(.name) -> \(.version)"' || echo "No version info available")
+ echo "SLACK_VERSIONS=$VERSIONS" >> $GITHUB_ENV
- name: Send Slack Notification
if: steps.changesets.outputs.published == 'true'
diff --git a/docs/api/tools.md b/docs/api/tools.md
index 04171d72f..4862f4e6c 100644
--- a/docs/api/tools.md
+++ b/docs/api/tools.md
@@ -195,7 +195,8 @@ type ToolkitsOnlyParams = {
important?: boolean; // Filter for important tools
cursor?: string; // Pagination cursor
limit?: number; // Limit the number of results
- search?: never; // Cannot be used with important flag
+ search?: never; // Cannot be used with important flag,
+ strict?: boolean; // Remove non-required properties from input/output parameters
};
type ToolkitSearchOnlyParams = {
@@ -205,6 +206,7 @@ type ToolkitSearchOnlyParams = {
cursor?: string; // Pagination cursor
limit?: number; // Limit the number of results
search: string; // Search term
+ strict?: boolean; // Remove non-required properties from input/output parameters
};
type ToolListParams = ToolsOnlyParams | ToolkitsOnlyParams | ToolkitSearchOnlyParams;
@@ -216,6 +218,8 @@ Note: The parameters are organized into three mutually exclusive combinations:
2. Using `toolkits` with optional `important` flag to fetch tools from specific toolkits
3. Using `search` with optional `toolkits` to search for tools by name/description
+The `strict` parameter is a special flag that can be used with any of the above combinations. When set to `true`, it removes all non-required properties from both input and output parameters of the tools. This is particularly useful when working with providers that have strict parameter requirements, such as VercelAISDK, OpenAI, [where all fields must be marked as required.](https://platform.openai.com/docs/guides/function-calling#strict-mode)
+
Examples:
```typescript
@@ -224,11 +228,12 @@ const specificTools = await composio.tools.get('default', {
tools: ['GITHUB_GET_REPO', 'GITHUB_LIST_ISSUES'],
});
-// Get all tools from specific toolkits
+// Get all tools from specific toolkits with strict mode enabled
const toolkitTools = await composio.tools.get('default', {
toolkits: ['github', 'gitlab'],
important: true,
limit: 10,
+ strict: true, // This will remove all non-required properties from tool parameters
});
// Search for tools across all or specific toolkits
diff --git a/packages/core/src/models/Tools.ts b/packages/core/src/models/Tools.ts
index 82de4ceaa..48b4047a2 100644
--- a/packages/core/src/models/Tools.ts
+++ b/packages/core/src/models/Tools.ts
@@ -40,6 +40,7 @@ import {
} from '../errors/ToolErrors';
import { ValidationError } from '../errors/ValidationErrors';
import { telemetry } from '../telemetry/Telemetry';
+import { removeNonRequiredProperties } from '../utils/jsonSchema';
/**
* This class is used to manage tools in the Composio SDK.
* It provides methods to list, get, and execute tools.
@@ -454,7 +455,19 @@ export class Tools<
return this.provider.wrapTools([tool], executeToolFn) as TToolCollection;
} else {
// if the first argument is an object, get a list of tools
- const tools = await this.getRawComposioTools(userId, arg2, options?.modifySchema);
+ let tools = await this.getRawComposioTools(userId, arg2, options?.modifySchema);
+ // if the strict flag is true, remove all non-required properties from the input and output parameters
+ if (arg2?.strict) {
+ tools = tools.map(tool => ({
+ ...tool,
+ inputParameters: tool.inputParameters
+ ? removeNonRequiredProperties(tool.inputParameters)
+ : undefined,
+ outputParameters: tool.outputParameters
+ ? removeNonRequiredProperties(tool.outputParameters)
+ : undefined,
+ }));
+ }
return this.provider.wrapTools(tools, executeToolFn) as TToolCollection;
}
}
diff --git a/packages/core/src/types/tool.types.ts b/packages/core/src/types/tool.types.ts
index 6c0356bb0..e4206aa43 100644
--- a/packages/core/src/types/tool.types.ts
+++ b/packages/core/src/types/tool.types.ts
@@ -89,6 +89,12 @@ export const ToolListFilterByToolkitsSchema = z.object({
cursor: z.string().optional(),
limit: z.number().optional(),
search: z.string().optional(),
+ strict: z
+ .boolean()
+ .optional()
+ .describe(
+ 'If true, all the non-required fields will be removed from the input/output params of the tool'
+ ),
});
export type ToolListFilterByToolkits = z.infer<typeof ToolListFilterByToolkitsSchema>;
@@ -97,6 +103,12 @@ export const ToolListFilterBySearchSchema = z.object({
toolkits: z.array(z.string()).optional(),
cursor: z.string().optional(),
limit: z.number().optional(),
+ strict: z
+ .boolean()
+ .optional()
+ .describe(
+ 'If true, all the non-required fields will be removed from the input/output params of the tool'
+ ),
});
export type ToolListFilterBySearch = z.infer<typeof ToolListFilterBySearchSchema>;
@@ -107,6 +119,7 @@ type ToolsOnlyParams = {
cursor?: never;
limit?: never;
search?: never;
+ strict?: boolean;
};
type ToolkitsOnlyParams = {
@@ -116,6 +129,7 @@ type ToolkitsOnlyParams = {
cursor?: string;
limit?: number;
search?: never;
+ strict?: boolean;
};
type ToolkitSearchOnlyParams = {
@@ -125,6 +139,7 @@ type ToolkitSearchOnlyParams = {
cursor?: string;
limit?: number;
search?: string;
+ strict?: boolean;
};
/**
* ToolListParams is the parameters for the list of tools.
diff --git a/packages/core/src/utils/jsonSchema.ts b/packages/core/src/utils/jsonSchema.ts
index 75ac4476a..68c04e47f 100644
--- a/packages/core/src/utils/jsonSchema.ts
+++ b/packages/core/src/utils/jsonSchema.ts
@@ -2,9 +2,32 @@ import { z } from 'zod';
import { JsonSchemaToZodError } from '../errors';
import { jsonSchemaToZod } from '@composio/json-schema-to-zod';
+/**
+ * Removes all non-required properties from the schema
+ *
+ * if no items are required, the schema is returned as is
+ * @param schema - The JSON schema to remove non-required properties from
+ * @returns The JSON schema with all non-required properties removed
+ */
+export const removeNonRequiredProperties = <
+ T extends { type: 'object'; properties: Record<string, unknown>; required?: string[] },
+>(
+ schema: T
+): T => {
+ if (schema && schema.type === 'object' && (schema.required as string[])?.length) {
+ schema.properties = Object.fromEntries(
+ Object.entries(schema.properties || {}).filter(([key]) =>
+ (schema.required as string[]).includes(key)
+ )
+ );
+ }
+ return schema as T;
+};
+
/**
* Convert a JSON schema to a Zod schema
* @param jsonSchema - The JSON schema to convert
+ * @param strict - Eliminates all non-required properties from the schema
* @returns The Zod schema
*
* @throws {JsonSchemaToZodError} If the JSON schema is invalid
@@ -20,13 +43,46 @@ import { jsonSchemaToZod } from '@composio/json-schema-to-zod';
*
* console.log(zodSchema);
* ```
+ *
+ * @example
+ * ```ts
+ * const zodSchema = jsonSchemaToZodSchema({
+ * type: 'object',
+ * properties: {
+ * name: { type: 'string' },
+ * age: { type: 'number' },
+ * },
+ * required: ['name'],
+ * }, { strict: true });
+ *
+ * console.log(zodSchema);
+ *
+ * // Output:
+ * // z.object({
+ * // name: z.string(),
+ * // })
+ * ```
*/
export function jsonSchemaToZodSchema(
- jsonSchema: Record<string, unknown>
+ jsonSchema: Record<string, unknown>,
+ { strict }: { strict?: boolean } = {
+ strict: false,
+ }
): z.ZodObject<Record<string, z.ZodTypeAny>> {
try {
+ let schema = jsonSchema;
+ // Remove all non-required properties from the schema if strict is true
+ if (strict && schema) {
+ schema = removeNonRequiredProperties(
+ schema as {
+ type: 'object';
+ properties: Record<string, unknown>;
+ required?: string[] | undefined;
+ }
+ );
+ }
// Convert the JSON schema properties to Zod schema
- return jsonSchemaToZod(jsonSchema);
+ return jsonSchemaToZod(schema);
} catch (error) {
throw new JsonSchemaToZodError('Failed to convert JSON Schema to Zod Schema', {
cause: error,
diff --git a/packages/core/test/tools/tools.test.ts b/packages/core/test/tools/tools.test.ts
index 5f1bdbdc3..c1d2e92db 100644
--- a/packages/core/test/tools/tools.test.ts
+++ b/packages/core/test/tools/tools.test.ts
@@ -329,6 +329,136 @@ describe('Tools', () => {
expect(getRawComposioToolBySlugSpy).toHaveBeenCalledWith(userId, slug, schemaModifier);
});
+
+ it('should remove non-required properties when strict mode is enabled', async () => {
+ const userId = 'test-user';
+ const filters = {
+ tools: ['TOOL1'],
+ strict: true,
+ };
+
+ const toolWithOptionalParams = {
+ ...toolMocks.transformedTool,
+ inputParameters: {
+ type: 'object',
+ required: ['requiredParam'],
+ properties: {
+ requiredParam: {
+ type: 'string',
+ description: 'Required parameter',
+ },
+ optionalParam: {
+ type: 'string',
+ description: 'Optional parameter',
+ },
+ },
+ },
+ outputParameters: {
+ type: 'object',
+ required: ['requiredOutput'],
+ properties: {
+ requiredOutput: {
+ type: 'string',
+ description: 'Required output',
+ },
+ optionalOutput: {
+ type: 'string',
+ description: 'Optional output',
+ },
+ },
+ },
+ };
+
+ const getRawComposioToolsSpy = vi.spyOn(context.tools, 'getRawComposioTools');
+ getRawComposioToolsSpy.mockResolvedValueOnce([toolWithOptionalParams as unknown as Tool]);
+
+ context.mockProvider.wrapTools.mockReturnValueOnce('wrapped-tools-collection');
+
+ const result = await context.tools.get(userId, filters);
+
+ expect(context.mockProvider.wrapTools).toHaveBeenCalledWith(
+ [
+ {
+ ...toolWithOptionalParams,
+ inputParameters: {
+ type: 'object',
+ required: ['requiredParam'],
+ properties: {
+ requiredParam: {
+ type: 'string',
+ description: 'Required parameter',
+ },
+ },
+ },
+ outputParameters: {
+ type: 'object',
+ required: ['requiredOutput'],
+ properties: {
+ requiredOutput: {
+ type: 'string',
+ description: 'Required output',
+ },
+ },
+ },
+ },
+ ],
+ expect.any(Function)
+ );
+ expect(result).toEqual('wrapped-tools-collection');
+ });
+
+ it('should not modify properties when strict mode is disabled', async () => {
+ const userId = 'test-user';
+ const filters = {
+ tools: ['TOOL1'],
+ strict: false,
+ };
+
+ const toolWithOptionalParams = {
+ ...toolMocks.transformedTool,
+ inputParameters: {
+ type: 'object',
+ required: ['requiredParam'],
+ properties: {
+ requiredParam: {
+ type: 'string',
+ description: 'Required parameter',
+ },
+ optionalParam: {
+ type: 'string',
+ description: 'Optional parameter',
+ },
+ },
+ },
+ outputParameters: {
+ type: 'object',
+ required: ['requiredOutput'],
+ properties: {
+ requiredOutput: {
+ type: 'string',
+ description: 'Required output',
+ },
+ optionalOutput: {
+ type: 'string',
+ description: 'Optional output',
+ },
+ },
+ },
+ };
+
+ const getRawComposioToolsSpy = vi.spyOn(context.tools, 'getRawComposioTools');
+ getRawComposioToolsSpy.mockResolvedValueOnce([toolWithOptionalParams as unknown as Tool]);
+
+ context.mockProvider.wrapTools.mockReturnValueOnce('wrapped-tools-collection');
+
+ const result = await context.tools.get(userId, filters);
+
+ expect(context.mockProvider.wrapTools).toHaveBeenCalledWith(
+ [toolWithOptionalParams],
+ expect.any(Function)
+ );
+ expect(result).toEqual('wrapped-tools-collection');
+ });
});
describe('execute', () => {
diff --git a/packages/providers/vercel/README.md b/packages/providers/vercel/README.md
index 56a5a440f..dad7fb19c 100644
--- a/packages/providers/vercel/README.md
+++ b/packages/providers/vercel/README.md
@@ -178,6 +178,27 @@ const provider = new VercelProvider({
});
```
+### Strict Mode
+
+When using tools with Vercel AI SDK, you might need to ensure that all tool parameters are marked as required. This is because Vercel AI SDK's function calling implementation requires all fields to be marked as required. You can enable strict mode when fetching tools to automatically remove all non-required properties from both input and output parameters:
+
+```typescript
+// Get tools with strict mode enabled
+const tools = await composio.tools.get('user123', {
+ toolkits: ['gmail', 'googlecalendar'],
+ strict: true, // This will remove all non-required properties from tool parameters
+});
+
+// Use these tools with Vercel AI SDK
+const stream = streamText({
+ model: openai('gpt-4'),
+ messages,
+ tools, // These tools will only have required parameters
+});
+```
+
+For more information about strict mode in function calling, see [OpenAI's documentation](https://platform.openai.com/docs/guides/function-calling#strict-mode).
+
## API Reference
### VercelProvider Class