Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion ads_mcp/tools/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def search(
conditions: List[str] = None,
orderings: List[str] = None,
limit: int | str = None,
login_customer_id: str = None,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The core of this PR. Add support and propagate properly in the project for this parameter.

) -> List[Dict[str, Any]]:
"""Fetches data from the Google Ads API using the search method

Expand All @@ -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}"]

Expand Down Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions ads_mcp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()]
)

Expand Down
2 changes: 1 addition & 1 deletion tests/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a typo..

Once fixed the tests are green:

OK
nox > Session tests-3.13 was successful in 34 seconds.
nox > Ran 4 sessions in 2 minutes:
nox > * tests-3.10: success, took 35 seconds
nox > * tests-3.11: success, took 31 seconds
nox > * tests-3.12: success, took 34 seconds
nox > * tests-3.13: success, took 34 seconds

self.assertEqual(
utils.format_output_value(
CampaignStatusEnum.CampaignStatus.ENABLED
Expand Down