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.

Add connectedAccount checks when executing a tool

haxzie1f4e02d72dda
diff --git a/examples/pre-processors/src/index.ts b/examples/pre-processors/src/index.ts
index 411152332..b06196335 100644
--- a/examples/pre-processors/src/index.ts
+++ b/examples/pre-processors/src/index.ts
@@ -8,19 +8,6 @@ const composio = new Composio({
apiKey: process.env.COMPOSIO_API_KEY,
});
-const userId = 'default';
+const toolkit = await composio.toolkits.getToolkitBySlug('HACKERNEWS');
-const tools = composio.tools.get(userId, 'HACKERNEWS_GET_USER', {
- modifyToolSchema: (toolSlug, tool) => {
- return {
- ...tool,
- description: 'This is a modified description',
- };
- },
-});
-
-const vercelComposio = new Composio({
- toolset: new VercelToolset(),
-});
-
-const vercelTools = vercelComposio.tools.get('default', {}, {});
+console.log(toolkit);
diff --git a/packages/core/src/composio.ts b/packages/core/src/composio.ts
index 2218d53b2..d4aec16bc 100644
--- a/packages/core/src/composio.ts
+++ b/packages/core/src/composio.ts
@@ -43,7 +43,7 @@ export class Composio<TToolset extends BaseComposioToolset<unknown, unknown> = O
/**
* The configuration for the Composio SDK.
- * @type {ComposioConfig<TTool, TToolset>}
+ * @type {ComposioConfig<TToolset>}
*/
private config: ComposioConfig<TToolset>;
diff --git a/packages/core/src/models/Tools.ts b/packages/core/src/models/Tools.ts
index 8a9fd8377..4911ca3e5 100644
--- a/packages/core/src/models/Tools.ts
+++ b/packages/core/src/models/Tools.ts
@@ -47,6 +47,11 @@ export class Tools<
this.toolset = toolset;
}
+ /**
+ * Transform the tool cases
+ * @param {ToolRetrieveResponse | ComposioToolListResponse['items'][0]} tool - The tool to transform
+ * @returns {Tool} The transformed tool
+ */
private transformToolCases(
tool: ToolRetrieveResponse | ComposioToolListResponse['items'][0]
): Tool {
@@ -57,6 +62,11 @@ export class Tools<
});
}
+ /**
+ * Transform the tool execute response to camelcase
+ * @param {ComposioToolExecuteResponse} response - The response to transform
+ * @returns {ToolExecuteResponse} The transformed response
+ */
private transformToolExecuteResponse(response: ComposioToolExecuteResponse): ToolExecuteResponse {
return ToolExecuteResponseSchema.parse({
data: response.data,
@@ -67,6 +77,107 @@ export class Tools<
});
}
+ /**
+ * Check if the connected account exists for the given tools.
+ * @param {string} userId - The user id.
+ * @param {ToolList} tools - The tools to check.
+ * @returns {Promise<boolean>} True if the connected account exists for the given tools, false otherwise.
+ */
+ private async checkIfConnectedAccountExistsForTools(
+ userId: string,
+ tools: ToolList
+ ): Promise<boolean> {
+ // @TODO: Filter out tools that don't require a connected account
+
+ // if no tools, return true as no connections needed
+ if (!tools.length) {
+ return true;
+ }
+ const connectedAccounts = await this.client.connectedAccounts.list({
+ user_id: userId,
+ });
+ // if no connected accounts, return false
+ if (connectedAccounts.items.length === 0) {
+ return false;
+ }
+
+ // create a map of toolkit slugs that have connected accounts
+ const connectedToolkitSlugs = connectedAccounts.items.reduce(
+ (acc, account) => {
+ if (account.toolkit.slug) {
+ acc[account.toolkit.slug] = true;
+ }
+ return acc;
+ },
+ {} as Record<string, boolean>
+ );
+
+ // create a map of tool slugs
+ const toolSlugs = tools.reduce(
+ (acc, tool) => {
+ acc[tool.slug] = true;
+ return acc;
+ },
+ {} as Record<string, boolean>
+ );
+
+ // check if each tool's toolkit has a connected account
+ for (const tool of Object.keys(toolSlugs)) {
+ if (!connectedToolkitSlugs[tool]) {
+ console.warn(
+ `Tool ${tool} requires a connected account but no connected account was found`
+ );
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Get the connected account id for a given tool
+ * @param {string} userId - The user id
+ * @param {string} toolSlug - The tool slug
+ * @returns {Promise<string | null>} The connected account id or null if the toolkit is a no auth app
+ */
+ private async getConnectedAccountIdForTool(
+ userId: string,
+ toolSlug: string
+ ): Promise<string | null> {
+ const tool = await this.getComposioToolBySlug(userId, toolSlug);
+ if (!tool.toolkit) {
+ throw new Error(`Unable to find toolkit for tool ${toolSlug}`);
+ }
+
+ const toolkit = await this.client.toolkits.retrieve(tool.toolkit.slug);
+ if (!toolkit) {
+ throw new Error(`Unable to find toolkit for tool ${toolSlug}`);
+ }
+
+ // check if the toolkit is a no auth app
+ const isNoAuthApp = toolkit.auth_config_details?.some(
+ authConfigDetails => authConfigDetails.mode === 'NO_AUTH'
+ );
+ // if the toolkit is not a no auth app, fetch connected accounts
+ if (!isNoAuthApp) {
+ const connectedAccounts = await this.client.connectedAccounts.list({
+ user_id: userId,
+ toolkit_slug: tool.toolkit.slug,
+ });
+ // if no connected accounts, throw an error
+ if (connectedAccounts.items.length === 0) {
+ throw new Error('No connected accounts found');
+ }
+ // by default, use the first connected account
+ // @TODO: Add support for multiple connected accounts
+ console.warn(
+ `Using the first connected account for tool ${toolSlug}. To change this behaviour please explicitly pass a connectedAccountId for the tool`
+ );
+ return connectedAccounts.items[0].id;
+ }
+ // if the toolkit is a no auth app, return null
+ return null;
+ }
+
/**
* Lists all tools available in the Composio SDK as well as custom tools.
* This method fetches the tools from the Composio API and wraps them using the toolset.
@@ -93,11 +204,21 @@ export class Tools<
if (!tools) {
return [];
}
+ const caseTransformedTools = tools.items.map(tool => this.transformToolCases(tool));
+ // @TODO add checks for connectedAccounts for fetched tools
+ // const connectedAccountExists = await this.checkIfConnectedAccountExistsForTools(
+ // userId,
+ // caseTransformedTools
+ // );
+ // if (!connectedAccountExists) {
+ // console.warn('No connected accounts found for the given tools');
+ // }
+
const customTools = await this.customTools.getCustomTools({
toolSlugs: queryParams.data.toolSlugs,
});
- let modifiedTools = [...tools.items.map(tool => this.transformToolCases(tool)), ...customTools];
+ let modifiedTools = [...caseTransformedTools, ...customTools];
// apply local modifiers if they are provided
if (modifier) {
@@ -199,20 +320,8 @@ export class Tools<
// fetch connected accounts if doesn't exist
let connectedAccountId = body.connectedAccountId;
if (!connectedAccountId) {
- const tool = await this.getComposioToolBySlug(body.userId, slug);
- if (!tool.toolkit) {
- throw new Error(`Unable to find toolkit for tool ${slug}`);
- }
- const connectedAccounts = await this.client.connectedAccounts.list({
- user_id: body.userId,
- toolkit_slug: tool.toolkit.slug,
- });
- // if no connected accounts, throw an error
- if (connectedAccounts.items.length === 0) {
- throw new Error('No connected accounts found');
- }
- // by default, use the first connected account
- connectedAccountId = connectedAccounts.items[0].id;
+ connectedAccountId =
+ (await this.getConnectedAccountIdForTool(body.userId, slug)) || undefined;
}
result = await this.client.tools.execute(slug, {