meta-ads-mcp-python 1.0.79__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.
@@ -0,0 +1,74 @@
1
+ """Adds Library-related functionality for Meta Ads API."""
2
+
3
+ import json
4
+ import os
5
+ from typing import Optional, List, Dict, Any
6
+ from .api import meta_api_tool, make_api_request
7
+ from .server import mcp_server
8
+
9
+
10
+ # Only register the search_ads_archive function if the environment variable is NOT set
11
+ DISABLE_ADS_LIBRARY = bool(os.environ.get("META_ADS_DISABLE_ADS_LIBRARY", ""))
12
+
13
+ if not DISABLE_ADS_LIBRARY:
14
+ @mcp_server.tool()
15
+ @meta_api_tool
16
+ async def search_ads_archive(
17
+ search_terms: str,
18
+ ad_reached_countries: List[str],
19
+ access_token: Optional[str] = None,
20
+ ad_type: str = "ALL",
21
+ limit: int = 25, # Default limit, adjust as needed
22
+ fields: str = "ad_creation_time,ad_creative_body,ad_creative_link_caption,ad_creative_link_description,ad_creative_link_title,ad_delivery_start_time,ad_delivery_stop_time,ad_snapshot_url,currency,demographic_distribution,funding_entity,impressions,page_id,page_name,publisher_platform,region_distribution,spend"
23
+ ) -> str:
24
+ """
25
+ Search the Facebook Ads Library archive.
26
+
27
+ Args:
28
+ search_terms: The search query for ads.
29
+ ad_reached_countries: List of country codes (e.g., ["US", "GB"]).
30
+ access_token: Meta API access token (optional - will use cached token if not provided).
31
+ ad_type: Type of ads to search for (e.g., POLITICAL_AND_ISSUE_ADS, HOUSING_ADS, ALL).
32
+ limit: Maximum number of ads to return.
33
+ fields: Comma-separated string of fields to retrieve for each ad.
34
+
35
+ Example Usage via curl equivalent:
36
+ curl -G \\
37
+ -d "search_terms='california'" \\
38
+ -d "ad_type=POLITICAL_AND_ISSUE_ADS" \\
39
+ -d "ad_reached_countries=['US']" \\
40
+ -d "fields=ad_snapshot_url,spend" \\
41
+ -d "access_token=<ACCESS_TOKEN>" \\
42
+ "https://graph.facebook.com/<API_VERSION>/ads_archive"
43
+ """
44
+ if not access_token:
45
+ # Attempt to get token implicitly if not provided - meta_api_tool handles this
46
+ pass
47
+
48
+ if not search_terms:
49
+ return json.dumps({"error": "search_terms parameter is required"}, indent=2)
50
+
51
+ if not ad_reached_countries:
52
+ return json.dumps({"error": "ad_reached_countries parameter is required"}, indent=2)
53
+
54
+ endpoint = "ads_archive"
55
+ params = {
56
+ "search_terms": search_terms,
57
+ "ad_type": ad_type,
58
+ "ad_reached_countries": json.dumps(ad_reached_countries), # API expects a JSON array string
59
+ "limit": limit,
60
+ "fields": fields,
61
+ }
62
+
63
+ try:
64
+ data = await make_api_request(endpoint, access_token, params, method="GET")
65
+ return json.dumps(data, indent=2)
66
+ except Exception as e:
67
+ error_msg = str(e)
68
+ # Consider logging the full error for debugging
69
+ # print(f"Error calling Ads Library API: {error_msg}")
70
+ return json.dumps({
71
+ "error": "Failed to search ads archive",
72
+ "details": error_msg,
73
+ "params_sent": {k: v for k, v in params.items() if k != 'access_token'} # Avoid logging token
74
+ }, indent=2)