Use Composio with OpenAI Agents in Python to create powerful AI assistants
This example demonstrates how to use Composio with OpenAI Agents framework in Python to create AI agents that can execute tools via MCP (Model Context Protocol).
from agents import Agent, HostedMCPTool, Runnerfrom composio import Composio# Initialize Composio and create a sessioncomposio = Composio()session = composio.create( user_id="user_123",)print(session.mcp)# Create MCP tool with Composio sessioncomposio_mcp = HostedMCPTool( tool_config={ "type": "mcp", "server_label": "tool_router", "server_url": session.mcp.url, "require_approval": "never", "headers": session.mcp.headers, })# Create an agent with Composio toolsagent = Agent( name="My Agent", instructions="You are a helpful assistant that can use the tools provided to you.", tools=[composio_mcp],)# Run the agentresult = Runner.run_sync( starting_agent=agent, input="Find my last email and summarize it.",)print(result.final_output)
Model Context Protocol (MCP) is an open standard for connecting AI models to external tools and data sources. Composio provides an MCP server that exposes all your connected tools through a standardized interface.
MCPSession( url='https://mcp.composio.dev/v1/session/abc123', headers={'Authorization': 'Bearer ...'})Summarizing your last email:From:[email protected]Subject: Project UpdateReceived: 2 hours agoSummary: John provided an update on the Q1 project timeline...