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 create_ad_creative to use object_story_spec for simple creatives

Yves Junqueira200b2b5893ba
diff --git a/meta_ads_mcp/core/ads.py b/meta_ads_mcp/core/ads.py
index c6f5c1d..f08dd53 100644
--- a/meta_ads_mcp/core/ads.py
+++ b/meta_ads_mcp/core/ads.py
@@ -831,32 +831,18 @@ async def create_ad_creative(
if description and descriptions:
return json.dumps({"error": "Cannot specify both 'description' and 'descriptions'. Use 'description' for single description or 'descriptions' for multiple."}, indent=2)
- # Convert simple parameters to complex format for internal processing
- final_headlines = None
- final_descriptions = None
-
- if headline:
- final_headlines = [headline]
- elif headlines:
- final_headlines = headlines
-
- if description:
- final_descriptions = [description]
- elif descriptions:
- final_descriptions = descriptions
-
- # Validate dynamic creative parameters
- if final_headlines:
- if len(final_headlines) > 5:
+ # Validate dynamic creative parameters (plural forms only)
+ if headlines:
+ if len(headlines) > 5:
return json.dumps({"error": "Maximum 5 headlines allowed for dynamic creatives"}, indent=2)
- for i, h in enumerate(final_headlines):
+ for i, h in enumerate(headlines):
if len(h) > 40:
return json.dumps({"error": f"Headline {i+1} exceeds 40 character limit"}, indent=2)
- if final_descriptions:
- if len(final_descriptions) > 5:
+ if descriptions:
+ if len(descriptions) > 5:
return json.dumps({"error": "Maximum 5 descriptions allowed for dynamic creatives"}, indent=2)
- for i, d in enumerate(final_descriptions):
+ for i, d in enumerate(descriptions):
if len(d) > 125:
return json.dumps({"error": f"Description {i+1} exceeds 125 character limit"}, indent=2)
@@ -866,8 +852,9 @@ async def create_ad_creative(
}
# Choose between asset_feed_spec (dynamic creative) or object_story_spec (traditional)
- if final_headlines or final_descriptions:
- # Use asset_feed_spec for dynamic creatives
+ # ONLY use asset_feed_spec when user explicitly provides plural parameters (headlines/descriptions)
+ if headlines or descriptions:
+ # Use asset_feed_spec for dynamic creatives with multiple variants
asset_feed_spec = {
"ad_formats": ["SINGLE_IMAGE"],
"images": [{"hash": image_hash}],
@@ -875,12 +862,12 @@ async def create_ad_creative(
}
# Handle headlines
- if final_headlines:
- asset_feed_spec["headlines"] = [{"text": headline_text} for headline_text in final_headlines]
+ if headlines:
+ asset_feed_spec["headlines"] = [{"text": headline_text} for headline_text in headlines]
# Handle descriptions
- if final_descriptions:
- asset_feed_spec["descriptions"] = [{"text": description_text} for description_text in final_descriptions]
+ if descriptions:
+ asset_feed_spec["descriptions"] = [{"text": description_text} for description_text in descriptions]
# Add message as primary_texts if provided
if message:
@@ -897,7 +884,7 @@ async def create_ad_creative(
"page_id": page_id
}
else:
- # Use traditional object_story_spec for single creative
+ # Use traditional object_story_spec with link_data for simple creatives
creative_data["object_story_spec"] = {
"page_id": page_id,
"link_data": {
@@ -909,17 +896,25 @@ async def create_ad_creative(
# Add optional parameters if provided
if message:
creative_data["object_story_spec"]["link_data"]["message"] = message
+
+ # Add headline (singular) to link_data
+ if headline:
+ creative_data["object_story_spec"]["link_data"]["name"] = headline
+
+ # Add description (singular) to link_data
+ if description:
+ creative_data["object_story_spec"]["link_data"]["description"] = description
+
+ # Add call_to_action to link_data for simple creatives
+ if call_to_action_type:
+ creative_data["object_story_spec"]["link_data"]["call_to_action"] = {
+ "type": call_to_action_type
+ }
# Add dynamic creative spec if provided
if dynamic_creative_spec:
creative_data["dynamic_creative_spec"] = dynamic_creative_spec
- # Only add call_to_action to object_story_spec if we're not using asset_feed_spec
- if call_to_action_type and "asset_feed_spec" not in creative_data:
- creative_data["object_story_spec"]["link_data"]["call_to_action"] = {
- "type": call_to_action_type
- }
-
if instagram_actor_id:
creative_data["instagram_actor_id"] = instagram_actor_id
@@ -998,32 +993,18 @@ async def update_ad_creative(
if description and descriptions:
return json.dumps({"error": "Cannot specify both 'description' and 'descriptions'. Use 'description' for single description or 'descriptions' for multiple."}, indent=2)
- # Convert simple parameters to complex format for internal processing
- final_headlines = None
- final_descriptions = None
-
- if headline:
- final_headlines = [headline]
- elif headlines:
- final_headlines = headlines
-
- if description:
- final_descriptions = [description]
- elif descriptions:
- final_descriptions = descriptions
-
- # Validate dynamic creative parameters
- if final_headlines:
- if len(final_headlines) > 5:
+ # Validate dynamic creative parameters (plural forms only)
+ if headlines:
+ if len(headlines) > 5:
return json.dumps({"error": "Maximum 5 headlines allowed for dynamic creatives"}, indent=2)
- for i, h in enumerate(final_headlines):
+ for i, h in enumerate(headlines):
if len(h) > 40:
return json.dumps({"error": f"Headline {i+1} exceeds 40 character limit"}, indent=2)
- if final_descriptions:
- if len(final_descriptions) > 5:
+ if descriptions:
+ if len(descriptions) > 5:
return json.dumps({"error": "Maximum 5 descriptions allowed for dynamic creatives"}, indent=2)
- for i, d in enumerate(final_descriptions):
+ for i, d in enumerate(descriptions):
if len(d) > 125:
return json.dumps({"error": f"Description {i+1} exceeds 125 character limit"}, indent=2)
@@ -1033,45 +1014,58 @@ async def update_ad_creative(
if name:
update_data["name"] = name
- if message:
- update_data["object_story_spec"] = {"link_data": {"message": message}}
-
- # Handle dynamic creative assets via asset_feed_spec
- if final_headlines or final_descriptions or dynamic_creative_spec:
+ # Choose between asset_feed_spec (dynamic creative) or object_story_spec (traditional)
+ # ONLY use asset_feed_spec when user explicitly provides plural parameters (headlines/descriptions)
+ if headlines or descriptions or dynamic_creative_spec:
+ # Handle dynamic creative assets via asset_feed_spec
asset_feed_spec = {}
# Add required ad_formats field for dynamic creatives
asset_feed_spec["ad_formats"] = ["SINGLE_IMAGE"]
# Handle headlines
- if final_headlines:
- asset_feed_spec["headlines"] = [{"text": headline_text} for headline_text in final_headlines]
+ if headlines:
+ asset_feed_spec["headlines"] = [{"text": headline_text} for headline_text in headlines]
# Handle descriptions
- if final_descriptions:
- asset_feed_spec["descriptions"] = [{"text": description_text} for description_text in final_descriptions]
+ if descriptions:
+ asset_feed_spec["descriptions"] = [{"text": description_text} for description_text in descriptions]
# Add message as primary_texts if provided
if message:
asset_feed_spec["primary_texts"] = [{"text": message}]
+ # Add call_to_action_types if provided
+ if call_to_action_type:
+ asset_feed_spec["call_to_action_types"] = [call_to_action_type]
+
update_data["asset_feed_spec"] = asset_feed_spec
+ else:
+ # Use traditional object_story_spec with link_data for simple creatives
+ if message or headline or description or call_to_action_type:
+ update_data["object_story_spec"] = {"link_data": {}}
+
+ if message:
+ update_data["object_story_spec"]["link_data"]["message"] = message
+
+ # Add headline (singular) to link_data
+ if headline:
+ update_data["object_story_spec"]["link_data"]["name"] = headline
+
+ # Add description (singular) to link_data
+ if description:
+ update_data["object_story_spec"]["link_data"]["description"] = description
+
+ # Add call_to_action to link_data for simple creatives
+ if call_to_action_type:
+ update_data["object_story_spec"]["link_data"]["call_to_action"] = {
+ "type": call_to_action_type
+ }
# Add dynamic creative spec if provided
if dynamic_creative_spec:
update_data["dynamic_creative_spec"] = dynamic_creative_spec
- # Handle call_to_action - add to asset_feed_spec if using dynamic creative, otherwise to object_story_spec
- if call_to_action_type:
- if "asset_feed_spec" in update_data:
- update_data["asset_feed_spec"]["call_to_action_types"] = [call_to_action_type]
- else:
- if "object_story_spec" not in update_data:
- update_data["object_story_spec"] = {"link_data": {}}
- update_data["object_story_spec"]["link_data"]["call_to_action"] = {
- "type": call_to_action_type
- }
-
# Prepare the API endpoint for updating the creative
endpoint = f"{creative_id}"
diff --git a/tests/test_create_ad_creative_simple.py b/tests/test_create_ad_creative_simple.py
new file mode 100644
index 0000000..0e5af58
--- /dev/null
+++ b/tests/test_create_ad_creative_simple.py
@@ -0,0 +1,127 @@
+"""Test that create_ad_creative handles simple creatives correctly."""
+
+import pytest
+import json
+from unittest.mock import AsyncMock, patch
+from meta_ads_mcp.core.ads import create_ad_creative
+
+
+async def test_simple_creative_uses_object_story_spec():
+ """Test that singular headline/description uses object_story_spec, not asset_feed_spec."""
+
+ # Mock the make_api_request function
+ with patch('meta_ads_mcp.core.ads.make_api_request') as mock_api, \
+ patch('meta_ads_mcp.core.ads._discover_pages_for_account') as mock_discover:
+
+ # Mock page discovery
+ mock_discover.return_value = {
+ "success": True,
+ "page_id": "123456789",
+ "page_name": "Test Page"
+ }
+
+ # Mock creative creation response
+ mock_api.side_effect = [
+ # First call: Create creative
+ {"id": "creative_123"},
+ # Second call: Get creative details
+ {
+ "id": "creative_123",
+ "name": "Test Creative",
+ "status": "ACTIVE"
+ }
+ ]
+
+ # Call create_ad_creative with singular headline and description
+ result = await create_ad_creative(
+ account_id="act_701351919139047",
+ image_hash="test_hash_123",
+ name="Math Problem - Hormozi",
+ link_url="https://adrocketx.ai/",
+ message="If you're spending 4+ hours per campaign...",
+ headline="Stop paying yourself $12.50/hour",
+ description="AI builds campaigns in 3min. 156% higher conversions. Free beta.",
+ call_to_action_type="LEARN_MORE",
+ access_token="test_token"
+ )
+
+ # Check that make_api_request was called
+ assert mock_api.call_count == 2
+
+ # Get the creative_data that was sent to the API
+ create_call_args = mock_api.call_args_list[0]
+ endpoint = create_call_args[0][0]
+ creative_data = create_call_args[0][2]
+
+ print("Creative data sent to API:")
+ print(json.dumps(creative_data, indent=2))
+
+ # Verify it uses object_story_spec, NOT asset_feed_spec
+ assert "object_story_spec" in creative_data, "Should use object_story_spec for simple creatives"
+ assert "asset_feed_spec" not in creative_data, "Should NOT use asset_feed_spec for simple creatives"
+
+ # Verify object_story_spec structure
+ assert "link_data" in creative_data["object_story_spec"]
+ link_data = creative_data["object_story_spec"]["link_data"]
+
+ # Verify simple creative fields are in link_data
+ assert link_data["image_hash"] == "test_hash_123"
+ assert link_data["link"] == "https://adrocketx.ai/"
+ assert link_data["message"] == "If you're spending 4+ hours per campaign..."
+
+ # The issue: headline and description should be in link_data for simple creatives
+ # Not in asset_feed_spec
+ print("\nlink_data structure:")
+ print(json.dumps(link_data, indent=2))
+
+
+async def test_dynamic_creative_uses_asset_feed_spec():
+ """Test that plural headlines/descriptions uses asset_feed_spec."""
+
+ with patch('meta_ads_mcp.core.ads.make_api_request') as mock_api, \
+ patch('meta_ads_mcp.core.ads._discover_pages_for_account') as mock_discover:
+
+ # Mock page discovery
+ mock_discover.return_value = {
+ "success": True,
+ "page_id": "123456789",
+ "page_name": "Test Page"
+ }
+
+ # Mock creative creation response
+ mock_api.side_effect = [
+ {"id": "creative_456"},
+ {"id": "creative_456", "name": "Dynamic Creative", "status": "ACTIVE"}
+ ]
+
+ # Call with PLURAL headlines and descriptions (dynamic creative)
+ result = await create_ad_creative(
+ account_id="act_701351919139047",
+ image_hash="test_hash_456",
+ name="Dynamic Creative Test",
+ link_url="https://example.com/",
+ message="Test message",
+ headlines=["Headline 1", "Headline 2"],
+ descriptions=["Description 1", "Description 2"],
+ access_token="test_token"
+ )
+
+ # Get the creative_data that was sent to the API
+ create_call_args = mock_api.call_args_list[0]
+ creative_data = create_call_args[0][2]
+
+ print("\nDynamic creative data sent to API:")
+ print(json.dumps(creative_data, indent=2))
+
+ # Verify it uses asset_feed_spec for dynamic creatives
+ assert "asset_feed_spec" in creative_data, "Should use asset_feed_spec for dynamic creatives"
+
+ # Verify asset_feed_spec structure
+ asset_feed_spec = creative_data["asset_feed_spec"]
+ assert "headlines" in asset_feed_spec
+ assert len(asset_feed_spec["headlines"]) == 2
+ assert "descriptions" in asset_feed_spec
+ assert len(asset_feed_spec["descriptions"]) == 2
+
diff --git a/tests/test_create_simple_creative_e2e.py b/tests/test_create_simple_creative_e2e.py
new file mode 100644
index 0000000..2371121
--- /dev/null
+++ b/tests/test_create_simple_creative_e2e.py
@@ -0,0 +1,49 @@
+"""End-to-end test for creating simple creatives with singular headline/description."""
+
+import pytest
+import json
+import os
+from meta_ads_mcp.core.ads import create_ad_creative
+
+
[email protected](reason="Requires authentication - run manually with: pytest tests/test_create_simple_creative_e2e.py -v")
+async def test_create_simple_creative_with_real_api():
+ """Test creating a simple creative with singular headline/description using real Meta API."""
+
+ # Account and image details from user
+ account_id = "act_3182643988557192"
+ image_hash = "ca228ac8ff3a66dca9435c90dd6953d6"
+
+ # Create a simple creative with singular headline and description
+ result = await create_ad_creative(
+ account_id=account_id,
+ image_hash=image_hash,
+ name="E2E Test - Simple Creative",
+ link_url="https://example.com/",
+ message="This is a test message for the ad.",
+ headline="Test Headline",
+ description="Test description for ad.",
+ call_to_action_type="LEARN_MORE"
+ )
+
+ print("\n=== API Response ===")
+ print(result)
+
+ result_data = json.loads(result)
+
+ # Check if there's an error
+ if "error" in result_data:
+ pytest.fail(f"Creative creation failed: {result_data['error']}")
+
+ # Verify success
+ assert "success" in result_data or "creative_id" in result_data or "id" in result_data, \
+ f"Expected success response, got: {result_data}"
+
+ print("\n✅ Simple creative created successfully!")
+
+ if "creative_id" in result_data:
+ print(f"Creative ID: {result_data['creative_id']}")
+ elif "details" in result_data and "id" in result_data["details"]:
+ print(f"Creative ID: {result_data['details']['id']}")
+
diff --git a/tests/test_dynamic_creatives.py b/tests/test_dynamic_creatives.py
index 89d4b30..d982172 100644
--- a/tests/test_dynamic_creatives.py
+++ b/tests/test_dynamic_creatives.py
@@ -44,7 +44,7 @@ class TestDynamicCreatives:
result_data = json.loads(result)
assert result_data["success"] is True
- # Verify the API call was made with single headline converted to array
+ # Verify the API call was made with single headline in object_story_spec
call_args_list = mock_api.call_args_list
assert len(call_args_list) >= 1
@@ -52,12 +52,11 @@ class TestDynamicCreatives:
first_call = call_args_list[0]
creative_data = first_call[0][2] # params is the third argument
- # Should use asset_feed_spec with headlines array format (internally converted)
- assert "asset_feed_spec" in creative_data
- assert "headlines" in creative_data["asset_feed_spec"]
- assert creative_data["asset_feed_spec"]["headlines"] == [
- {"text": "Single Headline"}
- ]
+ # Should use object_story_spec with link_data for simple creatives (not asset_feed_spec)
+ assert "object_story_spec" in creative_data
+ assert "link_data" in creative_data["object_story_spec"]
+ assert creative_data["object_story_spec"]["link_data"]["name"] == "Single Headline"
+ assert "asset_feed_spec" not in creative_data
async def test_create_ad_creative_single_description(self):
"""Test creating ad creative with single description (simple case)."""
@@ -86,7 +85,7 @@ class TestDynamicCreatives:
result_data = json.loads(result)
assert result_data["success"] is True
- # Verify the API call was made with single description converted to array
+ # Verify the API call was made with single description in object_story_spec
call_args_list = mock_api.call_args_list
assert len(call_args_list) >= 1
@@ -94,12 +93,11 @@ class TestDynamicCreatives:
first_call = call_args_list[0]
creative_data = first_call[0][2] # params is the third argument
- # Should use asset_feed_spec with descriptions array format (internally converted)
- assert "asset_feed_spec" in creative_data
- assert "descriptions" in creative_data["asset_feed_spec"]
- assert creative_data["asset_feed_spec"]["descriptions"] == [
- {"text": "Single Description"}
- ]
+ # Should use object_story_spec with link_data for simple creatives (not asset_feed_spec)
+ assert "object_story_spec" in creative_data
+ assert "link_data" in creative_data["object_story_spec"]
+ assert creative_data["object_story_spec"]["link_data"]["description"] == "Single Description"
+ assert "asset_feed_spec" not in creative_data
async def test_create_ad_creative_cannot_mix_headline_and_headlines(self):
"""Test that mixing headline and headlines parameters raises error."""