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

next
py 208 lines 6.4 KB
Raw
1 """
2 Advanced examples demonstrating the ToolRouter with all configuration options.
3
4 This example shows various ways to configure tool router sessions with
5 different parameters matching the TypeScript implementation.
6 """
7
8 import os
9
10 from composio import Composio
11
12 # Initialize Composio SDK
13 composio = Composio()
14
15 # Provisioned project state (raise KeyError when unset). Auth config and
16 # connected account ids are validated by the backend, so real ids are required.
17 user_id = os.environ["COMPOSIO_EXAMPLES_USER_ID"]
18 github_auth_config_id = os.environ["COMPOSIO_EXAMPLES_GITHUB_AUTH_CONFIG_ID"]
19 slack_auth_config_id = os.environ["COMPOSIO_EXAMPLES_SLACK_AUTH_CONFIG_ID"]
20 github_connected_account_id = os.environ[
21 "COMPOSIO_EXAMPLES_GITHUB_CONNECTED_ACCOUNT_ID"
22 ]
23 slack_connected_account_id = os.environ["COMPOSIO_EXAMPLES_SLACK_CONNECTED_ACCOUNT_ID"]
24
25
26 def example_with_specific_toolkits():
27 """Create a session with specific toolkits enabled."""
28 print("=== Specific Toolkits Example ===")
29
30 session = composio.tool_router.create(
31 user_id=user_id, toolkits=["github", "slack", "linear"]
32 )
33
34 print(f"Session ID: {session.session_id}")
35 print("Available toolkits: github, slack, linear")
36 return session
37
38
39 def example_with_disabled_toolkits():
40 """Create a session with specific toolkits disabled."""
41 print("\n=== Disabled Toolkits Example ===")
42
43 session = composio.tool_router.create(
44 user_id=user_id, toolkits={"disable": ["linear", "jira"]}
45 )
46
47 print(f"Session ID: {session.session_id}")
48 print("Disabled toolkits: linear, jira")
49 return session
50
51
52 def example_with_connection_management_config():
53 """Create a session with connection management and callback URL."""
54 print("\n=== Connection Management with Callback Example ===")
55
56 session = composio.tool_router.create(
57 user_id=user_id,
58 manage_connections={
59 "enable": True,
60 "callback_url": "https://myapp.com/oauth/callback",
61 },
62 )
63
64 print(f"Session ID: {session.session_id}")
65 print("Connection management enabled with custom callback")
66 return session
67
68
69 def example_with_auth_configs():
70 """Create a session with specific auth configs for toolkits."""
71 print("\n=== Auth Configs Example ===")
72
73 session = composio.tool_router.create(
74 user_id=user_id,
75 toolkits=["github", "slack"],
76 auth_configs={"github": github_auth_config_id, "slack": slack_auth_config_id},
77 )
78
79 print(f"Session ID: {session.session_id}")
80 print(
81 f"Auth configs: github → {github_auth_config_id}, slack → {slack_auth_config_id}"
82 )
83 return session
84
85
86 def example_with_connected_accounts():
87 """Create a session with pre-configured connected accounts."""
88 print("\n=== Connected Accounts Example ===")
89
90 session = composio.tool_router.create(
91 user_id=user_id,
92 toolkits=["github", "slack"],
93 connected_accounts={
94 "github": github_connected_account_id,
95 "slack": slack_connected_account_id,
96 },
97 )
98
99 print(f"Session ID: {session.session_id}")
100 print(
101 f"Connected accounts: github → {github_connected_account_id}, "
102 f"slack → {slack_connected_account_id}"
103 )
104 return session
105
106
107 def example_with_all_parameters():
108 """Create a session with all parameters configured."""
109 print("\n=== All Parameters Example ===")
110
111 session = composio.tool_router.create(
112 user_id=user_id,
113 toolkits=["github", "slack"],
114 manage_connections={
115 "enable": True,
116 "callback_url": "https://myapp.com/callback",
117 },
118 auth_configs={
119 "github": github_auth_config_id,
120 "slack": slack_auth_config_id,
121 },
122 connected_accounts={
123 "github": github_connected_account_id,
124 "slack": slack_connected_account_id,
125 },
126 )
127
128 print(f"✓ Session ID: {session.session_id}")
129 print("✓ Toolkits: github, slack")
130 print("✓ Connection management: enabled with callback")
131 print("✓ Auth configs: configured for 2 toolkits")
132 print("✓ Connected accounts: 2 pre-configured")
133
134 return session
135
136
137 def example_minimal_vs_maximal():
138 """Compare minimal and maximal configurations."""
139 print("\n=== Minimal vs Maximal Example ===")
140
141 # Minimal - just user ID
142 minimal_session = composio.tool_router.create(user_id=user_id)
143 print(f"Minimal session: {minimal_session.session_id}")
144
145 # Maximal - all options
146 maximal_session = composio.tool_router.create(
147 user_id=user_id,
148 toolkits=["github", "slack"],
149 manage_connections={"enable": True, "callback_url": "https://app.com/cb"},
150 auth_configs={"github": github_auth_config_id, "slack": slack_auth_config_id},
151 connected_accounts={"github": github_connected_account_id},
152 )
153 print(f"Maximal session: {maximal_session.session_id}")
154
155 return minimal_session, maximal_session
156
157
158 def example_type_safe_configuration():
159 """Demonstrate type-safe configuration using TypedDict."""
160 print("\n=== Type-Safe Configuration Example ===")
161
162 from composio.core.models.tool_router import (
163 ToolRouterManageConnectionsConfig,
164 ToolRouterToolkitsDisableConfig,
165 )
166
167 # Type-safe toolkit config
168 toolkit_config: ToolRouterToolkitsDisableConfig = {"disable": ["linear", "asana"]}
169
170 # Type-safe connection management config
171 connection_config: ToolRouterManageConnectionsConfig = {
172 "enable": True,
173 "callback_url": "https://secure.app.com/oauth",
174 }
175
176 session = composio.tool_router.create(
177 user_id=user_id,
178 toolkits=toolkit_config,
179 manage_connections=connection_config,
180 )
181
182 print(f"Session ID: {session.session_id}")
183 print("✓ Type-safe configuration applied")
184
185 return session
186
187
188 if __name__ == "__main__":
189 """Run all advanced examples."""
190 print("Composio ToolRouter Advanced Examples\n")
191 print("=" * 60)
192
193 try:
194 example_with_specific_toolkits()
195 example_with_disabled_toolkits()
196 example_with_connection_management_config()
197 example_with_auth_configs()
198 example_with_connected_accounts()
199 example_with_all_parameters()
200 example_minimal_vs_maximal()
201 example_type_safe_configuration()
202
203 print("\n" + "=" * 60)
204 print("✓ All advanced examples completed successfully!")
205
206 except Exception as e:
207 print(f"\n✗ Error running examples: {e}")
208 raise