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
Fix DOF + video + multi-text returning Meta error 2061015 (#130)
Yves Junqueira85b40cdad371
diff --git a/meta_ads_mcp/core/ads.py b/meta_ads_mcp/core/ads.pyindex cc8a442..d8dfda8 100644--- a/meta_ads_mcp/core/ads.py+++ b/meta_ads_mcp/core/ads.py@@ -2324,9 +2324,11 @@ async def create_ad_creative()# ------------------------------------------------------------------- # Build asset_feed_spec base: DOF vs non-DOF use different patterns.+ # Build asset_feed_spec base: DOF vs non-DOF use different patterns,+ # and DOF + video is a third case (link/CTA must be in asset_feed_spec,+ # not object_story_spec.video_data).#- # DOF (DEGREES_OF_FREEDOM / FLEX / Advantage+):+ # DOF (DEGREES_OF_FREEDOM / FLEX / Advantage+) + image:# 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@@ -2335,25 +2337,35 @@ async def create_ad_creative(# Including those fields causes Meta to silently ignore# asset_feed_spec for multi-image creatives.#+ # DOF + video:+ # object_story_spec must be bare {page_id} (Meta v24 rejects+ # video_data anchor inside object_story_spec with error 1443048).+ # So link_urls, ad_formats, and call_to_action_types MUST live+ # in asset_feed_spec — otherwise Meta returns error_subcode+ # 2061015 ("The link field is required"). Treated like the+ # non-DOF path below.+ ## Non-DOF (regular Dynamic Creative):# asset_feed_spec includes link_urls, ad_formats, call_to_action_types# as before (this path is verified working).# ------------------------------------------------------------------is_dof = optimization_type == "DEGREES_OF_FREEDOM"- if is_dof:- # DOF: asset_feed_spec has ONLY media, optimization_type, text variants.- # URL, ad_formats, and CTA go in object_story_spec.link_data.+ if is_dof and not is_video:+ # DOF + image: asset_feed_spec has ONLY media, optimization_type,+ # text variants. URL, ad_formats, and CTA go in object_story_spec.link_data.asset_feed_spec = {"optimization_type": optimization_type}# Only include ad_formats if explicitly provided by the callerif ad_formats:asset_feed_spec["ad_formats"] = ad_formatselse:- # Non-DOF (including PLACEMENT): link_urls and ad_formats in asset_feed_spec.+ # Non-DOF (any media) OR DOF + video: link_urls, ad_formats, and+ # CTA live in asset_feed_spec, object_story_spec stays bare.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,- }+ asset_feed_spec = {"ad_formats": resolved_ad_formats}+ # Lead-gen flows can omit a website link_url (lead_gen_form_id+ # provides the destination), so guard link_urls on link_url.+ if link_url:+ asset_feed_spec["link_urls"] = [{"website_url": link_url}]if optimization_type:asset_feed_spec["optimization_type"] = optimization_type@@ -2383,8 +2395,11 @@ async def create_ad_creative(elif message:asset_feed_spec["bodies"] = [{"text": message}]- # CTA in asset_feed_spec only for non-DOF (DOF puts CTA in link_data)- if call_to_action_type and not is_dof:+ # CTA in asset_feed_spec for:+ # - any non-DOF path (regular Dynamic Creative)+ # - DOF + video (object_story_spec is bare, so CTA must live here)+ # DOF + image puts CTA in object_story_spec.link_data instead.+ if call_to_action_type and (not is_dof or is_video):if lead_gen_form_id or phone_number:cta_value: Dict[str, Any] = {}if link_url:@@ -2411,24 +2426,42 @@ async def create_ad_creative(# ------------------------------------------------------------------# Build object_story_spec for asset_feed_spec creatives.#- # When asset_feed_spec.videos[] carries the video, object_story_spec- # MUST contain only page_id (plus instagram_user_id, appended later).- # Adding a video_data anchor here triggers Meta API v24 error 1443048- # ("object_story_spec ill formed"). Per Meta's official docs, the- # canonical shape for asset_feed_spec.videos[] is bare page_id —- # the video, thumbnail, link URL, and CTA all live in- # asset_feed_spec.+ # Three sub-cases:+ #+ # - Non-DOF (regular Dynamic Creative, PLACEMENT, etc.) — bare+ # {page_id}. Everything (link, CTA, media) lives in+ # asset_feed_spec. Verified working on v24.+ #+ # - DOF + video — object_story_spec MUST carry a `link_data.link`+ # anchor. Meta v24 rejects bare {page_id} for DOF + video with+ # error_subcode 2061015 ("The link field is required";+ # blame_field_specs=[["link"]]). A video_data anchor would trip+ # 1443048 ("object_story_spec ill formed") instead, so we use a+ # minimal link_data: {link: <url>}. The video/thumbnail/text+ # variants stay in asset_feed_spec — this is verified via the+ # sandbox account (creative ids 3081153525608342, 1026522600054410+ # created 2026-05-27).+ #+ # - DOF + image — handled in the `else` branch below: full+ # link_data anchor with image_hash, link, CTA, etc.+ ## Ref: https://developers.facebook.com/docs/marketing-api/dynamic-creative/dynamic-creative-optimization# ------------------------------------------------------------------- if video_id or not is_dof:- # video_id branch: asset_feed_spec.videos already carries the- # video + thumbnail; link_urls + call_to_action_types carry- # the destination + CTA. object_story_spec must be bare.- # Non-DOF image (PLACEMENT etc.) branch: same shape — URLs,- # images, CTA live exclusively in asset_feed_spec.+ if is_video or not is_dof:creative_data["object_story_spec"] = {"page_id": page_id,}+ if is_dof and is_video:+ # Anchor link for Meta v24 DOF + video. Use link_url when+ # provided; fall back to the Meta lead-gen placeholder so+ # lead_gen_form_id flows still produce a non-empty anchor.+ anchor_link = link_url or (+ "http://fb.me/" if lead_gen_form_id else None+ )+ if anchor_link:+ creative_data["object_story_spec"]["link_data"] = {+ "link": anchor_link,+ }else:# DOF image: link_data serves as the "anchor" creative template.link_data = {}diff --git a/tests/test_video_creatives.py b/tests/test_video_creatives.pyindex 8a37c30..6f4c435 100644--- a/tests/test_video_creatives.py+++ b/tests/test_video_creatives.py@@ -332,13 +332,148 @@ async def test_video_creative_with_dof_optimization():# PR-C: video metadata moved from object_story_spec.video_data to asset_feed_spec.# Thumbnail (was video_data.image_url) is now on asset_feed_spec.videos[].+ # DOF + video also requires a link_data.link anchor on object_story_spec+ # to satisfy Meta v24 (otherwise error_subcode 2061015).oss = creative_data["object_story_spec"]assert "video_data" not in oss- assert "link_data" not in oss- assert oss == {"page_id": "123456789"}+ assert oss == {+ "page_id": "123456789",+ "link_data": {"link": "https://example.com/"},+ }assert afs["videos"][0]["thumbnail_url"] == "https://example.com/auto-thumb.jpg"+async def test_video_creative_with_dof_includes_link_and_cta_in_asset_feed_spec():+ """Regression for DOF + video + multi-text: link_urls + call_to_action_types+ must land in asset_feed_spec AND object_story_spec must carry a+ link_data.link anchor.++ For DOF + video, Meta v24 requires both:+ - asset_feed_spec carries link_urls + call_to_action_types (a+ video_data anchor inside object_story_spec trips error 1443048).+ - object_story_spec carries `link_data: {link: <url>}` (a bare+ `{page_id}` trips error_subcode 2061015 "link field required").+ A video_data anchor would trip 1443226 ("thumbnail required") instead.+ Verified empirically against the v24 sandbox 2026-05-27.++ Before the fix, the DOF branch produced bare object_story_spec and+ omitted link_urls + call_to_action_types from asset_feed_spec, making+ DOF + video + multi-text creatives unreachable through the MCP tool.+ """++ 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_discover.return_value = {+ "success": True,+ "page_id": "123456789",+ "page_name": "Test Page"+ }++ mock_api.side_effect = [+ # 1) Auto-fetch video thumbnail+ {"picture": "https://example.com/auto-thumb.jpg"},+ # 2) POST create creative+ {"id": "creative_vid_dof_cta"},+ # 3) GET creative details+ {"id": "creative_vid_dof_cta", "name": "DOF Video", "status": "ACTIVE"},+ ]++ await create_ad_creative(+ account_id="act_123456",+ video_id="vid_dof_cta",+ name="DOF Video Creative",+ link_url="https://example.com/landing",+ optimization_type="DEGREES_OF_FREEDOM",+ messages=["Body A", "Body B"],+ headlines=["Headline A", "Headline B"],+ descriptions=["Desc A", "Desc B"],+ call_to_action_type="LEARN_MORE",+ access_token="test_token",+ )++ creative_data = mock_api.call_args_list[1][0][2]+ afs = creative_data["asset_feed_spec"]++ # DOF + video: AFS must carry the link and CTA — these were the missing+ # fields that caused error_subcode 2061015.+ assert afs["link_urls"] == [{"website_url": "https://example.com/landing"}]+ assert afs["call_to_action_types"] == ["LEARN_MORE"]++ # AFS also keeps the DOF optimization signal and the video itself.+ assert afs["optimization_type"] == "DEGREES_OF_FREEDOM"+ assert afs["ad_formats"] == ["SINGLE_VIDEO"]+ assert afs["videos"] == [+ {"video_id": "vid_dof_cta", "thumbnail_url": "https://example.com/auto-thumb.jpg"}+ ]++ # Multi-text variants flow through unchanged.+ assert afs["bodies"] == [{"text": "Body A"}, {"text": "Body B"}]+ assert afs["titles"] == [{"text": "Headline A"}, {"text": "Headline B"}]+ assert afs["descriptions"] == [{"text": "Desc A"}, {"text": "Desc B"}]++ # object_story_spec must carry the link_data.link anchor (Meta v24+ # rejects a bare {page_id} with error_subcode 2061015 "link field+ # required"). A video_data anchor would trip 1443226 instead.+ oss = creative_data["object_story_spec"]+ assert oss == {+ "page_id": "123456789",+ "link_data": {"link": "https://example.com/landing"},+ }+++async def test_video_creative_with_dof_and_call_now_cta_uses_call_to_actions_plural():+ """DOF + video + CALL_NOW: phone_number must travel as+ call_to_actions: [{type, value: {link: 'tel:...'}}] in asset_feed_spec+ (the singular call_to_action_types drops the phone payload).+ """++ 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_discover.return_value = {+ "success": True,+ "page_id": "123456789",+ "page_name": "Test Page",+ }++ mock_api.side_effect = [+ {"picture": "https://example.com/auto-thumb.jpg"},+ {"id": "creative_vid_dof_call_now"},+ {"id": "creative_vid_dof_call_now", "name": "DOF Video CALL_NOW", "status": "ACTIVE"},+ ]++ await create_ad_creative(+ account_id="act_123456",+ video_id="vid_dof_phone",+ name="DOF Video CALL_NOW",+ link_url="https://example.com/",+ optimization_type="DEGREES_OF_FREEDOM",+ messages=["Body"],+ call_to_action_type="CALL_NOW",+ phone_number="+15551234567",+ access_token="test_token",+ )++ creative_data = mock_api.call_args_list[1][0][2]+ afs = creative_data["asset_feed_spec"]++ # CALL_NOW with phone_number must land in the plural call_to_actions+ # binding with tel: link inside value — not call_to_action_types.+ assert afs["call_to_actions"] == [+ {"type": "CALL_NOW", "value": {"link": "tel:+15551234567"}}+ ]+ assert "call_to_action_types" not in afs++ # DOF + video object_story_spec needs the link_data.link anchor.+ assert creative_data["object_story_spec"] == {+ "page_id": "123456789",+ "link_data": {"link": "https://example.com/"},+ }++@pytest.mark.asyncioasync def test_video_and_image_hash_mutual_exclusivity():"""Test that providing both video_id and image_hash returns an error."""