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: use ad account advideos edge in get_ad_video to resolve BM token permission errors

Yves Junqueira4be80efe359c
diff --git a/meta_ads_mcp/core/ads.py b/meta_ads_mcp/core/ads.py
index 860086c..1037ce2 100644
--- a/meta_ads_mcp/core/ads.py
+++ b/meta_ads_mcp/core/ads.py
@@ -674,12 +674,38 @@ async def get_ad_video(ad_id: str = "", video_id: str = "", access_token: Option
"hint": "This ad may be an image ad. Use get_ad_image instead."
}, indent=2)
- # Fetch video details including source URL
- video_data = await make_api_request(
- video_id,
- access_token,
- {"fields": "source,title,description,length,picture,thumbnails,created_time"}
- )
+ video_fields = "source,title,description,length,picture,thumbnails,created_time"
+
+ # Strategy 1: Try fetching via the ad account's advideos edge.
+ # Direct GET /{video_id} fails for BM-shared tokens (error 100/33) and
+ # page-owned videos (error #10). The ad account edge works for any video
+ # that belongs to the account's video library.
+ account_id = ""
+ if ad_id:
+ ad_data = await make_api_request(ad_id, access_token, {"fields": "account_id"})
+ account_id = ad_data.get("account_id", "")
+
+ video_data = None
+ if account_id:
+ advideos_data = await make_api_request(
+ f"act_{account_id}/advideos",
+ access_token,
+ {
+ "fields": video_fields,
+ "filtering": json.dumps([{"field": "id", "operator": "IN", "value": [video_id]}]),
+ },
+ )
+ if "data" in advideos_data and advideos_data["data"]:
+ video_data = advideos_data["data"][0]
+ logger.debug(f"Video {video_id} resolved via ad account advideos edge")
+
+ # Strategy 2: Fall back to direct video node access.
+ if not video_data:
+ video_data = await make_api_request(
+ video_id,
+ access_token,
+ {"fields": video_fields}
+ )
if "error" in video_data:
return json.dumps({"error": f"Could not get video {video_id}", "details": video_data}, indent=2)
diff --git a/tests/test_get_ad_video.py b/tests/test_get_ad_video.py
index 888128f..fd7321f 100644
--- a/tests/test_get_ad_video.py
+++ b/tests/test_get_ad_video.py
@@ -1,12 +1,12 @@
"""Tests for get_ad_video function.
Tests video ID extraction from ad creatives (object_story_spec and asset_feed_spec)
-and Meta Graph API source URL retrieval.
+and Meta Graph API source URL retrieval, including the advideos edge fallback.
"""
import pytest
import json
-from unittest.mock import AsyncMock, patch
+from unittest.mock import AsyncMock, patch, call
from meta_ads_mcp.core.ads import get_ad_video
@@ -14,7 +14,7 @@ from meta_ads_mcp.core.ads import get_ad_video
class TestGetAdVideo:
async def test_get_ad_video_with_ad_id_object_story_spec(self):
- """Extract video_id from object_story_spec.video_data and return source URL."""
+ """Extract video_id from object_story_spec.video_data and return source URL via advideos edge."""
mock_creatives = {
"data": [{
"id": "creative_123",
@@ -27,20 +27,25 @@ class TestGetAdVideo:
}]
}
- mock_video_details = {
- "source": "https://video-xx.fbcdn.net/v/example.mp4",
- "picture": "https://example.com/thumb.jpg",
- "title": "My Ad Video",
- "description": "Test description",
- "length": 30.5,
- "created_time": "2026-03-01T12:00:00+0000",
+ mock_ad_data = {"account_id": "111222333"}
+
+ mock_advideos_response = {
+ "data": [{
+ "id": "9999",
+ "source": "https://video-xx.fbcdn.net/v/example.mp4",
+ "picture": "https://example.com/thumb.jpg",
+ "title": "My Ad Video",
+ "description": "Test description",
+ "length": 30.5,
+ "created_time": "2026-03-01T12:00:00+0000",
+ }]
}
with patch('meta_ads_mcp.core.ads.get_ad_creatives', new_callable=AsyncMock) as mock_get_creatives, \
patch('meta_ads_mcp.core.ads.make_api_request', new_callable=AsyncMock) as mock_api:
mock_get_creatives.return_value = json.dumps(mock_creatives)
- mock_api.return_value = mock_video_details
+ mock_api.side_effect = [mock_ad_data, mock_advideos_response]
result = await get_ad_video(access_token="test_token", ad_id="ad_123")
data = json.loads(result)
@@ -51,13 +56,12 @@ class TestGetAdVideo:
assert data["duration_seconds"] == 30.5
assert data["ad_id"] == "ad_123"
- mock_api.assert_called_once_with(
- "9999", "test_token",
- {"fields": "source,title,description,length,picture,thumbnails,created_time"}
- )
+ # Should call: 1) get account_id from ad, 2) advideos edge
+ assert mock_api.call_count == 2
+ mock_api.assert_any_call("ad_123", "test_token", {"fields": "account_id"})
- async def test_get_ad_video_with_ad_id_asset_feed_spec(self):
- """Extract video_id from asset_feed_spec.videos when object_story_spec has no video."""
+ async def test_get_ad_video_advideos_edge_empty_falls_back(self):
+ """When advideos edge returns empty (page-owned video), fall back to direct node."""
mock_creatives = {
"data": [{
"id": "creative_456",
@@ -65,14 +69,15 @@ class TestGetAdVideo:
"asset_feed_spec": {
"videos": [
{"video_id": "7777", "thumbnail_url": "https://example.com/thumb2.jpg"},
- {"video_id": "8888", "thumbnail_url": "https://example.com/thumb3.jpg"},
]
}
}]
}
- mock_video_details = {
- "source": "https://video-xx.fbcdn.net/v/flex-video.mp4",
+ mock_ad_data = {"account_id": "111222333"}
+ mock_advideos_empty = {"data": []}
+ mock_direct_video = {
+ "source": "https://video-xx.fbcdn.net/v/page-video.mp4",
"picture": "https://example.com/thumb2.jpg",
"length": 15.0,
}
@@ -81,16 +86,18 @@ class TestGetAdVideo:
patch('meta_ads_mcp.core.ads.make_api_request', new_callable=AsyncMock) as mock_api:
mock_get_creatives.return_value = json.dumps(mock_creatives)
- mock_api.return_value = mock_video_details
+ # 1) account_id lookup, 2) advideos edge (empty), 3) direct node fallback
+ mock_api.side_effect = [mock_ad_data, mock_advideos_empty, mock_direct_video]
result = await get_ad_video(access_token="test_token", ad_id="ad_456")
data = json.loads(result)
assert data["video_id"] == "7777"
- assert data["source_url"] == "https://video-xx.fbcdn.net/v/flex-video.mp4"
+ assert data["source_url"] == "https://video-xx.fbcdn.net/v/page-video.mp4"
+ assert mock_api.call_count == 3
async def test_get_ad_video_with_direct_video_id(self):
- """Bypass creative lookup when video_id is provided directly."""
+ """Bypass creative lookup when video_id is provided directly (no ad_id = no advideos edge)."""
mock_video_details = {
"source": "https://video-xx.fbcdn.net/v/direct.mp4",
"picture": "https://example.com/thumb.jpg",
@@ -108,6 +115,12 @@ class TestGetAdVideo:
assert data["source_url"] == "https://video-xx.fbcdn.net/v/direct.mp4"
assert "ad_id" not in data
+ # Without ad_id, only the direct node call is made
+ mock_api.assert_called_once_with(
+ "5555", "test_token",
+ {"fields": "source,title,description,length,picture,thumbnails,created_time"}
+ )
+
async def test_get_ad_video_no_video_in_creative(self):
"""Return helpful error when the ad is an image ad, not a video ad."""
mock_creatives = {
@@ -153,3 +166,35 @@ class TestGetAdVideo:
assert "error" in data
assert "Could not get video" in data["error"]
+
+ async def test_get_ad_video_account_id_lookup_fails(self):
+ """When account_id lookup fails, skip advideos edge and go direct."""
+ mock_creatives = {
+ "data": [{
+ "id": "creative_999",
+ "object_story_spec": {
+ "video_data": {"video_id": "4444"}
+ }
+ }]
+ }
+
+ mock_ad_data_error = {"error": {"message": "Not found"}}
+ mock_direct_video = {
+ "source": "https://video-xx.fbcdn.net/v/fallback.mp4",
+ "picture": "https://example.com/thumb.jpg",
+ "length": 20.0,
+ }
+
+ with patch('meta_ads_mcp.core.ads.get_ad_creatives', new_callable=AsyncMock) as mock_get_creatives, \
+ patch('meta_ads_mcp.core.ads.make_api_request', new_callable=AsyncMock) as mock_api:
+
+ mock_get_creatives.return_value = json.dumps(mock_creatives)
+ # 1) account_id lookup fails (no account_id key), 2) direct fallback
+ mock_api.side_effect = [mock_ad_data_error, mock_direct_video]
+
+ result = await get_ad_video(access_token="test_token", ad_id="ad_999")
+ data = json.loads(result)
+
+ assert data["video_id"] == "4444"
+ assert data["source_url"] == "https://video-xx.fbcdn.net/v/fallback.mp4"
+ assert mock_api.call_count == 2