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
refactor: extract ensure_act_prefix helper for account ID normalization (#51)
Yves Junqueira804a0156f39a
diff --git a/meta_ads_mcp/core/accounts.py b/meta_ads_mcp/core/accounts.pyindex f538116..8336b62 100644--- a/meta_ads_mcp/core/accounts.py+++ b/meta_ads_mcp/core/accounts.py@@ -2,7 +2,7 @@import jsonfrom typing import Optional, Dict, Any-from .api import meta_api_tool, make_api_request+from .api import meta_api_tool, make_api_request, ensure_act_prefixfrom .server import mcp_server# Currencies that have no sub-units (i.e., are not denominated in cents).@@ -86,9 +86,7 @@ async def get_account_info(account_id: str, access_token: Optional[str] = None)}}- # Ensure account_id has the 'act_' prefix for API compatibility- if not account_id.startswith("act_"):- account_id = f"act_{account_id}"+ account_id = ensure_act_prefix(account_id)# Try to get the account info directly firstendpoint = f"{account_id}"diff --git a/meta_ads_mcp/core/ads.py b/meta_ads_mcp/core/ads.pyindex df3a972..c3df2ba 100644--- a/meta_ads_mcp/core/ads.py+++ b/meta_ads_mcp/core/ads.py@@ -11,7 +11,7 @@ import timelogger = logging.getLogger(__name__)-from .api import meta_api_tool, make_api_request+from .api import meta_api_tool, make_api_request, ensure_act_prefixfrom .accounts import get_ad_accounts# ---------------------------------------------------------------------------@@ -945,10 +945,8 @@ async def upload_ad_image(if not file and not image_url:return json.dumps({"error": "Provide either 'file' (data URL or base64) or 'image_url'"}, indent=2)- # Ensure account_id has the 'act_' prefix for API compatibility- if not account_id.startswith("act_"):- account_id = f"act_{account_id}"-+ account_id = ensure_act_prefix(account_id)+try:# Determine encoded_image (base64 string without data URL prefix) and a sensible nameencoded_image: str = ""@@ -1330,10 +1328,8 @@ async def create_ad_creative(if not name:name = f"Creative {int(time.time())}"- # Ensure account_id has the 'act_' prefix- if not account_id.startswith("act_"):- account_id = f"act_{account_id}"-+ account_id = ensure_act_prefix(account_id)+# Enhanced page discovery: If no page ID is provided, use robust discovery methodsif not page_id:try:@@ -2076,10 +2072,8 @@ async def _search_pages_by_name_core(access_token: str, account_id: str, search_Returns:JSON string with search results"""- # Ensure account_id has the 'act_' prefix- if not account_id.startswith("act_"):- account_id = f"act_{account_id}"-+ account_id = ensure_act_prefix(account_id)+try:# Use the internal discovery function directlypage_discovery_result = await _discover_pages_for_account(account_id, access_token)@@ -2187,9 +2181,7 @@ async def get_account_pages(account_id: str, access_token: Optional[str] = None)"details": str(e)}, indent=2)- # Ensure account_id has the 'act_' prefix for regular accounts- if not account_id.startswith("act_"):- account_id = f"act_{account_id}"+ account_id = ensure_act_prefix(account_id)try:# Collect all page IDs from multiple approachesdiff --git a/meta_ads_mcp/core/adsets.py b/meta_ads_mcp/core/adsets.pyindex 7448d50..435c092 100644--- a/meta_ads_mcp/core/adsets.py+++ b/meta_ads_mcp/core/adsets.py@@ -2,7 +2,7 @@import jsonfrom typing import Optional, Dict, Any, List-from .api import meta_api_tool, make_api_request+from .api import meta_api_tool, make_api_request, ensure_act_prefixfrom .accounts import get_ad_accountsfrom .server import mcp_server@@ -22,7 +22,9 @@ async def get_adsets(account_id: str, access_token: Optional[str] = None, limit:# Require explicit account_idif not account_id:return json.dumps({"error": "No account ID specified"}, indent=2)-++ account_id = ensure_act_prefix(account_id)+# Change endpoint based on whether campaign_id is providedif campaign_id:endpoint = f"{campaign_id}/adsets"@@ -162,7 +164,9 @@ async def create_adset(# Check required parametersif not account_id:return json.dumps({"error": "No account ID provided"}, indent=2)-++ account_id = ensure_act_prefix(account_id)+if not campaign_id:return json.dumps({"error": "No campaign ID provided"}, indent=2)diff --git a/meta_ads_mcp/core/api.py b/meta_ads_mcp/core/api.pyindex 2b618fd..015af64 100644--- a/meta_ads_mcp/core/api.py+++ b/meta_ads_mcp/core/api.py@@ -23,6 +23,13 @@ class McpToolError(Exception):pass+def ensure_act_prefix(account_id: str) -> str:+ """Ensure account_id has the 'act_' prefix required by Meta's Graph API."""+ if account_id and not account_id.startswith("act_"):+ return f"act_{account_id}"+ return account_id++# ConstantsMETA_GRAPH_API_VERSION = "v24.0"META_GRAPH_API_BASE = f"https://graph.facebook.com/{META_GRAPH_API_VERSION}"diff --git a/meta_ads_mcp/core/campaigns.py b/meta_ads_mcp/core/campaigns.pyindex 4a4ec5a..c6b638c 100644--- a/meta_ads_mcp/core/campaigns.py+++ b/meta_ads_mcp/core/campaigns.py@@ -2,7 +2,7 @@import jsonfrom typing import List, Optional, Dict, Any, Union-from .api import meta_api_tool, make_api_request+from .api import meta_api_tool, make_api_request, ensure_act_prefixfrom .accounts import get_ad_accountsfrom .server import mcp_server@@ -43,7 +43,8 @@ async def get_campaigns(# Require explicit account_idif not account_id:return json.dumps({"error": "No account ID specified"}, indent=2)-++ account_id = ensure_act_prefix(account_id)endpoint = f"{account_id}/campaigns"params = {"fields": "id,name,objective,status,daily_budget,lifetime_budget,buying_type,start_time,stop_time,created_time,updated_time,bid_strategy,special_ad_categories",@@ -167,7 +168,9 @@ async def create_campaign(if not objective:return json.dumps({"error": "No campaign objective provided"}, indent=2)-++ account_id = ensure_act_prefix(account_id)+# Track whether the user explicitly provided special_ad_categories_user_provided_categories = special_ad_categories is not Nonediff --git a/meta_ads_mcp/core/openai_deep_research.py b/meta_ads_mcp/core/openai_deep_research.pyindex 25724bc..91db412 100644--- a/meta_ads_mcp/core/openai_deep_research.py+++ b/meta_ads_mcp/core/openai_deep_research.py@@ -11,7 +11,7 @@ and fetchable records for ChatGPT Deep Research analysis.import jsonimport refrom typing import List, Dict, Any, Optional-from .api import meta_api_tool, make_api_request+from .api import meta_api_tool, make_api_request, ensure_act_prefixfrom .server import mcp_serverfrom .utils import logger@@ -83,9 +83,7 @@ class MetaAdsDataManager:# Import the page discovery function from ads modulefrom .ads import _discover_pages_for_account- # Ensure account_id has the 'act_' prefix- if not account_id.startswith("act_"):- account_id = f"act_{account_id}"+ account_id = ensure_act_prefix(account_id)page_discovery_result = await _discover_pages_for_account(account_id, access_token)diff --git a/meta_ads_mcp/core/reports.py b/meta_ads_mcp/core/reports.pyindex 87536bc..595f04f 100644--- a/meta_ads_mcp/core/reports.py+++ b/meta_ads_mcp/core/reports.py@@ -3,7 +3,7 @@import jsonimport osfrom typing import Optional, Dict, Any, List, Union-from .api import meta_api_tool+from .api import meta_api_tool, ensure_act_prefixfrom .server import mcp_server@@ -52,7 +52,9 @@ if ENABLE_REPORT_GENERATION:"format": "act_XXXXXXXXX"}}, indent=2)-++ account_id = ensure_act_prefix(account_id)+# For campaign and comparison reports, campaign_ids are requiredif report_type in ["campaign", "comparison"] and not campaign_ids:return json.dumps({