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.

docs: add custom tool session reuse examples

Abir Taheera17ecef33a74
diff --git a/ts/examples/tool-router/src/custom-tools-agent.ts b/ts/examples/tool-router/src/custom-tools-agent.ts
index 00be15a0c..c51bbf287 100644
--- a/ts/examples/tool-router/src/custom-tools-agent.ts
+++ b/ts/examples/tool-router/src/custom-tools-agent.ts
@@ -12,6 +12,7 @@
*
* Usage:
* COMPOSIO_API_KEY=... OPENAI_API_KEY=... bun src/custom-tools-agent.ts
+ * COMPOSIO_SESSION_ID=... bun src/custom-tools-agent.ts # reuse a session
*/
import "dotenv/config";
import { Composio, experimental_createTool, experimental_createToolkit } from "@composio/core";
@@ -161,6 +162,9 @@ const userManagement = experimental_createToolkit("USER_MANAGEMENT", {
tools: [setUserRole, updateUserStatus],
});
+const customTools = [getUserDetails, listUserKeys, formatGmailEmail];
+const customToolkits = [userManagement];
+
// ── Agent ─────────────────────────────────────────────────────────
const composio = new Composio({
@@ -169,16 +173,19 @@ const composio = new Composio({
provider: new OpenAIAgentsProvider(),
});
-const session = await composio.create("custom-tools-agent-user", {
- toolkits: ["weathermap", "gmail"],
- manageConnections: false,
- experimental: {
- customTools: [getUserDetails, listUserKeys, formatGmailEmail],
- customToolkits: [userManagement],
- },
-});
-
-console.log(`Session: ${session.sessionId}\n`);
+const existingSessionId = process.env.COMPOSIO_SESSION_ID;
+const session = existingSessionId
+ ? await composio.use(existingSessionId, { customTools, customToolkits })
+ : await composio.create("custom-tools-agent-user", {
+ toolkits: ["weathermap", "gmail"],
+ manageConnections: false,
+ experimental: {
+ customTools,
+ customToolkits,
+ },
+ });
+
+console.log(`${existingSessionId ? "Reused" : "Created"} session: ${session.sessionId}\n`);
const tools = await session.tools();
const agent = new Agent({
diff --git a/ts/examples/tool-router/src/custom-tools-e2e.ts b/ts/examples/tool-router/src/custom-tools-e2e.ts
index f35c13350..e0e05f9ba 100644
--- a/ts/examples/tool-router/src/custom-tools-e2e.ts
+++ b/ts/examples/tool-router/src/custom-tools-e2e.ts
@@ -139,6 +139,9 @@ const userManagement = experimental_createToolkit("USER_MANAGEMENT", {
tools: [setUserRole, updateUserStatus],
});
+const customTools = [getUserDetails, listUserKeys, formatGmailEmail];
+const customToolkits = [userManagement];
+
// ────────────────────────────────────────────────────────────────
// Run tests
// ────────────────────────────────────────────────────────────────
@@ -157,8 +160,8 @@ async function main() {
toolkits: ["weathermap", "gmail"],
manageConnections: false,
experimental: {
- customTools: [getUserDetails, listUserKeys, formatGmailEmail],
- customToolkits: [userManagement],
+ customTools,
+ customToolkits,
},
});
ok(`Session created: ${session.sessionId}`);
@@ -168,6 +171,16 @@ async function main() {
process.exit(1);
}
+ // ── Existing session reuse with local custom definitions ──
+ try {
+ const reusedSession = await composio.use(session.sessionId, { customTools, customToolkits });
+ const result = await reusedSession.execute("GET_USER_DETAILS", { user_id: "user-1" });
+ if (result.data?.name !== "Alice Johnson") throw new Error(`Unexpected: ${JSON.stringify(result.data)}`);
+ ok("Use: existing session binds custom tools for local execution");
+ } catch (err) {
+ fail("Use: existing session with custom tools", err);
+ }
+
// ════════════════════════════════════════════════════════════════
// Standalone tools (LOCAL_GET_USER_DETAILS, LOCAL_LIST_USER_KEYS)
// ════════════════════════════════════════════════════════════════