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
feat: accept account_id, campaign_id, adset_id, ad_id as aliases for object_id in get_insights
Yves Junqueira6f59c19a60a5
diff --git a/meta_ads_mcp/core/insights.py b/meta_ads_mcp/core/insights.pyindex 398509f..fb7a741 100644--- a/meta_ads_mcp/core/insights.py+++ b/meta_ads_mcp/core/insights.py@@ -46,16 +46,22 @@ def _strip_redundant_actions(row: dict) -> dict:@mcp_server.tool()@meta_api_tool-async def get_insights(object_id: str, access_token: Optional[str] = None,+async def get_insights(object_id: str = "", access_token: Optional[str] = None,time_range: Union[str, Dict[str, str]] = "maximum", breakdown: str = "",level: str = "ad", limit: int = 25, after: str = "",action_attribution_windows: Optional[List[str]] = None,- compact: bool = False) -> str:+ compact: bool = False,+ account_id: str = "", campaign_id: str = "",+ adset_id: str = "", ad_id: str = "") -> str:"""Get performance insights for a campaign, ad set, ad or account.-+Args:- object_id: ID of the campaign, ad set, ad or account+ object_id: ID of the campaign, ad set, ad or account. You can also use the alias parameters below.+ account_id: Alias for object_id when querying account-level insights+ campaign_id: Alias for object_id when querying campaign-level insights+ adset_id: Alias for object_id when querying ad-set-level insights+ ad_id: Alias for object_id when querying ad-level insightsaccess_token: Meta API access token (optional - will use cached token if not provided)time_range: Either a preset time range string or a dictionary with "since" and "until" dates in YYYY-MM-DD formatPreset options: today, yesterday, this_month, last_month, this_quarter, maximum, data_maximum,@@ -103,8 +109,12 @@ async def get_insights(object_id: str, access_token: Optional[str] = None,bulk_get_insights(level="ad", account_ids=[...], compact=true, fields=["spend", "impressions"])bulk_get_insights supports level="ad", "adset", "campaign", and "account"."""+ # Accept common aliases for object_id (LLMs frequently use these instead)+ if not object_id:+ object_id = account_id or campaign_id or adset_id or ad_id+if not object_id:- return json.dumps({"error": "No object ID provided"}, indent=2)+ return json.dumps({"error": "No object ID provided. Use object_id, account_id, campaign_id, adset_id, or ad_id."}, indent=2)endpoint = f"{object_id}/insights"params = {diff --git a/tests/test_insights_actions_and_values_e2e.py b/tests/test_insights_actions_and_values_e2e.pyindex fd5cb02..819b8bf 100644--- a/tests/test_insights_actions_and_values_e2e.py+++ b/tests/test_insights_actions_and_values_e2e.py@@ -255,24 +255,71 @@ class TestInsightsActionsAndValues:@pytest.mark.asyncioasync def test_actions_without_object_id(self, mock_api_request, mock_auth_manager):"""Test error handling when no object_id is provided"""-+result = await get_insights(time_range="last_30d",level="campaign")-- # Parse the result. The decorator returns a dict error for missing required args++ # Parse the result - the function returns an error when no ID is providedif isinstance(result, dict):result_data = resultelse:result_data = json.loads(result)- assert 'error' in result_data- assert "missing 1 required positional argument: 'object_id'" in result_data['error']-+ # The error may be nested in a "data" key due to the decorator+ if "data" in result_data and isinstance(result_data["data"], str):+ inner = json.loads(result_data["data"])+ assert "error" in inner+ assert "No object ID provided" in inner["error"]+ else:+ assert 'error' in result_data+ assert "No object ID provided" in str(result_data['error'])+# Verify API was not calledmock_api_request.assert_not_called()+ @pytest.mark.asyncio+ async def test_account_id_alias(self, mock_api_request, mock_auth_manager):+ """Test that account_id works as an alias for object_id"""+ result = await get_insights(+ account_id="act_123456",+ time_range="last_30d",+ level="account"+ )++ # Verify the API was called with the account_id as object_id+ mock_api_request.assert_called_once()+ call_args = mock_api_request.call_args+ assert call_args[0][0] == "act_123456/insights"++ @pytest.mark.asyncio+ async def test_campaign_id_alias(self, mock_api_request, mock_auth_manager):+ """Test that campaign_id works as an alias for object_id"""+ result = await get_insights(+ campaign_id="camp_789",+ time_range="last_7d",+ level="campaign"+ )++ mock_api_request.assert_called_once()+ call_args = mock_api_request.call_args+ assert call_args[0][0] == "camp_789/insights"++ @pytest.mark.asyncio+ async def test_object_id_takes_precedence_over_alias(self, mock_api_request, mock_auth_manager):+ """Test that object_id takes precedence when both it and an alias are provided"""+ result = await get_insights(+ object_id="obj_real",+ account_id="act_alias",+ time_range="last_7d",+ level="account"+ )++ mock_api_request.assert_called_once()+ call_args = mock_api_request.call_args+ assert call_args[0][0] == "obj_real/insights"+@pytest.mark.asyncioasync def test_actions_with_invalid_time_range(self, mock_api_request, mock_auth_manager, valid_campaign_id):"""Test error handling with invalid time range"""