Research & Benchmarks Optimization AI Ads Analytics Why Madgicx? About Us Blog Careers Case Studies Events Glossary Guides Answers Pricing Login Try for Free
MCP docs menu

Connect the OpenAI Agents SDK to Madgicx MCP

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

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://app.madgicx.com/o/token/

# Choose ONE of the MCP server URLs (or run two clients to use both):
MADGICX_MCP_URL=https://mcp.madgicx.com/mcp                # Facebook Ads
# MADGICX_MCP_URL=https://mcp-google-ads.madgicx.com/mcp   # Google Ads

# LLM provider
OPENAI_API_KEY=your_openai_key

Connecting to both MCP servers

To use both Facebook Ads and Google Ads tools in the same agent, instantiate two MCPServerStreamableHttp instances (one per URL) and pass both to mcp_servers=[fb_server, google_server]. The same auth.headers works for both.

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",
]
Official Meta Business Partner

Connect your AI assistant to your ad accounts. The 7-day free trial includes both MCPs.

Sign in with your Madgicx login, add the server addresses and ask in plain words. Meta changes wait for your confirmation, and the Google Ads MCP only reads.

Try for Free ($0 Trial)
200,000+ advertisers
$10B+ annual ad spend visibility
4.6 / 5 on G2, from 220+ reviews