meta-ads-mcp 0.11.0__py3-none-any.whl → 0.11.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- meta_ads_mcp/__init__.py +1 -1
- meta_ads_mcp/core/ads.py +118 -113
- {meta_ads_mcp-0.11.0.dist-info → meta_ads_mcp-0.11.1.dist-info}/METADATA +1 -1
- {meta_ads_mcp-0.11.0.dist-info → meta_ads_mcp-0.11.1.dist-info}/RECORD +7 -7
- {meta_ads_mcp-0.11.0.dist-info → meta_ads_mcp-0.11.1.dist-info}/WHEEL +0 -0
- {meta_ads_mcp-0.11.0.dist-info → meta_ads_mcp-0.11.1.dist-info}/entry_points.txt +0 -0
- {meta_ads_mcp-0.11.0.dist-info → meta_ads_mcp-0.11.1.dist-info}/licenses/LICENSE +0 -0
meta_ads_mcp/__init__.py
CHANGED
meta_ads_mcp/core/ads.py
CHANGED
|
@@ -14,6 +14,10 @@ from .utils import download_image, try_multiple_download_methods, ad_creative_im
|
|
|
14
14
|
from .server import mcp_server
|
|
15
15
|
|
|
16
16
|
|
|
17
|
+
# Only register the save_ad_image_locally function if explicitly enabled via environment variable
|
|
18
|
+
ENABLE_SAVE_AD_IMAGE_LOCALLY = bool(os.environ.get("META_ADS_ENABLE_SAVE_AD_IMAGE_LOCALLY", ""))
|
|
19
|
+
|
|
20
|
+
|
|
17
21
|
@mcp_server.tool()
|
|
18
22
|
@meta_api_tool
|
|
19
23
|
async def get_ads(account_id: str, access_token: Optional[str] = None, limit: int = 10,
|
|
@@ -380,130 +384,131 @@ async def get_ad_image(ad_id: str, access_token: Optional[str] = None) -> Image:
|
|
|
380
384
|
return f"Error processing image: {str(e)}"
|
|
381
385
|
|
|
382
386
|
|
|
383
|
-
|
|
384
|
-
@
|
|
385
|
-
|
|
386
|
-
""
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
Args:
|
|
390
|
-
ad_id: Meta Ads ad ID
|
|
391
|
-
access_token: Meta API access token (optional - will use cached token if not provided)
|
|
392
|
-
output_dir: Directory to save the image file (default: 'ad_images')
|
|
393
|
-
|
|
394
|
-
Returns:
|
|
395
|
-
The file path to the saved image, or an error message string.
|
|
396
|
-
"""
|
|
397
|
-
if not ad_id:
|
|
398
|
-
return json.dumps({"error": "No ad ID provided"}, indent=2)
|
|
387
|
+
if ENABLE_SAVE_AD_IMAGE_LOCALLY:
|
|
388
|
+
@mcp_server.tool()
|
|
389
|
+
@meta_api_tool
|
|
390
|
+
async def save_ad_image_locally(ad_id: str, access_token: Optional[str] = None, output_dir: str = "ad_images") -> str:
|
|
391
|
+
"""
|
|
392
|
+
Get, download, and save a Meta ad image locally, returning the file path.
|
|
399
393
|
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
ad_params = {
|
|
405
|
-
"fields": "creative{id},account_id"
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
ad_data = await make_api_request(ad_endpoint, access_token, ad_params)
|
|
409
|
-
|
|
410
|
-
if "error" in ad_data:
|
|
411
|
-
return json.dumps({"error": f"Could not get ad data - {json.dumps(ad_data)}"}, indent=2)
|
|
412
|
-
|
|
413
|
-
account_id = ad_data.get("account_id")
|
|
414
|
-
if not account_id:
|
|
415
|
-
return json.dumps({"error": "No account ID found for ad"}, indent=2)
|
|
416
|
-
|
|
417
|
-
if "creative" not in ad_data:
|
|
418
|
-
return json.dumps({"error": "No creative found for this ad"}, indent=2)
|
|
394
|
+
Args:
|
|
395
|
+
ad_id: Meta Ads ad ID
|
|
396
|
+
access_token: Meta API access token (optional - will use cached token if not provided)
|
|
397
|
+
output_dir: Directory to save the image file (default: 'ad_images')
|
|
419
398
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
399
|
+
Returns:
|
|
400
|
+
The file path to the saved image, or an error message string.
|
|
401
|
+
"""
|
|
402
|
+
if not ad_id:
|
|
403
|
+
return json.dumps({"error": "No ad ID provided"}, indent=2)
|
|
404
|
+
|
|
405
|
+
print(f"Attempting to get and save creative image for ad {ad_id}")
|
|
406
|
+
|
|
407
|
+
# First, get creative and account IDs
|
|
408
|
+
ad_endpoint = f"{ad_id}"
|
|
409
|
+
ad_params = {
|
|
410
|
+
"fields": "creative{id},account_id"
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
ad_data = await make_api_request(ad_endpoint, access_token, ad_params)
|
|
414
|
+
|
|
415
|
+
if "error" in ad_data:
|
|
416
|
+
return json.dumps({"error": f"Could not get ad data - {json.dumps(ad_data)}"}, indent=2)
|
|
417
|
+
|
|
418
|
+
account_id = ad_data.get("account_id")
|
|
419
|
+
if not account_id:
|
|
420
|
+
return json.dumps({"error": "No account ID found for ad"}, indent=2)
|
|
421
|
+
|
|
422
|
+
if "creative" not in ad_data:
|
|
423
|
+
return json.dumps({"error": "No creative found for this ad"}, indent=2)
|
|
424
|
+
|
|
425
|
+
creative_data = ad_data.get("creative", {})
|
|
426
|
+
creative_id = creative_data.get("id")
|
|
427
|
+
if not creative_id:
|
|
428
|
+
return json.dumps({"error": "No creative ID found"}, indent=2)
|
|
429
|
+
|
|
430
|
+
# Get creative details to find image hash
|
|
431
|
+
creative_endpoint = f"{creative_id}"
|
|
432
|
+
creative_params = {
|
|
433
|
+
"fields": "id,name,image_hash,asset_feed_spec"
|
|
434
|
+
}
|
|
435
|
+
creative_details = await make_api_request(creative_endpoint, access_token, creative_params)
|
|
436
|
+
|
|
437
|
+
image_hashes = []
|
|
438
|
+
if "image_hash" in creative_details:
|
|
439
|
+
image_hashes.append(creative_details["image_hash"])
|
|
440
|
+
if "asset_feed_spec" in creative_details and "images" in creative_details["asset_feed_spec"]:
|
|
441
|
+
for image in creative_details["asset_feed_spec"]["images"]:
|
|
442
|
+
if "hash" in image:
|
|
443
|
+
image_hashes.append(image["hash"])
|
|
444
|
+
|
|
445
|
+
if not image_hashes:
|
|
446
|
+
# Fallback attempt (as in get_ad_image)
|
|
447
|
+
creative_json = await get_ad_creatives(ad_id=ad_id, access_token=access_token) # Ensure ad_id is passed correctly
|
|
448
|
+
creative_data_list = json.loads(creative_json)
|
|
449
|
+
if 'data' in creative_data_list and creative_data_list['data']:
|
|
450
|
+
first_creative = creative_data_list['data'][0]
|
|
451
|
+
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']:
|
|
452
|
+
image_hashes.append(first_creative['object_story_spec']['link_data']['image_hash'])
|
|
453
|
+
elif 'image_hash' in first_creative: # Check direct hash on creative data
|
|
454
|
+
image_hashes.append(first_creative['image_hash'])
|
|
450
455
|
|
|
451
456
|
|
|
452
|
-
|
|
453
|
-
|
|
457
|
+
if not image_hashes:
|
|
458
|
+
return json.dumps({"error": "No image hashes found in creative or fallback"}, indent=2)
|
|
454
459
|
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
# Fetch image data using the first hash
|
|
458
|
-
image_endpoint = f"act_{account_id}/adimages"
|
|
459
|
-
hashes_str = f'["{image_hashes[0]}"]'
|
|
460
|
-
image_params = {
|
|
461
|
-
"fields": "hash,url,width,height,name,status",
|
|
462
|
-
"hashes": hashes_str
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
print(f"Requesting image data with params: {image_params}")
|
|
466
|
-
image_data = await make_api_request(image_endpoint, access_token, image_params)
|
|
467
|
-
|
|
468
|
-
if "error" in image_data:
|
|
469
|
-
return json.dumps({"error": f"Failed to get image data - {json.dumps(image_data)}"}, indent=2)
|
|
470
|
-
|
|
471
|
-
if "data" not in image_data or not image_data["data"]:
|
|
472
|
-
return json.dumps({"error": "No image data returned from API"}, indent=2)
|
|
460
|
+
print(f"Found image hashes: {image_hashes}")
|
|
473
461
|
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
462
|
+
# Fetch image data using the first hash
|
|
463
|
+
image_endpoint = f"act_{account_id}/adimages"
|
|
464
|
+
hashes_str = f'["{image_hashes[0]}"]'
|
|
465
|
+
image_params = {
|
|
466
|
+
"fields": "hash,url,width,height,name,status",
|
|
467
|
+
"hashes": hashes_str
|
|
468
|
+
}
|
|
479
469
|
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
# Download and Save Image
|
|
483
|
-
image_bytes = await download_image(image_url)
|
|
484
|
-
|
|
485
|
-
if not image_bytes:
|
|
486
|
-
return json.dumps({"error": "Failed to download image"}, indent=2)
|
|
470
|
+
print(f"Requesting image data with params: {image_params}")
|
|
471
|
+
image_data = await make_api_request(image_endpoint, access_token, image_params)
|
|
487
472
|
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
473
|
+
if "error" in image_data:
|
|
474
|
+
return json.dumps({"error": f"Failed to get image data - {json.dumps(image_data)}"}, indent=2)
|
|
475
|
+
|
|
476
|
+
if "data" not in image_data or not image_data["data"]:
|
|
477
|
+
return json.dumps({"error": "No image data returned from API"}, indent=2)
|
|
478
|
+
|
|
479
|
+
first_image = image_data["data"][0]
|
|
480
|
+
image_url = first_image.get("url")
|
|
481
|
+
|
|
482
|
+
if not image_url:
|
|
483
|
+
return json.dumps({"error": "No valid image URL found in API response"}, indent=2)
|
|
484
|
+
|
|
485
|
+
print(f"Downloading image from URL: {image_url}")
|
|
486
|
+
|
|
487
|
+
# Download and Save Image
|
|
488
|
+
image_bytes = await download_image(image_url)
|
|
489
|
+
|
|
490
|
+
if not image_bytes:
|
|
491
|
+
return json.dumps({"error": "Failed to download image"}, indent=2)
|
|
492
492
|
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
f
|
|
493
|
+
try:
|
|
494
|
+
# Ensure output directory exists
|
|
495
|
+
if not os.path.exists(output_dir):
|
|
496
|
+
os.makedirs(output_dir)
|
|
497
|
+
|
|
498
|
+
# Create a filename (e.g., using ad_id and image hash)
|
|
499
|
+
file_extension = ".jpg" # Default extension, could try to infer from headers later
|
|
500
|
+
filename = f"{ad_id}_{image_hashes[0]}{file_extension}"
|
|
501
|
+
filepath = os.path.join(output_dir, filename)
|
|
501
502
|
|
|
502
|
-
|
|
503
|
-
|
|
503
|
+
# Save the image bytes to the file
|
|
504
|
+
with open(filepath, "wb") as f:
|
|
505
|
+
f.write(image_bytes)
|
|
506
|
+
|
|
507
|
+
print(f"Image saved successfully to: {filepath}")
|
|
508
|
+
return json.dumps({"filepath": filepath}, indent=2) # Return JSON with filepath
|
|
504
509
|
|
|
505
|
-
|
|
506
|
-
|
|
510
|
+
except Exception as e:
|
|
511
|
+
return json.dumps({"error": f"Failed to save image: {str(e)}"}, indent=2)
|
|
507
512
|
|
|
508
513
|
|
|
509
514
|
@mcp_server.tool()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: meta-ads-mcp
|
|
3
|
-
Version: 0.11.
|
|
3
|
+
Version: 0.11.1
|
|
4
4
|
Summary: Model Context Protocol (MCP) server for interacting with Meta Ads API
|
|
5
5
|
Project-URL: Homepage, https://github.com/pipeboard-co/meta-ads-mcp
|
|
6
6
|
Project-URL: Bug Tracker, https://github.com/pipeboard-co/meta-ads-mcp/issues
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
meta_ads_mcp/__init__.py,sha256=
|
|
1
|
+
meta_ads_mcp/__init__.py,sha256=PjB9-VtSzMqi_4ayhHLep5t9KBHvAdrVigxMbUGMNrQ,1477
|
|
2
2
|
meta_ads_mcp/__main__.py,sha256=XaQt3iXftG_7f0Zu7Wop9SeFgrD2WBn0EQOaPMc27d8,207
|
|
3
3
|
meta_ads_mcp/core/__init__.py,sha256=IEJtqpyUo0CZSUWeQPljQ-D2vKorTFwXnpBQWSi1hIM,1819
|
|
4
4
|
meta_ads_mcp/core/accounts.py,sha256=7Zoqq0zMIJi_Xsxe9-_b3EYx-UTeieJJvO7HxVRuUS0,4327
|
|
5
|
-
meta_ads_mcp/core/ads.py,sha256=
|
|
5
|
+
meta_ads_mcp/core/ads.py,sha256=yQl6YssmBhvEK_Sciy7xpcTBKQeVGD6TQGYyJI2bMI4,60129
|
|
6
6
|
meta_ads_mcp/core/ads_library.py,sha256=smGz9FhM6RIUjlQT4Jv1BaZmXahGdK21eRCB7QMhK-4,3228
|
|
7
7
|
meta_ads_mcp/core/adsets.py,sha256=HYaDv1AlTnHoAynT5hNruHu93pAZRjM7cDAffQMQgdU,15850
|
|
8
8
|
meta_ads_mcp/core/api.py,sha256=58F6fRrg3ny_vuLgHu1ZN1yueNAxVXz_nEcF6JlxlWk,16469
|
|
@@ -21,8 +21,8 @@ meta_ads_mcp/core/resources.py,sha256=-zIIfZulpo76vcKv6jhAlQq91cR2SZ3cjYZt3ek3x0
|
|
|
21
21
|
meta_ads_mcp/core/server.py,sha256=9SlgM_qvdlxo24ctnZzLgW1e1nfAspCSx3YyJQkKP64,17856
|
|
22
22
|
meta_ads_mcp/core/targeting.py,sha256=-QziS2MvTWzM02pwiUtId4sblWURd3UYPR_YYfVuiUk,13913
|
|
23
23
|
meta_ads_mcp/core/utils.py,sha256=ytj41yC5SqduLrAiZYBSd6OUwlJRaIClTwnnYKpNFds,9387
|
|
24
|
-
meta_ads_mcp-0.11.
|
|
25
|
-
meta_ads_mcp-0.11.
|
|
26
|
-
meta_ads_mcp-0.11.
|
|
27
|
-
meta_ads_mcp-0.11.
|
|
28
|
-
meta_ads_mcp-0.11.
|
|
24
|
+
meta_ads_mcp-0.11.1.dist-info/METADATA,sha256=aw2oEknvxU62yHKlcJr_zmECgks-AKFu7DDEjVr8BtI,24265
|
|
25
|
+
meta_ads_mcp-0.11.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
26
|
+
meta_ads_mcp-0.11.1.dist-info/entry_points.txt,sha256=Dv2RkoBjRJBqj6CyhwqGIiwPCD-SCL1-7B9-zmVRuv0,57
|
|
27
|
+
meta_ads_mcp-0.11.1.dist-info/licenses/LICENSE,sha256=E2d762fbhwKRYn8o7J6Szr6vyBPrHVDlK3jbHPx-d84,3851
|
|
28
|
+
meta_ads_mcp-0.11.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|