diff --git a/ads_mcp/tools/search.py b/ads_mcp/tools/search.py index a0b8ce1..83b2b85 100644 --- a/ads_mcp/tools/search.py +++ b/ads_mcp/tools/search.py @@ -26,6 +26,7 @@ def search( conditions: List[str] = None, orderings: List[str] = None, limit: int | str = None, + login_customer_id: str = None, ) -> List[Dict[str, Any]]: """Fetches data from the Google Ads API using the search method @@ -36,10 +37,13 @@ def search( conditions: List of conditions to filter the data, combined using AND clauses orderings: How the data is ordered limit: The maximum number of rows to return + login_customer_id: The id of the login customer (Manager Account) if accessing a client account. """ - ga_service = utils.get_googleads_service("GoogleAdsService") + ga_service = utils.get_googleads_service( + "GoogleAdsService", login_customer_id=login_customer_id + ) query_parts = [f"SELECT {','.join(fields)} FROM {resource}"] @@ -95,6 +99,10 @@ def _search_tool_description() -> str: should be a string of numbers without punctuation if presented in the form 123-456-7890 remove the hyphens and use 1234567890 +### Hint for login_customer_id + Required when accessing a client account through a Manager Account. + Should be the Manager Account ID (without punctuation). + ### Hints for Dates All dates should be in the form YYYY-MM-DD and must include the dashes (-) Date literals from the Grammar must NEVER be used diff --git a/ads_mcp/utils.py b/ads_mcp/utils.py index 413d165..b52fc15 100644 --- a/ads_mcp/utils.py +++ b/ads_mcp/utils.py @@ -61,13 +61,16 @@ def _get_login_customer_id() -> str: return os.environ.get("GOOGLE_ADS_LOGIN_CUSTOMER_ID") -def _get_googleads_client() -> GoogleAdsClient: +def _get_googleads_client(login_customer_id: str = None) -> GoogleAdsClient: # Use this line if you have a google-ads.yaml file # client = GoogleAdsClient.load_from_storage() + if not login_customer_id: + login_customer_id = _get_login_customer_id() + client = GoogleAdsClient( credentials=_create_credentials(), developer_token=_get_developer_token(), - login_customer_id=_get_login_customer_id(), + login_customer_id=login_customer_id, ) return client @@ -76,8 +79,14 @@ def _get_googleads_client() -> GoogleAdsClient: _googleads_client = _get_googleads_client() -def get_googleads_service(serviceName: str) -> GoogleAdsServiceClient: - return _googleads_client.get_service( +def get_googleads_service( + serviceName: str, login_customer_id: str = None +) -> GoogleAdsServiceClient: + client = _googleads_client + if login_customer_id: + client = _get_googleads_client(login_customer_id) + + return client.get_service( serviceName, interceptors=[MCPHeaderInterceptor()] ) diff --git a/tests/utils_test.py b/tests/utils_test.py index 6b81be5..5bba4a8 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -28,7 +28,7 @@ class TestUtils(unittest.TestCase): def test_format_output_value(self): """Tests that output values are formatted correctly.""" - client = utils.get_googleads_client() + client = utils._get_googleads_client() self.assertEqual( utils.format_output_value( CampaignStatusEnum.CampaignStatus.ENABLED