google-ads-mcp-python 0.2.3__tar.gz → 0.2.4__tar.gz
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.
- {google_ads_mcp_python-0.2.3 → google_ads_mcp_python-0.2.4}/PKG-INFO +1 -1
- {google_ads_mcp_python-0.2.3 → google_ads_mcp_python-0.2.4}/pyproject.toml +1 -1
- {google_ads_mcp_python-0.2.3 → google_ads_mcp_python-0.2.4}/src/ads_mcp/tools/core.py +6 -2
- {google_ads_mcp_python-0.2.3 → google_ads_mcp_python-0.2.4}/src/ads_mcp/tools/search.py +13 -4
- {google_ads_mcp_python-0.2.3 → google_ads_mcp_python-0.2.4}/src/ads_mcp/utils.py +47 -3
- {google_ads_mcp_python-0.2.3 → google_ads_mcp_python-0.2.4}/src/ads_mcp/__init__.py +0 -0
- {google_ads_mcp_python-0.2.3 → google_ads_mcp_python-0.2.4}/src/ads_mcp/coordinator.py +0 -0
- {google_ads_mcp_python-0.2.3 → google_ads_mcp_python-0.2.4}/src/ads_mcp/gaql_resources.txt +0 -0
- {google_ads_mcp_python-0.2.3 → google_ads_mcp_python-0.2.4}/src/ads_mcp/mcp_header_interceptor.py +0 -0
- {google_ads_mcp_python-0.2.3 → google_ads_mcp_python-0.2.4}/src/ads_mcp/server.py +0 -0
- {google_ads_mcp_python-0.2.3 → google_ads_mcp_python-0.2.4}/src/ads_mcp/update_references.py +0 -0
|
@@ -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 [
|
|
@@ -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 = []
|
|
@@ -16,7 +16,9 @@
|
|
|
16
16
|
|
|
17
17
|
"""Common utilities used by the MCP server."""
|
|
18
18
|
|
|
19
|
-
|
|
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 = "
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{google_ads_mcp_python-0.2.3 → google_ads_mcp_python-0.2.4}/src/ads_mcp/mcp_header_interceptor.py
RENAMED
|
File without changes
|
|
File without changes
|
{google_ads_mcp_python-0.2.3 → google_ads_mcp_python-0.2.4}/src/ads_mcp/update_references.py
RENAMED
|
File without changes
|