MadgicxMCP Docs

OpenAI Agents SDK

Connect Madgicx MCP to the OpenAI Agents SDK using client credentials authentication.

OpenAI Agents SDK

Build AI agents with the OpenAI Agents SDK that have access to all Madgicx MCP tools.

Prerequisites

Installation

pip install openai-agents python-dotenv 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 asyncio
import os
 
from dotenv import load_dotenv
 
load_dotenv()
 
from agents import Agent, Runner, set_tracing_disabled
from agents.mcp import MCPServerStreamableHttp
from openai import AsyncOpenAI
 
from auth import MadgicxConfidentialClientAuth
 
MADGICX_MCP_URL = os.getenv("MADGICX_MCP_URL", "https://mcp.madgicx.com/mcp")
 
 
async def main():
    auth = MadgicxConfidentialClientAuth()
 
    mcp_server = MCPServerStreamableHttp(
        name="madgicx",
        params={
            "url": MADGICX_MCP_URL,
            "headers": auth.headers,
        },
        cache_tools_list=True,
    )
 
    async with mcp_server:
        agent = Agent(
            name="Marketing Expert",
            instructions="You are a Facebook Marketing Expert powered by Madgicx tools.",
            mcp_servers=[mcp_server],
            model="gpt-4o",
        )
 
        result = await Runner.run(agent, "What ad accounts do I have access to?")
        print(result.final_output)
 
 
if __name__ == "__main__":
    asyncio.run(main())

How It Works

  1. MadgicxConfidentialClientAuth obtains an access token using your client credentials
  2. The token is passed as an HTTP header to MCPServerStreamableHttp
  3. The agent discovers all available Madgicx tools via the MCP protocol
  4. When the agent calls a tool, the SDK sends the request to the MCP server with the Bearer token

Dependencies

[project]
requires-python = ">=3.10,<3.14"
dependencies = [
    "openai-agents>=0.0.16",
    "python-dotenv>=1.0.0",
    "requests>=2.31.0",
]

On this page