Meta Ads (Facebook/Instagram) MCP server for Claude, ChatGPT, Perplexity & Cursor — the Meta node of Pipeboard’s 5-platform family (+ Google, TikTok, Snap, Reddit). Hosted remote MCP, badged Meta Business Partner, free plan — no self-hosting required.
Python advertisingai-agentschatgptclaudefacebook-adsinstagram-adsmcpmeta-adsmodel-context-protocolpipeboard
Implement search_ads_archive functionality and update version to 0.3.2
Yves Junqueira2fa1cc4ebd54
diff --git a/meta_ads_mcp/core/__init__.py b/meta_ads_mcp/core/__init__.pyindex 0c7e508..452d654 100644--- a/meta_ads_mcp/core/__init__.py+++ b/meta_ads_mcp/core/__init__.py@@ -9,6 +9,7 @@ from .insights import get_insights, debug_image_downloadfrom .authentication import get_login_linkfrom .server import login_cli, mainfrom .auth import login+from .ads_library import search_ads_archive__all__ = ['mcp_server',@@ -31,4 +32,5 @@ __all__ = ['login_cli','login','main',+ 'search_ads_archive',]\ No newline at end of filediff --git a/meta_ads_mcp/core/ads_library.py b/meta_ads_mcp/core/ads_library.pynew file mode 100644index 0000000..d5991dc--- /dev/null+++ b/meta_ads_mcp/core/ads_library.py@@ -0,0 +1,69 @@+"""Adds Library-related functionality for Meta Ads API."""++import json+from typing import Optional, List, Dict, Any+from .api import meta_api_tool, make_api_request+from .server import mcp_server+++@mcp_server.tool()+@meta_api_tool+async def search_ads_archive(+ access_token: str = None,+ search_terms: str = None,+ ad_type: str = "ALL",+ ad_reached_countries: List[str] = None,+ limit: int = 25, # Default limit, adjust as needed+ fields: str = "ad_creation_time,ad_creative_body,ad_creative_link_caption,ad_creative_link_description,ad_creative_link_title,ad_delivery_start_time,ad_delivery_stop_time,ad_snapshot_url,currency,demographic_distribution,funding_entity,impressions,page_id,page_name,publisher_platform,region_distribution,spend"+) -> str:+ """+ Search the Facebook Ads Library archive.++ Args:+ access_token: Meta API access token (optional - will use cached token if not provided).+ search_terms: The search query for ads.+ ad_type: Type of ads to search for (e.g., POLITICAL_AND_ISSUE_ADS, HOUSING_ADS, ALL).+ ad_reached_countries: List of country codes (e.g., ["US", "GB"]).+ limit: Maximum number of ads to return.+ fields: Comma-separated string of fields to retrieve for each ad.++ Example Usage via curl equivalent:+ curl -G \\+ -d "search_terms='california'" \\+ -d "ad_type=POLITICAL_AND_ISSUE_ADS" \\+ -d "ad_reached_countries=['US']" \\+ -d "fields=ad_snapshot_url,spend" \\+ -d "access_token=<ACCESS_TOKEN>" \\+ "https://graph.facebook.com/<API_VERSION>/ads_archive"+ """+ if not access_token:+ # Attempt to get token implicitly if not provided - meta_api_tool handles this+ pass++ if not search_terms:+ return json.dumps({"error": "search_terms parameter is required"}, indent=2)++ if not ad_reached_countries:+ return json.dumps({"error": "ad_reached_countries parameter is required"}, indent=2)++ endpoint = "ads_archive"+ params = {+ "search_terms": search_terms,+ "ad_type": ad_type,+ "ad_reached_countries": json.dumps(ad_reached_countries), # API expects a JSON array string+ "limit": limit,+ "fields": fields,+ }++ try:+ data = await make_api_request(endpoint, access_token, params, method="GET")+ return json.dumps(data, indent=2)+ except Exception as e:+ error_msg = str(e)+ # Consider logging the full error for debugging+ # print(f"Error calling Ads Library API: {error_msg}")+ return json.dumps({+ "error": "Failed to search ads archive",+ "details": error_msg,+ "params_sent": {k: v for k, v in params.items() if k != 'access_token'} # Avoid logging token+ }, indent=2)\ No newline at end of filediff --git a/meta_ads_mcp/core/campaigns.py b/meta_ads_mcp/core/campaigns.pyindex 0b51dc3..7758911 100644--- a/meta_ads_mcp/core/campaigns.py+++ b/meta_ads_mcp/core/campaigns.py@@ -23,7 +23,9 @@ async def get_campaigns(access_token: str = None, account_id: str = None, limit:access_token: Meta API access token (optional - will use cached token if not provided)account_id: Meta Ads account ID (format: act_XXXXXXXXX)limit: Maximum number of campaigns to return (default: 10)- status_filter: Filter by status (empty for all, or 'ACTIVE', 'PAUSED', etc.)+ status_filter: Filter by effective status (e.g., 'ACTIVE', 'PAUSED', 'ARCHIVED').+ Maps to the 'effective_status' API parameter, which expects an array+ (this function handles the required JSON formatting). Leave empty for all statuses."""# If no account ID is specified, try to get the first one for the userif not account_id:@@ -42,7 +44,8 @@ async def get_campaigns(access_token: str = None, account_id: str = None, limit:}if status_filter:- params["effective_status"] = status_filter+ # API expects an array, encode it as a JSON string+ params["effective_status"] = json.dumps([status_filter])data = await make_api_request(endpoint, access_token, params)diff --git a/uv.lock b/uv.lockindex 6d4caa6..19d60d5 100644--- a/uv.lock+++ b/uv.lock@@ -206,7 +206,7 @@ wheels = [[[package]]name = "meta-ads-mcp"-version = "0.3.1"+version = "0.3.2"source = { editable = "." }dependencies = [{ name = "httpx" },