MadgicxMCP Docs

CrewAI

Connect Madgicx MCP to CrewAI agents using client credentials authentication.

CrewAI

Orchestrate AI agent crews with CrewAI that have access to all Madgicx MCP tools.

Prerequisites

Installation

pip install "crewai[tools,litellm]" mcp mcpadapt python-dotenv pyyaml requests

Setup

1. Configure environment variables

# .env
MADGICX_CLIENT_ID=your_client_id
MADGICX_CLIENT_SECRET=your_client_secret
MADGICX_TOKEN_URL=https://mcp.madgicx.com/oauth/token
MADGICX_MCP_URL=https://mcp.madgicx.com/mcp
 
# LLM provider
OPENAI_API_KEY=your_openai_key

2. Add the auth helper

Copy the MadgicxConfidentialClientAuth helper into your project as auth.py. You can also create a custom auth handler if your setup requires different authentication logic.

3. Create the agent

import os
 
from dotenv import load_dotenv
 
load_dotenv()
 
from crewai import Agent, Crew, Process, Task, LLM
from crewai_tools import MCPServerAdapter
 
from auth import MadgicxConfidentialClientAuth
 
MADGICX_MCP_URL = os.getenv("MADGICX_MCP_URL", "https://mcp.madgicx.com/mcp")
 
 
def main():
    auth = MadgicxConfidentialClientAuth()
 
    # Connect to Madgicx MCP
    mcp_adapter = MCPServerAdapter({
        "url": MADGICX_MCP_URL,
        "transport": "streamable-http",
        "headers": auth.headers,
    })
    mcp_tools = list(mcp_adapter.tools)
 
    # Create the agent
    llm = LLM(model="gpt-4o")
 
    agent = Agent(
        role="Facebook Marketing Expert",
        goal="Help users with Facebook advertising using Madgicx tools.",
        backstory="You are an expert in Facebook advertising powered by Madgicx.",
        tools=mcp_tools,
        llm=llm,
        verbose=True,
    )
 
    task = Task(
        description="What ad accounts do I have access to?",
        expected_output="A list of ad accounts with their details.",
        agent=agent,
    )
 
    crew = Crew(
        agents=[agent],
        tasks=[task],
        process=Process.sequential,
        verbose=True,
    )
 
    result = crew.kickoff()
    print(result)
 
    mcp_adapter.stop()
 
 
if __name__ == "__main__":
    main()

How It Works

  1. MadgicxConfidentialClientAuth obtains an access token using your client credentials
  2. MCPServerAdapter connects to the MCP server and discovers all available tools
  3. Tools are converted to CrewAI-compatible BaseTool instances
  4. The Crew orchestrates the agent to execute tasks using the MCP tools

CrewAI tool compatibility

CrewAI uses Pydantic for tool argument validation. If your LLM injects extra arguments (e.g., security_context), you may need to patch the tool schemas to ignore unknown fields. See the CrewAI GitHub issues for details.

Dependencies

[project]
requires-python = ">=3.10,<3.14"
dependencies = [
    "crewai[tools,litellm]==1.11.1",
    "mcp>=1.0.0",
    "mcpadapt>=0.0.11",
    "python-dotenv>=1.0.0",
    "pyyaml>=6.0",
    "requests>=2.31.0",
]

On this page