Skip to main content

CrewAI Integration

CrewAI is a framework for orchestrating role-playing, autonomous AI agents. This article shows how to integrate CrewAI with Cequence AI Gateway using the MCP (Model Context Protocol) adapter.

Requirements​

  • Python 3.8 or higher
  • CrewAI
  • crewai-tools

Install the required dependencies:

pip install crewai crewai-tools

Configuration​

To integrate CrewAI with Cequence AI Gateway, you'll use the MCPServerAdapter from the crewai-tools library:

from crewai import Agent, Task, Crew, Process
from crewai_tools import MCPServerAdapter

# Configure the MCP server parameters for AI Gateway
server_params = {
"url": "<CEQUENCE_AI_GATEWAY_MCP_URL>", # Replace with your AI Gateway MCP URL
"transport": "streamable-http"
}

try:
with MCPServerAdapter(server_params) as tools:
print(f"Available tools from AI Gateway: {[tool.name for tool in tools]}")

# Create an agent with AI Gateway tools
gateway_agent = Agent(
role="AI Gateway Service Integrator",
goal="Utilize tools from Cequence AI Gateway through MCP.",
backstory="An AI agent specialized in leveraging enterprise AI gateway capabilities.",
tools=tools,
verbose=True,
)

# Define a task for the agent
gateway_task = Task(
description="Process requests using AI Gateway tools and capabilities.",
expected_output="Results from AI Gateway processing.",
agent=gateway_agent,
)

# Create a crew with the agent and task
gateway_crew = Crew(
agents=[gateway_agent],
tasks=[gateway_task],
verbose=True,
process=Process.sequential
)

# Execute the crew
result = gateway_crew.kickoff()
print("\nCrew Task Result:\n", result)

except Exception as e:
print(f"Error connecting to AI Gateway MCP server: {e}")
print("Ensure the AI Gateway MCP server is running and accessible at the specified URL.")

Key Components​

MCPServerAdapter​

The MCPServerAdapter allows CrewAI agents to connect to MCP (Model Context Protocol) servers, including Cequence AI Gateway endpoints.

Configuration Options​

  • url: Your AI Gateway MCP endpoint URL
  • transport: Use "streamable-http" for HTTP-based communication

Agent Creation​

Create CrewAI agents that can utilize tools and capabilities provided by AI Gateway through the MCP interface.

Crew Orchestration​

Use CrewAI's Crew class to orchestrate multiple agents working together with AI Gateway tools.

Tips​

  1. Authentication: Configure proper authentication for your AI Gateway connection
  2. Error Handling: Implement comprehensive error handling for network connections and tool execution
  3. Resource Management: Use context managers (with statements) to ensure proper resource cleanup
  4. Monitoring: Leverage AI Gateway's monitoring capabilities to track agent performance
  5. Security: Follow security best practices when configuring access to AI Gateway

Advanced Usage​

Multiple Agents with Shared Tools​

from crewai import Agent, Task, Crew, Process
from crewai_tools import MCPServerAdapter

server_params = {
"url": "<CEQUENCE_AI_GATEWAY_MCP_URL>",
"transport": "streamable-http"
}

with MCPServerAdapter(server_params) as tools:
# Create multiple agents with access to AI Gateway tools
researcher = Agent(
role="Research Analyst",
goal="Gather and analyze data using AI Gateway capabilities.",
tools=tools,
verbose=True
)

writer = Agent(
role="Content Writer",
goal="Create content based on research findings.",
tools=tools,
verbose=True
)

# Define tasks for each agent
research_task = Task(
description="Research market trends using AI Gateway tools.",
agent=researcher
)

writing_task = Task(
description="Write a report based on research findings.",
agent=writer
)

# Create and execute the crew
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, writing_task],
process=Process.sequential
)

result = crew.kickoff()

Repository Reference​

For more information about CrewAI and MCP integration, see the CrewAI documentation.

Next Steps​

  • Configure monitoring and logging for your AI Gateway integration
  • Explore advanced CrewAI features for complex multi-agent workflows
  • Set up authentication and security for your AI Gateway connection
  • Implement custom tools and capabilities through the MCP interface