Build AI agents with the OpenAI Agents SDK that have access to all Madgicx MCP tools.
Prerequisites
- Python 3.10+
- Madgicx client credentials
- An LLM API key (e.g., OpenAI)
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
MadgicxConfidentialClientAuthobtains an access token using your client credentials- The token is passed as an HTTP header to
MCPServerStreamableHttp - The agent discovers all available Madgicx tools via the MCP protocol
- 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",
]