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 | """ |
| 2 | Tool Router - OpenAI Agents Example |
| 3 | |
| 4 | This example demonstrates how to use Tool Router with OpenAI Agents framework. |
| 5 | OpenAI Agents provides a powerful way to build AI agents that can use tools |
| 6 | and execute complex workflows. |
| 7 | """ |
| 8 | |
| 9 | import asyncio |
| 10 | |
| 11 | from agents import Agent, Runner |
| 12 | from composio_openai_agents import OpenAIAgentsProvider |
| 13 | |
| 14 | from composio import Composio, after_execute, before_execute |
| 15 | from composio.types import ToolExecuteParams, ToolExecutionResponse |
| 16 | |
| 17 | |
| 18 | async def main(): |
| 19 | # Initialize Composio with OpenAI Agents provider |
| 20 | composio = Composio(provider=OpenAIAgentsProvider()) |
| 21 | |
| 22 | # Create a tool router session for a specific user |
| 23 | # This creates an isolated session with tools for the specified toolkits |
| 24 | session = composio.create( |
| 25 | user_id="user_123", |
| 26 | toolkits=["gmail"], |
| 27 | ) |
| 28 | |
| 29 | # Define logging modifiers to track tool calls |
| 30 | # Pass empty lists to apply to all tools |
| 31 | @before_execute(tools=[]) |
| 32 | def log_before_execute( |
| 33 | tool: str, |
| 34 | toolkit: str, |
| 35 | params: ToolExecuteParams, |
| 36 | ) -> ToolExecuteParams: |
| 37 | """Log tool execution before it runs.""" |
| 38 | print(f"🔧 Executing tool: {toolkit}.{tool}") |
| 39 | print(f" Arguments: {params.get('arguments', {})}") |
| 40 | return params |
| 41 | |
| 42 | @after_execute(tools=[]) |
| 43 | def log_after_execute( |
| 44 | tool: str, |
| 45 | toolkit: str, |
| 46 | response: ToolExecutionResponse, |
| 47 | ) -> ToolExecutionResponse: |
| 48 | """Log tool execution after it completes.""" |
| 49 | print(f"✅ Completed tool: {toolkit}.{tool}") |
| 50 | if "data" in response: |
| 51 | print(f" Response data: {response['data']}") |
| 52 | return response |
| 53 | |
| 54 | # Get tools wrapped for OpenAI Agents with logging modifiers |
| 55 | # These tools are ready to be used with the OpenAI Agents framework |
| 56 | tools = session.tools(modifiers=[log_before_execute, log_after_execute]) |
| 57 | |
| 58 | print(f"\nAvailable tools: {len(tools)}") |
| 59 | |
| 60 | # Create an agent with the tools from the session |
| 61 | agent = Agent( |
| 62 | name="Gmail Assistant", |
| 63 | instructions=( |
| 64 | "You are a helpful assistant that helps users manage their " |
| 65 | "Gmail accounts. You can check emails, " |
| 66 | "send messages, and perform various actions on the Gmail platform." |
| 67 | ), |
| 68 | tools=tools, |
| 69 | ) |
| 70 | |
| 71 | # Define the task |
| 72 | task = "Fetch my last email from gmail and summarize it" |
| 73 | |
| 74 | print(f"\nTask: {task}") |
| 75 | print("\nAgent working...\n") |
| 76 | |
| 77 | # Run the agent |
| 78 | result = await Runner.run( |
| 79 | starting_agent=agent, |
| 80 | input=task, |
| 81 | ) |
| 82 | |
| 83 | # Print the final output |
| 84 | print("\n" + "=" * 50) |
| 85 | print("RESULT:") |
| 86 | print("=" * 50) |
| 87 | print(result.final_output) |
| 88 | |
| 89 | |
| 90 | if __name__ == "__main__": |
| 91 | asyncio.run(main()) |