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.

Refactor Meta Ads MCP API and update documentation

Yves Junqueiracd36ce08a681
diff --git a/README.md b/README.md
index 551729f..72f428d 100644
--- a/README.md
+++ b/README.md
@@ -174,14 +174,7 @@ Add this to your `claude_desktop_config.json` to integrate with Claude in Cursor
- `ad_id`: Meta Ads ad ID (optional, used if url is not provided)
- Returns: Diagnostic information about image download attempts
-14. `mcp_meta_ads_save_ad_image_via_api`
- - Try to save an ad image by using the Marketing API's attachment endpoints
- - Inputs:
- - `access_token` (optional): Meta API access token (will use cached token if not provided)
- - `ad_id`: Meta Ads ad ID
- - Returns: Results of attempts to save the ad image
-
-15. `mcp_meta_ads_get_login_link`
+14. `mcp_meta_ads_get_login_link`
- Get a clickable login link for Meta Ads authentication
- Inputs:
- `access_token` (optional): Meta API access token (will use cached token if not provided)
diff --git a/meta_ads_mcp/__init__.py b/meta_ads_mcp/__init__.py
index 2eca9c1..840ffd5 100644
--- a/meta_ads_mcp/__init__.py
+++ b/meta_ads_mcp/__init__.py
@@ -9,7 +9,25 @@ from meta_ads_mcp.core.server import main
__version__ = "0.2.3"
-__all__ = ["main"]
+__all__ = [
+ 'get_ad_accounts',
+ 'get_account_info',
+ 'get_campaigns',
+ 'get_campaign_details',
+ 'create_campaign',
+ 'get_adsets',
+ 'get_adset_details',
+ 'update_adset',
+ 'get_ads',
+ 'get_ad_details',
+ 'get_ad_creatives',
+ 'get_ad_image',
+ 'get_insights',
+ 'debug_image_download',
+ 'get_login_link',
+ 'login_cli',
+ 'main'
+]
# Import key functions to make them available at package level
from .core import (
@@ -27,9 +45,9 @@ from .core import (
get_ad_image,
get_insights,
debug_image_download,
- save_ad_image_via_api,
get_login_link,
login_cli,
+ main
)
# Define a main function to be used as a package entry point
diff --git a/meta_ads_mcp/api.py b/meta_ads_mcp/api.py
index 2487a34..31c39aa 100644
--- a/meta_ads_mcp/api.py
+++ b/meta_ads_mcp/api.py
@@ -1345,193 +1345,6 @@ async def debug_image_download(url="", ad_id="", access_token=None):
return json.dumps(results, indent=2)
-@mcp_server.tool()
-@meta_api_tool
-async def save_ad_image_via_api(access_token: str = None, ad_id: str = None) -> str:
- """
- Try to save an ad image by using the Marketing API's attachment endpoints.
- This is an alternative approach when direct image download fails.
-
- Args:
- access_token: Meta API access token (optional - will use cached token if not provided)
- ad_id: Meta Ads ad ID
- """
- if not ad_id:
- return json.dumps({"error": "No ad ID provided"}, indent=2)
-
- # First get the ad's creative ID
- ad_endpoint = f"{ad_id}"
- ad_params = {
- "fields": "creative,account_id"
- }
-
- ad_data = await make_api_request(ad_endpoint, access_token, ad_params)
-
- if "error" in ad_data:
- return json.dumps({
- "error": "Could not get ad data",
- "details": ad_data
- }, indent=2)
-
- if "creative" not in ad_data or "id" not in ad_data["creative"]:
- return json.dumps({
- "error": "No creative ID found for this ad",
- "ad_data": ad_data
- }, indent=2)
-
- creative_id = ad_data["creative"]["id"]
- account_id = ad_data.get("account_id", "")
-
- # Now get the creative object
- creative_endpoint = f"{creative_id}"
- creative_params = {
- "fields": "id,name,thumbnail_url,image_hash,asset_feed_spec"
- }
-
- creative_data = await make_api_request(creative_endpoint, access_token, creative_params)
-
- if "error" in creative_data:
- return json.dumps({
- "error": "Could not get creative data",
- "details": creative_data
- }, indent=2)
-
- # Approach 1: Try to get image through adimages endpoint if we have image_hash
- image_hash = None
- if "image_hash" in creative_data:
- image_hash = creative_data["image_hash"]
- elif "asset_feed_spec" in creative_data and "images" in creative_data["asset_feed_spec"] and len(creative_data["asset_feed_spec"]["images"]) > 0:
- image_hash = creative_data["asset_feed_spec"]["images"][0].get("hash")
-
- result = {
- "ad_id": ad_id,
- "creative_id": creative_id,
- "attempts": []
- }
-
- if image_hash and account_id:
- attempt = {
- "method": "adimages endpoint with hash",
- "success": False
- }
- result["attempts"].append(attempt)
-
- try:
- image_endpoint = f"act_{account_id}/adimages"
- image_params = {
- "hashes": [image_hash]
- }
- image_data = await make_api_request(image_endpoint, access_token, image_params)
- attempt["response"] = image_data
-
- if "data" in image_data and len(image_data["data"]) > 0 and "url" in image_data["data"][0]:
- url = image_data["data"][0]["url"]
- attempt["url"] = url
-
- # Try to download the image
- image_bytes = await download_image(url)
- if image_bytes:
- attempt["success"] = True
- attempt["image_size"] = len(image_bytes)
-
- # Save the image
- resource_id = f"ad_creative_{ad_id}_method1"
- resource_uri = f"meta-ads://images/{resource_id}"
- ad_creative_images[resource_id] = {
- "data": image_bytes,
- "mime_type": "image/jpeg",
- "name": f"Ad Creative for {ad_id} (Method 1)"
- }
-
- # Return success with resource info
- result["resource_uri"] = resource_uri
- result["success"] = True
- base64_sample = base64.b64encode(image_bytes[:100]).decode("utf-8") + "..."
- result["base64_sample"] = base64_sample
- except Exception as e:
- attempt["error"] = str(e)
-
- # Approach 2: Try directly with the thumbnails endpoint
- attempt = {
- "method": "thumbnails endpoint on creative",
- "success": False
- }
- result["attempts"].append(attempt)
-
- try:
- thumbnails_endpoint = f"{creative_id}/thumbnails"
- thumbnails_params = {}
- thumbnails_data = await make_api_request(thumbnails_endpoint, access_token, thumbnails_params)
- attempt["response"] = thumbnails_data
-
- if "data" in thumbnails_data and len(thumbnails_data["data"]) > 0:
- for thumbnail in thumbnails_data["data"]:
- if "uri" in thumbnail:
- url = thumbnail["uri"]
- attempt["url"] = url
-
- # Try to download the image
- image_bytes = await download_image(url)
- if image_bytes:
- attempt["success"] = True
- attempt["image_size"] = len(image_bytes)
-
- # Save the image if method 1 didn't already succeed
- if "success" not in result or not result["success"]:
- resource_id = f"ad_creative_{ad_id}_method2"
- resource_uri = f"meta-ads://images/{resource_id}"
- ad_creative_images[resource_id] = {
- "data": image_bytes,
- "mime_type": "image/jpeg",
- "name": f"Ad Creative for {ad_id} (Method 2)"
- }
-
- # Return success with resource info
- result["resource_uri"] = resource_uri
- result["success"] = True
- base64_sample = base64.b64encode(image_bytes[:100]).decode("utf-8") + "..."
- result["base64_sample"] = base64_sample
-
- # No need to try more thumbnails if we succeeded
- break
- except Exception as e:
- attempt["error"] = str(e)
-
- # Approach 3: Try using the preview shareable link as an alternate source
- attempt = {
- "method": "preview_shareable_link",
- "success": False
- }
- result["attempts"].append(attempt)
-
- try:
- # Get ad details with preview link
- ad_preview_endpoint = f"{ad_id}"
- ad_preview_params = {
- "fields": "preview_shareable_link"
- }
- ad_preview_data = await make_api_request(ad_preview_endpoint, access_token, ad_preview_params)
-
- if "preview_shareable_link" in ad_preview_data:
- preview_link = ad_preview_data["preview_shareable_link"]
- attempt["preview_link"] = preview_link
-
- # We can't directly download the preview image, but let's note it for manual inspection
- attempt["note"] = "Preview link available for manual inspection in browser"
-
- # This approach doesn't actually download the image, but the link might be useful
- # for debugging purposes or manual verification
- except Exception as e:
- attempt["error"] = str(e)
-
- # Overall result
- if "success" in result and result["success"]:
- result["message"] = "Successfully retrieved ad image through one of the API methods"
- else:
- result["message"] = "Failed to retrieve ad image through any API method"
-
- return json.dumps(result, indent=2)
-
@mcp_server.tool()
@meta_api_tool
async def get_login_link(access_token: str = None) -> str:
diff --git a/meta_ads_mcp/core/__init__.py b/meta_ads_mcp/core/__init__.py
index 6520b71..9b6c081 100644
--- a/meta_ads_mcp/core/__init__.py
+++ b/meta_ads_mcp/core/__init__.py
@@ -5,7 +5,7 @@ from .accounts import get_ad_accounts, get_account_info
from .campaigns import get_campaigns, get_campaign_details, create_campaign
from .adsets import get_adsets, get_adset_details, update_adset
from .ads import get_ads, get_ad_details, get_ad_creatives, get_ad_image
-from .insights import get_insights, debug_image_download, save_ad_image_via_api
+from .insights import get_insights, debug_image_download
from .authentication import get_login_link
from .server import login_cli, main
from .auth import login
@@ -26,7 +26,6 @@ __all__ = [
'get_ad_image',
'get_insights',
'debug_image_download',
- 'save_ad_image_via_api',
'get_login_link',
'login_cli',
'login',