Facebook Ads MCP Server

Facebook Ads MCP Server

Connects AI assistants to Facebook's Ads API to enable natural language queries for campaign data, insights, and performance metrics. It allows users to manage ad accounts and retrieve detailed analytics like impressions, clicks, and spend through MCP-compatible interfaces.

Category
访问服务器

README

Facebook Ads MCP Server

A Model Context Protocol (MCP) server that connects AI assistants to Facebook's Ads API. This enables natural language queries to fetch campaign data, insights, and ad performance metrics directly through Claude or other MCP-compatible AI assistants.

Features

  • Account Management: List and view details of ad accounts
  • Campaign Tools: Retrieve campaigns, filter by status, get campaign details
  • Ad Set Tools: Access ad sets within campaigns with targeting information
  • Ad Tools: View individual ads and their creative details
  • Insights & Analytics: Get performance metrics (impressions, clicks, spend, CTR, etc.)
  • Pagination Support: Handle large datasets with pagination helpers
  • Error Handling: Comprehensive error messages and rate limit handling

Prerequisites

  • Python 3.10 or higher
  • Facebook Developer account with Ads API access
  • Facebook Access Token with ads_read permission

Installation

1. Clone or Download

cd fb-ads-mcp-server

2. Create Virtual Environment

python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

3. Install Dependencies

pip install -r requirements.txt

Getting Your Facebook Access Token

Option 1: Graph API Explorer (Quick Testing)

  1. Go to Facebook Graph API Explorer
  2. Select your app or create a new one
  3. Click "Generate Access Token"
  4. Add the ads_read permission
  5. Copy the generated token

Note: Tokens from Graph API Explorer expire in 1-2 hours. For production use, implement a proper OAuth flow.

Option 2: Meta Developer Portal (Long-lived Tokens)

  1. Go to Meta for Developers
  2. Create or select an app
  3. Navigate to ToolsAccess Token Tool
  4. Generate a User Access Token with ads_read permission
  5. Optionally exchange for a long-lived token (60 days)

Required Permissions

  • ads_read - Read access to ad accounts, campaigns, and insights

Running the Server

Local Testing

python server.py --fb-token YOUR_FACEBOOK_ACCESS_TOKEN

The server will start and listen for MCP protocol requests.

Claude Desktop Configuration

To use this MCP server with Claude Desktop, add the following to your Claude configuration file:

macOS/Linux

Edit: ~/Library/Application Support/Claude/claude_desktop_config.json

{
  "mcpServers": {
    "fb-ads-mcp-server": {
      "command": "python",
      "args": [
        "/absolute/path/to/fb-ads-mcp-server/server.py",
        "--fb-token",
        "YOUR_FACEBOOK_ACCESS_TOKEN"
      ]
    }
  }
}

Windows

Edit: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "fb-ads-mcp-server": {
      "command": "python",
      "args": [
        "C:\\absolute\\path\\to\\fb-ads-mcp-server\\server.py",
        "--fb-token",
        "YOUR_FACEBOOK_ACCESS_TOKEN"
      ]
    }
  }
}

Important: Replace /absolute/path/to/fb-ads-mcp-server/server.py with the actual absolute path to your server.py file.

Using Virtual Environment with Claude Desktop

If you want to use the virtual environment with Claude Desktop:

{
  "mcpServers": {
    "fb-ads-mcp-server": {
      "command": "/absolute/path/to/fb-ads-mcp-server/venv/bin/python",
      "args": [
        "/absolute/path/to/fb-ads-mcp-server/server.py",
        "--fb-token",
        "YOUR_FACEBOOK_ACCESS_TOKEN"
      ]
    }
  }
}

Available Tools

Account Management

  • list_ad_accounts() - Lists all ad accounts linked to your access token
  • get_details_of_ad_account(act_id, fields) - Get detailed information about a specific ad account

Campaign Management

  • get_campaigns_by_adaccount(act_id, fields, limit, filtering) - Retrieve all campaigns for an ad account
  • get_campaign_by_id(campaign_id, fields) - Get details for a specific campaign

Ad Set Management

  • get_adsets_by_campaign(campaign_id, fields, limit) - Retrieve all ad sets within a campaign
  • get_adset_by_id(adset_id, fields) - Get details for a specific ad set

Ad Management

  • get_ads_by_adset(adset_id, fields, limit) - Retrieve all ads within an ad set
  • get_ad_by_id(ad_id, fields) - Get details for a specific ad

Insights & Analytics

  • get_campaign_insights(campaign_id, fields, date_preset, time_range) - Get performance metrics for a campaign
  • get_adset_insights(adset_id, fields, date_preset) - Get performance metrics for an ad set
  • get_ad_insights(ad_id, fields, date_preset) - Get performance metrics for a specific ad
  • get_summary_report(act_id, date_preset, time_range, limit) - Get lightweight summary with only key metrics (optimized to avoid context limits)

Pagination

  • fetch_pagination_url(url) - Fetch next/previous page of results

Usage Examples

Once configured with Claude Desktop, you can ask natural language questions:

Basic Queries

"List my Facebook ad accounts"
"Show me all campaigns for account act_123456789"
"Get the active campaigns for my ad account"

Filtering & Details

"Show me only active campaigns for account act_123456789"
"Get details for campaign 120210000000001"
"What ad sets are in campaign 120210000000001?"

Insights & Analytics

"Show me insights for campaign 120210000000001 for the last 7 days"
"Get performance metrics for ad set 120210000000002"
"What's the CTR and spend for ad 120210000000003?"

Custom Fields

"Get campaigns with fields: name, status, budget_remaining, created_time"
"Show ad account details with fields: name, currency, timezone_name"

Date Ranges

"Get campaign insights for last_30d"
"Show me lifetime performance for this ad set"
"Get insights from 2024-01-01 to 2024-01-31"

Common Field Names

Campaign Fields

  • name, objective, status, effective_status
  • daily_budget, lifetime_budget, budget_remaining
  • created_time, updated_time, start_time, stop_time

Ad Set Fields

  • name, effective_status, optimization_goal
  • daily_budget, lifetime_budget
  • targeting, bid_amount, billing_event

Ad Fields

  • name, effective_status, creative
  • tracking_specs, conversion_specs

Insight Metrics

  • impressions, clicks, spend, reach, frequency
  • cpc (Cost Per Click), cpm (Cost Per 1000 Impressions)
  • ctr (Click-Through Rate), conversions, cost_per_conversion

Date Presets

  • today, yesterday
  • last_7d, last_14d, last_30d, last_90d
  • this_month, last_month
  • this_quarter, last_quarter
  • this_year, last_year
  • lifetime

Error Handling

The server includes comprehensive error handling for:

  • Missing Token: Clear error message if --fb-token is not provided
  • Invalid Token: Facebook API errors are caught and formatted clearly
  • Rate Limits: HTTP 429 errors are properly handled
  • Network Errors: Timeout and connection errors include helpful messages
  • Invalid Parameters: Type validation and required field checking

Security Best Practices

  1. Never Commit Tokens: The .gitignore file excludes .env files and logs
  2. Minimum Permissions: Use tokens with only ads_read permission for read-only access
  3. Token Rotation: Regularly rotate your access tokens
  4. Secure Storage: Store tokens securely; avoid hardcoding in configuration files
  5. Monitor Usage: Check your Meta Developer dashboard for unusual API activity

Troubleshooting

"Facebook access token not provided"

Make sure you're running the server with the --fb-token argument:

python server.py --fb-token YOUR_TOKEN

"Invalid OAuth access token"

Your token may have expired. Generate a new token from the Graph API Explorer or Meta Developer Portal.

"Unsupported get request"

Check that your account IDs are in the correct format (e.g., act_123456789).

Rate Limit Errors

Facebook has rate limits on API calls. If you hit limits, wait a few minutes before retrying. Consider implementing caching for frequently accessed data.

API Version

This server uses Facebook Graph API v22.0. Check Facebook's API Changelog for version updates.

Limitations

  • Read-Only: This server only supports read operations. Write operations (creating/updating campaigns) require additional permissions and implementation.
  • Token Expiration: Short-lived tokens expire in 1-2 hours. Implement token refresh logic for production use.
  • Rate Limits: Subject to Facebook's API rate limits based on your app tier.

Future Enhancements

  • Token refresh automation
  • Write operations (create/update campaigns, ad sets, ads)
  • Webhook support for real-time updates
  • Response caching layer
  • Bulk operations for efficiency
  • Custom audience management
  • Ad creative operations
  • Activity/change history tracking

Contributing

Contributions are welcome! Areas for improvement:

  • Enhanced error messages and retry logic
  • Additional API endpoints (custom audiences, pixels, etc.)
  • Performance optimizations and caching
  • Write operation support
  • Automated testing suite

License

This project is provided as-is for educational and development purposes.

Resources

Support

For issues related to:


Happy advertising! 🚀

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选