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 23, 01:45 UTC
Add discriminated unions for get tools
haxzie committed
May 29, 2025 at 12:56 UTC
93b9c69c8444282109fad3c2a2f653a5298e4eb4
6 files changed
+242
-35
docs/api/tools.md
+62
-7
@@ -175,14 +175,69 @@ const tool = await composio.tools.getRawComposioToolBySlug('default', 'github');
175
### ToolListParams
176
177
```typescript
178
-interface ToolListParams {
179
- tools?: string[]; // List of tool slugs to filter by
178
+// You must provide one of the following parameter combinations:
179
+// 1. tools array only
180
+// 2. toolkits with optional important flag
181
+// 3. toolkits with search functionality
182
+
183
+type ToolsOnlyParams = {
184
+ tools: string[]; // List of tool slugs to filter by
185
+ toolkits?: never; // Cannot be used with tools
186
+ important?: never;
187
+ cursor?: never;
188
+ limit?: never;
189
+ search?: never;
190
+};
191
+
192
+type ToolkitsOnlyParams = {
193
+ tools?: never; // Cannot be used with toolkits
194
+ toolkits: string[]; // List of toolkit slugs to filter by
195
+ important?: boolean; // Filter for important tools
196
cursor?: string; // Pagination cursor
181
- important?: string; // Filter for important tools
182
- limit?: string; // Limit the number of results
183
- search?: string; // Search term
184
- toolkits?: string[]; // List of toolkit slugs to filter by
185
-}
197
+ limit?: number; // Limit the number of results
198
+ search?: never; // Cannot be used with important flag
199
+};
200
+
201
+type ToolkitSearchOnlyParams = {
202
+ tools?: never; // Cannot be used with search
203
+ toolkits?: string[]; // Optional list of toolkit slugs to filter by
204
+ important?: never; // Cannot be used with search
205
+ cursor?: string; // Pagination cursor
206
+ limit?: number; // Limit the number of results
207
+ search: string; // Search term
208
+};
209
+
210
+type ToolListParams = ToolsOnlyParams | ToolkitsOnlyParams | ToolkitSearchOnlyParams;
211
+```
212
+
213
+Note: The parameters are organized into three mutually exclusive combinations:
214
+
215
+1. Using `tools` array to fetch specific tools by their slugs
216
+2. Using `toolkits` with optional `important` flag to fetch tools from specific toolkits
217
+3. Using `search` with optional `toolkits` to search for tools by name/description
218
+
219
+Examples:
220
+
221
+```typescript
222
+// Get specific tools by slug
223
+const specificTools = await composio.tools.get('default', {
224
+ tools: ['GITHUB_GET_REPO', 'GITHUB_LIST_ISSUES'],
225
+});
226
+
227
+// Get all tools from specific toolkits
228
+const toolkitTools = await composio.tools.get('default', {
229
+ toolkits: ['github', 'gitlab'],
230
+ important: true,
231
+ limit: 10,
232
+});
233
+
234
+// Search for tools across all or specific toolkits
235
+const searchResults = await composio.tools.get('default', {
236
+ search: 'repository',
237
+ toolkits: ['github'], // optional
238
+ limit: 10,
239
+ cursor: 'next-page',
240
+});
241
```
242
243
### ToolExecuteParams
packages/core/src/models/CustomTools.ts
+1
-1
@@ -70,7 +70,7 @@ export class CustomTools {
70
outputParameters: {
71
type: 'object',
72
title: `Response for ${name}`,
73
- properties: [],
73
+ properties: {},
74
},
75
tags: [],
76
toolkit: { name: 'custom', slug: 'custom' },
packages/core/src/models/Tools.ts
+30
-11
@@ -248,28 +248,47 @@ export class Tools<
248
*/
249
async getRawComposioTools(
250
userId: string,
251
- query: ToolListParams = {},
251
+ query: ToolListParams,
252
modifier?: TransformToolSchemaModifier
253
): Promise<ToolList> {
254
+ if ('tools' in query && 'toolkits' in query) {
255
+ throw new ValidationError(
256
+ 'Invalid tool list parameters. You should not use tools and toolkits filter together.'
257
+ );
258
+ }
259
+
260
const queryParams = ToolListParamsSchema.safeParse(query);
261
if (queryParams.error) {
262
throw new ValidationError('Invalid tool list parameters', {
263
cause: queryParams.error,
264
});
265
}
266
+ // check if the query params contains atleast one of the following: tools, toolkits, search
267
+ if (
268
+ !(
269
+ 'tools' in queryParams.data ||
270
+ 'toolkits' in queryParams.data ||
271
+ 'search' in queryParams.data
272
+ )
273
+ ) {
274
+ throw new ValidationError(
275
+ 'Invalid tool list parameters, atleast one of the following parameters is required: tools, toolkits, search'
276
+ );
277
+ }
278
279
const tools = await this.client.tools.list({
262
- tool_slugs: queryParams.data.tools?.join(','),
263
- toolkit_slug: queryParams.data.toolkits?.join(','),
264
- cursor: queryParams.data.cursor,
280
+ tool_slugs: 'tools' in queryParams.data ? queryParams.data.tools?.join(',') : undefined,
281
+ toolkit_slug:
282
+ 'toolkits' in queryParams.data ? queryParams.data.toolkits?.join(',') : undefined,
283
+ cursor: 'cursor' in queryParams.data ? queryParams.data.cursor : undefined,
284
important:
266
- typeof queryParams.data.important === 'undefined'
267
- ? undefined
268
- : queryParams.data.important
285
+ 'important' in queryParams.data
286
+ ? queryParams.data.important
287
? 'true'
270
- : 'false',
271
- limit: queryParams.data.limit?.toString(),
272
- search: queryParams.data.search,
288
+ : 'false'
289
+ : undefined,
290
+ limit: 'limit' in queryParams.data ? queryParams.data.limit?.toString() : undefined,
291
+ search: 'search' in queryParams.data ? queryParams.data.search : undefined,
292
});
293
294
if (!tools) {
@@ -285,7 +304,7 @@ export class Tools<
304
// console.warn('No connected accounts found for the given tools');
305
// }
306
const customTools = await this.customTools.getCustomTools({
288
- toolSlugs: queryParams.data.tools,
307
+ toolSlugs: 'tools' in queryParams.data ? queryParams.data.tools : undefined,
308
});
309
310
let modifiedTools = [...caseTransformedTools, ...customTools];
packages/core/src/types/tool.types.ts
+54
-7
@@ -78,18 +78,65 @@ export type ToolListResponse = z.infer<typeof ToolListResponseSchema>;
78
*/
79
export type ToolList = Array<Tool>;
80
81
-/**
82
- * ToolListParams is the parameters for the list of tools.
83
- */
84
-export const ToolListParamsSchema = z.object({
85
- tools: z.array(z.string()).optional(),
86
- cursor: z.string().optional(),
81
+export const ToolListFilterByToolsSchema = z.object({
82
+ tools: z.array(z.string()),
83
+});
84
+export type ToolListFilterByTools = z.infer<typeof ToolListFilterByToolsSchema>;
85
+
86
+export const ToolListFilterByToolkitsSchema = z.object({
87
+ toolkits: z.array(z.string()),
88
important: z.boolean().optional(),
89
+ cursor: z.string().optional(),
90
limit: z.number().optional(),
91
search: z.string().optional(),
92
+});
93
+export type ToolListFilterByToolkits = z.infer<typeof ToolListFilterByToolkitsSchema>;
94
+
95
+export const ToolListFilterBySearchSchema = z.object({
96
+ search: z.string(),
97
toolkits: z.array(z.string()).optional(),
98
+ cursor: z.string().optional(),
99
+ limit: z.number().optional(),
100
});
92
-export type ToolListParams = z.infer<typeof ToolListParamsSchema>;
101
+export type ToolListFilterBySearch = z.infer<typeof ToolListFilterBySearchSchema>;
102
+
103
+type ToolsOnlyParams = {
104
+ tools: string[];
105
+ toolkits?: never;
106
+ important?: never;
107
+ cursor?: never;
108
+ limit?: never;
109
+ search?: never;
110
+};
111
+
112
+type ToolkitsOnlyParams = {
113
+ tools?: never;
114
+ toolkits?: string[];
115
+ important?: boolean;
116
+ cursor?: string;
117
+ limit?: number;
118
+ search?: never;
119
+};
120
+
121
+type ToolkitSearchOnlyParams = {
122
+ tools?: never;
123
+ toolkits?: string[];
124
+ important?: never;
125
+ cursor?: string;
126
+ limit?: number;
127
+ search?: string;
128
+};
129
+/**
130
+ * ToolListParams is the parameters for the list of tools.
131
+ * You must provide either tools or toolkits, but not both.
132
+ */
133
+export type ToolListParams = ToolsOnlyParams | ToolkitsOnlyParams | ToolkitSearchOnlyParams;
134
+
135
+export const ToolListParamsSchema = z.union([
136
+ ToolListFilterByToolsSchema,
137
+ ToolListFilterByToolkitsSchema,
138
+ ToolListFilterBySearchSchema,
139
+]);
140
141
/**
142
* CustomAuthParams is the parameters for the custom authentication.
packages/core/test/tools/modifiers.test.ts
+5
-1
@@ -53,7 +53,11 @@ describe('Tools Modifiers', () => {
53
totalPages: 1,
54
});
55
56
- const result = await context.tools.getRawComposioTools(userId, {}, schemaModifier);
56
+ const result = await context.tools.getRawComposioTools(
57
+ userId,
58
+ { tools: ['TOOL1', 'TOOL2'] },
59
+ schemaModifier
60
+ );
61
62
expect(schemaModifier).toHaveBeenCalledTimes(2);
63
expect(result[0].name).toContain('Modified');
packages/core/test/tools/tools.test.ts
+90
-8
@@ -10,6 +10,7 @@ import {
10
mockToolExecution,
11
createSchemaModifier,
12
} from '../utils/toolExecuteUtils';
13
+import { ValidationError } from '../../src/errors/ValidationErrors';
14
15
describe('Tools', () => {
16
const context = createTestContext();
@@ -42,7 +43,7 @@ describe('Tools', () => {
43
totalPages: 1,
44
});
45
45
- const result = await context.tools.getRawComposioTools(userId);
46
+ const result = await context.tools.getRawComposioTools(userId, { tools: ['TEST_TOOL'] });
47
48
expect(mockClient.tools.list).toHaveBeenCalledTimes(1);
49
expect(result).toHaveLength(1);
@@ -54,7 +55,6 @@ describe('Tools', () => {
55
const userId = 'test-user';
56
const query = {
57
tools: ['TOOL1', 'TOOL2'],
57
- limit: 10,
58
};
59
60
mockClient.tools.list.mockResolvedValueOnce({
@@ -66,7 +66,7 @@ describe('Tools', () => {
66
67
expect(mockClient.tools.list).toHaveBeenCalledWith({
68
tool_slugs: 'TOOL1,TOOL2',
69
- limit: '10',
69
+ limit: undefined,
70
cursor: undefined,
71
important: undefined,
72
search: undefined,
@@ -74,6 +74,57 @@ describe('Tools', () => {
74
});
75
});
76
77
+ it('should handle toolkit query parameters correctly', async () => {
78
+ const userId = 'test-user';
79
+ const query = {
80
+ toolkits: ['github'],
81
+ limit: 10,
82
+ important: true,
83
+ };
84
+
85
+ mockClient.tools.list.mockResolvedValueOnce({
86
+ items: [toolMocks.rawTool],
87
+ totalPages: 1,
88
+ });
89
+
90
+ await context.tools.getRawComposioTools(userId, query);
91
+
92
+ expect(mockClient.tools.list).toHaveBeenCalledWith({
93
+ tool_slugs: undefined,
94
+ toolkit_slug: 'github',
95
+ limit: '10',
96
+ cursor: undefined,
97
+ important: 'true',
98
+ search: undefined,
99
+ });
100
+ });
101
+
102
+ it('should handle toolkit search parameters correctly', async () => {
103
+ const userId = 'test-user';
104
+ const query = {
105
+ toolkits: ['github'],
106
+ search: 'test',
107
+ limit: 10,
108
+ cursor: 'next-page',
109
+ };
110
+
111
+ mockClient.tools.list.mockResolvedValueOnce({
112
+ items: [toolMocks.rawTool],
113
+ totalPages: 1,
114
+ });
115
+
116
+ await context.tools.getRawComposioTools(userId, query);
117
+
118
+ expect(mockClient.tools.list).toHaveBeenCalledWith({
119
+ tool_slugs: undefined,
120
+ toolkit_slug: 'github',
121
+ limit: '10',
122
+ cursor: 'next-page',
123
+ important: undefined,
124
+ search: 'test',
125
+ });
126
+ });
127
+
128
it('should transform tool case correctly', async () => {
129
const userId = 'test-user';
130
@@ -82,7 +133,7 @@ describe('Tools', () => {
133
totalPages: 1,
134
});
135
85
- const result = await context.tools.getRawComposioTools(userId);
136
+ const result = await context.tools.getRawComposioTools(userId, { tools: ['TEST_TOOL'] });
137
138
expect(result[0].inputParameters).toEqual(toolMocks.transformedTool.inputParameters);
139
expect(result[0].outputParameters).toEqual(toolMocks.transformedTool.outputParameters);
@@ -99,7 +150,7 @@ describe('Tools', () => {
150
const getCustomToolsSpy = vi.spyOn(context.tools['customTools'], 'getCustomTools');
151
getCustomToolsSpy.mockResolvedValueOnce([toolMocks.customTool as unknown as Tool]);
152
102
- const result = await context.tools.getRawComposioTools(userId);
153
+ const result = await context.tools.getRawComposioTools(userId, { tools: ['TEST_TOOL'] });
154
155
expect(result).toHaveLength(2);
156
expect(result[1].slug).toEqual(toolMocks.customTool.slug);
@@ -116,7 +167,11 @@ describe('Tools', () => {
167
totalPages: 1,
168
});
169
119
- const result = await context.tools.getRawComposioTools(userId, {}, schemaModifier);
170
+ const result = await context.tools.getRawComposioTools(
171
+ userId,
172
+ { tools: ['TEST_TOOL'] },
173
+ schemaModifier
174
+ );
175
176
expect(schemaModifier).toHaveBeenCalled();
177
expect(result[0].description).toEqual('Modified description');
@@ -131,8 +186,35 @@ describe('Tools', () => {
186
totalPages: 1,
187
});
188
134
- await expect(context.tools.getRawComposioTools(userId, {}, invalidModifier)).rejects.toThrow(
135
- 'Invalid schema modifier. Not a function.'
189
+ await expect(
190
+ context.tools.getRawComposioTools(
191
+ userId,
192
+ {
193
+ toolkits: ['invalid'],
194
+ },
195
+ invalidModifier
196
+ )
197
+ ).rejects.toThrow('Invalid schema modifier. Not a function.');
198
+ });
199
+
200
+ it('should throw a validation error when both tools and toolkits are provided', async () => {
201
+ const userId = 'test-user';
202
+ const invalidQuery = {
203
+ tools: ['TOOL1'],
204
+ toolkits: ['github'],
205
+ } as any;
206
+
207
+ await expect(context.tools.getRawComposioTools(userId, invalidQuery)).rejects.toThrow(
208
+ 'Invalid tool list parameters'
209
+ );
210
+ });
211
+
212
+ it('should throw a validation error when no required parameters are provided', async () => {
213
+ const userId = 'test-user';
214
+ const emptyQuery = {} as any;
215
+
216
+ await expect(context.tools.getRawComposioTools(userId, emptyQuery)).rejects.toThrow(
217
+ ValidationError
218
);
219
});
220
});