@devries-cameron20 / composio-1 / commits / 39e293521
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Custom tools documentation and types

haxzie committed Jun 3, 2025 at 15:18 UTC 39e2935215ed6cc5f88c72b97740ab985fb43a1f
7 files changed +157 -28
docs/api/custom-tools.md
+84 -17
@@ -34,33 +34,43 @@ const tool = await composio.tools.createCustomTool({
34
35 A toolkit-based tool has access to two ways of making authenticated requests:
36
37 -1. Using `executeToolRequest` - The recommended way to make authenticated requests to the toolkit's API endpoints. Composio automatically handles credential injection using the connected account:
37 +1. Using `executeToolRequest` - The recommended way to make authenticated requests to the toolkit's API endpoints. Composio automatically handles credential injection and baseURL resolution:
38
39 ```typescript
40 const tool = await composio.tools.createCustomTool({
41 - slug: 'GITHUB_STAR_COMPOSIO_REPOSITORY',
42 - name: 'Star Composios Repository',
43 - description: 'Stars a repository under composiohq org',
41 + slug: 'GITHUB_STAR_COMPOSIOHQ_REPOSITORY',
42 + name: 'Github star composio repositories',
43 toolkitSlug: 'github',
44 + description: 'For any given repository of the user composiohq, star the repository',
45 inputParams: z.object({
46 - repository: z.string().describe('Name of the repository to star'),
46 + repository: z.string().describe('The repository to star'),
47 + page: z.number().optional().describe('Page number for pagination'),
48 + customHeader: z.string().optional().describe('Custom header value'),
49 }),
50 execute: async (input, authCredentials, executeToolRequest) => {
51 // executeToolRequest makes authenticated requests to the toolkit's API
50 - // It automatically uses the connected account's credentials
51 - const starResult = await executeToolRequest({
52 - endpoint: `https://api.github.com/user/starred/composiohq/${input.repository}`,
52 + // You can use relative paths - Composio will automatically inject the baseURL
53 + const result = await executeToolRequest({
54 + endpoint: `/user/starred/composiohq/${input.repository}`,
55 method: 'PUT',
56 body: {},
57 + // Add custom headers or query parameters
58 + parameters: [
59 + // Add query parameters
60 + {
61 + name: 'page',
62 + value: input.page?.toString() || '1',
63 + in: 'query',
64 + },
65 + // Add custom headers
66 + {
67 + name: 'x-custom-header',
68 + value: input.customHeader || 'default-value',
69 + in: 'header',
70 + },
71 + ],
72 });
56 -
57 - return {
58 - data: {
59 - message: 'Successfully stared repository',
60 - },
61 - error: null,
62 - successful: true,
63 - };
73 + return result;
74 },
75 });
76 ```
@@ -76,7 +86,7 @@ const tool = await composio.tools.createCustomTool({
86 inputParams: z.object({
87 repo: z.string().describe('Repository name'),
88 }),
79 - execute: async (input, authCredentials) => {
89 + execute: async (input, authCredentials, executeToolRequest) => {
90 // Use authCredentials for direct API calls
91 const result = await fetch(`https://api.github.com/repos/${input.repo}`, {
92 headers: {
@@ -93,6 +103,55 @@ const tool = await composio.tools.createCustomTool({
103 });
104 ```
105
106 +### Using Custom Headers and Query Parameters
107 +
108 +You can add custom headers and query parameters to your toolkit-based tools using the `parameters` option in `executeToolRequest`:
109 +
110 +```typescript
111 +const tool = await composio.tools.createCustomTool({
112 + slug: 'GITHUB_SEARCH_REPOSITORIES',
113 + name: 'Search GitHub Repositories',
114 + description: 'Search for repositories with custom parameters',
115 + toolkitSlug: 'github',
116 + inputParams: z.object({
117 + query: z.string().describe('Search query'),
118 + perPage: z.number().optional().describe('Results per page'),
119 + acceptType: z.string().optional().describe('Custom accept header'),
120 + }),
121 + execute: async (input, authCredentials, executeToolRequest) => {
122 + const result = await executeToolRequest({
123 + endpoint: '/search/repositories',
124 + method: 'GET',
125 + parameters: [
126 + // Add query parameters for pagination
127 + {
128 + name: 'q',
129 + value: input.query,
130 + in: 'query',
131 + },
132 + {
133 + name: 'per_page',
134 + value: (input.perPage || 30).toString(),
135 + in: 'query',
136 + },
137 + // Add custom headers
138 + {
139 + name: 'Accept',
140 + value: input.acceptType || 'application/vnd.github.v3+json',
141 + in: 'header',
142 + },
143 + {
144 + name: 'X-Custom-Header',
145 + value: 'custom-value',
146 + in: 'header',
147 + },
148 + ],
149 + });
150 + return result;
151 + },
152 +});
153 +```
154 +
155 ## Executing Custom Tools
156
157 You can execute custom tools just like any other tool:
@@ -123,6 +182,14 @@ The custom tools implementation provides full type safety:
182 3. Handle errors gracefully in your execute function
183 4. For toolkit-based tools:
184 - Prefer `executeToolRequest` over direct API calls when possible
185 + - Use relative paths with `executeToolRequest` - Composio will automatically inject the correct baseURL
186 + - Use the `parameters` option to add custom headers or query parameters:
187 + ```typescript
188 + parameters: [
189 + { name: 'page', value: '1', in: 'query' }, // Adds ?page=1 to URL
190 + { name: 'x-custom', value: 'value', in: 'header' }, // Adds header
191 + ];
192 + ```
193 - Remember that `executeToolRequest` can only call tools from the same toolkit
194 - Use `executeToolRequest` to leverage Composio's automatic credential handling
195 - Only use `authCredentials` when you need to make direct API calls or interact with different services
examples/custom-tools/src/index.ts
+1 -1
@@ -37,7 +37,7 @@ const tool = await composio.tools.createCustomTool({
37 console.log('🚀 ~ execute: ~ authCredentials:', authCredentials);
38
39 const result = await executeToolRequest({
40 - endpoint: `https://api.github.com/user/starred/composiohq/${input.repository}`,
40 + endpoint: `/user/starred/composiohq/${input.repository}`,
41 method: 'PUT',
42 body: {},
43 });
packages/core/src/models/CustomTools.ts
+20 -4
@@ -13,8 +13,13 @@ import {
13 InputParamsSchema,
14 } from '../types/customTool.types';
15 import zodToJsonSchema from 'zod-to-json-schema';
16 -import { Tool, ToolExecuteParams, ToolExecuteResponse, ToolList } from '../types/tool.types';
17 -import { ToolProxyParams } from '@composio/client/resources/tools';
16 +import {
17 + Tool,
18 + ToolExecuteParams,
19 + ToolExecuteResponse,
20 + ToolList,
21 + ToolProxyParams,
22 +} from '../types/tool.types';
23 import logger from '../utils/logger';
24 import {
25 ComposioInvalidExecuteFunctionError,
@@ -315,9 +320,20 @@ export class CustomTools {
320 }
321 );
322 }
323 + // map the parameters to the composio format
324 + const parameters = data.parameters?.map(param => ({
325 + name: param.name,
326 + type: param.in,
327 + value: param.value.toString(),
328 + }));
329 +
330 + // execute the tool
331 const response = await this.client.tools.proxy({
319 - ...data,
320 - connected_account_id: connectedAccountId,
332 + endpoint: data.endpoint,
333 + method: data.method,
334 + parameters: parameters,
335 + body: data.body,
336 + connected_account_id: data.connectedAccountId ?? connectedAccountId,
337 });
338
339 return {
packages/core/src/models/Tools.ts
+32 -2
@@ -8,11 +8,13 @@ import {
8 ToolSchema,
9 ToolListParams,
10 ToolExecuteResponseSchema,
11 + ToolProxyParamsSchema,
12 + ToolProxyParams,
13 } from '../types/tool.types';
14 import {
15 ToolGetInputParams,
16 ToolGetInputResponse,
15 - ToolProxyParams,
17 + ToolProxyParams as ComposioToolProxyParams,
18 ToolProxyResponse,
19 ToolRetrieveEnumResponse,
20 ToolRetrieveResponse,
@@ -703,7 +705,35 @@ export class Tools<
705 * ```
706 */
707 async proxyExecute(body: ToolProxyParams): Promise<ToolProxyResponse> {
706 - return this.client.tools.proxy(body);
708 + const toolProxyParams = ToolProxyParamsSchema.safeParse(body);
709 + if (!toolProxyParams.success) {
710 + throw new ValidationError('Invalid tool proxy parameters', { cause: toolProxyParams.error });
711 + }
712 + // convert the headers and query to the composio format
713 + // { name: string, type: 'header' | 'query', value: string }
714 + const parameters: ComposioToolProxyParams.Parameter[] = [];
715 + const parameterTypes = {
716 + header: 'header',
717 + query: 'query',
718 + } as const;
719 +
720 + if (toolProxyParams.data.parameters) {
721 + parameters.push(
722 + ...Object.entries(toolProxyParams.data.parameters).map(([key, value]) => ({
723 + name: key,
724 + type: value.in === 'header' ? parameterTypes.header : parameterTypes.query,
725 + value: value.value.toString(),
726 + }))
727 + );
728 + }
729 +
730 + return this.client.tools.proxy({
731 + endpoint: toolProxyParams.data.endpoint,
732 + method: toolProxyParams.data.method,
733 + body: toolProxyParams.data.body,
734 + connected_account_id: toolProxyParams.data.connectedAccountId,
735 + parameters: parameters,
736 + });
737 }
738
739 /**
packages/core/src/provider/BaseProvider.ts
+3 -2
@@ -39,10 +39,11 @@ abstract class BaseProvider {
39 /**
40 * @public
41 * Global function to execute a tool.
42 - * This function is used by provider providers to implement helper functions to execute tools.
42 + * This function is used by providerds to implement helper functions to execute tools.
43 * This is a 1:1 mapping of the `execute` method in the `Tools` class.
44 * @param {string} toolSlug - The slug of the tool to execute.
45 - * @param {Record<string, unknown>} input - The input to the tool.
45 + * @param {ToolExecuteParams} body - The body of the tool execution.
46 + * @param {ExecuteToolModifiers} modifers - The modifiers of the tool execution.
47 * @returns {Promise<string>} The result of the tool execution.
48 */
49 executeTool(
packages/core/src/types/customTool.types.ts
+2 -2
@@ -1,6 +1,6 @@
1 import { z } from 'zod';
2 -import { Tool } from './tool.types';
3 -import { ToolExecuteResponse, ToolProxyParams } from '@composio/client/resources/tools';
2 +import { Tool, ToolProxyParams } from './tool.types';
3 +import { ToolExecuteResponse } from '@composio/client/resources/tools';
4
5 type BaseCustomToolOptions<T extends z.ZodType> = {
6 name: string;
packages/core/src/types/tool.types.ts
+15
@@ -179,3 +179,18 @@ export const ToolExecuteResponseSchema = z.object({
179 sessionInfo: z.unknown().optional(),
180 });
181 export type ToolExecuteResponse = z.infer<typeof ToolExecuteResponseSchema>;
182 +
183 +export const ToolProxyParamsSchema = z.object({
184 + endpoint: z.string(),
185 + method: z.enum(['GET', 'POST', 'PUT', 'DELETE', 'PATCH']),
186 + body: z.unknown().optional(),
187 + parameters: z.array(
188 + z.object({
189 + in: z.enum(['query', 'header']),
190 + name: z.string(),
191 + value: z.union([z.string(), z.number()]),
192 + })
193 + ),
194 + connectedAccountId: z.string().optional(),
195 +});
196 +export type ToolProxyParams = z.infer<typeof ToolProxyParamsSchema>;