Composio CrewAI Integration

0.11.5 · active · verified Thu Apr 16

Composio-crewai is a Python library that integrates Composio's extensive array of tools with CrewAI agents. It allows AI agents to connect to over 250 external services and applications, providing enterprise-grade authentication, full observability, and a streamlined way to empower AI workflows with real-world actions. The current version is 0.11.5, and the project demonstrates a rapid release cadence with frequent updates across the core Composio ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a CrewAI agent with Composio tools using the Model Context Protocol (MCP). It initializes Composio, creates a session for a user, and then exposes Composio's tools to a CrewAI agent via an `MCPServerHTTP` instance. The agent is then given a task to use these tools. Ensure `COMPOSIO_API_KEY`, `COMPOSIO_USER_ID`, and `OPENAI_API_KEY` are set as environment variables. You must also authorize the desired toolkits (e.g., 'gmail', 'github') within your Composio dashboard or via the CLI for them to be available to the agent.

import os
from crewai import Agent, Task, Crew
from crewai.mcp import MCPServerHTTP
from composio import Composio
from dotenv import load_dotenv

load_dotenv() # Load environment variables from .env file

COMPOSIO_API_KEY = os.environ.get('COMPOSIO_API_KEY', '')
COMPOSIO_USER_ID = os.environ.get('COMPOSIO_USER_ID', '')
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY', '')

if not COMPOSIO_API_KEY:
    raise ValueError("COMPOSIO_API_KEY environment variable not set.")
if not COMPOSIO_USER_ID:
    raise ValueError("COMPOSIO_USER_ID environment variable not set.")
if not OPENAI_API_KEY:
    raise ValueError("OPENAI_API_KEY environment variable not set.")

# Initialize Composio client and create a session
composio_client = Composio(api_key=COMPOSIO_API_KEY)
# Specify the toolkits your agent needs access to (e.g., 'gmail', 'github', 'composio')
# Ensure these toolkits are authenticated in your Composio dashboard.
session = composio_client.create(user_id=COMPOSIO_USER_ID, toolkits=['composio'])

# Create an MCPServerHTTP instance from the Composio session
mcp_server = MCPServerHTTP(
    url=session.mcp.url,
    headers=session.mcp.headers,
)

# Define the agent with Composio MCP tools
researcher = Agent(
    role="Research Analyst",
    goal="Gather and summarize information using available tools.",
    backstory="An expert at finding and synthesizing information from various sources.",
    verbose=True,
    allow_delegation=False,
    llm=os.environ.get('OPENAI_MODEL_NAME', 'gpt-4o-mini'), # Use an LLM, e.g., OpenAI's
    mcps=[mcp_server]
)

# Define a task that leverages Composio tools (e.g., 'composio' toolkit offers general Composio actions)
task = Task(
    description="List all available toolkits connected to Composio and summarize their purpose.",
    expected_output="A concise list of all connected Composio toolkits and a one-sentence summary for each.",
    agent=researcher
)

# Create and run the Crew
crew = Crew(agents=[researcher], tasks=[task], verbose=True)
result = crew.kickoff()

print("\n\n------------------------")
print("Crew Work Results:")
print(result)

view raw JSON →