Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | import ComposioClient from '@composio/client'; |
| 3 | import { Composio } from '../../src/composio'; |
| 4 | import { CustomToolkits } from '../../src/models/CustomToolkits'; |
| 5 | import { Experimental } from '../../src/models/Experimental'; |
| 6 | import { telemetry } from '../../src/telemetry/Telemetry'; |
| 7 | import { ValidationError } from '../../src/errors/ValidationErrors'; |
| 8 | import { MockProvider } from '../utils/mocks/provider.mock'; |
| 9 | |
| 10 | vi.mock('../../src/telemetry/Telemetry', () => ({ |
| 11 | telemetry: { |
| 12 | instrument: vi.fn(), |
| 13 | }, |
| 14 | })); |
| 15 | |
| 16 | const createMockClient = () => ({ |
| 17 | custom: { |
| 18 | createToolkit: vi.fn(), |
| 19 | updateToolkits: vi.fn(), |
| 20 | upsert: vi.fn(), |
| 21 | sync: vi.fn(), |
| 22 | deleteToolkit: vi.fn(), |
| 23 | }, |
| 24 | }); |
| 25 | |
| 26 | describe('composio.customToolkits', () => { |
| 27 | it('is the same object as the deprecated composio.experimental.customToolkits', () => { |
| 28 | const composio = new Composio({ |
| 29 | apiKey: 'ak_test', |
| 30 | baseURL: 'https://api.test.com', |
| 31 | provider: new MockProvider(), |
| 32 | allowTracking: false, |
| 33 | disableVersionCheck: true, |
| 34 | }); |
| 35 | |
| 36 | expect(composio.customToolkits).toBeInstanceOf(CustomToolkits); |
| 37 | expect(composio.experimental.customToolkits).toBe(composio.customToolkits); |
| 38 | }); |
| 39 | }); |
| 40 | |
| 41 | describe('Experimental.customToolkits', () => { |
| 42 | let customToolkits: CustomToolkits; |
| 43 | let mockClient: ReturnType<typeof createMockClient>; |
| 44 | |
| 45 | beforeEach(() => { |
| 46 | vi.clearAllMocks(); |
| 47 | mockClient = createMockClient(); |
| 48 | customToolkits = new Experimental(mockClient as unknown as ComposioClient).customToolkits; |
| 49 | }); |
| 50 | |
| 51 | it('mounts on the experimental namespace and instruments it', () => { |
| 52 | expect(customToolkits).toBeInstanceOf(CustomToolkits); |
| 53 | expect(telemetry.instrument).toHaveBeenCalledWith(customToolkits, 'CustomToolkits'); |
| 54 | }); |
| 55 | |
| 56 | describe('create', () => { |
| 57 | it('maps the toolkit to the wire and transforms the connect link', async () => { |
| 58 | mockClient.custom.createToolkit.mockResolvedValue({ |
| 59 | slug: 'CUSTOM_MY_TOOLKIT_K7F2Q', |
| 60 | connect_link: { |
| 61 | redirect_url: 'https://connect.composio.dev/link/lk_1', |
| 62 | expires_at: '2026-11-03T12:00:00Z', |
| 63 | connected_account_id: 'ca_abc123', |
| 64 | }, |
| 65 | }); |
| 66 | const signal = new AbortController().signal; |
| 67 | |
| 68 | const result = await customToolkits.create( |
| 69 | { |
| 70 | name: 'My Toolkit', |
| 71 | mcpUrl: 'https://mcp.example.com/mcp', |
| 72 | logoFile: { content: 'aGVsbG8=', mimeType: 'image/png' }, |
| 73 | authSchemes: [ |
| 74 | { |
| 75 | mode: 'API_KEY', |
| 76 | headers: { |
| 77 | Authorization: 'Bearer {{generic_api_key}}', |
| 78 | 'X-Org-Id': '{{generic_id}}', |
| 79 | }, |
| 80 | apiKeyField: { displayName: 'API key' }, |
| 81 | additionalFields: { |
| 82 | genericId: { |
| 83 | displayName: 'Organization ID', |
| 84 | description: 'From your settings page', |
| 85 | }, |
| 86 | }, |
| 87 | }, |
| 88 | ], |
| 89 | userId: 'user_123', |
| 90 | callbackUrl: 'https://app.example.com/connected', |
| 91 | }, |
| 92 | { signal } |
| 93 | ); |
| 94 | |
| 95 | const [body, options] = mockClient.custom.createToolkit.mock.calls[0]; |
| 96 | expect(JSON.parse(JSON.stringify(body))).toEqual({ |
| 97 | name: 'My Toolkit', |
| 98 | mcp_url: 'https://mcp.example.com/mcp', |
| 99 | logo_file: { content: 'aGVsbG8=', mime_type: 'image/png' }, |
| 100 | auth_schemes: [ |
| 101 | { |
| 102 | mode: 'API_KEY', |
| 103 | headers: { |
| 104 | Authorization: 'Bearer {{generic_api_key}}', |
| 105 | 'X-Org-Id': '{{generic_id}}', |
| 106 | }, |
| 107 | api_key_field: { display_name: 'API key' }, |
| 108 | additional_fields: { |
| 109 | generic_id: { |
| 110 | display_name: 'Organization ID', |
| 111 | description: 'From your settings page', |
| 112 | }, |
| 113 | }, |
| 114 | }, |
| 115 | ], |
| 116 | user_id: 'user_123', |
| 117 | callback_url: 'https://app.example.com/connected', |
| 118 | }); |
| 119 | expect(options).toEqual({ signal }); |
| 120 | expect(result).toEqual({ |
| 121 | slug: 'CUSTOM_MY_TOOLKIT_K7F2Q', |
| 122 | connectLink: { |
| 123 | redirectUrl: 'https://connect.composio.dev/link/lk_1', |
| 124 | expiresAt: '2026-11-03T12:00:00Z', |
| 125 | connectedAccountId: 'ca_abc123', |
| 126 | }, |
| 127 | }); |
| 128 | }); |
| 129 | |
| 130 | it('returns a null connect link for a shared toolkit', async () => { |
| 131 | mockClient.custom.createToolkit.mockResolvedValue({ |
| 132 | slug: 'CUSTOM_MY_TOOLKIT_K7F2Q', |
| 133 | connect_link: null, |
| 134 | }); |
| 135 | |
| 136 | const result = await customToolkits.create({ |
| 137 | name: 'My Toolkit', |
| 138 | mcpUrl: 'https://mcp.example.com/mcp', |
| 139 | authSchemes: [{ mode: 'NO_AUTH' }], |
| 140 | userId: 'all', |
| 141 | }); |
| 142 | |
| 143 | expect(result).toEqual({ slug: 'CUSTOM_MY_TOOLKIT_K7F2Q', connectLink: null }); |
| 144 | }); |
| 145 | |
| 146 | it.each([ |
| 147 | ['no user ID', { userId: undefined }], |
| 148 | ['no auth schemes', { authSchemes: [] }], |
| 149 | ['an MCP URL that is not a URL', { mcpUrl: 'not a url' }], |
| 150 | [ |
| 151 | 'an additional API key field without a display name', |
| 152 | { |
| 153 | authSchemes: [ |
| 154 | { |
| 155 | mode: 'API_KEY', |
| 156 | headers: { 'X-Org-Id': '{{generic_id}}' }, |
| 157 | additionalFields: { genericId: { description: 'From your settings page' } }, |
| 158 | }, |
| 159 | ], |
| 160 | }, |
| 161 | ], |
| 162 | ])('rejects %s before calling the API', async (_label, override) => { |
| 163 | await expect( |
| 164 | customToolkits.create({ |
| 165 | name: 'My Toolkit', |
| 166 | mcpUrl: 'https://mcp.example.com/mcp', |
| 167 | authSchemes: [{ mode: 'NO_AUTH' }], |
| 168 | userId: 'all', |
| 169 | ...(override as object), |
| 170 | }) |
| 171 | ).rejects.toThrow(ValidationError); |
| 172 | expect(mockClient.custom.createToolkit).not.toHaveBeenCalled(); |
| 173 | }); |
| 174 | }); |
| 175 | |
| 176 | describe('update', () => { |
| 177 | it('sends only the given fields and transforms the response', async () => { |
| 178 | mockClient.custom.updateToolkits.mockResolvedValue({ success: true }); |
| 179 | |
| 180 | const result = await customToolkits.update('CUSTOM_MY_TOOLKIT_K7F2Q', { |
| 181 | apiKeyField: { displayName: 'Token', description: 'From your settings page' }, |
| 182 | userId: 'user_123', |
| 183 | }); |
| 184 | |
| 185 | const [slug, body] = mockClient.custom.updateToolkits.mock.calls[0]; |
| 186 | expect(slug).toBe('CUSTOM_MY_TOOLKIT_K7F2Q'); |
| 187 | expect(JSON.parse(JSON.stringify(body))).toEqual({ |
| 188 | api_key_field: { display_name: 'Token', description: 'From your settings page' }, |
| 189 | user_id: 'user_123', |
| 190 | }); |
| 191 | expect(result).toEqual({ success: true }); |
| 192 | }); |
| 193 | |
| 194 | it('rejects an update with nothing to change before calling the API', async () => { |
| 195 | await expect( |
| 196 | customToolkits.update('CUSTOM_MY_TOOLKIT_K7F2Q', { userId: 'user_123' }) |
| 197 | ).rejects.toThrow(ValidationError); |
| 198 | expect(mockClient.custom.updateToolkits).not.toHaveBeenCalled(); |
| 199 | }); |
| 200 | }); |
| 201 | |
| 202 | describe('upsert', () => { |
| 203 | it('warns once per process that it is deprecated', async () => { |
| 204 | vi.resetModules(); |
| 205 | const { CustomToolkits: FreshCustomToolkits } = |
| 206 | await import('../../src/models/CustomToolkits'); |
| 207 | const { default: freshLogger } = await import('../../src/utils/logger'); |
| 208 | const warn = vi.spyOn(freshLogger, 'warn').mockImplementation(() => {}); |
| 209 | mockClient.custom.upsert.mockResolvedValue({ slug: 'CUSTOM_MY_TOOLKIT' }); |
| 210 | const fresh = new FreshCustomToolkits(mockClient as unknown as ComposioClient); |
| 211 | const params = { |
| 212 | slug: 'MY_TOOLKIT', |
| 213 | toolkitConfig: { |
| 214 | name: 'My Toolkit', |
| 215 | mcpUrl: 'https://mcp.example.com/mcp', |
| 216 | authSchemes: [{ mode: 'NO_AUTH' as const }], |
| 217 | }, |
| 218 | }; |
| 219 | |
| 220 | await fresh.upsert(params); |
| 221 | await fresh.upsert(params); |
| 222 | |
| 223 | const deprecationWarnings = warn.mock.calls.filter(([message]) => |
| 224 | String(message).includes('upsert() is deprecated') |
| 225 | ); |
| 226 | expect(deprecationWarnings).toHaveLength(1); |
| 227 | }); |
| 228 | |
| 229 | it('maps every auth scheme and the logo to the wire', async () => { |
| 230 | mockClient.custom.upsert.mockResolvedValue({ slug: 'CUSTOM_MY_TOOLKIT' }); |
| 231 | |
| 232 | const result = await customToolkits.upsert({ |
| 233 | slug: 'MY TOOLKIT', |
| 234 | toolkitConfig: { |
| 235 | name: 'My Toolkit', |
| 236 | mcpUrl: 'https://mcp.example.com/mcp', |
| 237 | logoFile: { content: 'aGVsbG8=', mimeType: 'image/png' }, |
| 238 | authSchemes: [ |
| 239 | { mode: 'NO_AUTH' }, |
| 240 | { |
| 241 | mode: 'API_KEY', |
| 242 | headers: { Authorization: 'Bearer {{generic_api_key}}' }, |
| 243 | apiKeyField: { displayName: 'API key', description: 'From your settings page' }, |
| 244 | }, |
| 245 | { |
| 246 | mode: 'DCR_OAUTH', |
| 247 | discoveryUrl: 'https://mcp.example.com/.well-known/oauth-authorization-server', |
| 248 | }, |
| 249 | ], |
| 250 | }, |
| 251 | }); |
| 252 | |
| 253 | expect(mockClient.custom.upsert).toHaveBeenCalledWith( |
| 254 | { |
| 255 | slug: 'MY TOOLKIT', |
| 256 | toolkit_config: { |
| 257 | name: 'My Toolkit', |
| 258 | mcp_url: 'https://mcp.example.com/mcp', |
| 259 | logo_file: { content: 'aGVsbG8=', mime_type: 'image/png' }, |
| 260 | auth_schemes: [ |
| 261 | { mode: 'NO_AUTH' }, |
| 262 | { |
| 263 | mode: 'API_KEY', |
| 264 | headers: { Authorization: 'Bearer {{generic_api_key}}' }, |
| 265 | api_key_field: { display_name: 'API key', description: 'From your settings page' }, |
| 266 | }, |
| 267 | { |
| 268 | mode: 'DCR_OAUTH', |
| 269 | discovery_url: 'https://mcp.example.com/.well-known/oauth-authorization-server', |
| 270 | }, |
| 271 | ], |
| 272 | }, |
| 273 | }, |
| 274 | undefined |
| 275 | ); |
| 276 | expect(result).toEqual({ slug: 'CUSTOM_MY_TOOLKIT' }); |
| 277 | }); |
| 278 | |
| 279 | it('omits the optional logo and API key copy', async () => { |
| 280 | mockClient.custom.upsert.mockResolvedValue({ slug: 'CUSTOM_MY_TOOLKIT' }); |
| 281 | |
| 282 | await customToolkits.upsert({ |
| 283 | slug: 'MY_TOOLKIT', |
| 284 | toolkitConfig: { |
| 285 | name: 'My Toolkit', |
| 286 | mcpUrl: 'https://mcp.example.com/mcp', |
| 287 | authSchemes: [{ mode: 'API_KEY', headers: { 'x-api-key': '{{generic_api_key}}' } }], |
| 288 | }, |
| 289 | }); |
| 290 | |
| 291 | const [body] = mockClient.custom.upsert.mock.calls[0]; |
| 292 | expect(JSON.parse(JSON.stringify(body))).toEqual({ |
| 293 | slug: 'MY_TOOLKIT', |
| 294 | toolkit_config: { |
| 295 | name: 'My Toolkit', |
| 296 | mcp_url: 'https://mcp.example.com/mcp', |
| 297 | auth_schemes: [{ mode: 'API_KEY', headers: { 'x-api-key': '{{generic_api_key}}' } }], |
| 298 | }, |
| 299 | }); |
| 300 | }); |
| 301 | |
| 302 | it.each([ |
| 303 | ['a slug outside the API grammar', { slug: 'my-toolkit' }], |
| 304 | ['no auth schemes', { authSchemes: [] }], |
| 305 | ['an MCP URL that is not a URL', { mcpUrl: 'not a url' }], |
| 306 | ])('rejects %s before calling the API', async (_label, override) => { |
| 307 | const { slug = 'MY_TOOLKIT', ...config } = override as Record<string, unknown>; |
| 308 | |
| 309 | await expect( |
| 310 | customToolkits.upsert({ |
| 311 | slug: slug as string, |
| 312 | toolkitConfig: { |
| 313 | name: 'My Toolkit', |
| 314 | mcpUrl: 'https://mcp.example.com/mcp', |
| 315 | authSchemes: [{ mode: 'NO_AUTH' }], |
| 316 | ...config, |
| 317 | }, |
| 318 | }) |
| 319 | ).rejects.toThrow(ValidationError); |
| 320 | expect(mockClient.custom.upsert).not.toHaveBeenCalled(); |
| 321 | }); |
| 322 | }); |
| 323 | |
| 324 | describe('sync', () => { |
| 325 | it('sends the slug and connected account and transforms the response', async () => { |
| 326 | mockClient.custom.sync.mockResolvedValue({ |
| 327 | slug: 'CUSTOM_MY_TOOLKIT', |
| 328 | version: '20260911_00', |
| 329 | synced_count: 12, |
| 330 | }); |
| 331 | const signal = new AbortController().signal; |
| 332 | |
| 333 | const result = await customToolkits.sync( |
| 334 | 'CUSTOM_MY_TOOLKIT', |
| 335 | { connectedAccountId: 'ca_abc123' }, |
| 336 | { signal } |
| 337 | ); |
| 338 | |
| 339 | expect(mockClient.custom.sync).toHaveBeenCalledWith( |
| 340 | { slug: 'CUSTOM_MY_TOOLKIT', connected_account_id: 'ca_abc123' }, |
| 341 | { signal } |
| 342 | ); |
| 343 | expect(result).toEqual({ |
| 344 | slug: 'CUSTOM_MY_TOOLKIT', |
| 345 | version: '20260911_00', |
| 346 | syncedCount: 12, |
| 347 | }); |
| 348 | }); |
| 349 | |
| 350 | it('sends only the slug when called without params', async () => { |
| 351 | mockClient.custom.sync.mockResolvedValue({ |
| 352 | slug: 'CUSTOM_MY_TOOLKIT', |
| 353 | version: '20260911_00', |
| 354 | synced_count: 0, |
| 355 | }); |
| 356 | |
| 357 | await customToolkits.sync('CUSTOM_MY_TOOLKIT'); |
| 358 | |
| 359 | expect(mockClient.custom.sync).toHaveBeenCalledWith( |
| 360 | { slug: 'CUSTOM_MY_TOOLKIT', connected_account_id: undefined }, |
| 361 | undefined |
| 362 | ); |
| 363 | }); |
| 364 | }); |
| 365 | |
| 366 | describe('delete', () => { |
| 367 | it('deletes the toolkit and transforms the response', async () => { |
| 368 | mockClient.custom.deleteToolkit.mockResolvedValue({ |
| 369 | slug: 'CUSTOM_MY_TOOLKIT', |
| 370 | deleted: true, |
| 371 | revoke_job_ids: ['job_1'], |
| 372 | auth_configs_deleted: 1, |
| 373 | connected_accounts_deleted: 2, |
| 374 | }); |
| 375 | |
| 376 | const result = await customToolkits.delete('CUSTOM_MY_TOOLKIT'); |
| 377 | |
| 378 | expect(mockClient.custom.deleteToolkit).toHaveBeenCalledWith( |
| 379 | 'CUSTOM_MY_TOOLKIT', |
| 380 | {}, |
| 381 | undefined |
| 382 | ); |
| 383 | expect(result).toEqual({ |
| 384 | slug: 'CUSTOM_MY_TOOLKIT', |
| 385 | deleted: true, |
| 386 | revokeJobIds: ['job_1'], |
| 387 | authConfigsDeleted: 1, |
| 388 | connectedAccountsDeleted: 2, |
| 389 | }); |
| 390 | }); |
| 391 | }); |
| 392 | }); |