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
fix(targeting): accept custom_locations/places/electoral_districts in estimate_audience_size preflight check (#147)
Yves Junqueiradb8b9b8de5d6
diff --git a/meta_ads_mcp/core/targeting.py b/meta_ads_mcp/core/targeting.pyindex 344fffc..e4307d1 100644--- a/meta_ads_mcp/core/targeting.py+++ b/meta_ads_mcp/core/targeting.py@@ -164,7 +164,10 @@ async def estimate_audience_size("cities","zips","geo_markets",- "country_groups"+ "country_groups",+ "custom_locations",+ "places",+ "electoral_districts",]:val = geo.get(key)if isinstance(val, list) and len(val) > 0:@@ -187,7 +190,7 @@ async def estimate_audience_size(return json.dumps({"error": "Missing target audience location","details": "Select at least one location in targeting.geo_locations or include a custom audience.",- "action_required": "Add geo_locations with countries/regions/cities/zips or include custom_audiences.",+ "action_required": "Add geo_locations with countries/regions/cities/zips/geo_markets/custom_locations/places/electoral_districts, or include custom_audiences.","example": {"geo_locations": {"countries": ["US"]},"age_min": 25,@@ -224,7 +227,7 @@ async def estimate_audience_size("error": "Missing target audience location","details": raw_err.get("error_user_msg") or "Select at least one location, or choose a custom audience.","endpoint_used": f"{account_id}/reachestimate",- "action_required": "Add geo_locations with at least one of countries/regions/cities/zips or include custom_audiences.",+ "action_required": "Add geo_locations with at least one of countries/regions/cities/zips/geo_markets/custom_locations/places/electoral_districts or include custom_audiences.","blame_field_specs": raw_err.get("error_data", {}).get("blame_field_specs") if isinstance(raw_err.get("error_data"), dict) else None}, indent=2)except Exception:diff --git a/tests/test_estimate_audience_size.py b/tests/test_estimate_audience_size.pyindex ce38c33..1897337 100644--- a/tests/test_estimate_audience_size.py+++ b/tests/test_estimate_audience_size.py@@ -275,6 +275,153 @@ class TestEstimateAudienceSize:assert "targeting specification is required" in nested_data["error"]assert "example" in nested_data+ @pytest.mark.asyncio+ async def test_missing_location_rejected_by_preflight(self):+ """Targeting with no location keys and no custom audience should be rejected before+ ever reaching the reachestimate endpoint."""+ targeting_spec = {+ "age_min": 22,+ "age_max": 55+ }++ with patch('meta_ads_mcp.core.targeting.make_api_request', new_callable=AsyncMock) as mock_api:+ result = await estimate_audience_size(+ access_token="test_token",+ account_id="act_123456789",+ targeting=targeting_spec+ )++ # Preflight check should short-circuit before calling the Graph API+ mock_api.assert_not_called()++ result_data = json.loads(result)+ assert "data" in result_data+ nested_data = json.loads(result_data["data"])+ assert nested_data["error"] == "Missing target audience location"+ assert "action_required" in nested_data++ @pytest.mark.asyncio+ async def test_custom_locations_only_passes_preflight(self):+ """Regression test: a targeting spec whose only location signal is+ geo_locations.custom_locations (radius targeting) must pass Pipeboard's preflight+ check and reach the reachestimate endpoint. Previously this was incorrectly+ rejected with "Missing target audience location" (custom_locations was missing+ from the recognized geo_locations keys)."""+ mock_response = {+ "data": [+ {+ "estimate_mau": 42000,+ "estimate_dau": [],+ "bid_estimates": {},+ "unsupported_targeting": []+ }+ ]+ }++ targeting_spec = {+ "age_min": 22,+ "age_max": 55,+ "geo_locations": {+ "custom_locations": [+ {"latitude": 40.759, "longitude": -73.9895, "radius": 1.5, "distance_unit": "mile"}+ ],+ "location_types": ["home"]+ },+ "targeting_automation": {"advantage_audience": 0}+ }++ with patch('meta_ads_mcp.core.targeting.make_api_request', new_callable=AsyncMock) as mock_api:+ mock_api.return_value = mock_response++ result = await estimate_audience_size(+ access_token="test_token",+ account_id="act_123456789",+ targeting=targeting_spec+ )++ mock_api.assert_called_once_with(+ "act_123456789/reachestimate",+ "test_token",+ {"targeting_spec": targeting_spec},+ method="GET"+ )++ result_data = json.loads(result)+ assert result_data["success"] is True+ assert result_data["estimated_audience_size"] == 42000++ @pytest.mark.asyncio+ async def test_places_only_passes_preflight(self):+ """A targeting spec using only geo_locations.places (e.g. targeting around a named+ place/landmark) should also pass the preflight location check."""+ mock_response = {+ "data": [+ {+ "estimate_mau": 15000,+ "estimate_dau": [],+ "bid_estimates": {},+ "unsupported_targeting": []+ }+ ]+ }++ targeting_spec = {+ "age_min": 18,+ "age_max": 65,+ "geo_locations": {+ "places": [{"key": 129672430416115, "name": "SFO", "radius": 10, "distance_unit": "mile"}]+ }+ }++ with patch('meta_ads_mcp.core.targeting.make_api_request', new_callable=AsyncMock) as mock_api:+ mock_api.return_value = mock_response++ result = await estimate_audience_size(+ access_token="test_token",+ account_id="act_123456789",+ targeting=targeting_spec+ )++ mock_api.assert_called_once()+ result_data = json.loads(result)+ assert result_data["success"] is True++ @pytest.mark.asyncio+ async def test_electoral_districts_only_passes_preflight(self):+ """A targeting spec using only geo_locations.electoral_districts should also pass+ the preflight location check."""+ mock_response = {+ "data": [+ {+ "estimate_mau": 8000,+ "estimate_dau": [],+ "bid_estimates": {},+ "unsupported_targeting": []+ }+ ]+ }++ targeting_spec = {+ "age_min": 18,+ "age_max": 65,+ "geo_locations": {+ "electoral_districts": [{"key": "US:CA14"}]+ }+ }++ with patch('meta_ads_mcp.core.targeting.make_api_request', new_callable=AsyncMock) as mock_api:+ mock_api.return_value = mock_response++ result = await estimate_audience_size(+ access_token="test_token",+ account_id="act_123456789",+ targeting=targeting_spec+ )++ mock_api.assert_called_once()+ result_data = json.loads(result)+ assert result_data["success"] is True+@pytest.mark.asyncioasync def test_error_no_parameters(self):"""Test error when no parameters provided"""