google-ads-mcp-python 0.2.3__py3-none-any.whl → 0.2.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ads_mcp/tools/core.py CHANGED
@@ -34,11 +34,15 @@ from google.ads.googleads.v21.services.types.customer_service import (
34
34
  openWorldHint=True,
35
35
  )
36
36
  )
37
- def list_accessible_customers() -> List[str]:
37
+ async def list_accessible_customers() -> List[str]:
38
38
  """Returns ids of customers directly accessible by the user authenticating the call."""
39
+ return await utils.call_api(_list_accessible_customers)
40
+
41
+
42
+ def _list_accessible_customers() -> List[str]:
39
43
  ga_service = utils.get_googleads_service("CustomerService")
40
44
  accessible_customers: ListAccessibleCustomersResponse = (
41
- ga_service.list_accessible_customers()
45
+ ga_service.list_accessible_customers(timeout=utils.REQUEST_TIMEOUT)
42
46
  )
43
47
  # remove customer/ from the start of each resource
44
48
  return [
ads_mcp/tools/search.py CHANGED
@@ -22,7 +22,7 @@ from ads_mcp.coordinator import mcp
22
22
  import ads_mcp.utils as utils
23
23
 
24
24
 
25
- def search(
25
+ async def search(
26
26
  customer_id: str,
27
27
  fields: List[str],
28
28
  resource: str,
@@ -42,8 +42,6 @@ def search(
42
42
 
43
43
  """
44
44
 
45
- ga_service = utils.get_googleads_service("GoogleAdsService")
46
-
47
45
  query_parts = [f"SELECT {','.join(fields)} FROM {resource}"]
48
46
 
49
47
  if conditions:
@@ -58,8 +56,19 @@ def search(
58
56
  query = "".join(query_parts)
59
57
  utils.logger.info(f"ads_mcp.search query {query}")
60
58
 
59
+ try:
60
+ return await utils.call_api(_run_query, customer_id, query)
61
+ except TimeoutError as e:
62
+ raise TimeoutError(
63
+ f"{e} Narrow the date range or lower the LIMIT and try again."
64
+ ) from e
65
+
66
+
67
+ def _run_query(customer_id: str, query: str) -> List[Dict[str, Any]]:
68
+ """Runs the query on a worker thread so a slow call can't block the server."""
69
+ ga_service = utils.get_googleads_service("GoogleAdsService")
61
70
  query_result = ga_service.search_stream(
62
- customer_id=customer_id, query=query
71
+ customer_id=customer_id, query=query, timeout=utils.REQUEST_TIMEOUT
63
72
  )
64
73
 
65
74
  final_output: List = []
ads_mcp/utils.py CHANGED
@@ -16,7 +16,9 @@
16
16
 
17
17
  """Common utilities used by the MCP server."""
18
18
 
19
- from typing import Any
19
+ import math
20
+ from typing import Any, Callable, TypeVar
21
+ import anyio
20
22
  import proto
21
23
  import logging
22
24
  import json
@@ -27,11 +29,40 @@ from google.ads.googleads.v21.services.services.google_ads_service import (
27
29
 
28
30
  from google.ads.googleads.util import get_nested_attr
29
31
  import google.auth
32
+ from google.api_core.exceptions import DeadlineExceeded
30
33
  from google.oauth2 import service_account
31
34
  from ads_mcp.mcp_header_interceptor import MCPHeaderInterceptor
32
35
  import os
36
+ from pathlib import Path
33
37
 
34
- GAQL_FILEPATH = "ads_mcp/gaql_resources.txt"
38
+ GAQL_FILEPATH = Path(__file__).parent / "gaql_resources.txt"
39
+
40
+
41
+ def _request_timeout() -> float:
42
+ """Returns the seconds a Google Ads API call may take before it fails.
43
+
44
+ Without a deadline a call Google never answers holds the server forever.
45
+ """
46
+ raw = os.environ.get("GOOGLE_ADS_REQUEST_TIMEOUT", "120")
47
+ try:
48
+ seconds = float(raw)
49
+ except ValueError:
50
+ seconds = math.nan
51
+ if not math.isfinite(seconds) or seconds <= 0:
52
+ raise ValueError(
53
+ f"GOOGLE_ADS_REQUEST_TIMEOUT must be a positive number of "
54
+ f"seconds, got {raw!r}."
55
+ )
56
+ return seconds
57
+
58
+
59
+ REQUEST_TIMEOUT = _request_timeout()
60
+
61
+ # Google Ads calls get their own threads so the stdio transport, which also
62
+ # reads and writes on anyio's worker threads, always has one free.
63
+ _API_THREADS = anyio.CapacityLimiter(10)
64
+
65
+ T = TypeVar("T")
35
66
 
36
67
  logger = logging.getLogger(__name__)
37
68
  logging.basicConfig(level=logging.INFO)
@@ -137,4 +168,17 @@ def format_output_row(row: proto.Message, attributes):
137
168
  return {
138
169
  attr: format_output_value(get_nested_attr(row, attr))
139
170
  for attr in attributes
140
- }
171
+ }
172
+
173
+
174
+ async def call_api(fn: Callable[..., T], *args: Any) -> T:
175
+ """Runs a blocking Google Ads call on a worker thread.
176
+
177
+ Raises TimeoutError when Google does not answer within REQUEST_TIMEOUT.
178
+ """
179
+ try:
180
+ return await anyio.to_thread.run_sync(fn, *args, limiter=_API_THREADS)
181
+ except DeadlineExceeded as e:
182
+ raise TimeoutError(
183
+ f"Google Ads did not answer within {REQUEST_TIMEOUT:g}s."
184
+ ) from e
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: google-ads-mcp-python
3
- Version: 0.2.3
3
+ Version: 0.2.4
4
4
  License-Expression: Apache-2.0
5
5
  Requires-Dist: google-ads>=28.0.0
6
6
  Requires-Dist: mcp[cli]>=1.2.0,<2
@@ -3,11 +3,11 @@ ads_mcp/coordinator.py,sha256=VzaeAmgx7FBzSUUWeSsPSX45NncI31KHU42AgU6iiuo,876
3
3
  ads_mcp/gaql_resources.txt,sha256=nWdM4_1aYOnozqVj2uf6Mi6V31-1oCpcYk3DxnZk4sI,394275
4
4
  ads_mcp/mcp_header_interceptor.py,sha256=nt9EycVVpT3f7emGUYWAEFeFWgGPtD8gJbe3FHa_n6I,3751
5
5
  ads_mcp/server.py,sha256=xpD0Y6lZ2rGfnqXYSpiAYteELgh_yfyzrGkFTty-VJc,1019
6
- ads_mcp/tools/core.py,sha256=Pfda0IUjLfE7Lv8JArPi_tBj8UypQoCT6gFCY4l7jOI,1520
7
- ads_mcp/tools/search.py,sha256=KwTD3yTaAG6MGAyis4qXt_2WH-51RYxBkJyL7Qm2BpU,4600
6
+ ads_mcp/tools/core.py,sha256=8Rdwb4Y1rQG6MXb_NzGCuhKT0IAPAFopcicrgmfbSTM,1664
7
+ ads_mcp/tools/search.py,sha256=e00vbm_YNel6FLTkCHsbMjm8vLlTDWbHNNlKKhkKgmg,5018
8
8
  ads_mcp/update_references.py,sha256=78iw2cNf3XEY9XqyRCx3D-19MwjWinO_-anU0Yd9ByE,3481
9
- ads_mcp/utils.py,sha256=kJNqUpRLSh_SGXVZTUJkZm9dWppGKzjyW_4fKhPVGag,4967
10
- google_ads_mcp_python-0.2.3.dist-info/WHEEL,sha256=eh7sammvW2TypMMMGKgsM83HyA_3qQ5Lgg3ynoecH3M,79
11
- google_ads_mcp_python-0.2.3.dist-info/entry_points.txt,sha256=D2VEZxru6DR0dZ-sy5Ra0Z6knbbbQnrnS8uSMauoe08,143
12
- google_ads_mcp_python-0.2.3.dist-info/METADATA,sha256=cGKszVLtphhIyDtUb4T7HDiWSUWI36dqn6QaETSx3wM,424
13
- google_ads_mcp_python-0.2.3.dist-info/RECORD,,
9
+ ads_mcp/utils.py,sha256=1SaHxhjQkr7Fdbt1A6o46N2QYqxggpQLyKIGm7X4M4A,6354
10
+ google_ads_mcp_python-0.2.4.dist-info/WHEEL,sha256=eh7sammvW2TypMMMGKgsM83HyA_3qQ5Lgg3ynoecH3M,79
11
+ google_ads_mcp_python-0.2.4.dist-info/entry_points.txt,sha256=D2VEZxru6DR0dZ-sy5Ra0Z6knbbbQnrnS8uSMauoe08,143
12
+ google_ads_mcp_python-0.2.4.dist-info/METADATA,sha256=jN6G7SrCdCDu-E9sUhuP0cqrrmTORXbStKBUifjBLOA,424
13
+ google_ads_mcp_python-0.2.4.dist-info/RECORD,,