Overview
In this example, you’ll learn how to:- Create a Composio session with MCP server
- Integrate Composio with OpenAI Agents
- Use MCP for tool execution
- Build agents that can access external APIs
Prerequisites
1
Install dependencies
2
Set up environment variables
Create a
.env file with your API keys:3
Authenticate with Gmail
Complete Example
How It Works
1
Create Composio Session
Initialize Composio and create a session for a specific user. This session provides an MCP server URL that the agent can connect to.
2
Configure MCP Tool
Create a
HostedMCPTool that connects to Composio’s MCP server. This tool gives the agent access to all Composio tools.3
Create Agent
Instantiate an OpenAI Agent with instructions and the MCP tool.
4
Run Agent
Use
Runner.run_sync() to execute the agent with your input. The agent will automatically use the appropriate Composio tools to complete the task.What is MCP?
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.
Session Configuration
Thesession.mcp object contains:
string
The MCP server URL for this session
dict
Authentication headers for the MCP server
Expected Output
Multi-User Support
Each user gets their own session with isolated credentials:Advanced Configuration
Available Tools
The agent automatically has access to all tools in the user’s connected accounts:- Gmail: Send/read emails, search, manage labels
- Slack: Send messages, create channels, manage users
- GitHub: Create issues, PRs, manage repositories
- Google Calendar: Create events, manage schedules
- And 200+ more integrations
Best Practices
User-Specific Sessions: Always create separate sessions for different users to maintain data isolation
Error Handling: Wrap agent execution in try-catch blocks to handle authentication or API errors
Clear Instructions: Provide clear, specific instructions to help the agent choose the right tools
Next Steps
LangChain Example
Use Composio with LangChain in Python
CrewAI Example
Build multi-agent systems with CrewAI