1
+/**
2
+ * Focused E2E test: Custom tools + custom toolkits against live backend.
3
+ *
4
+ * Tests session creation, local execution, search indexing, and all three
5
+ * tool types (standalone, extendsToolkit, custom toolkit).
6
+ *
7
+ * Usage:
8
+ * COMPOSIO_API_KEY=... COMPOSIO_BASE_URL=... bun src/custom-tools-e2e.ts
9
+ */
10
+import { Composio, experimental_createTool, experimental_createToolkit } from "@composio/core";
11
+import { z } from "zod/v3";
12
+
13
+const apiKey = process.env.COMPOSIO_API_KEY;
14
+const baseURL = process.env.COMPOSIO_BASE_URL;
15
+if (!apiKey) {
16
+ console.error("COMPOSIO_API_KEY is required");
17
+ process.exit(1);
18
+}
19
+
20
+let passed = 0;
21
+let failed = 0;
22
+
23
+function ok(label: string) {
24
+ passed++;
25
+ console.log(` ✓ ${label}`);
26
+}
27
+function fail(label: string, err: unknown) {
28
+ failed++;
29
+ console.error(` ✗ ${label}: ${err instanceof Error ? err.message : err}`);
30
+}
31
+
32
+// ── Shared in-memory user store ──────────────────────────────────
33
+
34
+const userStore: Record<string, Record<string, unknown>> = {
35
+ "user-1": { id: "user-1", name: "Alice Johnson", email: "alice@acme.com", role: "admin", status: "active" },
36
+ "user-2": { id: "user-2", name: "Bob Smith", email: "bob@acme.com", role: "developer", status: "active" },
37
+ "user-3": { id: "user-3", name: "Carol Williams", email: "carol@acme.com", role: "viewer", status: "suspended" },
38
+};
39
+
40
+// ────────────────────────────────────────────────────────────────
41
+// 1. Standalone tools (no auth) → LOCAL_<SLUG>
42
+// ────────────────────────────────────────────────────────────────
43
+
44
+const getUserDetails = experimental_createTool("GET_USER_DETAILS", {
45
+ name: "Get user details",
46
+ description: "Retrieve user profile information including name, email, role, and account status by user ID",
47
+ inputParams: z.object({
48
+ user_id: z.string().describe("The unique user identifier"),
49
+ }),
50
+ execute: async (input) => {
51
+ const user = userStore[input.user_id];
52
+ if (!user) throw new Error(`User "${input.user_id}" not found`);
53
+ return { ...user };
54
+ },
55
+});
56
+
57
+const listUserKeys = experimental_createTool("LIST_USER_KEYS", {
58
+ name: "List user API keys",
59
+ description: "List all active API keys for a given user, including creation date and last used timestamp",
60
+ inputParams: z.object({
61
+ user_id: z.string().describe("The user whose keys to list"),
62
+ include_expired: z.boolean().default(false),
63
+ }),
64
+ execute: async (input) => {
65
+ const keys = [
66
+ { key_id: "key_abc", prefix: "sk-***abc", expired: false },
67
+ { key_id: "key_old", prefix: "sk-***old", expired: true },
68
+ ];
69
+ const filtered = input.include_expired ? keys : keys.filter((k) => !k.expired);
70
+ return { user_id: input.user_id, api_keys: filtered, total: filtered.length };
71
+ },
72
+});
73
+
74
+// ────────────────────────────────────────────────────────────────
75
+// 2. Extension tool (inherits gmail auth) → LOCAL_GMAIL_<SLUG>
76
+// ────────────────────────────────────────────────────────────────
77
+
78
+const formatGmailEmail = experimental_createTool("FORMAT_GMAIL_EMAIL", {
79
+ name: "Format Gmail email",
80
+ description: "Format and prepare an email draft with proper HTML formatting for sending via Gmail",
81
+ extendsToolkit: "gmail",
82
+ inputParams: z.object({
83
+ to: z.string().describe("Recipient email address"),
84
+ subject: z.string().describe("Email subject line"),
85
+ body: z.string().describe("Email body content"),
86
+ }),
87
+ execute: async (input, ctx) => {
88
+ const htmlBody = input.body.replace(/\n/g, "<br>");
89
+ return {
90
+ formatted: true,
91
+ to: input.to,
92
+ subject: input.subject,
93
+ html_body: htmlBody,
94
+ user_id: ctx.userId,
95
+ };
96
+ },
97
+});
98
+
99
+// ────────────────────────────────────────────────────────────────
100
+// 3. Custom toolkit (USER_MANAGEMENT) → LOCAL_USER_MANAGEMENT_<SLUG>
101
+// ────────────────────────────────────────────────────────────────
102
+
103
+const setUserRole = experimental_createTool("SET_USER_ROLE", {
104
+ name: "Set user role",
105
+ description: "Change the role of an existing user (admin, developer, viewer)",
106
+ inputParams: z.object({
107
+ user_id: z.string().describe("The user to update"),
108
+ role: z.enum(["admin", "developer", "viewer"]).describe("The new role to assign"),
109
+ }),
110
+ execute: async (input) => {
111
+ const user = userStore[input.user_id];
112
+ if (!user) throw new Error(`User "${input.user_id}" not found`);
113
+ const previousRole = user.role;
114
+ user.role = input.role;
115
+ return { user_id: input.user_id, previous_role: previousRole, new_role: input.role };
116
+ },
117
+});
118
+
119
+const updateUserStatus = experimental_createTool("UPDATE_USER_STATUS", {
120
+ name: "Update user status",
121
+ description: "Activate or suspend a user account",
122
+ inputParams: z.object({
123
+ user_id: z.string().describe("The user to update"),
124
+ status: z.enum(["active", "suspended"]).describe("The new account status"),
125
+ reason: z.string().optional().describe("Reason for the status change"),
126
+ }),
127
+ execute: async (input) => {
128
+ const user = userStore[input.user_id];
129
+ if (!user) throw new Error(`User "${input.user_id}" not found`);
130
+ const previousStatus = user.status;
131
+ user.status = input.status;
132
+ return { user_id: input.user_id, previous_status: previousStatus, new_status: input.status, reason: input.reason ?? "No reason provided" };
133
+ },
134
+});
135
+
136
+const userManagement = experimental_createToolkit("USER_MANAGEMENT", {
137
+ name: "User Management",
138
+ description: "Tools for managing user profiles, roles, and account status",
139
+ tools: [setUserRole, updateUserStatus],
140
+});
141
+
142
+// ────────────────────────────────────────────────────────────────
143
+// Run tests
144
+// ────────────────────────────────────────────────────────────────
145
+
146
+const composio = new Composio({ apiKey, baseURL: baseURL ?? undefined });
147
+
148
+async function main() {
149
+ const userId = `e2e-custom-${Date.now()}`;
150
+ console.log(`\nCreating session for user: ${userId}`);
151
+ console.log(`Base URL: ${baseURL ?? "(default)"}\n`);
152
+
153
+ // ── Session creation with all 3 tool types ──
154
+ let session: Awaited<ReturnType<typeof composio.create>>;
155
+ try {
156
+ session = await composio.create(userId, {
157
+ toolkits: ["weathermap", "gmail"],
158
+ manageConnections: false,
159
+ experimental: {
160
+ customTools: [getUserDetails, listUserKeys, formatGmailEmail],
161
+ customToolkits: [userManagement],
162
+ },
163
+ });
164
+ ok(`Session created: ${session.sessionId}`);
165
+ } catch (err) {
166
+ fail("Session creation", err);
167
+ console.error("\nCannot continue without a session.");
168
+ process.exit(1);
169
+ }
170
+
171
+ // ════════════════════════════════════════════════════════════════
172
+ // Standalone tools (LOCAL_GET_USER_DETAILS, LOCAL_LIST_USER_KEYS)
173
+ // ════════════════════════════════════════════════════════════════
174
+
175
+ try {
176
+ const result = await session.execute("GET_USER_DETAILS", { user_id: "user-1" });
177
+ if (result.data?.name !== "Alice Johnson") throw new Error(`Unexpected: ${JSON.stringify(result.data)}`);
178
+ ok("Standalone: execute by original slug (GET_USER_DETAILS)");
179
+ } catch (err) {
180
+ fail("Standalone: execute by original slug", err);
181
+ }
182
+
183
+ try {
184
+ const result = await session.execute("LOCAL_GET_USER_DETAILS", { user_id: "user-2" });
185
+ if (result.data?.name !== "Bob Smith") throw new Error(`Unexpected: ${JSON.stringify(result.data)}`);
186
+ ok("Standalone: execute by prefixed slug (LOCAL_GET_USER_DETAILS)");
187
+ } catch (err) {
188
+ fail("Standalone: execute by prefixed slug", err);
189
+ }
190
+
191
+ try {
192
+ const result = await session.execute("LIST_USER_KEYS", { user_id: "user-1" });
193
+ if (result.data?.total !== 1) throw new Error(`Expected 1 active key, got: ${JSON.stringify(result.data)}`);
194
+ ok("Standalone: second tool routes correctly (LIST_USER_KEYS)");
195
+ } catch (err) {
196
+ fail("Standalone: second tool routes correctly", err);
197
+ }
198
+
199
+ try {
200
+ const result = await session.execute("LIST_USER_KEYS", { user_id: "user-1", include_expired: true });
201
+ if (result.data?.total !== 2) throw new Error(`Expected 2 keys with expired, got: ${JSON.stringify(result.data)}`);
202
+ ok("Standalone: Zod defaults overridden (include_expired=true → 2 keys)");
203
+ } catch (err) {
204
+ fail("Standalone: Zod defaults overridden", err);
205
+ }
206
+
207
+ // ════════════════════════════════════════════════════════════════
208
+ // Extension tool (LOCAL_GMAIL_FORMAT_GMAIL_EMAIL)
209
+ // ════════════════════════════════════════════════════════════════
210
+
211
+ try {
212
+ const result = await session.execute("FORMAT_GMAIL_EMAIL", {
213
+ to: "test@example.com",
214
+ subject: "Hello",
215
+ body: "Line 1\nLine 2",
216
+ });
217
+ if (result.error) throw new Error(`Execute error: ${result.error}`);
218
+ if (result.data?.formatted !== true) throw new Error(`Not formatted: ${JSON.stringify(result.data)}`);
219
+ if (result.data?.html_body !== "Line 1<br>Line 2") throw new Error(`HTML mismatch: ${result.data?.html_body}`);
220
+ if (result.data?.user_id !== userId) throw new Error(`UserId mismatch: ${result.data?.user_id}`);
221
+ ok("Extension: execute by original slug (FORMAT_GMAIL_EMAIL) + session context");
222
+ } catch (err) {
223
+ fail("Extension: execute by original slug", err);
224
+ }
225
+
226
+ try {
227
+ const result = await session.execute("LOCAL_GMAIL_FORMAT_GMAIL_EMAIL", {
228
+ to: "x@y.com",
229
+ subject: "Test",
230
+ body: "body",
231
+ });
232
+ if (result.data?.to !== "x@y.com") throw new Error(`Unexpected: ${JSON.stringify(result.data)}`);
233
+ ok("Extension: execute by prefixed slug (LOCAL_GMAIL_FORMAT_GMAIL_EMAIL)");
234
+ } catch (err) {
235
+ fail("Extension: execute by prefixed slug", err);
236
+ }
237
+
238
+ // ════════════════════════════════════════════════════════════════
239
+ // Custom toolkit (LOCAL_USER_MANAGEMENT_SET_USER_ROLE, etc.)
240
+ // ════════════════════════════════════════════════════════════════
241
+
242
+ try {
243
+ const result = await session.execute("SET_USER_ROLE", { user_id: "user-3", role: "developer" });
244
+ if (result.data?.previous_role !== "viewer") throw new Error(`Previous role wrong: ${JSON.stringify(result.data)}`);
245
+ if (result.data?.new_role !== "developer") throw new Error(`New role wrong: ${JSON.stringify(result.data)}`);
246
+ ok("Toolkit: execute by original slug (SET_USER_ROLE)");
247
+ } catch (err) {
248
+ fail("Toolkit: execute by original slug", err);
249
+ }
250
+
251
+ try {
252
+ const result = await session.execute("LOCAL_USER_MANAGEMENT_SET_USER_ROLE", { user_id: "user-2", role: "admin" });
253
+ if (result.data?.new_role !== "admin") throw new Error(`Unexpected: ${JSON.stringify(result.data)}`);
254
+ ok("Toolkit: execute by prefixed slug (LOCAL_USER_MANAGEMENT_SET_USER_ROLE)");
255
+ } catch (err) {
256
+ fail("Toolkit: execute by prefixed slug", err);
257
+ }
258
+
259
+ try {
260
+ const result = await session.execute("UPDATE_USER_STATUS", { user_id: "user-3", status: "active", reason: "Suspended by mistake" });
261
+ if (result.data?.previous_status !== "suspended") throw new Error(`Previous status wrong: ${JSON.stringify(result.data)}`);
262
+ if (result.data?.new_status !== "active") throw new Error(`New status wrong: ${JSON.stringify(result.data)}`);
263
+ if (result.data?.reason !== "Suspended by mistake") throw new Error(`Reason wrong: ${JSON.stringify(result.data)}`);
264
+ ok("Toolkit: second tool + optional param (UPDATE_USER_STATUS with reason)");
265
+ } catch (err) {
266
+ fail("Toolkit: second tool + optional param", err);
267
+ }
268
+
269
+ try {
270
+ const result = await session.execute("LOCAL_USER_MANAGEMENT_UPDATE_USER_STATUS", { user_id: "user-1", status: "suspended" });
271
+ if (result.data?.reason !== "No reason provided") throw new Error(`Default reason not applied: ${JSON.stringify(result.data)}`);
272
+ ok("Toolkit: prefixed slug + optional param default (reason → 'No reason provided')");
273
+ } catch (err) {
274
+ fail("Toolkit: prefixed slug + optional param default", err);
275
+ }
276
+
277
+ // Verify state mutations persisted across calls
278
+ try {
279
+ const result = await session.execute("GET_USER_DETAILS", { user_id: "user-3" });
280
+ if (result.data?.role !== "developer") throw new Error(`Role not updated: ${JSON.stringify(result.data)}`);
281
+ if (result.data?.status !== "active") throw new Error(`Status not updated: ${JSON.stringify(result.data)}`);
282
+ ok("Toolkit: state mutations persisted (user-3 is now developer+active)");
283
+ } catch (err) {
284
+ fail("Toolkit: state mutations persisted", err);
285
+ }
286
+
287
+ // ════════════════════════════════════════════════════════════════
288
+ // Cross-cutting: case insensitive, error handling, search, remote
289
+ // ════════════════════════════════════════════════════════════════
290
+
291
+ try {
292
+ const result = await session.execute("get_user_details", { user_id: "user-1" });
293
+ if (!result.data?.name) throw new Error(`Unexpected: ${JSON.stringify(result.data)}`);
294
+ ok("Case-insensitive slug lookup (get_user_details → GET_USER_DETAILS)");
295
+ } catch (err) {
296
+ fail("Case-insensitive slug", err);
297
+ }
298
+
299
+ try {
300
+ const result = await session.execute("GET_USER_DETAILS", { user_id: "nonexistent" });
301
+ if (!result.error?.includes("not found")) throw new Error(`Expected 'not found' error, got: ${result.error}`);
302
+ ok("Error handling: thrown error wrapped in response");
303
+ } catch (err) {
304
+ fail("Error handling", err);
305
+ }
306
+
307
+ // Search — standalone tools
308
+ try {
309
+ const searchResult = await session.search({ query: "get user profile details" });
310
+ const toolSlugs = searchResult.results.flatMap((r) => r.primaryToolSlugs);
311
+ const found = toolSlugs.find((s) => s === "LOCAL_GET_USER_DETAILS");
312
+ if (!found) console.log(" (search returned:", toolSlugs.join(", "), ")");
313
+ ok(`Search: standalone tool found=${!!found} (${searchResult.results.length} results)`);
314
+ } catch (err) {
315
+ fail("Search: standalone tools", err);
316
+ }
317
+
318
+ // Search — toolkit tools
319
+ try {
320
+ const searchResult = await session.search({ query: "change user role to admin" });
321
+ const toolSlugs = searchResult.results.flatMap((r) => r.primaryToolSlugs);
322
+ const found = toolSlugs.find((s) => s.includes("USER_MANAGEMENT"));
323
+ if (!found) console.log(" (search returned:", toolSlugs.join(", "), ")");
324
+ ok(`Search: toolkit tool found=${!!found} (${searchResult.results.length} results)`);
325
+ } catch (err) {
326
+ fail("Search: toolkit tools", err);
327
+ }
328
+
329
+ // Search — extension tool
330
+ try {
331
+ const searchResult = await session.search({ query: "format email draft for gmail" });
332
+ const toolSlugs = searchResult.results.flatMap((r) => r.primaryToolSlugs);
333
+ const found = toolSlugs.find((s) => s.includes("GMAIL") && s.startsWith("LOCAL_"));
334
+ if (!found) console.log(" (search returned:", toolSlugs.join(", "), ")");
335
+ ok(`Search: extension tool found=${!!found} (${searchResult.results.length} results)`);
336
+ } catch (err) {
337
+ fail("Search: extension tool", err);
338
+ }
339
+
340
+ // Remote tool still works alongside custom tools
341
+ try {
342
+ const result = await session.execute("WEATHERMAP_WEATHER", { location: "Tokyo" });
343
+ if (result.error) throw new Error(`Remote error: ${result.error}`);
344
+ ok(`Remote tool execution (WEATHERMAP_WEATHER) — keys: ${Object.keys(result.data ?? {}).join(", ")}`);
345
+ } catch (err) {
346
+ fail("Remote tool execution", err);
347
+ }
348
+
349
+ // ── Summary ──
350
+ console.log(`\n${passed} passed, ${failed} failed out of ${passed + failed} tests`);
351
+ if (failed > 0) process.exit(1);
352
+}
353
+
354
+main().catch((err) => {
355
+ console.error("Fatal:", err);
356
+ process.exit(1);
357
+});