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 get_ad_creatives and get_ad_image JSON parsing errors

Yves Junqueiraadbf32b459f8
diff --git a/meta_ads_mcp/__init__.py b/meta_ads_mcp/__init__.py
index f6a9b3e..5a29081 100644
--- a/meta_ads_mcp/__init__.py
+++ b/meta_ads_mcp/__init__.py
@@ -7,7 +7,7 @@ with the Claude LLM.
from meta_ads_mcp.core.server import main
-__version__ = "0.4.7"
+__version__ = "0.4.8"
__all__ = [
'get_ad_accounts',
diff --git a/meta_ads_mcp/core/ads.py b/meta_ads_mcp/core/ads.py
index d1f11dc..488c32f 100644
--- a/meta_ads_mcp/core/ads.py
+++ b/meta_ads_mcp/core/ads.py
@@ -251,14 +251,27 @@ async def get_ad_image(access_token: str = None, ad_id: str = None) -> Image:
if not image_hashes:
# If no hashes found, try to extract from the first creative we found in the API
# Get creative for ad to try to extract hash
- creative_json = await get_ad_creatives(ad_id, "", access_token)
- creative_data = json.loads(creative_json)
-
- # Try to extract hash from asset_feed_spec
- if "asset_feed_spec" in creative_data and "images" in creative_data["asset_feed_spec"]:
- images = creative_data["asset_feed_spec"]["images"]
- if images and len(images) > 0 and "hash" in images[0]:
- image_hashes.append(images[0]["hash"])
+ creative_json = await get_ad_creatives(access_token=access_token, ad_id=ad_id)
+ # The result is wrapped by @meta_api_tool, so we need to extract the data
+ creative_wrapper = json.loads(creative_json)
+ creative_data = json.loads(creative_wrapper["data"])
+
+ # Try to extract hash from data array
+ if "data" in creative_data and creative_data["data"]:
+ for creative in creative_data["data"]:
+ # Check object_story_spec for image hash
+ if "object_story_spec" in creative and "link_data" in creative["object_story_spec"]:
+ link_data = creative["object_story_spec"]["link_data"]
+ if "image_hash" in link_data:
+ image_hashes.append(link_data["image_hash"])
+ # Check direct image_hash on creative
+ elif "image_hash" in creative:
+ image_hashes.append(creative["image_hash"])
+ # Check asset_feed_spec for image hashes
+ elif "asset_feed_spec" in creative and "images" in creative["asset_feed_spec"]:
+ images = creative["asset_feed_spec"]["images"]
+ if images and len(images) > 0 and "hash" in images[0]:
+ image_hashes.append(images[0]["hash"])
if not image_hashes:
return "Error: No image hashes found in creative"
@@ -380,7 +393,9 @@ async def save_ad_image_locally(access_token: str = None, ad_id: str = None, out
if not image_hashes:
# Fallback attempt (as in get_ad_image)
creative_json = await get_ad_creatives(ad_id=ad_id, access_token=access_token) # Ensure ad_id is passed correctly
- creative_data_list = json.loads(creative_json)
+ # The result is wrapped by @meta_api_tool, so we need to extract the data
+ creative_wrapper = json.loads(creative_json)
+ creative_data_list = json.loads(creative_wrapper["data"])
if 'data' in creative_data_list and creative_data_list['data']:
first_creative = creative_data_list['data'][0]
if 'object_story_spec' in first_creative and 'link_data' in first_creative['object_story_spec'] and 'image_hash' in first_creative['object_story_spec']['link_data']:
diff --git a/pyproject.toml b/pyproject.toml
index 16533b4..c2d7faa 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "meta-ads-mcp"
-version = "0.4.7"
+version = "0.4.8"
description = "Model Context Protocol (MCP) plugin for interacting with Meta Ads API"
readme = "README.md"
requires-python = ">=3.10"
diff --git a/tests/test_get_ad_image_regression.py b/tests/test_get_ad_image_regression.py
new file mode 100644
index 0000000..0120f94
--- /dev/null
+++ b/tests/test_get_ad_image_regression.py
@@ -0,0 +1,188 @@
+"""Regression tests for get_ad_image function JSON parsing fix.
+
+Tests for issue where get_ad_image would throw:
+'TypeError: the JSON object must be str, bytes or bytearray, not dict'
+
+This was caused by:
+1. Wrong parameter order when calling get_ad_creatives (ad_id, "", access_token instead of access_token=x, ad_id=y)
+2. Incorrect JSON parsing of the @meta_api_tool decorator wrapped response
+
+The fix:
+1. Corrected the parameter order in get_ad_creatives calls
+2. Updated JSON parsing to handle the wrapped response format: {"data": "JSON_STRING"}
+"""
+
+import pytest
+import json
+from unittest.mock import AsyncMock, patch, MagicMock
+from meta_ads_mcp.core.ads import get_ad_image
+
+
+class TestGetAdImageRegressionFix:
+ """Regression test cases for the get_ad_image JSON parsing bug fix."""
+
+ async def test_get_ad_image_json_parsing_regression_fix(self):
+ """Regression test: ensure get_ad_image doesn't throw JSON parsing error."""
+
+ # Mock responses for the main API flow
+ mock_ad_data = {
+ "account_id": "act_123456789",
+ "creative": {"id": "creative_123456789"}
+ }
+
+ mock_creative_details = {
+ "id": "creative_123456789",
+ "name": "Test Creative",
+ "image_hash": "test_hash_123"
+ }
+
+ mock_image_data = {
+ "data": [{
+ "hash": "test_hash_123",
+ "url": "https://example.com/image.jpg",
+ "width": 1200,
+ "height": 628,
+ "name": "test_image.jpg",
+ "status": "ACTIVE"
+ }]
+ }
+
+ # Mock PIL Image processing to return a valid Image object
+ mock_pil_image = MagicMock()
+ mock_pil_image.mode = "RGB"
+ mock_pil_image.convert.return_value = mock_pil_image
+
+ mock_byte_stream = MagicMock()
+ mock_byte_stream.getvalue.return_value = b"fake_jpeg_data"
+
+ with patch('meta_ads_mcp.core.ads.make_api_request', new_callable=AsyncMock) as mock_api, \
+ patch('meta_ads_mcp.core.ads.download_image', new_callable=AsyncMock) as mock_download, \
+ patch('meta_ads_mcp.core.ads.PILImage.open') as mock_pil_open, \
+ patch('meta_ads_mcp.core.ads.io.BytesIO') as mock_bytesio:
+
+ mock_api.side_effect = [mock_ad_data, mock_creative_details, mock_image_data]
+ mock_download.return_value = b"fake_image_bytes"
+ mock_pil_open.return_value = mock_pil_image
+ mock_bytesio.return_value = mock_byte_stream
+
+ # This should NOT raise "the JSON object must be str, bytes or bytearray, not dict"
+ # Previously this would fail with: TypeError: the JSON object must be str, bytes or bytearray, not dict
+ result = await get_ad_image(access_token="test_token", ad_id="120228922871870272")
+
+ # Verify we get an Image object (success) - the exact test depends on the mocking
+ # The key is that we don't get the JSON parsing error
+ assert result is not None
+
+ # The main regression check: if we got here without an exception, the JSON parsing is fixed
+ # We might get different results based on mocking, but the critical JSON parsing should work
+
+ async def test_get_ad_image_fallback_path_json_parsing(self):
+ """Test the fallback path that calls get_ad_creatives handles JSON parsing correctly."""
+
+ # Mock responses that trigger the fallback path (no direct image hash)
+ mock_ad_data = {
+ "account_id": "act_123456789",
+ "creative": {"id": "creative_123456789"}
+ }
+
+ mock_creative_details = {
+ "id": "creative_123456789",
+ "name": "Test Creative"
+ # No image_hash - this will trigger the fallback
+ }
+
+ # Mock get_ad_creatives response (wrapped format that caused the original bug)
+ mock_get_ad_creatives_response = json.dumps({
+ "data": json.dumps({
+ "data": [
+ {
+ "id": "creative_123456789",
+ "name": "Test Creative",
+ "object_story_spec": {
+ "link_data": {
+ "image_hash": "fallback_hash_123"
+ }
+ }
+ }
+ ]
+ })
+ })
+
+ mock_image_data = {
+ "data": [{
+ "hash": "fallback_hash_123",
+ "url": "https://example.com/fallback_image.jpg",
+ "width": 1200,
+ "height": 628
+ }]
+ }
+
+ # Mock PIL Image processing
+ mock_pil_image = MagicMock()
+ mock_pil_image.mode = "RGB"
+ mock_pil_image.convert.return_value = mock_pil_image
+
+ mock_byte_stream = MagicMock()
+ mock_byte_stream.getvalue.return_value = b"fake_jpeg_data"
+
+ with patch('meta_ads_mcp.core.ads.make_api_request', new_callable=AsyncMock) as mock_api, \
+ patch('meta_ads_mcp.core.ads.get_ad_creatives', new_callable=AsyncMock) as mock_get_creatives, \
+ patch('meta_ads_mcp.core.ads.download_image', new_callable=AsyncMock) as mock_download, \
+ patch('meta_ads_mcp.core.ads.PILImage.open') as mock_pil_open, \
+ patch('meta_ads_mcp.core.ads.io.BytesIO') as mock_bytesio:
+
+ mock_api.side_effect = [mock_ad_data, mock_creative_details, mock_image_data]
+ mock_get_creatives.return_value = mock_get_ad_creatives_response
+ mock_download.return_value = b"fake_image_bytes"
+ mock_pil_open.return_value = mock_pil_image
+ mock_bytesio.return_value = mock_byte_stream
+
+ # This should handle the wrapped JSON response correctly
+ # Previously would fail: TypeError: the JSON object must be str, bytes or bytearray, not dict
+ result = await get_ad_image(access_token="test_token", ad_id="120228922871870272")
+
+ # Verify the fallback path worked - key is no JSON parsing exception
+ assert result is not None
+ # Verify get_ad_creatives was called (fallback path was triggered)
+ mock_get_creatives.assert_called_once()
+
+ async def test_get_ad_image_no_ad_id(self):
+ """Test get_ad_image with no ad_id provided."""
+
+ result = await get_ad_image(access_token="test_token", ad_id=None)
+
+ # Should return error string, not throw JSON parsing error
+ assert isinstance(result, str)
+ assert "Error: No ad ID provided" in result
+
+ async def test_get_ad_image_parameter_order_regression(self):
+ """Regression test: ensure get_ad_creatives is called with correct parameter order."""
+
+ # This test ensures we don't regress to calling get_ad_creatives(ad_id, "", access_token)
+ # which was the original bug
+
+ mock_ad_data = {
+ "account_id": "act_123456789",
+ "creative": {"id": "creative_123456789"}
+ }
+
+ mock_creative_details = {
+ "id": "creative_123456789",
+ "name": "Test Creative"
+ # No image_hash to trigger fallback
+ }
+
+ with patch('meta_ads_mcp.core.ads.make_api_request', new_callable=AsyncMock) as mock_api, \
+ patch('meta_ads_mcp.core.ads.get_ad_creatives', new_callable=AsyncMock) as mock_get_creatives:
+
+ mock_api.side_effect = [mock_ad_data, mock_creative_details]
+ mock_get_creatives.return_value = json.dumps({"data": json.dumps({"data": []})})
+
+ # Call get_ad_image - it should reach the fallback path
+ result = await get_ad_image(access_token="test_token", ad_id="test_ad_id")
+
+ # Verify get_ad_creatives was called with correct parameter names (not positional)
+ mock_get_creatives.assert_called_once_with(ad_id="test_ad_id", access_token="test_token")
+
+ # The key regression test: this should not have raised a JSON parsing error
\ No newline at end of file