ts/examples/custom-tools/README.md
This directory contains examples demonstrating how to create and use custom tools with Composio SDK.
simple.ts)Shows how to create a basic standalone tool that doesn't require authentication. The example creates a tool that calculates the square of a number:
const tool = await composio.tools.createCustomTool({
slug: 'CALCULATE_SQUARE',
name: 'Calculate Square',
description: 'Calculates the square of a number',
inputParams: z.object({
number: z.number().describe('The number to calculate the square of'),
}),
execute: async input => {
const { number } = input;
return {
data: { result: number * number },
error: null,
successful: true,
};
},
});
auth-credentials.ts)Demonstrates two approaches to making authenticated requests in toolkit-based tools:
const tool = await composio.tools.createCustomTool({
slug: 'GITHUB_LIST_AND_STAR',
name: 'List and Star Repository',
description: 'Lists repositories and stars them',
toolkitSlug: 'github',
inputParams: z.object({
owner: z.string().describe('Repository owner'),
}),
execute: async (input, connectionConfig, executeToolRequest) => {
// executeToolRequest can only call tools from the 'github' toolkit
// Uses the same connected account credentials automatically
const listResult = await executeToolRequest({
slug: 'LIST_REPOSITORIES', // must be a github toolkit tool
arguments: { owner: input.owner },
});
const starResult = await executeToolRequest({
slug: 'STAR_REPOSITORY', // must be a github toolkit tool
arguments: {
owner: input.owner,
repo: listResult.data.repositories[0].name,
},
});
return {
data: { listed: listResult.data, starred: starResult.data },
error: null,
successful: true,
};
},
});
const tool = await composio.tools.createCustomTool({
slug: 'GITHUB_STAR_REPO',
name: 'Star GitHub Repository',
toolkitSlug: 'github',
description: 'Stars a GitHub repository using direct API call',
inputParams: z.object({
repository: z.string().describe('The repository to star'),
}),
execute: async (input, connectionConfig) => {
// Use connectionConfig for direct API calls or when interacting with different services
const result = await fetch(
`https://api.github.com/user/starred/composiohq/${input.repository}`,
{
method: 'PUT',
headers: {
Authorization: `Bearer ${connectionConfig.val?.access_token}`,
},
}
);
return {
data: await result.json(),
error: null,
successful: true,
};
},
});
Set up your environment:
cp .env.example .env
Add your Composio API key to .env:
COMPOSIO_API_KEY=your_api_key_here
Run an example:
# Run the simple example
pnpm start:simple
# Run the auth credentials example
pnpm start:auth
Standalone vs Toolkit-based Tools
executeToolRequest and connectionConfigAuthentication Methods
executeToolRequest: Recommended way to use toolkit tools
connectionConfig: For direct API calls
Type Safety
Error Handling