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.

fix: restructure DOF payload to match working duplication pattern

Yves Junqueiraba9ec7220d38
diff --git a/meta_ads_mcp/core/ads.py b/meta_ads_mcp/core/ads.py
index 553b460..5145270 100644
--- a/meta_ads_mcp/core/ads.py
+++ b/meta_ads_mcp/core/ads.py
@@ -1216,34 +1216,40 @@ async def create_ad_creative(
asset_customization_rules, images_array
)
- # Determine ad_formats: use explicit value if provided, otherwise smart default.
- # NOTE: AUTOMATIC_FORMAT is NOT valid for creation — Meta silently
- # ignores the entire asset_feed_spec when it encounters it.
- # Always use SINGLE_IMAGE (or SINGLE_VIDEO); Meta handles format
- # selection automatically via optimization_type=DEGREES_OF_FREEDOM.
- if ad_formats:
- resolved_ad_formats = ad_formats
- elif is_video:
- resolved_ad_formats = ["SINGLE_VIDEO"]
+ # ------------------------------------------------------------------
+ # Build asset_feed_spec base: DOF vs non-DOF use different patterns.
+ #
+ # DOF (DEGREES_OF_FREEDOM / FLEX / Advantage+):
+ # asset_feed_spec has ONLY: media, optimization_type, text variants.
+ # URL, ad_formats, and CTA go in object_story_spec.link_data.
+ # This matches the working Next.js duplication pattern — Meta's
+ # own GET response omits link_urls/ad_formats/call_to_action_types
+ # from asset_feed_spec, and the duplication passes it through AS-IS.
+ # Including those fields causes Meta to silently ignore
+ # asset_feed_spec for multi-image creatives.
+ #
+ # Non-DOF (regular Dynamic Creative):
+ # asset_feed_spec includes link_urls, ad_formats, call_to_action_types
+ # as before (this path is verified working).
+ # ------------------------------------------------------------------
+ if optimization_type:
+ asset_feed_spec = {"optimization_type": optimization_type}
+ # Only include ad_formats if explicitly provided by the caller
+ if ad_formats:
+ asset_feed_spec["ad_formats"] = ad_formats
else:
- resolved_ad_formats = ["SINGLE_IMAGE"]
-
- # Use asset_feed_spec for dynamic/FLEX creatives with multiple variants
- asset_feed_spec = {
- "link_urls": [{"website_url": link_url}],
- "ad_formats": resolved_ad_formats
- }
+ resolved_ad_formats = ad_formats or (["SINGLE_VIDEO"] if is_video else ["SINGLE_IMAGE"])
+ asset_feed_spec = {
+ "link_urls": [{"website_url": link_url}],
+ "ad_formats": resolved_ad_formats,
+ }
- # Add media to asset_feed_spec
+ # Add media to asset_feed_spec (shared by both paths)
if is_video:
asset_feed_spec["videos"] = videos_array
else:
asset_feed_spec["images"] = images_array
- # Add optimization_type for FLEX (Advantage+) creatives
- if optimization_type:
- asset_feed_spec["optimization_type"] = optimization_type
-
# Handle headlines - Meta API uses "titles" not "headlines" in asset_feed_spec
# Auto-promote singular headline to single-element array when in asset_feed_spec path
if headlines:
@@ -1264,8 +1270,8 @@ async def create_ad_creative(
elif message:
asset_feed_spec["bodies"] = [{"text": message}]
- # Add call_to_action_types if provided
- if call_to_action_type:
+ # CTA in asset_feed_spec only for non-DOF (DOF puts CTA in link_data)
+ if call_to_action_type and not optimization_type:
asset_feed_spec["call_to_action_types"] = [call_to_action_type]
# Add placement-specific asset customization rules if provided
@@ -1274,8 +1280,10 @@ async def create_ad_creative(
creative_data["asset_feed_spec"] = asset_feed_spec
- # For asset_feed_spec creatives, object_story_spec needs page_id
- # plus a link anchor. Meta rejects bare page_id (error 2061015).
+ # ------------------------------------------------------------------
+ # Build object_story_spec for asset_feed_spec creatives.
+ # Meta rejects bare page_id (error 2061015) — needs a link anchor.
+ # ------------------------------------------------------------------
if is_video:
# Video FLEX: use video_data with call_to_action carrying
# the link URL. This is required for Meta to associate the
@@ -1298,21 +1306,21 @@ async def create_ad_creative(
"video_data": video_anchor
}
else:
- # Image creative with asset_feed_spec (FLEX / Dynamic Creative).
- #
- # Meta requires link_data in object_story_spec even when
- # asset_feed_spec is present (error 2061015 without it).
- #
- # For multi-image creatives (image_hashes), link_data MUST
- # include an image_hash as the "primary" image anchor.
- # Without it, Meta silently ignores the asset_feed_spec and
- # creates a bare link creative instead. This matches what
- # Meta's own GET response returns for FLEX creatives — both
- # link_data.image_hash (primary) and asset_feed_spec.images
- # (all variants) are present.
+ # Image creative: build link_data anchor.
link_data = {"link": link_url}
if image_hashes:
link_data["image_hash"] = image_hashes[0]
+ # DOF: put CTA in link_data (not in asset_feed_spec)
+ if optimization_type and call_to_action_type:
+ cta = {"type": call_to_action_type}
+ cta_value = {}
+ if link_url:
+ cta_value["link"] = link_url
+ if lead_gen_form_id:
+ cta_value["lead_gen_form_id"] = lead_gen_form_id
+ if cta_value:
+ cta["value"] = cta_value
+ link_data["call_to_action"] = cta
creative_data["object_story_spec"] = {
"page_id": page_id,
"link_data": link_data,
diff --git a/tests/test_ad_formats_flexible.py b/tests/test_ad_formats_flexible.py
index 1a309e7..87441be 100644
--- a/tests/test_ad_formats_flexible.py
+++ b/tests/test_ad_formats_flexible.py
@@ -16,12 +16,11 @@ from meta_ads_mcp.core.ads import create_ad_creative, update_ad_creative
class TestAdFormatsDefaultCreate:
"""Test ad_formats defaults in create_ad_creative."""
- async def test_dof_with_image_hashes_defaults_to_single_image(self):
- """DEGREES_OF_FREEDOM + image_hashes should default ad_formats to SINGLE_IMAGE.
+ async def test_dof_with_image_hashes_omits_ad_formats(self):
+ """DEGREES_OF_FREEDOM + image_hashes should omit ad_formats entirely.
- AUTOMATIC_FORMAT is NOT valid for creation — Meta silently ignores the
- entire asset_feed_spec when it encounters it. Use SINGLE_IMAGE; Meta
- handles format selection via optimization_type=DEGREES_OF_FREEDOM.
+ For DOF creatives, ad_formats is not needed — Meta handles format
+ selection via optimization_type=DEGREES_OF_FREEDOM.
"""
sample_creative_data = {"id": "123", "name": "Flex", "status": "ACTIVE"}
@@ -44,10 +43,10 @@ class TestAdFormatsDefaultCreate:
creative_data = mock_api.call_args_list[0][0][2]
afs = creative_data["asset_feed_spec"]
- assert afs["ad_formats"] == ["SINGLE_IMAGE"]
+ assert "ad_formats" not in afs
- async def test_dof_without_image_hashes_defaults_to_single_image(self):
- """DEGREES_OF_FREEDOM with single image_hash (no image_hashes) defaults to SINGLE_IMAGE."""
+ async def test_dof_without_image_hashes_omits_ad_formats(self):
+ """DEGREES_OF_FREEDOM with single image_hash (no image_hashes) omits ad_formats."""
sample_creative_data = {"id": "123", "name": "Flex", "status": "ACTIVE"}
with patch('meta_ads_mcp.core.ads.make_api_request', new_callable=AsyncMock) as mock_api:
@@ -69,7 +68,7 @@ class TestAdFormatsDefaultCreate:
creative_data = mock_api.call_args_list[0][0][2]
afs = creative_data["asset_feed_spec"]
- assert afs["ad_formats"] == ["SINGLE_IMAGE"]
+ assert "ad_formats" not in afs
async def test_no_dof_defaults_to_single_image(self):
"""Without DEGREES_OF_FREEDOM, ad_formats defaults to SINGLE_IMAGE."""
@@ -275,8 +274,8 @@ class TestFlexibleCreativeFullFlow:
creative_data = mock_api.call_args_list[0][0][2]
afs = creative_data["asset_feed_spec"]
- # SINGLE_IMAGE — Meta handles Flexible via optimization_type
- assert afs["ad_formats"] == ["SINGLE_IMAGE"]
+ # DOF creatives omit ad_formats — Meta handles format via optimization_type
+ assert "ad_formats" not in afs
assert afs["optimization_type"] == "DEGREES_OF_FREEDOM"
assert afs["images"] == [
{"hash": "hash1"}, {"hash": "hash2"}, {"hash": "hash3"}
@@ -290,16 +289,20 @@ class TestFlexibleCreativeFullFlow:
assert afs["descriptions"] == [
{"text": "Desc 1"}, {"text": "Desc 2"}
]
- assert afs["call_to_action_types"] == ["SHOP_NOW"]
- assert afs["link_urls"] == [{"website_url": "https://example.com"}]
+ # DOF: CTA goes in object_story_spec.link_data, not in asset_feed_spec
+ assert "call_to_action_types" not in afs
+ # DOF: link_urls omitted, link goes in link_data.link
+ assert "link_urls" not in afs
# Multi-image: link_data must include image_hash as primary anchor.
# Without it, Meta silently ignores asset_feed_spec.
+ # CTA is placed in link_data for DOF creatives.
assert creative_data["object_story_spec"] == {
"page_id": "987654321",
"link_data": {
"link": "https://example.com",
"image_hash": "hash1",
+ "call_to_action": {"type": "SHOP_NOW", "value": {"link": "https://example.com"}},
},
}
@@ -333,14 +336,18 @@ class TestFlexibleCreativeFullFlow:
creative_data = mock_api.call_args_list[0][0][2]
afs = creative_data["asset_feed_spec"]
- assert afs["ad_formats"] == ["SINGLE_IMAGE"]
+ assert "ad_formats" not in afs
assert afs["optimization_type"] == "DEGREES_OF_FREEDOM"
assert afs["images"] == [{"hash": "hash1"}]
# Single-image: object_story_spec keeps link_data (error 2061015 without it)
+ # CTA is placed in link_data for DOF creatives.
assert creative_data["object_story_spec"] == {
"page_id": "987654321",
- "link_data": {"link": "https://example.com"}
+ "link_data": {
+ "link": "https://example.com",
+ "call_to_action": {"type": "SHOP_NOW", "value": {"link": "https://example.com"}},
+ }
}
async def test_backward_compat_simple_creative_unaffected(self):
diff --git a/tests/test_flex_creatives.py b/tests/test_flex_creatives.py
index 8cc4aaa..7bc546d 100644
--- a/tests/test_flex_creatives.py
+++ b/tests/test_flex_creatives.py
@@ -318,13 +318,21 @@ class TestFlexCreatives:
assert afs["bodies"] == [{"text": "Text A"}, {"text": "Text B"}]
assert afs["titles"] == [{"text": "Headline 1"}, {"text": "Headline 2"}]
assert afs["descriptions"] == [{"text": "Desc 1"}, {"text": "Desc 2"}]
- assert afs["call_to_action_types"] == ["SHOP_NOW"]
+ # DOF: CTA goes in object_story_spec.link_data, not in asset_feed_spec
+ assert "call_to_action_types" not in afs
+ # DOF: link_urls omitted, link goes in link_data.link
+ assert "link_urls" not in afs
# Multi-image: link_data must include image_hash as primary anchor.
# Without it, Meta silently ignores asset_feed_spec.
+ # CTA is placed in link_data for DOF creatives.
assert creative_data["object_story_spec"] == {
"page_id": "987654321",
- "link_data": {"link": "https://example.com", "image_hash": "hash1"},
+ "link_data": {
+ "link": "https://example.com",
+ "image_hash": "hash1",
+ "call_to_action": {"type": "SHOP_NOW", "value": {"link": "https://example.com"}},
+ },
}
async def test_image_hashes_without_optimization_type_uses_asset_feed(self):
@@ -616,9 +624,15 @@ class TestSingularParamPromotion:
assert afs["titles"] == [{"text": "My headline"}]
assert afs["descriptions"] == [{"text": "My description"}]
assert afs["bodies"] == [{"text": "My message"}]
- assert afs["call_to_action_types"] == ["LEARN_MORE"]
+ # DOF: CTA goes in object_story_spec.link_data, not in asset_feed_spec
+ assert "call_to_action_types" not in afs
assert afs["images"] == [{"hash": "abc123"}]
+ # CTA is placed in link_data for DOF creatives
+ assert creative_data["object_story_spec"]["link_data"]["call_to_action"] == {
+ "type": "LEARN_MORE", "value": {"link": "https://example.com"}
+ }
+
async def test_update_singular_headline_promoted_with_optimization_type(self):
"""Singular headline promoted in update_ad_creative when optimization_type is set."""
sample_creative_data = {