Building AI Agents for Facebook Ads Management
Learn how to build AI agents that manage Facebook ad campaigns using the Model Context Protocol (MCP). Step-by-step setup with Claude Desktop and Xylo's MCP server.
Why AI Agents for Ad Management?
Managing Facebook ad campaigns at scale is repetitive, data-heavy work. Check performance metrics. Pause underperformers. Adjust budgets. Create new variations. Report results. Every one of these tasks follows patterns that AI agents can learn and execute.
The challenge has always been access. Large language models like Claude and GPT-4 are excellent at reasoning about data and making decisions, but they cannot interact with the Meta Ads API on their own. They need a structured interface -- a way to read campaign data, execute changes, and verify results.
That is exactly what the Model Context Protocol (MCP) provides.
What Is MCP?
The Model Context Protocol is an open standard that lets AI models interact with external tools and data sources. Think of it as a structured API specifically designed for AI consumption. Instead of requiring the AI to construct HTTP requests and parse responses, MCP provides typed tools with clear descriptions that the model can invoke directly.
For ad management, this means an AI agent can:
- List all active campaigns and their performance metrics
- Pause or activate specific campaigns
- Adjust daily budgets up or down
- Read audience targeting details
- Create new ad sets with specified parameters
All through natural conversation, with the MCP server handling the actual API calls.
The Xylo MCP Server
Xylo provides an MCP server that exposes Meta Ads API functionality as MCP tools. The server sits between the AI model and the Xylo REST API, translating natural language requests into structured API calls.
Here is the architecture:
User (natural language)
|
v
AI Model (Claude, GPT-4, etc.)
|
v
Xylo MCP Server (tool definitions)
|
v
Xylo REST API
|
v
Meta Graph API v22.0
Each layer adds structure. The user says "pause campaigns spending more than $50 with no conversions." The AI model breaks that into steps: list campaigns, filter by spend and conversions, pause the matching ones. The MCP server provides the tools for each step. The REST API handles authentication, rate limiting, and response normalization.
Setting Up Claude Desktop with Xylo MCP
Prerequisites
- A Xylo account with a connected Meta ad account
- A Xylo API key (generate one from the dashboard)
- Claude Desktop installed (version 3.0 or later)
Step 1: Configure the MCP Server
Open your Claude Desktop configuration file. On macOS, this is located at ~/Library/Application Support/Claude/claude_desktop_config.json. On Windows, check %APPDATA%/Claude/claude_desktop_config.json.
Add the Xylo MCP server:
{
"mcpServers": {
"xylo-ads": {
"command": "npx",
"args": ["-y", "@xylo/mcp-server"],
"env": {
"XYLO_API_KEY": "xylo_live_your_key_here",
"XYLO_AD_ACCOUNT": "act_123456789"
}
}
}
}
Step 2: Restart Claude Desktop
After saving the configuration, restart Claude Desktop. You should see the Xylo tools appear in the tools panel. The available tools include:
list_campaigns-- get all campaigns with performance dataget_campaign-- get details for a specific campaignupdate_campaign-- modify campaign status, budget, or namelist_ad_sets-- get ad sets with targeting and performancelist_ads-- get individual ads and their metricsget_insights-- pull detailed performance analyticslist_audiences-- view custom and lookalike audiences
Step 3: Start Managing Ads
Now you can interact with your ad campaigns through natural conversation. Here are some examples.
Example: Performance Review
You: How are my campaigns doing this week?
Claude: Let me check your campaign performance for the last 7 days.
[Calls list_campaigns with date_preset=last_7d]
You have 8 active campaigns. Here is a summary:
Campaign Spend Conversions CPA Summer Sale Prospecting $1,247.30 89 $14.01 Retargeting - Cart Abandoners $432.10 67 $6.45 Brand Awareness - Q2 $890.50 12 $74.21 Lookalike - Top Buyers $567.20 43 $13.19 ... ... ... ... Your retargeting campaign has the best CPA at $6.45. The Brand Awareness campaign is expensive at $74.21 per conversion, but that may be expected for a top-of-funnel objective.
Example: Budget Optimization
You: Move $200 of daily budget from Brand Awareness to the retargeting campaign.
Claude: I will adjust the budgets. Let me confirm the current values first.
[Calls get_campaign for both campaigns]
Current budgets:
- Brand Awareness - Q2: $150/day
- Retargeting - Cart Abandoners: $75/day
You want to move $200, but Brand Awareness only has a $150 daily budget. I can reduce it to $50/day and increase retargeting to $175/day, which moves $100. Or I can pause Brand Awareness entirely and move the full $150 to retargeting. Which would you prefer?
Example: Creating Ad Variations
You: Create a new ad set in the Summer Sale campaign targeting women 25-44 in California interested in fashion, with a $50 daily budget.
Claude: I will create that ad set now.
[Calls create_ad_set with the specified parameters]
Done. Created ad set "Summer Sale - Women 25-44 CA Fashion" with:
- Daily budget: $50.00
- Targeting: Women, ages 25-44, California, interest: Fashion
- Placements: Automatic (Facebook + Instagram)
- Status: Paused (ready for your review before going live)
Would you like me to activate it, or do you want to review the targeting first?
Building Custom Agents
Beyond Claude Desktop, you can build custom AI agents that use Xylo's MCP server programmatically. This is useful for automated workflows that run on a schedule.
Automated Daily Reports
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
// Connect to Xylo MCP server
const transport = new StdioClientTransport({
command: "npx",
args: ["-y", "@xylo/mcp-server"],
env: {
XYLO_API_KEY: process.env.XYLO_API_KEY,
XYLO_AD_ACCOUNT: process.env.XYLO_AD_ACCOUNT,
},
});
const client = new Client({ name: "ad-reporter", version: "1.0.0" });
await client.connect(transport);
// Fetch campaign data
const result = await client.callTool({
name: "list_campaigns",
arguments: { date_preset: "yesterday" },
});
// Process and send report...
Automated Rules Engine
You can combine MCP tools with custom logic to build rule-based automation:
// Fetch all active campaigns
const campaigns = await client.callTool({
name: "list_campaigns",
arguments: { status: "active", date_preset: "last_3d" },
});
// Apply rules
for (const campaign of campaigns.data) {
const { spend, conversions, cpa } = campaign.insights;
// Rule: pause campaigns with high spend and zero conversions
if (spend > 100 && conversions === 0) {
await client.callTool({
name: "update_campaign",
arguments: {
campaign_id: campaign.id,
status: "paused",
},
});
console.log(`Paused ${campaign.name}: $${spend} spent, 0 conversions`);
}
// Rule: increase budget for campaigns beating CPA target
if (conversions > 10 && cpa < TARGET_CPA * 0.7) {
const newBudget = campaign.daily_budget * 1.2;
await client.callTool({
name: "update_campaign",
arguments: {
campaign_id: campaign.id,
daily_budget: newBudget,
},
});
console.log(`Increased ${campaign.name} budget to $${newBudget}`);
}
}
Why MCP Over Direct REST Calls for AI?
You might wonder: why not just have the AI agent make REST calls directly? The advantages of MCP are:
-
Type safety. MCP tools have schemas that tell the AI exactly what parameters are available and what types they expect. No guessing at field names.
-
Tool discovery. The AI can list available tools and read their descriptions. It does not need to know API endpoints in advance.
-
Error handling. MCP provides structured error responses that the AI can reason about and recover from, rather than trying to parse HTTP status codes and error bodies.
-
Conversation context. MCP maintains state across tool calls within a conversation. The AI can reference results from previous calls without re-fetching.
-
Safety. MCP tools can be audited and restricted. You can expose read-only tools for reporting agents and read-write tools for management agents.
Security Considerations
When building AI agents that manage real ad spend, security matters:
- Use separate API keys for different agents with appropriate permissions.
- Start with read-only access. Get comfortable with reporting before enabling write operations.
- Set budget limits. Use Meta's campaign spending limits as a safety net.
- Log everything. Xylo logs all API calls in the usage dashboard so you can audit agent actions.
- Review before activating. Have agents create campaigns in a paused state and require human approval before going live.
Getting Started
- Sign up for Xylo and connect your Meta ad account.
- Generate an API key from the dashboard.
- Follow the Claude Desktop setup above, or read the MCP protocol explainer for deeper context.
- Start with read-only queries to build confidence, then graduate to campaign management.
The combination of AI reasoning and structured API access is transformative for ad management. Instead of clicking through the Ads Manager interface or writing complex scripts, you describe what you want in plain English and let the agent handle the rest.
Ready to simplify your ads API integration?
Get started with Xylo in minutes. One API key for every ad platform.