Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Add preprocessor hooks
haxzie committed
May 5, 2025 at 18:53 UTC
1107dd593b113c0f085d6235a9f03423291bfe12
19 files changed
+469
-71
examples/langchain/src/index.ts
+3
@@ -11,3 +11,6 @@ const composio = new Composio({
11
const tools = await composio.getTools();
12
13
console.log(tools);
14
+
15
+
16
+
examples/openai/src/index.ts
+2
-1
@@ -11,9 +11,10 @@ const composio = new Composio({
11
apiKey: process.env.COMPOSIO_API_KEY,
12
});
13
14
+
15
// Fetch all the tools from the Composio API
16
// these tools are automatically typed and wrapped with the OpenAI Toolset
16
-const tool = await composio.getTool('HACKERNEWS_GET_USER');
17
+const tool = await composio.getToolBySlug('HACKERNEWS_GET_USER');
18
19
const task = "Fetch the details of the user 'haxzie'";
20
examples/vercel/package.json
+1
-1
@@ -14,7 +14,7 @@
14
"dependencies": {
15
"@ai-sdk/openai": "^1.3.16",
16
"@composio/core": "workspace:*",
17
- "@composio/vercel": "workspace:0.1.0-rc.1",
17
+ "@composio/vercel": "workspace:*",
18
"ai": "^4.3.8"
19
},
20
"devDependencies": {
examples/vercel/src/example.ts
new
+40
@@ -0,0 +1,40 @@
1
+import { Composio } from "@composio/core";
2
+import { generateText } from "ai";
3
+import { openai } from "@ai-sdk/openai";
4
+import { VercelToolset } from "@composio/vercel";
5
+
6
+const composio = new Composio({
7
+ apiKey: process.env.COMPOSIO_API_KEY,
8
+ toolset: new VercelToolset(),
9
+ userId: process.env.COMPOSIO_USER_ID, // pass the user at the global level
10
+});
11
+
12
+
13
+
14
+/**
15
+ * For tools that have manual execute, we need to pass userId while executing the tool
16
+ */
17
+// @ts-ignore
18
+const result = composio.toolset.handleToolCall(tool, { userId: process.env.COMPOSIO_USER_ID });
19
+
20
+/**
21
+ * For tools that have auto execute, we have to pass userId (required)
22
+ */
23
+const tools = await composio.getTools({
24
+ // @ts-ignore
25
+ filters: {},
26
+ userId: process.env.COMPOSIO_USER_ID, // this is mandatory
27
+ connectedAccountId: process.env.COMPOSIO_CONNECTED_ACCOUNT_ID, // this is optional
28
+});
29
+
30
+
31
+/**
32
+ * If the user wants to change the userId for a specifically fetched tool
33
+ */
34
+// @ts-ignore
35
+const tool = await composio.wrapTool(
36
+ // @ts-ignore
37
+ hackerNewsTool,
38
+ { userId: process.env.COMPOSIO_USER_ID }
39
+);
40
+
examples/vercel/src/index.ts
+1
-1
@@ -8,7 +8,7 @@ const composio = new Composio({
8
toolset: new VercelToolset()
9
});
10
11
-const hackerNewsTool = await composio.toolset.getTool("HACKERNEWS_GET_FRONTPAGE");
11
+const hackerNewsTool = await composio.toolset.getToolBySlug("HACKERNEWS_GET_FRONTPAGE");
12
13
const MessageRoles = {
14
USER: "user",
packages/core/src/composio.ts
+17
-3
@@ -16,6 +16,7 @@ import { IS_DEVELOPMENT_OR_CI } from './utils/constants';
16
import { checkForLatestVersionFromNPM } from './utils/version';
17
import { OpenAIToolset } from './toolset/OpenAIToolset';
18
import { version } from '../package.json';
19
+import { Modifiers } from './models/Modifiers';
20
21
export type ComposioConfig<TToolset extends BaseComposioToolset<unknown, unknown> = OpenAIToolset> =
22
{
@@ -66,6 +67,10 @@ export class Composio<TToolset extends BaseComposioToolset<unknown, unknown> = O
67
toolset: TToolset;
68
authConfigs: AuthConfigs;
69
connectedAccounts: ConnectedAccounts;
70
+ modifiers: Modifiers;
71
+ useBeforeToolExecute: Modifiers['useBeforeToolExecute'];
72
+ useAfterToolExecute: Modifiers['useAfterToolExecute'];
73
+ useTransformToolSchema: Modifiers['useTransformToolSchema'];
74
75
/**
76
* @param {Object} config Configuration for the Composio SDK.
@@ -121,10 +126,19 @@ export class Composio<TToolset extends BaseComposioToolset<unknown, unknown> = O
126
this.toolset = (config.toolset ?? new ComposioToolset()) as TToolset;
127
this.toolset.setClient(this);
128
129
+ /**
130
+ * Modifiers are used to modify the tools and toolkits.
131
+ * Initialize the modifiers into global scope.
132
+ */
133
+ this.modifiers = new Modifiers();
134
+ this.useBeforeToolExecute = this.modifiers.useBeforeToolExecute;
135
+ this.useAfterToolExecute = this.modifiers.useAfterToolExecute;
136
+ this.useTransformToolSchema = this.modifiers.useTransformToolSchema;
137
+
138
/**
139
* Initialize all the models with composio client.
140
*/
127
- this.tools = new Tools(this.client);
141
+ this.tools = new Tools(this.client, this.modifiers);
142
this.toolkits = new Toolkits(this.client);
143
this.triggers = new Triggers(this.client);
144
this.authConfigs = new AuthConfigs(this.client);
@@ -195,8 +209,8 @@ export class Composio<TToolset extends BaseComposioToolset<unknown, unknown> = O
209
* Generic function to get all the tools
210
* @returns {Promise<ReturnType<TToolset["getTool"]>>} Promise with list of tools
211
*/
198
- async getTool(slug: string): Promise<ReturnType<TToolset['getTool']>> {
199
- return this.toolset.getTool(slug) as Promise<ReturnType<TToolset['getTool']>>;
212
+ async getToolBySlug(slug: string): Promise<ReturnType<TToolset['getToolBySlug']>> {
213
+ return this.toolset.getToolBySlug(slug) as Promise<ReturnType<TToolset['getToolBySlug']>>;
214
}
215
216
/**
packages/core/src/models/Modifiers.ts
new
+110
@@ -0,0 +1,110 @@
1
+/**
2
+ * @fileoverview Modifiers class for Composio SDK, used to manage tool execution modifiers.
3
+ *
4
+ * @author Musthaq Ahamad <musthaq@composio.dev>
5
+ * @date 2025-05-05
6
+ * @module Modifiers
7
+ */
8
+import { AfterToolExecuteModifier, BeforeToolExecuteModifer, TransformToolSchemaModifier } from "../types/modifiers.types";
9
+import { InstrumentedInstance } from "../types/telemetry.types";
10
+import { Tool, ToolExecuteParams, ToolExecuteResponse } from "../types/tool.types";
11
+
12
+/**
13
+ * Modifiers class
14
+ *
15
+ * This class is used to manage the modifiers for the tools in the Composio SDK.
16
+ * It provides methods to add before and after execution modifiers, as well as schema transformation modifiers.
17
+ */
18
+export class Modifiers implements InstrumentedInstance {
19
+ readonly FILE_NAME: string = 'core/models/Modifiers.ts';
20
+ private beforeToolExecuteModifiers: Map<string, Set<BeforeToolExecuteModifer>> = new Map();
21
+ private afterToolExecuteModifiers: Map<string, Set<AfterToolExecuteModifier>> = new Map();
22
+ private transformToolSchemaModifiers: Map<string, Set<TransformToolSchemaModifier>> = new Map();
23
+
24
+ constructor() {}
25
+
26
+ /**
27
+ * Method to modify the tool schema before it is executed.
28
+ * @param {string} toolSlug slug of the tool to be modified
29
+ * @param modifier function to modify the tool execution parameters
30
+ */
31
+ useBeforeToolExecute(toolSlug: string, modifier: BeforeToolExecuteModifer) {
32
+ if (!this.beforeToolExecuteModifiers.has(toolSlug)) {
33
+ this.beforeToolExecuteModifiers.set(toolSlug, new Set());
34
+ }
35
+ this.beforeToolExecuteModifiers.get(toolSlug)?.add(modifier);
36
+ }
37
+
38
+ /**
39
+ * Method to apply the before tool execute modifiers.
40
+ * @param {string} toolSlug slug of the tool to be modified
41
+ * @param {ToolExecuteParams} toolExecuteParams parameters to be passed to the tool
42
+ * @returns {ToolExecuteParams} modified parameters
43
+ */
44
+ applyBeforeToolExecute(toolSlug: string, toolExecuteParams: ToolExecuteParams): ToolExecuteParams {
45
+ const modifiers = this.beforeToolExecuteModifiers.get(toolSlug);
46
+ if (modifiers) {
47
+ for (const modifier of modifiers) {
48
+ toolExecuteParams = modifier(toolSlug, toolExecuteParams);
49
+ }
50
+ }
51
+ return toolExecuteParams;
52
+ }
53
+
54
+
55
+ /**
56
+ * Method to modify the tool schema after it is executed.
57
+ * @param {string} toolSlug slug of the tool to be modified
58
+ * @param modifier function to modify the tool execution response
59
+ */
60
+ useAfterToolExecute(toolSlug: string, modifier: AfterToolExecuteModifier) {
61
+ if (!this.afterToolExecuteModifiers.has(toolSlug)) {
62
+ this.afterToolExecuteModifiers.set(toolSlug, new Set());
63
+ }
64
+ this.afterToolExecuteModifiers.get(toolSlug)?.add(modifier);
65
+ }
66
+
67
+ /**
68
+ * Method to apply the after tool execute modifiers.
69
+ * @param {string} toolSlug slug of the tool to be modified
70
+ * @param {ToolExecuteResponse} toolExecuteResponse response from the tool execution
71
+ * @returns {ToolExecuteResponse} modified response
72
+ */
73
+ applyAfterToolExecute(toolSlug: string, toolExecuteResponse: ToolExecuteResponse): ToolExecuteResponse {
74
+ const modifiers = this.afterToolExecuteModifiers.get(toolSlug);
75
+ if (modifiers) {
76
+ for (const modifier of modifiers) {
77
+ toolExecuteResponse = modifier(toolSlug, toolExecuteResponse);
78
+ }
79
+ }
80
+ return toolExecuteResponse;
81
+ }
82
+
83
+ /**
84
+ * Method to register modifiers for the tool schema.
85
+ * @param {string} toolSlug slug of the tool to be modified
86
+ * @param modifier function to modify the tool schema
87
+ */
88
+ useTransformToolSchema(toolSlug: string, modifier:TransformToolSchemaModifier) {
89
+ if (!this.transformToolSchemaModifiers.has(toolSlug)) {
90
+ this.transformToolSchemaModifiers.set(toolSlug, new Set());
91
+ }
92
+ this.transformToolSchemaModifiers.get(toolSlug)?.add(modifier);
93
+ }
94
+
95
+ /**
96
+ * Method to apply the transform tool schema modifiers.
97
+ * @param toolSlug slug of the tool to be modified
98
+ * @param toolSchema schema of the tool to be modified
99
+ * @returns {Tool}
100
+ */
101
+ applyTransformToolSchema(toolSlug: string, toolSchema: Tool): Tool {
102
+ const modifiers = this.transformToolSchemaModifiers.get(toolSlug);
103
+ if (modifiers) {
104
+ for (const modifier of modifiers) {
105
+ toolSchema = modifier(toolSlug, toolSchema);
106
+ }
107
+ }
108
+ return toolSchema;
109
+ }
110
+}
\ No newline at end of file
packages/core/src/models/Tools.ts
+33
-31
@@ -1,27 +1,27 @@
1
-import ComposioClient from "@composio/client";
2
-import { Tool } from "../types/tool.types";
1
+import ComposioClient from '@composio/client';
2
+import { Tool, ToolExecuteParams, ToolExecuteResponse } from '../types/tool.types';
3
import {
4
- ToolExecuteParams,
5
- ToolExecuteResponse,
4
ToolGetInputParams,
5
ToolGetInputResponse,
6
ToolListParams,
7
ToolListResponse,
8
ToolRetrieveEnumResponse,
11
-} from "@composio/client/resources/tools";
12
-import { InstrumentedInstance } from "../types/telemetry.types";
13
-
9
+} from '@composio/client/resources/tools';
10
+import { InstrumentedInstance } from '../types/telemetry.types';
11
+import { Modifiers } from './Modifiers';
12
13
/**
14
* This class is used to manage tools in the Composio SDK.
15
* It provides methods to list, get, and execute tools.
16
*/
17
export class Tools implements InstrumentedInstance {
20
- readonly FILE_NAME: string = "core/models/Tools.ts";
18
+ readonly FILE_NAME: string = 'core/models/Tools.ts';
19
private client: ComposioClient;
22
-
23
- constructor(client: ComposioClient) {
20
+ private modifiers: Modifiers;
21
+
22
+ constructor(client: ComposioClient, modifiers?: Modifiers) {
23
this.client = client;
24
+ this.modifiers = modifiers || new Modifiers();
25
}
26
27
/**
@@ -29,16 +29,16 @@ export class Tools implements InstrumentedInstance {
29
* This method fetches the tools from the Composio API and wraps them using the toolset.
30
* @returns {Tools[]>} List of tools
31
*/
32
- async list(query: ToolListParams = {}): Promise<ToolListResponse> {
33
- const tools = await this.client.tools.list(query);
34
- if (!tools) {
35
- return {
36
- items: [],
37
- next_cursor: null,
38
- total_pages: 0,
39
- };
40
- }
41
- return tools;
32
+ async getTools(query: ToolListParams = {}): Promise<ToolListResponse> {
33
+ const tools = await this.client.tools.list(query);
34
+ if (!tools) {
35
+ return {
36
+ items: [],
37
+ next_cursor: null,
38
+ total_pages: 0,
39
+ };
40
+ }
41
+ return tools;
42
}
43
44
/**
@@ -46,8 +46,13 @@ export class Tools implements InstrumentedInstance {
46
* @param slug The ID of the tool to be retrieved
47
* @returns {Promise<Tool>} The tool
48
*/
49
- async get(slug: string): Promise<Tool> {
50
- return this.client.tools.retrieve(slug);
49
+ async getToolBySlug(slug: string): Promise<Tool> {
50
+ const tool = await this.client.tools.retrieve(slug);
51
+ if (!tool) {
52
+ throw new Error(`Tool with slug ${slug} not found`);
53
+ }
54
+ const modifiedTool = this.modifiers.applyTransformToolSchema(slug, tool);
55
+ return modifiedTool;
56
}
57
58
/**
@@ -59,11 +64,11 @@ export class Tools implements InstrumentedInstance {
64
* @param {ToolExecuteParams} body - The parameters to be passed to the tool
65
* @returns {Promise<ToolExecuteResponse>} - The response from the tool execution
66
*/
62
- async execute(
63
- slug: string,
64
- body: ToolExecuteParams
65
- ): Promise<ToolExecuteResponse> {
66
- return this.client.tools.execute(slug, body);
67
+ async execute(slug: string, body: ToolExecuteParams): Promise<ToolExecuteResponse> {
68
+ const modifiedBody = this.modifiers.applyBeforeToolExecute(slug, body);
69
+ const result = await this.client.tools.execute(slug, modifiedBody);
70
+ const modifiedResult = this.modifiers.applyAfterToolExecute(slug, result);
71
+ return modifiedResult;
72
}
73
74
/**
@@ -86,10 +91,7 @@ export class Tools implements InstrumentedInstance {
91
* @param {ToolGetInputParams} body - The parameters to be passed to the tool
92
* @returns
93
*/
89
- async getInput(
90
- slug: string,
91
- body: ToolGetInputParams
92
- ): Promise<ToolGetInputResponse> {
94
+ async getInput(slug: string, body: ToolGetInputParams): Promise<ToolGetInputResponse> {
95
return this.client.tools.getInput(slug, body);
96
}
97
}
packages/core/src/toolset/BaseToolset.ts
+47
-7
@@ -1,6 +1,7 @@
1
import type { Toolset } from '../types/toolset.types';
2
-import type { Tool, ToolListParams } from '../types/tool.types';
2
+import type { CustomAuthParams, Tool, ToolListParams } from '../types/tool.types';
3
import type { Composio } from '../composio';
4
+import { ComposioError } from '../utils/error';
5
6
/**
7
* Base toolset implementation with proper generic defaults
@@ -40,9 +41,9 @@ export abstract class BaseComposioToolset<TToolCollection, TTool>
41
* @param slug - The slug of the tool.
42
* @returns The tool.
43
*/
43
- async getTool(slug: string): Promise<TTool> {
44
- const tool = await this.client?.tools.get(slug);
45
- return this._wrapTool(tool as Tool);
44
+ async getToolBySlug(slug: string): Promise<TTool> {
45
+ const tool = await this.getComposioClient().tools.getToolBySlug(slug);
46
+ return this._wrapTool(tool);
47
}
48
49
/**
@@ -53,14 +54,53 @@ export abstract class BaseComposioToolset<TToolCollection, TTool>
54
abstract _wrapTool(tool: Tool): TTool;
55
56
/**
56
- * Ensure the client is initialized.
57
- * This is automatically done by the Composio class.
57
+ * Execute an action
58
*/
59
- protected ensureClient(): void {
59
+ async execute({
60
+ toolSlug,
61
+ params,
62
+ entityId,
63
+ connectedAccountId,
64
+ text,
65
+ customAuthParams,
66
+ }: {
67
+ toolSlug: string;
68
+ params: Record<string, unknown>;
69
+ entityId?: string;
70
+ connectedAccountId?: string;
71
+ text?: string;
72
+ customAuthParams?: CustomAuthParams;
73
+ }) {
74
+ const tool = await this.client?.tools.getToolBySlug(toolSlug);
75
+
76
+ if (!tool) {
77
+ throw new ComposioError(`Tool with slug ${toolSlug} not found`);
78
+ }
79
+
80
+ try {
81
+ const result = await this.getComposioClient().tools.execute(toolSlug, {
82
+ arguments: params,
83
+ userId: entityId,
84
+ connectedAccountId: connectedAccountId,
85
+ text: text,
86
+ customAuthParams: customAuthParams,
87
+ });
88
+ return result;
89
+ } catch (error) {
90
+ throw new ComposioError(`Error executing tool ${toolSlug}: ${error}`);
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Get the Composio client.
96
+ * @returns The Composio client.
97
+ */
98
+ protected getComposioClient(): Composio<this> {
99
if (!this.client) {
100
throw new Error(
101
'Client not initialized. Make sure the toolset is properly initialized with Composio.'
102
);
103
}
104
+ return this.client;
105
}
106
}
packages/core/src/toolset/ComposioToolset.ts
+1
-1
@@ -19,7 +19,7 @@ export class ComposioToolset extends BaseComposioToolset<Array<CustomTool>, Cust
19
};
20
21
async getTools(params?: ToolListParams): Promise<Array<CustomTool>> {
22
- const tools = await this.client?.tools.list(params);
22
+ const tools = await this.client?.tools.getTools(params);
23
return tools?.items.map((tool) => this._wrapTool(tool as Tool)) ?? [];
24
}
25
packages/core/src/toolset/OpenAIToolset.ts
+7
-7
@@ -43,7 +43,7 @@ export class OpenAIToolset extends BaseComposioToolset<OpenAiToolCollection, Ope
43
* @returns The tools.
44
*/
45
async getTools(params?: ToolListParams): Promise<OpenAiToolCollection> {
46
- const tools = await this.client?.tools.list(params);
46
+ const tools = await this.client?.tools.getTools(params);
47
return tools?.items.map((tool) => this._wrapTool(tool as Tool)) ?? [];
48
}
49
@@ -65,18 +65,18 @@ export class OpenAIToolset extends BaseComposioToolset<OpenAiToolCollection, Ope
65
/**
66
* @TODO: This is redundant, we should be able to get the tool name from the tool call
67
*/
68
- const toolSchema = await this.client.tools.get(tool.function.name);
69
- const appName = toolSchema?.toolkit?.name.toLowerCase();
70
- const connectedAccountId = this.client.getConnectedAccountId(appName);
71
-
68
+ const toolSchema = await this.client.tools.getToolBySlug(tool.function.name);
69
+ const appSlug = toolSchema?.toolkit?.slug.toLowerCase();
70
+ if (!appSlug) {
71
+ throw new Error("App slug not found");
72
+ }
73
+ const connectedAccountId = this.client.getConnectedAccountId(appSlug);
74
const payload = {
75
entity_id: userId ?? this.client.userId,
76
connected_account_id: connectedAccountId,
77
arguments: JSON.parse(tool.function.arguments),
78
}
77
-
79
const results = await this.client.tools.execute(toolSchema.slug, payload);
79
-
80
return JSON.stringify(results);
81
}
82
packages/core/src/toolset/ToolsRegistry.ts
new
+50
@@ -0,0 +1,50 @@
1
+import { Tool, ToolSchema } from '../types/tool.types';
2
+
3
+/**
4
+ * TOOLS REGISTRY
5
+ *
6
+ * This singlton class is used to register user provided tools in the toolset.
7
+ *
8
+ *
9
+ */
10
+export class ToolsRegistry {
11
+ private tools: Map<string, Tool> = new Map<string, Tool>();
12
+
13
+ constructor() {}
14
+
15
+ /**
16
+ * Create a tool in the registry.
17
+ * @param {Tool} tool - The tool to create.
18
+ * @returns {Tool} The created tool.
19
+ */
20
+ createTool(tool: Tool): Tool {
21
+ try {
22
+ const parsedTool = ToolSchema.parse(tool);
23
+ const lowerCaseSlug = parsedTool.slug.toLowerCase();
24
+ this.tools.set(lowerCaseSlug, parsedTool);
25
+ return parsedTool;
26
+ } catch (error) {
27
+ throw new Error(`Failed to create tool ${tool.slug}: ${error}`);
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Get a tool from the registry.
33
+ * @param {string} slug - The slug of the tool.
34
+ * @returns {Tool | undefined} The tool.
35
+ */
36
+ getToolBySlug(slug: string): Tool | undefined {
37
+ const lowerCaseSlug = slug.toLowerCase();
38
+ return this.tools.get(lowerCaseSlug);
39
+ }
40
+
41
+ /**
42
+ * Get all the tools from the registry.
43
+ * @returns {Tool[]} The tools.
44
+ */
45
+ getTools(): Tool[] {
46
+ return Array.from(this.tools.values());
47
+ }
48
+}
49
+
50
+export const toolsRegistry = new ToolsRegistry();
packages/core/src/types/modifiers.types.ts
new
+5
@@ -0,0 +1,5 @@
1
+import { ToolExecuteParams, ToolExecuteResponse, Tool } from "./tool.types";
2
+
3
+export type BeforeToolExecuteModifer = (toolSlug: string, toolExecuteParams: ToolExecuteParams) => ToolExecuteParams;
4
+export type AfterToolExecuteModifier = (toolSlug: string, toolExecuteResponse: ToolExecuteResponse) => ToolExecuteResponse;
5
+export type TransformToolSchemaModifier = (toolSlug: string, toolSchemModifier: Tool) => Tool;
\ No newline at end of file
packages/core/src/types/tool.types.ts
+98
-10
@@ -1,23 +1,111 @@
1
-import { ToolRetrieveResponse } from '@composio/client/resources/tools';
1
import { z } from 'zod';
2
4
-export type Tool = ToolRetrieveResponse;
3
+/**
4
+ * Toolkit is the collection of tools,
5
+ * A toolkit can be thought of as an app which provides a set of tools/actions/triggers.
6
+ *
7
+ * eg. Google Toolkit, which provides tools like Google Search, Google Maps, Google Translate, etc.
8
+ */
9
+export const ToolkitSchema = z.object({
10
+ slug: z.string().describe('The slug of the toolkit'),
11
+ name: z.string().describe('The name of the toolkit'),
12
+ logo: z.string().describe('The logo of the toolkit'),
13
+});
14
+export type Toolkit = z.infer<typeof ToolkitSchema>;
15
6
-export interface BaseTool {
7
- name: string;
8
- description?: string;
9
- input_parameters?: Record<string, unknown>;
10
- output_parameters?: Record<string, unknown>;
11
-}
16
+/**
17
+ * Tool is a single action that can be performed by a toolkit.
18
+ * Tool is simlar to an action that an app can perform.
19
+ *
20
+ * eg. Google Search, Google Maps, Google Translate, etc.
21
+ */
22
+export const ToolSchema = z.object({
23
+ slug: z.string().describe('The slug of the tool. eg. "GOOGLE_SEARCH"'),
24
+ name: z.string().describe(`The name of the tool. eg. "Google Search"`),
25
+ description: z.string().optional().describe('The description of the tool'),
26
+ input_parameters: z
27
+ .record(z.string(), z.unknown())
28
+ .optional()
29
+ .describe('The input parameters of the tool'),
30
+ output_parameters: z
31
+ .record(z.string(), z.unknown())
32
+ .optional()
33
+ .describe('The output parameters of the tool'),
34
+ tags: z.optional(z.array(z.string())).describe('The tags of the tool. eg: Important').default([]),
35
+ toolkit: z.optional(ToolkitSchema).describe('The toolkit of the tool'),
36
+ version: z.optional(z.string()).describe('The version of the tool, e.g. "1.0.0"'),
37
+});
38
+export type Tool = z.infer<typeof ToolSchema>;
39
40
+/**
41
+ * BaseTool is the base type for all tools.
42
+ */
43
+export const BaseToolSchema = z.object({
44
+ name: z.string().describe('The name of the tool'),
45
+ description: z.string().optional().describe('The description of the tool'),
46
+ inpputParameters: z
47
+ .record(z.string(), z.unknown())
48
+ .optional()
49
+ .describe('The input parameters of the tool'),
50
+ outputParameters: z
51
+ .record(z.string(), z.unknown())
52
+ .optional()
53
+ .describe('The output parameters of the tool'),
54
+});
55
+export type BaseTool = z.infer<typeof BaseToolSchema>;
56
export type ToolType<T extends BaseTool> = T;
57
58
+/**
59
+ * ToolListParams is the parameters for the list of tools.
60
+ */
61
export const ToolListParamsSchema = z.object({
62
cursor: z.string().optional(),
63
important: z.string().optional(),
64
limit: z.string().optional(),
65
search: z.string().optional(),
20
- toolkit_slug: z.string().optional(),
66
+ toolkitSlug: z.string().optional(),
67
});
22
-
68
export type ToolListParams = z.infer<typeof ToolListParamsSchema>;
69
+
70
+/**
71
+ * CustomAuthParams is the parameters for the custom authentication.
72
+ */
73
+export const CustomAuthParamsSchema = z.object({
74
+ baseURL: z.string().optional(),
75
+ body: z.record(z.string(), z.unknown()).optional(),
76
+ parameters: z.array(
77
+ z.object({
78
+ in: z.enum(['query', 'header']),
79
+ name: z.string(),
80
+ value: z.union([z.string(), z.number()]),
81
+ })
82
+ ),
83
+});
84
+export type CustomAuthParams = z.infer<typeof CustomAuthParamsSchema>;
85
+
86
+/**
87
+ * ToolExecuteParams is the parameters for the tool execution.
88
+ */
89
+export const ToolExecuteParamsSchema = z.object({
90
+ allowTracing: z.boolean().optional(),
91
+ connectedAccountId: z.string().optional(),
92
+ customAuthParams: CustomAuthParamsSchema.optional(),
93
+ arguments: z.record(z.string(), z.unknown()).optional(),
94
+ userId: z.string().optional(),
95
+ version: z.string().optional(),
96
+ text: z.string().optional(),
97
+});
98
+export type ToolExecuteParams = z.infer<typeof ToolExecuteParamsSchema>;
99
+
100
+
101
+/**
102
+ * ToolResponse Schema
103
+ */
104
+export const ToolExecuteResponseSchema = z.object({
105
+ data: z.record(z.string(), z.unknown()),
106
+ error: z.string().nullable(),
107
+ successful: z.boolean(),
108
+ log_id: z.string().optional(),
109
+ session_info: z.unknown().optional(),
110
+});
111
+export type ToolExecuteResponse = z.infer<typeof ToolExecuteResponseSchema>;
\ No newline at end of file
packages/core/src/types/toolset.types.ts
+5
-4
@@ -1,5 +1,5 @@
1
-import { InstrumentedInstance } from "./telemetry.types";
2
-import { Tool, BaseTool, ToolListParams } from "./tool.types";
1
+import { InstrumentedInstance } from './telemetry.types';
2
+import { Tool, BaseTool, ToolListParams } from './tool.types';
3
4
/**
5
* Base toolset implementation, which needs to be implemented by the extended class.
@@ -11,12 +11,13 @@ export interface Toolset<TTool = BaseTool, TToolCollection = TTool[]> extends In
11
getTools: (params?: ToolListParams) => Promise<TToolCollection>;
12
}
13
14
-
14
/**
15
* This type is used to infer the wrapped tool type from the toolset.
16
* It checks if the toolset has a method `_wrapTool` and infers the return type.
17
*/
18
export type WrappedTool<TToolset> = TToolset extends {
19
_wrapTool: (tool: Tool) => infer TTool;
21
-} ? TTool : never;
20
+}
21
+ ? TTool
22
+ : never;
23
packages/core/src/utils/error.ts
new
+44
@@ -0,0 +1,44 @@
1
+import { ZodError } from 'zod';
2
+
3
+export class ComposioError extends Error {
4
+ private error: Error;
5
+
6
+ constructor(messageOrError: string | unknown, error?: unknown) {
7
+ let message: string;
8
+ let err: Error;
9
+
10
+ if (error instanceof ZodError) {
11
+ const formattedMessage = error.errors
12
+ .map(e => `Path: ${e.path.join('.') || '(root)'} - ${e.message}`)
13
+ .join('; ');
14
+ message = `${messageOrError}: ${formattedMessage}`;
15
+ err = error;
16
+ } else {
17
+ message = typeof messageOrError === 'string' ? messageOrError : String(messageOrError);
18
+ err = error as Error;
19
+ }
20
+
21
+ super(message);
22
+ this.error = err;
23
+
24
+ // Capture the stack trace of the caller
25
+ if (Error.captureStackTrace) {
26
+ Error.captureStackTrace(this, ComposioError);
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Static method to handle Zod errors.
32
+ * If the provided error is a ZodError, it formats the error message and returns a new ComposioError.
33
+ * Otherwise, it returns a new ComposioError with the original message and error.
34
+ */
35
+ static fromZodError(message: string, error: unknown): ComposioError {
36
+ if (error instanceof ZodError) {
37
+ const formattedMessage = error.errors
38
+ .map(e => `Path: ${e.path.join('.') || '(root)'} - ${e.message}`)
39
+ .join('; ');
40
+ return new ComposioError(`${message}: ${formattedMessage}`, error);
41
+ }
42
+ return new ComposioError(message, error as Error);
43
+ }
44
+}
packages/toolsets/vercel/package.json
+1
-1
@@ -33,7 +33,7 @@
33
"typescript": "^5.8.3"
34
},
35
"dependencies": {
36
- "@composio/core": "^0.1.0-rc.1",
36
+ "@composio/core": "workspace:*",
37
"ai": "^4.3.8"
38
}
39
}
packages/toolsets/vercel/src/index.ts
+1
-1
@@ -70,7 +70,7 @@ export class VercelToolset extends BaseComposioToolset<VercelToolCollection, Ver
70
_wrapTool(composioTool: ComposioTool): VercelTool {
71
return tool({
72
description: composioTool.description,
73
- parameters: jsonSchema(composioTool.input_parameters),
73
+ parameters: jsonSchema(composioTool.input_parameters ?? {}),
74
execute: async params => {
75
return await this.executeToolCall(
76
{
pnpm-lock.yaml
+3
-3
@@ -74,7 +74,7 @@ importers:
74
specifier: workspace:*
75
version: link:../../packages/core
76
'@composio/vercel':
77
- specifier: workspace:0.1.0-rc.1
77
+ specifier: workspace:*
78
version: link:../../packages/toolsets/vercel
79
ai:
80
specifier: ^4.3.8
@@ -163,8 +163,8 @@ importers:
163
packages/toolsets/vercel:
164
dependencies:
165
'@composio/core':
166
- specifier: ^0.1.0-rc.1
167
- version: 0.1.0-rc.3
166
+ specifier: workspace:0.1.0-rc.5
167
+ version: link:../../core
168
ai:
169
specifier: ^4.3.8
170
version: 4.3.8(react@19.1.0)(zod@3.24.2)