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.
Python advertisingai-agentschatgptclaudefacebook-adsinstagram-adsmcpmeta-adsmodel-context-protocolpipeboard
Enhance get_insights function to support custom time ranges
Yves Junqueira5d6e49b5b562
diff --git a/meta_ads_mcp/core/insights.py b/meta_ads_mcp/core/insights.pyindex 8e7cf3c..5bb7e12 100644--- a/meta_ads_mcp/core/insights.py+++ b/meta_ads_mcp/core/insights.py@@ -1,7 +1,7 @@"""Insights and Reporting functionality for Meta Ads API."""import json-from typing import Optional+from typing import Optional, Union, Dictfrom .api import meta_api_tool, make_api_requestfrom .utils import download_image, try_multiple_download_methods, ad_creative_images, create_resource_from_imagefrom .server import mcp_server@@ -12,7 +12,7 @@ import datetime@mcp_server.tool()@meta_api_toolasync def get_insights(access_token: str = None, object_id: str = None,- time_range: str = "maximum", breakdown: str = "",+ time_range: Union[str, Dict[str, str]] = "maximum", breakdown: str = "",level: str = "ad") -> str:"""Get performance insights for a campaign, ad set, ad or account.@@ -20,7 +20,11 @@ async def get_insights(access_token: str = None, object_id: str = None,Args:access_token: Meta API access token (optional - will use cached token if not provided)object_id: ID of the campaign, ad set, ad or account- time_range: Time range for insights (default: last_30_days, options: today, yesterday, this_month, last_month, this_quarter, maximum, data_maximum, last_3d, last_7d, last_14d, last_28d, last_30d, last_90d, last_week_mon_sun, last_week_sun_sat, last_quarter, last_year, this_week_mon_today, this_week_sun_today, this_year)+ time_range: Either a preset time range string or a dictionary with "since" and "until" dates in YYYY-MM-DD format+ Preset options: today, yesterday, this_month, last_month, this_quarter, maximum, data_maximum,+ last_3d, last_7d, last_14d, last_28d, last_30d, last_90d, last_week_mon_sun,+ last_week_sun_sat, last_quarter, last_year, this_week_mon_today, this_week_sun_today, this_year+ Dictionary example: {"since":"2023-01-01","until":"2023-01-31"}breakdown: Optional breakdown dimension (e.g., age, gender, country)level: Level of aggregation (ad, adset, campaign, account)"""@@ -29,11 +33,21 @@ async def get_insights(access_token: str = None, object_id: str = None,endpoint = f"{object_id}/insights"params = {- "date_preset": time_range,"fields": "account_id,account_name,campaign_id,campaign_name,adset_id,adset_name,ad_id,ad_name,impressions,clicks,spend,cpc,cpm,ctr,reach,frequency,actions,conversions,unique_clicks,cost_per_action_type","level": level}+ # Handle time range based on type+ if isinstance(time_range, dict):+ # Use custom date range with since/until parameters+ if "since" in time_range and "until" in time_range:+ params["time_range"] = json.dumps(time_range)+ else:+ return json.dumps({"error": "Custom time_range must contain both 'since' and 'until' keys in YYYY-MM-DD format"}, indent=2)+ else:+ # Use preset date range+ params["date_preset"] = time_range+if breakdown:params["breakdowns"] = breakdown