Skip to content

Commit 5eee32e

Browse files
committed
fix: normalize page_id to string in create_ad_creative and discovery functions
Updated the handling of page_id in create_ad_creative to ensure it is coerced to a string, accommodating both string and integer inputs. Adjusted the _discover_pages_for_account function to return page_id as a string, enhancing consistency in API responses. This change improves compatibility with the Meta API, which may return page IDs as integers.
1 parent 1aebb94 commit 5eee32e

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

‎meta_ads_mcp/core/ads.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ async def create_ad_creative(
770770
image_hash: Hash of the uploaded image
771771
access_token: Meta API access token (optional - will use cached token if not provided)
772772
name: Creative name
773-
page_id: Facebook Page ID to be used for the ad
773+
page_id: Facebook Page ID (string or int; coerced to string)
774774
link_url: Destination URL for the ad
775775
message: Ad copy/text
776776
headline: Single headline for simple ads (cannot be used with headlines)
@@ -800,6 +800,10 @@ async def create_ad_creative(
800800
if not account_id.startswith("act_"):
801801
account_id = f"act_{account_id}"
802802

803+
# Normalize page_id to string (Meta API and get_account_pages often return id as int in JSON)
804+
if page_id is not None:
805+
page_id = str(page_id).strip() or None
806+
803807
# Enhanced page discovery: If no page ID is provided, use robust discovery methods
804808
if not page_id:
805809
try:
@@ -1175,7 +1179,7 @@ async def _discover_pages_for_account(account_id: str, access_token: str) -> dic
11751179
page = client_pages_data["data"][0]
11761180
return {
11771181
"success": True,
1178-
"page_id": page["id"],
1182+
"page_id": str(page["id"]),
11791183
"page_name": page.get("name", "Unknown"),
11801184
"source": "client_pages"
11811185
}
@@ -1193,7 +1197,7 @@ async def _discover_pages_for_account(account_id: str, access_token: str) -> dic
11931197
page = pages_data["data"][0]
11941198
return {
11951199
"success": True,
1196-
"page_id": page["id"],
1200+
"page_id": str(page["id"]),
11971201
"page_name": page.get("name", "Unknown"),
11981202
"source": "assigned_pages"
11991203
}

0 commit comments

Comments
 (0)