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
Enhance get_account_pages function to improve page retrieval logic
Yves Junqueira388a3ee377e5
diff --git a/meta_ads_mcp/core/ads.py b/meta_ads_mcp/core/ads.pyindex 3ea2b5d..40b35ad 100644--- a/meta_ads_mcp/core/ads.py+++ b/meta_ads_mcp/core/ads.py@@ -713,17 +713,91 @@ async def get_account_pages(access_token: str = None, account_id: str = None) ->if not account_id.startswith("act_"):account_id = f"act_{account_id}"- # Get pages associated with the account- endpoint = f"{account_id}/assigned_pages"- params = {- "fields": "id,name,username,category,fan_count,link,verification_status,picture"- }-try:- data = await make_api_request(endpoint, access_token, params)- return json.dumps(data, indent=2)- except Exception as e:+ # First attempt: Use the client_pages endpoint+ endpoint = f"{account_id}/client_pages"+ params = {+ "fields": "id,name,username,category,fan_count,link,verification_status,picture"+ }++ client_pages_data = await make_api_request(endpoint, access_token, params)++ # If successful with data, return it+ if "data" in client_pages_data and client_pages_data["data"]:+ return json.dumps(client_pages_data, indent=2)++ # Second attempt: Get active ads and extract page IDs+ endpoint = f"{account_id}/ads"+ params = {+ "fields": "creative{object_story_spec{page_id}}",+ "status": "ACTIVE",+ "limit": 100+ }++ ads_data = await make_api_request(endpoint, access_token, params)++ # Extract unique page IDs from ads+ page_ids = set()+ if "data" in ads_data:+ for ad in ads_data.get("data", []):+ if "creative" in ad and "object_story_spec" in ad["creative"] and "page_id" in ad["creative"]["object_story_spec"]:+ page_ids.add(ad["creative"]["object_story_spec"]["page_id"])++ # Fetch details for each page if we found any+ if page_ids:+ page_details = {"data": []}++ for page_id in page_ids:+ page_endpoint = f"{page_id}"+ page_params = {+ "fields": "id,name,username,category,fan_count,link,verification_status,picture"+ }++ page_data = await make_api_request(page_endpoint, access_token, page_params)+ if "id" in page_data:+ page_details["data"].append(page_data)++ if page_details["data"]:+ return json.dumps(page_details, indent=2)++ # Third attempt: Retrieve the account's client ad accounts+ endpoint = f"me/adaccounts"+ params = {+ "fields": "id,name,account_id",+ "limit": 10+ }++ account_data = await make_api_request(endpoint, access_token, params)++ # Return whatever we got, even if empty or errorreturn json.dumps({- "error": "Failed to get account pages",- "details": str(e)- }, indent=2)\ No newline at end of file+ "message": "Retrieved account data instead of pages",+ "account_data": account_data+ }, indent=2)++ except Exception as e:+ try:+ # Final fallback - try getting user pages directly+ endpoint = "me/accounts"+ params = {+ "fields": "id,name,username,category,fan_count,link,verification_status,picture,access_token"+ }++ user_pages_data = await make_api_request(endpoint, access_token, params)++ # Return the user's pages if available+ if "data" in user_pages_data and user_pages_data["data"]:+ return json.dumps(user_pages_data, indent=2)++ # If we got here, all attempts failed+ return json.dumps({+ "error": "Could not find any pages associated with this account",+ "details": str(e),+ "suggestion": "Check if your access token has 'pages_show_list' permission."+ }, indent=2)++ except Exception as e2:+ return json.dumps({+ "error": "Failed to get any pages after multiple attempts",+ "details": f"Original error: {str(e)}. Fallback error: {str(e2)}"+ }, indent=2)\ No newline at end of file