Skip to content

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.

feat: detect error 3858355 in update_ad and return helpful FLEX creative swap workaround

Yves Junqueira88cd1c3538f7
diff --git a/meta_ads_mcp/core/ads.py b/meta_ads_mcp/core/ads.py
index 5145270..4e76303 100644
--- a/meta_ads_mcp/core/ads.py
+++ b/meta_ads_mcp/core/ads.py
@@ -731,6 +731,40 @@ async def update_ad(
endpoint = f"{ad_id}"
try:
data = await make_api_request(endpoint, access_token, params, method='POST')
+
+ # Check for FLEX creative image mismatch error (3858355)
+ if creative_id is not None and "error" in data:
+ error_obj = data.get("error", {})
+ if isinstance(error_obj, dict):
+ error_details = error_obj.get("details", {})
+ if isinstance(error_details, dict):
+ inner_error = error_details.get("error", {})
+ error_subcode = inner_error.get("error_subcode") if isinstance(inner_error, dict) else None
+ else:
+ error_subcode = error_obj.get("error_subcode")
+ else:
+ error_subcode = None
+
+ if error_subcode == 3858355:
+ return json.dumps({
+ "error": "Cannot swap creative on this ad due to FLEX image mismatch",
+ "error_subcode": 3858355,
+ "explanation": (
+ "Meta requires the first image in the new creative's asset_feed_spec "
+ "to match the image in its object_story_spec. When swapping a FLEX "
+ "creative on an existing ad, this validation can fail if the new "
+ "creative has different images than the original."
+ ),
+ "workaround": (
+ "Create a new ad with the new creative instead of swapping: "
+ "(1) call create_ad with the new creative_id and the same adset_id, "
+ "(2) pause the old ad with update_ad(ad_id, status='PAUSED'). "
+ "Note: this will lose social proof (likes, comments, shares) from the original ad."
+ ),
+ "ad_id": ad_id,
+ "creative_id": creative_id
+ }, indent=2)
+
return json.dumps(data, indent=2)
except Exception as e:
return json.dumps({"error": f"Failed to update ad: {str(e)}"}, indent=2)
diff --git a/tests/test_update_ad_flex_swap.py b/tests/test_update_ad_flex_swap.py
new file mode 100644
index 0000000..1abf287
--- /dev/null
+++ b/tests/test_update_ad_flex_swap.py
@@ -0,0 +1,132 @@
+"""Tests for update_ad FLEX creative swap error handling.
+
+Tests that update_ad provides clear, actionable error messages
+when Meta API rejects creative swaps on FLEX ads (error_subcode 3858355).
+"""
+
+import pytest
+import json
+from unittest.mock import AsyncMock, patch
+from meta_ads_mcp.core.ads import update_ad
+
+
+def _unwrap_result(result_str):
+ """Unwrap the result, handling the meta_api_tool data wrapping."""
+ result_data = json.loads(result_str)
+ if "data" in result_data:
+ return json.loads(result_data["data"])
+ return result_data
+
+
+class TestUpdateAdFlexSwap:
+ """Test cases for update_ad FLEX creative swap (error 3858355)."""
+
+ async def test_flex_swap_3858355_returns_workaround(self):
+ """When Meta rejects creative swap with 3858355, return clear workaround."""
+
+ api_error = {
+ "error": {
+ "message": "HTTP Error: 400",
+ "details": {
+ "error": {
+ "message": (
+ "The first image or video of the first asset group for "
+ "flexible format must match the image or video in the "
+ "creative specification."
+ ),
+ "type": "OAuthException",
+ "code": 100,
+ "error_subcode": 3858355,
+ "fbtrace_id": "abc123"
+ }
+ },
+ "full_response": {
+ "status_code": 400
+ }
+ }
+ }
+
+ with patch('meta_ads_mcp.core.ads.make_api_request', new_callable=AsyncMock) as mock_api:
+ mock_api.return_value = api_error
+
+ result = await update_ad(
+ ad_id="test_ad_123",
+ creative_id="new_creative_456",
+ access_token="test_token"
+ )
+
+ result_data = _unwrap_result(result)
+ assert result_data["error"] == "Cannot swap creative on this ad due to FLEX image mismatch"
+ assert result_data["error_subcode"] == 3858355
+ assert "workaround" in result_data
+ assert "create_ad" in result_data["workaround"]
+ assert "PAUSED" in result_data["workaround"]
+ assert result_data["ad_id"] == "test_ad_123"
+ assert result_data["creative_id"] == "new_creative_456"
+
+ async def test_flex_swap_success_passes_through(self):
+ """Successful creative swap returns the API response."""
+
+ success_response = {"success": True}
+
+ with patch('meta_ads_mcp.core.ads.make_api_request', new_callable=AsyncMock) as mock_api:
+ mock_api.return_value = success_response
+
+ result = await update_ad(
+ ad_id="test_ad_123",
+ creative_id="new_creative_456",
+ access_token="test_token"
+ )
+
+ result_data = _unwrap_result(result)
+ assert result_data["success"] is True
+
+ async def test_other_errors_not_intercepted(self):
+ """Non-3858355 errors pass through unchanged."""
+
+ api_error = {
+ "error": {
+ "message": "HTTP Error: 400",
+ "details": {
+ "error": {
+ "message": "Some other error",
+ "type": "OAuthException",
+ "code": 100,
+ "error_subcode": 9999,
+ }
+ },
+ "full_response": {"status_code": 400}
+ }
+ }
+
+ with patch('meta_ads_mcp.core.ads.make_api_request', new_callable=AsyncMock) as mock_api:
+ mock_api.return_value = api_error
+
+ result = await update_ad(
+ ad_id="test_ad_123",
+ creative_id="new_creative_456",
+ access_token="test_token"
+ )
+
+ result_data = _unwrap_result(result)
+ # Should pass through the raw error, not the workaround message
+ assert "workaround" not in result_data
+ assert "error" in result_data
+
+ async def test_status_update_ignores_3858355_check(self):
+ """Status-only updates skip the creative swap error check."""
+
+ success_response = {"success": True}
+
+ with patch('meta_ads_mcp.core.ads.make_api_request', new_callable=AsyncMock) as mock_api:
+ mock_api.return_value = success_response
+
+ result = await update_ad(
+ ad_id="test_ad_123",
+ status="PAUSED",
+ access_token="test_token"
+ )
+
+ result_data = _unwrap_result(result)
+ assert result_data["success"] is True