Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
test(python): add LLM agent e2e test for custom tools
Full agent loop using OpenAI Agents SDK with custom tools routing. Verifies COMPOSIO_SEARCH_TOOLS discovers custom tools and COMPOSIO_MULTI_EXECUTE_TOOL routes them in-process. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Dhawal Upadhyay committed
Mar 20, 2026 at 15:28 UTC
4940181bb31524f040c810e51e5b707b48875ffe
1 file changed
+128
python/examples/custom_tools_agent_test.py
new
+128
@@ -0,0 +1,128 @@
1
+"""
2
+Custom Tools Agent Test — full LLM agent loop with OpenAI Agents SDK.
3
+
4
+Tests that custom tools work end-to-end through an actual multi-turn agent
5
+loop. The agent uses COMPOSIO_SEARCH_TOOLS to discover custom tools, then
6
+COMPOSIO_MULTI_EXECUTE_TOOL to execute them — with local routing for custom
7
+tools and remote routing for Composio tools.
8
+
9
+Equivalent of ts/examples/tool-router/src/custom-tools.ts
10
+
11
+Usage:
12
+ COMPOSIO_API_KEY=... OPENAI_API_KEY=... python examples/custom_tools_agent_test.py
13
+"""
14
+
15
+import asyncio
16
+import os
17
+import sys
18
+
19
+from agents import Agent, Runner
20
+from pydantic import BaseModel, Field
21
+
22
+from composio import Composio, experimental_create_tool, experimental_create_toolkit
23
+from composio_openai_agents import OpenAIAgentsProvider
24
+
25
+
26
+# ── Custom tools ────────────────────────────────────────────────
27
+
28
+
29
+class UserLookupInput(BaseModel):
30
+ user_id: str = Field(description="User ID (e.g. user-1)")
31
+
32
+
33
+USERS = {
34
+ "user-1": {"name": "Alice Johnson", "email": "alice@acme.com", "role": "admin"},
35
+ "user-2": {"name": "Bob Smith", "email": "bob@acme.com", "role": "developer"},
36
+}
37
+
38
+
39
+def get_user_fn(input: UserLookupInput, ctx):
40
+ user = USERS.get(input.user_id)
41
+ if not user:
42
+ raise ValueError(f'User "{input.user_id}" not found')
43
+ return user
44
+
45
+
46
+get_user = experimental_create_tool(
47
+ "GET_USER",
48
+ name="Get user",
49
+ description="Look up an internal user by ID. Returns name, email, and role.",
50
+ input_params=UserLookupInput,
51
+ execute=get_user_fn,
52
+)
53
+
54
+
55
+class SetRoleInput(BaseModel):
56
+ user_id: str = Field(description="User ID")
57
+ role: str = Field(description="New role (admin, developer, viewer)")
58
+
59
+
60
+role_manager = experimental_create_toolkit(
61
+ "ROLE_MANAGER",
62
+ name="Role Manager",
63
+ description="Manage user roles in the system",
64
+ tools=[
65
+ experimental_create_tool(
66
+ "SET_ROLE",
67
+ name="Set role",
68
+ description="Set a user's role. Returns confirmation with updated role.",
69
+ input_params=SetRoleInput,
70
+ execute=lambda input, ctx: {
71
+ "user_id": input.user_id,
72
+ "role": input.role,
73
+ "updated": True,
74
+ "message": f"Role for {input.user_id} changed to {input.role}",
75
+ },
76
+ ),
77
+ ],
78
+)
79
+
80
+
81
+# ── Agent ───────────────────────────────────────────────────────
82
+
83
+
84
+async def main():
85
+ composio = Composio(provider=OpenAIAgentsProvider())
86
+
87
+ print("Creating session with custom tools...")
88
+ session = composio.create(
89
+ user_id="default",
90
+ manage_connections=False,
91
+ experimental={
92
+ "custom_tools": [get_user],
93
+ "custom_toolkits": [role_manager],
94
+ },
95
+ )
96
+ print(f"Session: {session.session_id}")
97
+
98
+ # Get tools wrapped for OpenAI Agents SDK (agentic — includes routing)
99
+ tools = session.tools()
100
+ print(f"Got {len(tools)} tools")
101
+
102
+ agent = Agent(
103
+ name="Assistant",
104
+ instructions=(
105
+ "You are a helpful assistant. Use Composio tools to execute tasks. "
106
+ "In MULTI_EXECUTE, always pass arguments inside the arguments field."
107
+ ),
108
+ model="gpt-4.1-mini",
109
+ tools=tools,
110
+ )
111
+
112
+ prompt = (
113
+ sys.argv[1]
114
+ if len(sys.argv) > 1
115
+ else "Look up user-1's info, then set their role to viewer"
116
+ )
117
+
118
+ print(f"\n> {prompt}\n")
119
+ result = await Runner.run(agent, prompt, max_turns=25)
120
+ print(f"\nAgent: {result.final_output}")
121
+
122
+
123
+if __name__ == "__main__":
124
+ if not os.environ.get("OPENAI_API_KEY"):
125
+ print("Error: OPENAI_API_KEY env var required")
126
+ sys.exit(1)
127
+
128
+ asyncio.run(main())