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: robustify create_ad_creative for MCP transport and Meta API requirements

Yves Junqueira64375ded6143
diff --git a/meta_ads_mcp/core/ads.py b/meta_ads_mcp/core/ads.py
index 6f4d92b..52dc3e3 100644
--- a/meta_ads_mcp/core/ads.py
+++ b/meta_ads_mcp/core/ads.py
@@ -1,6 +1,7 @@
"""Ad and Creative-related functionality for Meta Ads API."""
import json
+import logging
from typing import Optional, Dict, Any, List
import io
from PIL import Image as PILImage
@@ -8,6 +9,8 @@ from mcp.server.fastmcp import Image
import os
import time
+logger = logging.getLogger(__name__)
+
from .api import meta_api_tool, make_api_request
from .accounts import get_ad_accounts
from .utils import download_image, try_multiple_download_methods, ad_creative_images, extract_creative_image_urls
@@ -774,7 +777,7 @@ async def create_ad_creative(
access_token: Meta API access token (optional - will use cached token if not provided)
name: Creative name
page_id: Facebook Page ID (string or int; coerced to string)
- link_url: Destination URL for the ad
+ link_url: Destination URL for the ad (required unless using lead_gen_form_id)
message: Single ad copy/text (cannot be used with messages)
messages: List of primary text variants for FLEX/dynamic creatives (cannot be used with message)
headline: Single headline for simple ads (cannot be used with headlines)
@@ -783,8 +786,9 @@ async def create_ad_creative(
descriptions: List of descriptions for dynamic creative testing (cannot be used with description)
image_hashes: List of image hashes for FLEX creatives (up to 10, cannot be used with image_hash)
optimization_type: Set to "DEGREES_OF_FREEDOM" for FLEX (Advantage+) creatives that allow
- Meta to auto-optimize across all asset combinations without requiring
- is_dynamic_creative=true on the ad set
+ Meta to auto-optimize across all asset combinations. When using
+ DEGREES_OF_FREEDOM, at least one asset field (image_hashes, messages,
+ headlines, or descriptions) must contain more than one variant.
dynamic_creative_spec: Dynamic creative optimization settings
call_to_action_type: Call to action button type (e.g., 'LEARN_MORE', 'SIGN_UP', 'SHOP_NOW')
lead_gen_form_id: Lead generation form ID for lead generation campaigns. Required when using
@@ -798,6 +802,39 @@ async def create_ad_creative(
if not account_id:
return json.dumps({"error": "No account ID provided"}, indent=2)
+ # Defensive coercion: some MCP transports deliver array params as JSON strings
+ for _param_name, _param_val in [
+ ('image_hashes', image_hashes),
+ ('messages', messages),
+ ('headlines', headlines),
+ ('descriptions', descriptions),
+ ]:
+ if isinstance(_param_val, str):
+ try:
+ _parsed = json.loads(_param_val)
+ if isinstance(_parsed, list):
+ if _param_name == 'image_hashes':
+ image_hashes = _parsed
+ elif _param_name == 'messages':
+ messages = _parsed
+ elif _param_name == 'headlines':
+ headlines = _parsed
+ elif _param_name == 'descriptions':
+ descriptions = _parsed
+ except (json.JSONDecodeError, TypeError):
+ pass
+
+ logger.debug(
+ "create_ad_creative called: image_hash=%s, image_hashes=%s(%s), "
+ "messages=%s, headlines=%s, descriptions=%s, optimization_type=%s",
+ type(image_hash).__name__,
+ type(image_hashes).__name__, image_hashes,
+ type(messages).__name__,
+ type(headlines).__name__,
+ type(descriptions).__name__,
+ optimization_type,
+ )
+
# Validate image_hash / image_hashes mutual exclusivity
if image_hash and image_hashes:
return json.dumps({"error": "Cannot specify both 'image_hash' and 'image_hashes'. Use 'image_hash' for a single image or 'image_hashes' for multiple."}, indent=2)
@@ -818,9 +855,12 @@ async def create_ad_creative(
if message and messages:
return json.dumps({"error": "Cannot specify both 'message' and 'messages'. Use 'message' for single text or 'messages' for multiple variants."}, indent=2)
+ if not link_url and not lead_gen_form_id:
+ return json.dumps({"error": "No link_url provided. A destination URL is required for ad creatives (unless using lead_gen_form_id)."}, indent=2)
+
if not name:
name = f"Creative {int(time.time())}"
-
+
# Ensure account_id has the 'act_' prefix
if not account_id.startswith("act_"):
account_id = f"act_{account_id}"
@@ -877,104 +917,114 @@ async def create_ad_creative(
if len(d) > 125:
return json.dumps({"error": f"Description {i+1} exceeds 125 character limit"}, indent=2)
- # Prepare the creative data
- creative_data = {
- "name": name
- }
-
- # Determine whether to use asset_feed_spec path:
- # - plural parameters (headlines/descriptions/messages/image_hashes), OR
- # - optimization_type is set (FLEX creatives always use asset_feed_spec)
- use_asset_feed = bool(headlines or descriptions or messages or image_hashes or optimization_type)
-
- if use_asset_feed:
- # Build the images array from image_hashes (plural) or image_hash (singular)
- if image_hashes:
- images_array = [{"hash": h} for h in image_hashes]
- else:
- images_array = [{"hash": image_hash}]
+ # Prepare the API endpoint for creating a creative
+ endpoint = f"{account_id}/adcreatives"
- # Use asset_feed_spec for dynamic/FLEX creatives with multiple variants
- asset_feed_spec = {
- "images": images_array,
- "link_urls": [{"website_url": link_url if link_url else "https://facebook.com"}],
- "ad_formats": ["SINGLE_IMAGE"]
+ try:
+ # Prepare the creative data
+ creative_data = {
+ "name": name
}
- # Add optimization_type for FLEX (Advantage+) creatives
- if optimization_type:
- asset_feed_spec["optimization_type"] = optimization_type
+ # Determine whether to use asset_feed_spec path:
+ # - plural parameters (headlines/descriptions/messages/image_hashes), OR
+ # - optimization_type is set (FLEX creatives always use asset_feed_spec)
+ use_asset_feed = bool(headlines or descriptions or messages or image_hashes or optimization_type)
- # Handle headlines - Meta API uses "titles" not "headlines" in asset_feed_spec
- if headlines:
- asset_feed_spec["titles"] = [{"text": headline_text} for headline_text in headlines]
+ if use_asset_feed:
+ # Build the images array from image_hashes (plural) or image_hash (singular)
+ if image_hashes:
+ images_array = [{"hash": h} for h in image_hashes]
+ else:
+ images_array = [{"hash": image_hash}]
- # Handle descriptions
- if descriptions:
- asset_feed_spec["descriptions"] = [{"text": description_text} for description_text in descriptions]
+ # Use asset_feed_spec for dynamic/FLEX creatives with multiple variants
+ asset_feed_spec = {
+ "images": images_array,
+ "link_urls": [{"website_url": link_url}],
+ "ad_formats": ["SINGLE_IMAGE"]
+ }
- # Handle bodies: messages (plural) or message (singular)
- if messages:
- asset_feed_spec["bodies"] = [{"text": m} for m in messages]
- elif message:
- asset_feed_spec["bodies"] = [{"text": message}]
+ # 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:
+ asset_feed_spec["titles"] = [{"text": headline_text} for headline_text in headlines]
+ elif headline:
+ asset_feed_spec["titles"] = [{"text": headline}]
+
+ # Handle descriptions
+ # Auto-promote singular description to single-element array when in asset_feed_spec path
+ if descriptions:
+ asset_feed_spec["descriptions"] = [{"text": description_text} for description_text in descriptions]
+ elif description:
+ asset_feed_spec["descriptions"] = [{"text": description}]
+
+ # Handle bodies: messages (plural) or message (singular)
+ if messages:
+ asset_feed_spec["bodies"] = [{"text": m} for m in messages]
+ elif message:
+ asset_feed_spec["bodies"] = [{"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]
+
+ creative_data["asset_feed_spec"] = asset_feed_spec
+
+ # For dynamic/FLEX creatives with asset_feed_spec, object_story_spec still
+ # needs page_id and a link_data.link (Meta API requires the link field).
+ creative_data["object_story_spec"] = {
+ "page_id": page_id,
+ "link_data": {
+ "link": link_url
+ }
+ }
+ else:
+ # Use traditional object_story_spec with link_data for simple creatives
+ creative_data["object_story_spec"] = {
+ "page_id": page_id,
+ "link_data": {
+ "image_hash": image_hash,
+ "link": link_url
+ }
+ }
- # Add call_to_action_types if provided
- if call_to_action_type:
- asset_feed_spec["call_to_action_types"] = [call_to_action_type]
+ # Add optional parameters if provided
+ if message:
+ creative_data["object_story_spec"]["link_data"]["message"] = message
- creative_data["asset_feed_spec"] = asset_feed_spec
+ # Add headline (singular) to link_data
+ if headline:
+ creative_data["object_story_spec"]["link_data"]["name"] = headline
- # For dynamic/FLEX creatives with asset_feed_spec, object_story_spec only needs page_id
- creative_data["object_story_spec"] = {
- "page_id": page_id
- }
- else:
- # Use traditional object_story_spec with link_data for simple creatives
- creative_data["object_story_spec"] = {
- "page_id": page_id,
- "link_data": {
- "image_hash": image_hash,
- "link": link_url if link_url else "https://facebook.com"
- }
- }
+ # Add description (singular) to link_data
+ if description:
+ creative_data["object_story_spec"]["link_data"]["description"] = description
- # Add optional parameters if provided
- if message:
- creative_data["object_story_spec"]["link_data"]["message"] = message
+ # Add call_to_action to link_data for simple creatives
+ if call_to_action_type:
+ cta_data = {"type": call_to_action_type}
- # Add headline (singular) to link_data
- if headline:
- creative_data["object_story_spec"]["link_data"]["name"] = headline
+ # Add lead form ID to value object if provided (required for lead generation campaigns)
+ if lead_gen_form_id:
+ cta_data["value"] = {"lead_gen_form_id": lead_gen_form_id}
- # Add description (singular) to link_data
- if description:
- creative_data["object_story_spec"]["link_data"]["description"] = description
+ creative_data["object_story_spec"]["link_data"]["call_to_action"] = cta_data
- # Add call_to_action to link_data for simple creatives
- if call_to_action_type:
- cta_data = {"type": call_to_action_type}
+ # Add dynamic creative spec if provided
+ if dynamic_creative_spec:
+ creative_data["dynamic_creative_spec"] = dynamic_creative_spec
- # Add lead form ID to value object if provided (required for lead generation campaigns)
- if lead_gen_form_id:
- cta_data["value"] = {"lead_gen_form_id": lead_gen_form_id}
+ if instagram_actor_id:
+ creative_data["instagram_actor_id"] = instagram_actor_id
- creative_data["object_story_spec"]["link_data"]["call_to_action"] = cta_data
-
- # Add dynamic creative spec if provided
- if dynamic_creative_spec:
- creative_data["dynamic_creative_spec"] = dynamic_creative_spec
-
- if instagram_actor_id:
- creative_data["instagram_actor_id"] = instagram_actor_id
-
- # Prepare the API endpoint for creating a creative
- endpoint = f"{account_id}/adcreatives"
-
- try:
# Make API request to create the creative
data = await make_api_request(endpoint, access_token, creative_data, method="POST")
-
+
# If successful, get more details about the created creative
if "id" in data:
creative_id = data["id"]
@@ -982,21 +1032,21 @@ async def create_ad_creative(
creative_params = {
"fields": "id,name,status,thumbnail_url,image_url,image_hash,object_story_spec,asset_feed_spec,url_tags,link_url"
}
-
+
creative_details = await make_api_request(creative_endpoint, access_token, creative_params)
return json.dumps({
"success": True,
"creative_id": creative_id,
"details": creative_details
}, indent=2)
-
+
return json.dumps(data, indent=2)
-
+
except Exception as e:
+ logger.exception("create_ad_creative failed")
return json.dumps({
"error": "Failed to create ad creative",
- "details": str(e),
- "creative_data_sent": creative_data
+ "details": str(e)
}, indent=2)
@@ -1094,12 +1144,18 @@ async def update_ad_creative(
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:
asset_feed_spec["titles"] = [{"text": headline_text} for headline_text in headlines]
+ elif headline:
+ asset_feed_spec["titles"] = [{"text": headline}]
# Handle descriptions
+ # Auto-promote singular description to single-element array when in asset_feed_spec path
if descriptions:
asset_feed_spec["descriptions"] = [{"text": description_text} for description_text in descriptions]
+ elif description:
+ asset_feed_spec["descriptions"] = [{"text": description}]
# Handle bodies: messages (plural) or message (singular)
if messages:
diff --git a/tests/test_dynamic_creatives.py b/tests/test_dynamic_creatives.py
index 644e295..10d87b0 100644
--- a/tests/test_dynamic_creatives.py
+++ b/tests/test_dynamic_creatives.py
@@ -347,6 +347,7 @@ class TestDynamicCreatives:
name="Test Creative",
image_hash="abc123",
page_id="987654321",
+ link_url="https://example.com",
headlines=too_many_headlines
)
@@ -372,6 +373,7 @@ class TestDynamicCreatives:
name="Test Creative",
image_hash="abc123",
page_id="987654321",
+ link_url="https://example.com",
descriptions=too_many_descriptions
)
diff --git a/tests/test_flex_creatives.py b/tests/test_flex_creatives.py
index ee5e501..6033cc7 100644
--- a/tests/test_flex_creatives.py
+++ b/tests/test_flex_creatives.py
@@ -238,8 +238,11 @@ class TestFlexCreatives:
assert "asset_feed_spec" in creative_data
assert creative_data["asset_feed_spec"]["images"] == [{"hash": "abc123"}]
assert creative_data["asset_feed_spec"]["optimization_type"] == "DEGREES_OF_FREEDOM"
- # object_story_spec should only have page_id (no link_data)
- assert creative_data["object_story_spec"] == {"page_id": "987654321"}
+ # object_story_spec needs page_id and link_data (Meta API requires the link field)
+ assert creative_data["object_story_spec"] == {
+ "page_id": "987654321",
+ "link_data": {"link": "https://example.com"}
+ }
async def test_no_optimization_type_unchanged_behavior(self):
"""Without optimization_type, single image+headline uses object_story_spec (backward compat)."""
@@ -492,3 +495,140 @@ class TestFlexCreativesUpdate:
assert "asset_feed_spec" in creative_data
assert creative_data["asset_feed_spec"]["optimization_type"] == "DEGREES_OF_FREEDOM"
+
+
+class TestSingularParamPromotion:
+ """Test that singular headline/description/message are auto-promoted in asset_feed_spec path."""
+
+ async def test_singular_headline_promoted_with_optimization_type(self):
+ """Singular headline is auto-promoted to titles array when optimization_type forces asset_feed_spec."""
+ sample_creative_data = {
+ "id": "123456789",
+ "name": "FLEX with singular headline",
+ "status": "ACTIVE"
+ }
+
+ with patch('meta_ads_mcp.core.ads.make_api_request', new_callable=AsyncMock) as mock_api:
+ mock_api.return_value = sample_creative_data
+
+ result = await create_ad_creative(
+ access_token="test_token",
+ account_id="act_123456789",
+ name="FLEX with singular headline",
+ image_hash="abc123",
+ page_id="987654321",
+ link_url="https://example.com",
+ headline="My Single Headline",
+ optimization_type="DEGREES_OF_FREEDOM"
+ )
+
+ result_data = json.loads(result)
+ assert result_data["success"] is True
+
+ call_args_list = mock_api.call_args_list
+ first_call = call_args_list[0]
+ creative_data = first_call[0][2]
+
+ assert "asset_feed_spec" in creative_data
+ assert creative_data["asset_feed_spec"]["titles"] == [{"text": "My Single Headline"}]
+
+ async def test_singular_description_promoted_with_optimization_type(self):
+ """Singular description is auto-promoted to descriptions array when optimization_type forces asset_feed_spec."""
+ sample_creative_data = {
+ "id": "123456789",
+ "name": "FLEX with singular description",
+ "status": "ACTIVE"
+ }
+
+ with patch('meta_ads_mcp.core.ads.make_api_request', new_callable=AsyncMock) as mock_api:
+ mock_api.return_value = sample_creative_data
+
+ result = await create_ad_creative(
+ access_token="test_token",
+ account_id="act_123456789",
+ name="FLEX with singular description",
+ image_hash="abc123",
+ page_id="987654321",
+ link_url="https://example.com",
+ description="My Single Description",
+ optimization_type="DEGREES_OF_FREEDOM"
+ )
+
+ result_data = json.loads(result)
+ assert result_data["success"] is True
+
+ call_args_list = mock_api.call_args_list
+ first_call = call_args_list[0]
+ creative_data = first_call[0][2]
+
+ assert "asset_feed_spec" in creative_data
+ assert creative_data["asset_feed_spec"]["descriptions"] == [{"text": "My Single Description"}]
+
+ async def test_all_singular_params_promoted_with_optimization_type(self):
+ """All singular params (headline, description, message) promoted when optimization_type is set."""
+ sample_creative_data = {
+ "id": "123456789",
+ "name": "FLEX all singular",
+ "status": "ACTIVE"
+ }
+
+ with patch('meta_ads_mcp.core.ads.make_api_request', new_callable=AsyncMock) as mock_api:
+ mock_api.return_value = sample_creative_data
+
+ result = await create_ad_creative(
+ access_token="test_token",
+ account_id="act_123456789",
+ name="FLEX all singular",
+ image_hash="abc123",
+ page_id="987654321",
+ link_url="https://example.com",
+ message="My message",
+ headline="My headline",
+ description="My description",
+ optimization_type="DEGREES_OF_FREEDOM",
+ call_to_action_type="LEARN_MORE"
+ )
+
+ result_data = json.loads(result)
+ assert result_data["success"] is True
+
+ call_args_list = mock_api.call_args_list
+ first_call = call_args_list[0]
+ creative_data = first_call[0][2]
+
+ afs = creative_data["asset_feed_spec"]
+ assert afs["optimization_type"] == "DEGREES_OF_FREEDOM"
+ 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"]
+ assert afs["images"] == [{"hash": "abc123"}]
+
+ 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 = {
+ "id": "123456789",
+ "name": "Updated",
+ "status": "ACTIVE"
+ }
+
+ with patch('meta_ads_mcp.core.ads.make_api_request', new_callable=AsyncMock) as mock_api:
+ mock_api.return_value = sample_creative_data
+
+ result = await update_ad_creative(
+ access_token="test_token",
+ creative_id="123456789",
+ headline="Updated Headline",
+ optimization_type="DEGREES_OF_FREEDOM"
+ )
+
+ result_data = json.loads(result)
+ assert result_data["success"] is True
+
+ call_args_list = mock_api.call_args_list
+ first_call = call_args_list[0]
+ creative_data = first_call[0][2]
+
+ assert "asset_feed_spec" in creative_data
+ assert creative_data["asset_feed_spec"]["titles"] == [{"text": "Updated Headline"}]