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, 03:00 UTC
Add connectedAccount checks when executing a tool
haxzie committed
May 14, 2025 at 17:58 UTC
1f4e02d72dda04af30e2d64d1d8533aa74f6043e
3 files changed
+127
-31
examples/pre-processors/src/index.ts
+2
-15
@@ -8,19 +8,6 @@ const composio = new Composio({
8
apiKey: process.env.COMPOSIO_API_KEY,
9
});
10
11
-const userId = 'default';
11
+const toolkit = await composio.toolkits.getToolkitBySlug('HACKERNEWS');
12
13
-const tools = composio.tools.get(userId, 'HACKERNEWS_GET_USER', {
14
- modifyToolSchema: (toolSlug, tool) => {
15
- return {
16
- ...tool,
17
- description: 'This is a modified description',
18
- };
19
- },
20
-});
21
-
22
-const vercelComposio = new Composio({
23
- toolset: new VercelToolset(),
24
-});
25
-
26
-const vercelTools = vercelComposio.tools.get('default', {}, {});
13
+console.log(toolkit);
packages/core/src/composio.ts
+1
-1
@@ -43,7 +43,7 @@ export class Composio<TToolset extends BaseComposioToolset<unknown, unknown> = O
43
44
/**
45
* The configuration for the Composio SDK.
46
- * @type {ComposioConfig<TTool, TToolset>}
46
+ * @type {ComposioConfig<TToolset>}
47
*/
48
private config: ComposioConfig<TToolset>;
49
packages/core/src/models/Tools.ts
+124
-15
@@ -47,6 +47,11 @@ export class Tools<
47
this.toolset = toolset;
48
}
49
50
+ /**
51
+ * Transform the tool cases
52
+ * @param {ToolRetrieveResponse | ComposioToolListResponse['items'][0]} tool - The tool to transform
53
+ * @returns {Tool} The transformed tool
54
+ */
55
private transformToolCases(
56
tool: ToolRetrieveResponse | ComposioToolListResponse['items'][0]
57
): Tool {
@@ -57,6 +62,11 @@ export class Tools<
62
});
63
}
64
65
+ /**
66
+ * Transform the tool execute response to camelcase
67
+ * @param {ComposioToolExecuteResponse} response - The response to transform
68
+ * @returns {ToolExecuteResponse} The transformed response
69
+ */
70
private transformToolExecuteResponse(response: ComposioToolExecuteResponse): ToolExecuteResponse {
71
return ToolExecuteResponseSchema.parse({
72
data: response.data,
@@ -67,6 +77,107 @@ export class Tools<
77
});
78
}
79
80
+ /**
81
+ * Check if the connected account exists for the given tools.
82
+ * @param {string} userId - The user id.
83
+ * @param {ToolList} tools - The tools to check.
84
+ * @returns {Promise<boolean>} True if the connected account exists for the given tools, false otherwise.
85
+ */
86
+ private async checkIfConnectedAccountExistsForTools(
87
+ userId: string,
88
+ tools: ToolList
89
+ ): Promise<boolean> {
90
+ // @TODO: Filter out tools that don't require a connected account
91
+
92
+ // if no tools, return true as no connections needed
93
+ if (!tools.length) {
94
+ return true;
95
+ }
96
+ const connectedAccounts = await this.client.connectedAccounts.list({
97
+ user_id: userId,
98
+ });
99
+ // if no connected accounts, return false
100
+ if (connectedAccounts.items.length === 0) {
101
+ return false;
102
+ }
103
+
104
+ // create a map of toolkit slugs that have connected accounts
105
+ const connectedToolkitSlugs = connectedAccounts.items.reduce(
106
+ (acc, account) => {
107
+ if (account.toolkit.slug) {
108
+ acc[account.toolkit.slug] = true;
109
+ }
110
+ return acc;
111
+ },
112
+ {} as Record<string, boolean>
113
+ );
114
+
115
+ // create a map of tool slugs
116
+ const toolSlugs = tools.reduce(
117
+ (acc, tool) => {
118
+ acc[tool.slug] = true;
119
+ return acc;
120
+ },
121
+ {} as Record<string, boolean>
122
+ );
123
+
124
+ // check if each tool's toolkit has a connected account
125
+ for (const tool of Object.keys(toolSlugs)) {
126
+ if (!connectedToolkitSlugs[tool]) {
127
+ console.warn(
128
+ `Tool ${tool} requires a connected account but no connected account was found`
129
+ );
130
+ return false;
131
+ }
132
+ }
133
+ return true;
134
+ }
135
+
136
+ /**
137
+ * Get the connected account id for a given tool
138
+ * @param {string} userId - The user id
139
+ * @param {string} toolSlug - The tool slug
140
+ * @returns {Promise<string | null>} The connected account id or null if the toolkit is a no auth app
141
+ */
142
+ private async getConnectedAccountIdForTool(
143
+ userId: string,
144
+ toolSlug: string
145
+ ): Promise<string | null> {
146
+ const tool = await this.getComposioToolBySlug(userId, toolSlug);
147
+ if (!tool.toolkit) {
148
+ throw new Error(`Unable to find toolkit for tool ${toolSlug}`);
149
+ }
150
+
151
+ const toolkit = await this.client.toolkits.retrieve(tool.toolkit.slug);
152
+ if (!toolkit) {
153
+ throw new Error(`Unable to find toolkit for tool ${toolSlug}`);
154
+ }
155
+
156
+ // check if the toolkit is a no auth app
157
+ const isNoAuthApp = toolkit.auth_config_details?.some(
158
+ authConfigDetails => authConfigDetails.mode === 'NO_AUTH'
159
+ );
160
+ // if the toolkit is not a no auth app, fetch connected accounts
161
+ if (!isNoAuthApp) {
162
+ const connectedAccounts = await this.client.connectedAccounts.list({
163
+ user_id: userId,
164
+ toolkit_slug: tool.toolkit.slug,
165
+ });
166
+ // if no connected accounts, throw an error
167
+ if (connectedAccounts.items.length === 0) {
168
+ throw new Error('No connected accounts found');
169
+ }
170
+ // by default, use the first connected account
171
+ // @TODO: Add support for multiple connected accounts
172
+ console.warn(
173
+ `Using the first connected account for tool ${toolSlug}. To change this behaviour please explicitly pass a connectedAccountId for the tool`
174
+ );
175
+ return connectedAccounts.items[0].id;
176
+ }
177
+ // if the toolkit is a no auth app, return null
178
+ return null;
179
+ }
180
+
181
/**
182
* Lists all tools available in the Composio SDK as well as custom tools.
183
* This method fetches the tools from the Composio API and wraps them using the toolset.
@@ -93,11 +204,21 @@ export class Tools<
204
if (!tools) {
205
return [];
206
}
207
+ const caseTransformedTools = tools.items.map(tool => this.transformToolCases(tool));
208
+ // @TODO add checks for connectedAccounts for fetched tools
209
+ // const connectedAccountExists = await this.checkIfConnectedAccountExistsForTools(
210
+ // userId,
211
+ // caseTransformedTools
212
+ // );
213
+ // if (!connectedAccountExists) {
214
+ // console.warn('No connected accounts found for the given tools');
215
+ // }
216
+
217
const customTools = await this.customTools.getCustomTools({
218
toolSlugs: queryParams.data.toolSlugs,
219
});
220
100
- let modifiedTools = [...tools.items.map(tool => this.transformToolCases(tool)), ...customTools];
221
+ let modifiedTools = [...caseTransformedTools, ...customTools];
222
223
// apply local modifiers if they are provided
224
if (modifier) {
@@ -199,20 +320,8 @@ export class Tools<
320
// fetch connected accounts if doesn't exist
321
let connectedAccountId = body.connectedAccountId;
322
if (!connectedAccountId) {
202
- const tool = await this.getComposioToolBySlug(body.userId, slug);
203
- if (!tool.toolkit) {
204
- throw new Error(`Unable to find toolkit for tool ${slug}`);
205
- }
206
- const connectedAccounts = await this.client.connectedAccounts.list({
207
- user_id: body.userId,
208
- toolkit_slug: tool.toolkit.slug,
209
- });
210
- // if no connected accounts, throw an error
211
- if (connectedAccounts.items.length === 0) {
212
- throw new Error('No connected accounts found');
213
- }
214
- // by default, use the first connected account
215
- connectedAccountId = connectedAccounts.items[0].id;
323
+ connectedAccountId =
324
+ (await this.getConnectedAccountIdForTool(body.userId, slug)) || undefined;
325
}
326
327
result = await this.client.tools.execute(slug, {