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 future improvements documentation and update ad set functionality
Yves Junqueira4b4daa02d6aa
diff --git a/future_improvements.md b/future_improvements.mdindex 598cf90..d39d15d 100644--- a/future_improvements.md+++ b/future_improvements.md@@ -1,5 +1,32 @@# Future Improvements for Meta Ads MCP+## Access token should remain internal only++Don't share it ever with the LLM, only update the auth cache.++## Frequency Cap Visibility Issue++Currently, our tools are unable to view frequency cap settings after they are applied to ad sets. While we can set frequency caps through the `update_adset` function, the subsequent API calls (`get_adsets`, `get_adset_details`) do not show these settings in their responses. This makes it difficult to verify if frequency caps were successfully applied. We need to:++1. Investigate why frequency cap settings are not visible in API responses+2. Determine if this is a limitation of the Meta Marketing API+3. Consider adding alternative ways to verify frequency cap settings (e.g., monitoring actual frequency metrics over time)+4. Update our documentation to clarify this limitation++## JSON File Dependency for Ad Set Updates++Currently, the `update_adset` function requires reading from a JSON file to apply changes to ad sets. This is not ideal because:++1. It requires maintaining separate JSON files for different update configurations+2. It makes the update process less transparent and harder to debug+3. It's not user-friendly for quick updates or changes++We should improve this by:+1. Allowing direct parameter passing for ad set updates+2. Supporting both JSON file and direct parameter methods+3. Adding better validation and error handling for update parameters+4. Providing clear documentation for both update methods+## Planned Improvement: Ad Set Update Confirmation Flow### Overviewdiff --git a/meta_ads_mcp/core/adsets.py b/meta_ads_mcp/core/adsets.pyindex e61622f..6baed50 100644--- a/meta_ads_mcp/core/adsets.py+++ b/meta_ads_mcp/core/adsets.py@@ -33,7 +33,7 @@ async def get_adsets(access_token: str = None, account_id: str = None, limit: inendpoint = f"{account_id}/adsets"params = {- "fields": "id,name,campaign_id,status,daily_budget,lifetime_budget,targeting,bid_amount,bid_strategy,optimization_goal,billing_event,start_time,end_time,created_time,updated_time",+ "fields": "id,name,campaign_id,status,daily_budget,lifetime_budget,targeting,bid_amount,bid_strategy,optimization_goal,billing_event,start_time,end_time,created_time,updated_time,frequency_control_specs","limit": limit}@@ -66,7 +66,7 @@ async def get_adset_details(access_token: str = None, adset_id: str = None) -> sendpoint = f"{adset_id}"params = {- "fields": "id,name,campaign_id,status,daily_budget,lifetime_budget,targeting,bid_amount,bid_strategy,optimization_goal,billing_event,start_time,end_time,created_time,updated_time,attribution_spec,destination_type,promoted_object,pacing_type,budget_remaining"+ "fields": "id,name,campaign_id,status,daily_budget,lifetime_budget,targeting,bid_amount,bid_strategy,optimization_goal,billing_event,start_time,end_time,created_time,updated_time,attribution_spec,destination_type,promoted_object,pacing_type,budget_remaining,frequency_control_specs{event,interval_days,max_frequency}"}data = await make_api_request(endpoint, access_token, params)@@ -95,25 +95,42 @@ async def update_adset(args: str = "", kwargs: str = None, access_token: str = Nif not adset_id:return json.dumps({"error": "No ad set ID provided"}, indent=2)+ # Debug print+ print(f"DEBUG: update_adset called with adset_id: {adset_id}")+ print(f"DEBUG: kwargs type: {type(kwargs)}, value: {kwargs}")+# Try to read parameters from file if not providedif not kwargs:try:with open('frequency_cap.json', 'r') as f:kwargs = f.read().strip()+ print(f"DEBUG: Read kwargs from file: {kwargs}")except (FileNotFoundError, IOError):# If file doesn't exist, use default empty objectkwargs = "{}"- # Extract optional parameters+ # Convert kwargs to dictionarytry:- if isinstance(kwargs, (dict, list)):+ if isinstance(kwargs, dict):kwargs_dict = kwargs+ print("DEBUG: kwargs is already a dictionary")+ elif isinstance(kwargs, str):+ # Try to parse as JSON+ try:+ kwargs_dict = json.loads(kwargs)+ print("DEBUG: Successfully parsed kwargs string as JSON")+ except json.JSONDecodeError as e:+ error_msg = f"Failed to parse kwargs as JSON: {str(e)}"+ print(f"DEBUG ERROR: {error_msg}")+ return json.dumps({"error": error_msg, "kwargs_received": kwargs}, indent=2)else:- kwargs_dict = json.loads(kwargs) if kwargs else {}- except (json.JSONDecodeError, TypeError) as e:- return json.dumps({"error": f"Invalid kwargs format: {str(e)}, received: {kwargs}"}, indent=2)+ error_msg = f"Unexpected kwargs type: {type(kwargs)}"+ print(f"DEBUG ERROR: {error_msg}")+ return json.dumps({"error": error_msg, "kwargs_received": str(kwargs)}, indent=2)except Exception as e:- return json.dumps({"error": f"Error parsing kwargs: {str(e)}, received: {kwargs}"}, indent=2)+ error_msg = f"Error handling kwargs: {str(e)}"+ print(f"DEBUG ERROR: {error_msg}")+ return json.dumps({"error": error_msg, "kwargs_received": str(kwargs)}, indent=2)# Build the changes dictionary directly from kwargs_dictchanges = {}@@ -124,9 +141,11 @@ async def update_adset(args: str = "", kwargs: str = None, access_token: str = Nif not changes:return json.dumps({"error": "No update parameters provided"}, indent=2)+ print(f"DEBUG: Final changes to apply: {changes}")+# Get current ad set details for comparison- current_details = await get_adset_details(args=adset_id, kwargs="", access_token=access_token)- current_details = json.loads(current_details)+ current_details_json = await get_adset_details(adset_id=adset_id, access_token=access_token)+ current_details = json.loads(current_details_json)# Start the callback server if not already runningport = start_callback_server()@@ -147,7 +166,7 @@ async def update_adset(args: str = "", kwargs: str = None, access_token: str = N"current_details": current_details,"proposed_changes": changes,"instructions_for_llm": "You must present this link as clickable Markdown to the user using the markdown_link format provided.",- "note": "A confirmation page will open in your browser. Please review and confirm the changes."+ "note": "After authenticating, the token will be automatically saved and your ad set will be updated. Refresh the browser page if it doesn't load immediately."}return json.dumps(response, indent=2)\ No newline at end of filediff --git a/meta_ads_mcp/core/api.py b/meta_ads_mcp/core/api.pyindex 05f8f68..261e5b8 100644--- a/meta_ads_mcp/core/api.py+++ b/meta_ads_mcp/core/api.py@@ -129,18 +129,33 @@ def meta_api_tool(func):# If parsing fails, treat it as a single positional argargs = (args[0],)+ # Debug print the effective args and kwargs+ print(f"DEBUG: meta_api_tool wrapper called for {func.__name__}")+ print(f"DEBUG: args: {args}")+ print(f"DEBUG: kwargs: {kwargs}")+# Check if access_token is provided in kwargsaccess_token = kwargs.get('access_token')# If not, try to get it from the auth managerif not access_token:- access_token = await get_current_access_token()+ try:+ print("DEBUG: No access_token in kwargs, trying to get from auth_manager")+ access_token = await get_current_access_token()+ if access_token:+ print(f"DEBUG: Got token from auth_manager (first 10 chars): {access_token[:10]}")+ else:+ print("DEBUG: No token available from auth_manager")+ # Set the global flag to indicate authentication is needed+ global needs_authentication+ needs_authentication = True+ except Exception as e:+ print(f"DEBUG: Error getting token: {e}")+ needs_authentication = True# If still no token, we need authentication- if not access_token:- global needs_authentication- needs_authentication = True-+ if not access_token or needs_authentication:+ print("DEBUG: Initiating authentication flow")# Start the callback serverport = start_callback_server()@@ -166,7 +181,7 @@ def meta_api_tool(func):return json.dumps(response, indent=2)- # Update kwargs with the token+ # Update kwargs with the token (this section won't be reached right now)kwargs['access_token'] = access_token# Call the original functiondiff --git a/meta_ads_mcp/core/auth.py b/meta_ads_mcp/core/auth.pyindex fce13ee..1053d6c 100644--- a/meta_ads_mcp/core/auth.py+++ b/meta_ads_mcp/core/auth.py@@ -265,8 +265,8 @@ class CallbackHandler(BaseHTTPRequestHandler):<head><title>Confirm Ad Set Update</title><style>- body { font-family: Arial, sans-serif; margin: 20px; }- .warning { color: #d73a49; margin: 20px 0; }+ body { font-family: Arial, sans-serif; margin: 20px; max-width: 1000px; margin: 0 auto; }+ .warning { color: #d73a49; margin: 20px 0; padding: 15px; border-left: 4px solid #d73a49; background-color: #fff8f8; }.changes { background: #f6f8fa; padding: 15px; border-radius: 6px; }.buttons { margin-top: 20px; }button { padding: 10px 20px; margin-right: 10px; border-radius: 6px; cursor: pointer; }@@ -275,6 +275,10 @@ class CallbackHandler(BaseHTTPRequestHandler):.diff-table { width: 100%; border-collapse: collapse; margin: 15px 0; }.diff-table td { padding: 8px; border: 1px solid #ddd; }.diff-table .header { background: #f1f8ff; font-weight: bold; }+ .status { padding: 15px; margin-top: 20px; border-radius: 6px; display: none; }+ .success { background-color: #e6ffed; border: 1px solid #2ea44f; color: #22863a; }+ .error { background-color: #ffeef0; border: 1px solid #d73a49; color: #d73a49; }+ pre { white-space: pre-wrap; word-break: break-all; }</style></head><body>@@ -292,13 +296,30 @@ class CallbackHandler(BaseHTTPRequestHandler):<tr class="header"><td>Setting</td><td>New Value</td>+ <td>Description</td></tr>- """ + "\n".join(f"""+ """++ # Special handling for frequency_control_specs+ for k, v in changes_dict.items():+ description = ""+ if k == "frequency_control_specs" and isinstance(v, list) and len(v) > 0:+ spec = v[0]+ if all(key in spec for key in ["event", "interval_days", "max_frequency"]):+ description = f"Cap to {spec['max_frequency']} {spec['event'].lower()} per {spec['interval_days']} days"++ # Format the value for display+ display_value = json.dumps(v, indent=2) if isinstance(v, (dict, list)) else str(v)++ html += f"""<tr><td>{k}</td>- <td>{v}</td>+ <td><pre>{display_value}</pre></td>+ <td>{description}</td></tr>- """ for k, v in changes_dict.items()) + """+ """++ html += """</table></div>@@ -306,9 +327,29 @@ class CallbackHandler(BaseHTTPRequestHandler):<button class="approve" onclick="approveChanges()">Approve Changes</button><button class="cancel" onclick="cancelChanges()">Cancel</button></div>++ <div id="status" class="status"></div><script>+ function showStatus(message, isError = false) {+ const statusElement = document.getElementById('status');+ statusElement.textContent = message;+ statusElement.style.display = 'block';+ if (isError) {+ statusElement.classList.add('error');+ statusElement.classList.remove('success');+ } else {+ statusElement.classList.add('success');+ statusElement.classList.remove('error');+ }+ }+function approveChanges() {+ showStatus("Processing update...");++ const buttons = document.querySelectorAll('button');+ buttons.forEach(button => button.disabled = true);+fetch('/update-confirm?' + new URLSearchParams({adset_id: '""" + adset_id + """',token: '""" + token + """',@@ -317,22 +358,35 @@ class CallbackHandler(BaseHTTPRequestHandler):})).then(response => response.json()).then(data => {- alert('Changes approved and applied successfully!');- window.close();+ if (data.error) {+ showStatus('Error applying changes: ' + data.error, true);+ buttons.forEach(button => button.disabled = false);+ } else {+ showStatus('Changes approved and will be applied shortly!');+ setTimeout(() => {+ window.location.href = '/verify-update?' + new URLSearchParams({+ adset_id: '""" + adset_id + """',+ token: '""" + token + """'+ });+ }, 3000);+ }}).catch(error => {- alert('Error applying changes: ' + error);+ showStatus('Error applying changes: ' + error, true);+ buttons.forEach(button => button.disabled = false);});}function cancelChanges() {+ showStatus("Cancelling update...");+fetch('/update-confirm?' + new URLSearchParams({adset_id: '""" + adset_id + """',action: 'cancel'})).then(() => {- alert('Update cancelled.');- window.close();+ showStatus('Update cancelled.');+ setTimeout(() => window.close(), 2000);});}</script>@@ -342,6 +396,119 @@ class CallbackHandler(BaseHTTPRequestHandler):self.wfile.write(html.encode())return+ if self.path.startswith("/verify-update"):+ # Parse query parameters+ query = parse_qs(urlparse(self.path).query)+ adset_id = query.get("adset_id", [""])[0]+ token = query.get("token", [""])[0]++ # Respond with a verification page+ self.send_response(200)+ self.send_header("Content-type", "text/html")+ self.end_headers()++ html = """+ <html>+ <head>+ <title>Verifying Ad Set Update</title>+ <style>+ body { font-family: Arial, sans-serif; margin: 20px; max-width: 800px; margin: 0 auto; }+ .status { padding: 15px; margin-top: 20px; border-radius: 6px; }+ .loading { background-color: #f1f8ff; border: 1px solid #0366d6; }+ .success { background-color: #e6ffed; border: 1px solid #2ea44f; color: #22863a; }+ .error { background-color: #ffeef0; border: 1px solid #d73a49; color: #d73a49; }+ .details { background: #f6f8fa; padding: 15px; border-radius: 6px; margin-top: 20px; }+ pre { white-space: pre-wrap; word-break: break-all; }+ .spinner { display: inline-block; width: 20px; height: 20px; border: 3px solid rgba(0, 0, 0, 0.1); border-radius: 50%; border-top-color: #0366d6; animation: spin 1s ease-in-out infinite; margin-right: 10px; }+ @keyframes spin { to { transform: rotate(360deg); } }+ </style>+ </head>+ <body>+ <h1>Verifying Ad Set Update</h1>+ <p>Checking the status of your update for Ad Set <strong>""" + adset_id + """</strong></p>++ <div id="status" class="status loading">+ <div class="spinner"></div> Verifying update...+ </div>++ <div id="details" class="details" style="display: none;">+ <h3>Updated Ad Set Details:</h3>+ <pre id="adset-details">Loading...</pre>+ </div>++ <script>+ // Function to fetch the ad set details and check if frequency_control_specs was updated+ async function verifyUpdate() {+ try {+ const response = await fetch('/api/adset?' + new URLSearchParams({+ adset_id: '""" + adset_id + """',+ token: '""" + token + """'+ }));++ const data = await response.json();+ const statusElement = document.getElementById('status');+ const detailsElement = document.getElementById('details');+ const adsetDetailsElement = document.getElementById('adset-details');++ detailsElement.style.display = 'block';+ adsetDetailsElement.textContent = JSON.stringify(data, null, 2);++ if (data.frequency_control_specs && data.frequency_control_specs.length > 0) {+ statusElement.classList.remove('loading');+ statusElement.classList.add('success');+ statusElement.innerHTML = '✅ Update successful! Frequency cap has been set.';+ } else {+ statusElement.classList.remove('loading');+ statusElement.classList.add('error');+ statusElement.innerHTML = '❌ Update may not have been applied. No frequency cap found in the ad set.';+ }+ } catch (error) {+ const statusElement = document.getElementById('status');+ statusElement.classList.remove('loading');+ statusElement.classList.add('error');+ statusElement.textContent = 'Error verifying update: ' + error;+ }+ }++ // Wait a moment before verifying+ setTimeout(verifyUpdate, 3000);+ </script>+ </body>+ </html>+ """+ self.wfile.write(html.encode())+ return++ if self.path.startswith("/api/adset"):+ # Parse query parameters+ query = parse_qs(urlparse(self.path).query)+ adset_id = query.get("adset_id", [""])[0]+ token = query.get("token", [""])[0]++ from .api import make_api_request++ # Call the Graph API directly+ async def get_adset_data():+ endpoint = f"{adset_id}"+ params = {+ "fields": "id,name,campaign_id,status,daily_budget,lifetime_budget,targeting,bid_amount,bid_strategy,optimization_goal,billing_event,start_time,end_time,created_time,updated_time,attribution_spec,destination_type,promoted_object,pacing_type,budget_remaining,frequency_control_specs"+ }++ return await make_api_request(endpoint, token, params)++ # Run the async function+ loop = asyncio.new_event_loop()+ asyncio.set_event_loop(loop)+ result = loop.run_until_complete(get_adset_data())+ loop.close()++ # Return the result+ self.send_response(200)+ self.send_header("Content-type", "application/json")+ self.end_headers()+ self.wfile.write(json.dumps(result, indent=2).encode())+ return+if self.path.startswith("/update-confirm"):# Handle update confirmation responsequery = parse_qs(urlparse(self.path).query)@@ -365,7 +532,35 @@ class CallbackHandler(BaseHTTPRequestHandler):"changes": changes}- self.wfile.write(json.dumps({"status": "approved"}).encode())+ # Prepare the API call to actually execute the update+ from .api import make_api_request++ # Function to perform the actual update+ async def perform_update():+ try:+ changes_dict = json.loads(changes)+ endpoint = f"{adset_id}"++ # Create a copy of changes_dict for the API call+ api_params = dict(changes_dict)+ api_params["access_token"] = token++ # Make the API request to update the ad set+ result = await make_api_request(endpoint, token, api_params, method="POST")+ return {"status": "approved", "api_result": result}+ except Exception as e:+ return {"status": "error", "error": str(e)}++ # Run the async function+ try:+ loop = asyncio.new_event_loop()+ asyncio.set_event_loop(loop)+ result = loop.run_until_complete(perform_update())+ loop.close()++ self.wfile.write(json.dumps(result).encode())+ except Exception as e:+ self.wfile.write(json.dumps({"status": "error", "error": str(e)}).encode())else:# Store the cancellationupdate_confirmation = {@@ -391,6 +586,11 @@ class CallbackHandler(BaseHTTPRequestHandler):self.end_headers()self.wfile.write(b"Token received")+ # Debug prints+ print("DEBUG: Token received from Facebook")+ print(f"DEBUG: Token value (first 10 chars): {token_container['token'][:10] if token_container['token'] else 'None'}")+ print(f"DEBUG: Expires in: {token_container['expires_in']}")+# Process the token (save it) immediatelyif token_container["token"]:# Create token info and save to cache@@ -398,13 +598,30 @@ class CallbackHandler(BaseHTTPRequestHandler):access_token=token_container["token"],expires_in=token_container["expires_in"])- auth_manager.token_info = token_info- auth_manager._save_token_to_cache()++ print(f"DEBUG: Created TokenInfo object with access_token (first 10 chars): {token_info.access_token[:10] if token_info.access_token else 'None'}")+ print(f"DEBUG: TokenInfo expires_in: {token_info.expires_in}")++ # Set token in auth_manager+ if 'auth_manager' in globals():+ auth_manager.token_info = token_info+ print("DEBUG: Set token_info in auth_manager")+ else:+ print("DEBUG: ERROR - auth_manager not in globals!")++ try:+ # Save to cache+ auth_manager._save_token_to_cache()+ print(f"DEBUG: Saved token to cache at {auth_manager._get_token_cache_path()}")+ except Exception as e:+ print(f"DEBUG: ERROR saving token to cache: {str(e)}")# Reset auth needed flagneeds_authentication = False- print(f"Token received and cached (expires in {token_container['expires_in']} seconds)")+ print(f"DEBUG: Token processed, needs_authentication = {needs_authentication}")+ else:+ print("DEBUG: ERROR - Received empty token from Facebook")return# Silence server logs@@ -501,7 +718,30 @@ async def get_current_access_token() -> Optional[str]:Returns:Current access token or None if not available"""- return auth_manager.get_access_token()+ print("DEBUG: get_current_access_token called")++ if auth_manager:+ token = auth_manager.get_access_token()+ if token:+ print(f"DEBUG: Returned token from auth_manager (first 10 chars): {token[:10]}")+ else:+ print("DEBUG: No token available from auth_manager")++ # Check if token is in token_container but not in auth_manager+ if token_container and token_container.get("token") and not token:+ print("DEBUG: Found token in token_container but not in auth_manager, updating...")+ token_info = TokenInfo(+ access_token=token_container["token"],+ expires_in=token_container.get("expires_in")+ )+ auth_manager.token_info = token_info+ auth_manager._save_token_to_cache()+ return token_container["token"]++ return token+ else:+ print("DEBUG: auth_manager is not initialized")+ return Nonedef login():