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.

fix: validate bid_amount requirement from parent campaign before creating adset

Yves Junqueira4062eb5575ef
diff --git a/meta_ads_mcp/core/adsets.py b/meta_ads_mcp/core/adsets.py
index ad6bcea..0a2aa8a 100644
--- a/meta_ads_mcp/core/adsets.py
+++ b/meta_ads_mcp/core/adsets.py
@@ -231,6 +231,14 @@ async def create_adset(
"targeting_automation": {"advantage_audience": 1}
}
+ # Bid strategies that require bid_amount
+ strategies_requiring_bid_amount = [
+ 'LOWEST_COST_WITH_BID_CAP',
+ 'COST_CAP',
+ 'LOWEST_COST_WITH_MIN_ROAS',
+ 'TARGET_COST',
+ ]
+
# Validate bid_strategy and bid_amount requirements
if bid_strategy:
# Check for invalid 'LOWEST_COST' value (common mistake)
@@ -247,14 +255,7 @@ async def create_adset(
],
"example": '{"bid_strategy": "LOWEST_COST_WITHOUT_CAP"}'
}, indent=2)
-
- # Bid strategies that require bid_amount
- strategies_requiring_bid_amount = [
- 'LOWEST_COST_WITH_BID_CAP',
- 'COST_CAP',
- 'LOWEST_COST_WITH_MIN_ROAS'
- ]
-
+
if bid_strategy in strategies_requiring_bid_amount and bid_amount is None:
return json.dumps({
"error": f"bid_amount is required when using bid_strategy '{bid_strategy}'",
@@ -263,7 +264,28 @@ async def create_adset(
"example_with_bid_amount": f'{{"bid_strategy": "{bid_strategy}", "bid_amount": 500}}',
"example_without_bid_amount": '{"bid_strategy": "LOWEST_COST_WITHOUT_CAP"}'
}, indent=2)
-
+
+ # Pre-flight check: if no bid_amount provided, check whether the parent campaign's
+ # bid_strategy requires one. This prevents a confusing error from Meta's API when
+ # the campaign-level bid strategy forces child ad sets to provide bid_amount.
+ if bid_amount is None:
+ try:
+ campaign_data = await make_api_request(
+ campaign_id, access_token, {"fields": "bid_strategy,name"}
+ )
+ campaign_bid_strategy = campaign_data.get("bid_strategy")
+ if campaign_bid_strategy and campaign_bid_strategy in strategies_requiring_bid_amount:
+ campaign_name = campaign_data.get("name", campaign_id)
+ return json.dumps({
+ "error": f"bid_amount is required because the parent campaign uses bid_strategy '{campaign_bid_strategy}'",
+ "details": f"Campaign '{campaign_name}' ({campaign_id}) uses '{campaign_bid_strategy}', which requires all child ad sets to provide a bid_amount (in cents).",
+ "workaround": "Either provide the bid_amount parameter, or change the campaign's bid_strategy to 'LOWEST_COST_WITHOUT_CAP'",
+ "example_with_bid_amount": f'{{"bid_amount": 500}} (= $5.00 bid cap)',
+ "example_without_bid_amount": 'Change campaign bid strategy: update_campaign(campaign_id="' + campaign_id + '", bid_strategy="LOWEST_COST_WITHOUT_CAP")'
+ }, indent=2)
+ except Exception:
+ pass # If the pre-flight check fails, let the create request proceed normally
+
endpoint = f"{account_id}/adsets"
params = {
@@ -403,9 +425,10 @@ async def update_adset(adset_id: str, frequency_control_specs: Optional[List[Dic
strategies_requiring_bid_amount = [
'LOWEST_COST_WITH_BID_CAP',
'COST_CAP',
- 'LOWEST_COST_WITH_MIN_ROAS'
+ 'LOWEST_COST_WITH_MIN_ROAS',
+ 'TARGET_COST',
]
-
+
if bid_strategy in strategies_requiring_bid_amount and bid_amount is None:
return json.dumps({
"error": f"bid_amount is required when using bid_strategy '{bid_strategy}'",
@@ -414,7 +437,7 @@ async def update_adset(adset_id: str, frequency_control_specs: Optional[List[Dic
"example_with_bid_amount": f'{{"bid_strategy": "{bid_strategy}", "bid_amount": 500}}',
"example_without_bid_amount": '{"bid_strategy": "LOWEST_COST_WITHOUT_CAP"}'
}, indent=2)
-
+
params = {}
if frequency_control_specs is not None:
diff --git a/tests/test_bid_strategy_validation.py b/tests/test_bid_strategy_validation.py
index 0365188..b908dfd 100644
--- a/tests/test_bid_strategy_validation.py
+++ b/tests/test_bid_strategy_validation.py
@@ -226,9 +226,9 @@ class TestBidStrategyValidation:
# Should succeed
assert "error" not in result_data
assert result_data["id"] == "test_adset_id"
-
- # API should have been called
- mock_api_request.assert_called_once()
+
+ # API should have been called (pre-flight campaign check + actual create)
+ assert mock_api_request.call_count >= 1
call_args = mock_api_request.call_args
params = call_args[0][2]
assert params["bid_strategy"] == "LOWEST_COST_WITHOUT_CAP"
@@ -317,9 +317,9 @@ class TestBidStrategyValidation:
# Should succeed - let Meta API handle defaults
assert "error" not in result_data
-
- # API should have been called
- mock_api_request.assert_called_once()
+
+ # API should have been called (pre-flight campaign check + actual create)
+ assert mock_api_request.call_count >= 1
@pytest.mark.asyncio
async def test_update_adset_lowest_cost_without_cap_no_bid_amount_needed(
diff --git a/tests/test_dsa_beneficiary.py b/tests/test_dsa_beneficiary.py
index 071a3c9..19ba223 100644
--- a/tests/test_dsa_beneficiary.py
+++ b/tests/test_dsa_beneficiary.py
@@ -188,8 +188,8 @@ class TestDSABeneficiaryParameter:
dsa_beneficiary="Test Organization GmbH"
)
- # Verify the API was called with DSA beneficiary parameter
- mock_api.assert_called_once()
+ # Verify the API was called with DSA beneficiary parameter (pre-flight campaign check + actual create)
+ assert mock_api.call_count >= 1
call_args = mock_api.call_args
assert "dsa_beneficiary" in str(call_args)
@@ -279,10 +279,10 @@ class TestDSABeneficiaryParameter:
dsa_beneficiary="Test Organization GmbH"
)
- # Verify the API was called
- mock_api.assert_called_once()
+ # Verify the API was called (pre-flight campaign check + actual create)
+ assert mock_api.call_count >= 1
call_args = mock_api.call_args
-
+
# Verify DSA beneficiary is sent as separate parameter, not in targeting
call_str = str(call_args)
assert "dsa_beneficiary" in call_str
@@ -313,8 +313,8 @@ class TestDSABeneficiaryParameter:
dsa_beneficiary=beneficiary_name
)
- # Verify the API was called with the beneficiary name
- mock_api.assert_called_once()
+ # Verify the API was called with the beneficiary name (pre-flight campaign check + actual create)
+ assert mock_api.call_count >= 1
call_args = mock_api.call_args
assert beneficiary_name in str(call_args)
diff --git a/tests/test_mobile_app_adset_creation.py b/tests/test_mobile_app_adset_creation.py
index efbc5d2..839e3f5 100644
--- a/tests/test_mobile_app_adset_creation.py
+++ b/tests/test_mobile_app_adset_creation.py
@@ -123,10 +123,10 @@ class TestMobileAppAdsetCreation:
# Parse the result
result_data = json.loads(result)
- # Verify the API was called with correct parameters
- mock_api_request.assert_called_once()
+ # Verify the API was called (pre-flight campaign check + actual create)
+ assert mock_api_request.call_count >= 1
call_args = mock_api_request.call_args
-
+
# Check endpoint (first argument)
assert call_args[0][0] == f"{valid_mobile_app_params['account_id']}/adsets"
@@ -162,11 +162,11 @@ class TestMobileAppAdsetCreation:
# Parse the result
result_data = json.loads(result)
- # Verify the API was called
- mock_api_request.assert_called_once()
+ # Verify the API was called (pre-flight campaign check + actual create)
+ assert mock_api_request.call_count >= 1
call_args = mock_api_request.call_args
params = call_args[0][2]
-
+
# Verify Android-specific promoted_object
promoted_obj_param = json.loads(params['promoted_object']) if isinstance(params['promoted_object'], str) else params['promoted_object']
assert promoted_obj_param['application_id'] == android_promoted_object['application_id']
@@ -517,8 +517,8 @@ class TestMobileAppAdsetCreation:
result = await create_adset(**non_mobile_params)
- # Should work without mobile app parameters
- mock_api_request.assert_called_once()
+ # Should work without mobile app parameters (pre-flight campaign check + actual create)
+ assert mock_api_request.call_count >= 1
call_args = mock_api_request.call_args
params = call_args[0][2]
@@ -542,8 +542,8 @@ class TestMobileAppAdsetCreation:
destination_type="APP_STORE"
)
- # Should work and include mobile parameters if provided
- mock_api_request.assert_called_once()
+ # Should work and include mobile parameters if provided (pre-flight campaign check + actual create)
+ assert mock_api_request.call_count >= 1
call_args = mock_api_request.call_args
params = call_args[0][2]