mcp-ga4 2.0.2 → 2.0.4
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.
- package/README.md +1 -8
- package/dist/build-info.json +1 -1
- package/dist/index.js +25 -1
- package/dist/resilience.js +42 -15
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ npm install mcp-ga4
|
|
|
19
19
|
Or clone the repository:
|
|
20
20
|
|
|
21
21
|
```bash
|
|
22
|
-
git clone https://github.com/
|
|
22
|
+
git clone https://github.com/mharnett/mcp-ga4.git
|
|
23
23
|
cd mcp-ga4
|
|
24
24
|
npm install
|
|
25
25
|
npm run build
|
|
@@ -36,13 +36,6 @@ GA4_PROPERTY_ID=123456789
|
|
|
36
36
|
GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
-
For OAuth credentials instead of a service account:
|
|
40
|
-
|
|
41
|
-
```bash
|
|
42
|
-
GA4_PROPERTY_ID=123456789
|
|
43
|
-
GA4_CREDENTIALS_FILE=/path/to/oauth-credentials.json
|
|
44
|
-
```
|
|
45
|
-
|
|
46
39
|
### Mode 2: Multi-Client (config.json)
|
|
47
40
|
|
|
48
41
|
Create a `config.json` in the project root to map multiple GA4 properties to project directories. The server auto-detects which property to use based on the caller's working directory.
|
package/dist/build-info.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"sha":"
|
|
1
|
+
{"sha":"9b66c73","builtAt":"2026-04-09T21:19:01.910Z"}
|
package/dist/index.js
CHANGED
|
@@ -20,6 +20,22 @@ try {
|
|
|
20
20
|
catch {
|
|
21
21
|
// dev mode
|
|
22
22
|
}
|
|
23
|
+
// CLI flags
|
|
24
|
+
const __cliPkg = JSON.parse(readFileSync(join(dirname(new URL(import.meta.url).pathname), "..", "package.json"), "utf-8"));
|
|
25
|
+
if (process.argv.includes("--help") || process.argv.includes("-h")) {
|
|
26
|
+
console.log(`${__cliPkg.name} v${__cliPkg.version}\n`);
|
|
27
|
+
console.log(`Usage: ${__cliPkg.name} [options]\n`);
|
|
28
|
+
console.log("MCP server communicating via stdio. Configure in your .mcp.json.\n");
|
|
29
|
+
console.log("Options:");
|
|
30
|
+
console.log(" --help, -h Show this help message");
|
|
31
|
+
console.log(" --version, -v Show version number");
|
|
32
|
+
console.log(`\nDocumentation: https://github.com/mharnett/mcp-ga4`);
|
|
33
|
+
process.exit(0);
|
|
34
|
+
}
|
|
35
|
+
if (process.argv.includes("--version") || process.argv.includes("-v")) {
|
|
36
|
+
console.log(__cliPkg.version);
|
|
37
|
+
process.exit(0);
|
|
38
|
+
}
|
|
23
39
|
function loadConfig() {
|
|
24
40
|
// Single-property mode via env vars
|
|
25
41
|
const propertyId = process.env.GA4_PROPERTY_ID;
|
|
@@ -298,7 +314,7 @@ Metrics: sessions, totalUsers, newUsers, activeUsers, screenPageViews,
|
|
|
298
314
|
|
|
299
315
|
## Date Formats
|
|
300
316
|
Use YYYY-MM-DD or relative: "today", "yesterday", "7daysAgo", "30daysAgo", "90daysAgo"`;
|
|
301
|
-
const server = new Server({ name:
|
|
317
|
+
const server = new Server({ name: __cliPkg.name, version: __cliPkg.version }, { capabilities: { tools: {} } });
|
|
302
318
|
server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools }));
|
|
303
319
|
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
304
320
|
const { name, arguments: args } = request.params;
|
|
@@ -427,4 +443,12 @@ async function main() {
|
|
|
427
443
|
await server.connect(transport);
|
|
428
444
|
console.error("[startup] MCP GA4 server running");
|
|
429
445
|
}
|
|
446
|
+
process.on("SIGTERM", () => {
|
|
447
|
+
console.error("[shutdown] SIGTERM received, exiting");
|
|
448
|
+
process.exit(0);
|
|
449
|
+
});
|
|
450
|
+
process.on("SIGINT", () => {
|
|
451
|
+
console.error("[shutdown] SIGINT received, exiting");
|
|
452
|
+
process.exit(0);
|
|
453
|
+
});
|
|
430
454
|
main().catch(console.error);
|
package/dist/resilience.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { retry, circuitBreaker, wrap,
|
|
1
|
+
import { retry, circuitBreaker, wrap, handleWhen, timeout, TimeoutStrategy, ExponentialBackoff, ConsecutiveBreaker, } from "cockatiel";
|
|
2
2
|
import pino from "pino";
|
|
3
3
|
export const logger = pino({
|
|
4
4
|
level: process.env.LOG_LEVEL || "info",
|
|
@@ -15,28 +15,55 @@ export const logger = pino({
|
|
|
15
15
|
});
|
|
16
16
|
const MAX_RESPONSE_SIZE = 200_000;
|
|
17
17
|
export function safeResponse(data, context) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (
|
|
23
|
-
return
|
|
18
|
+
let current = data;
|
|
19
|
+
for (let pass = 0; pass < 10; pass++) {
|
|
20
|
+
const jsonStr = JSON.stringify(current);
|
|
21
|
+
const sizeBytes = Buffer.byteLength(jsonStr, "utf-8");
|
|
22
|
+
if (sizeBytes <= MAX_RESPONSE_SIZE)
|
|
23
|
+
return current;
|
|
24
|
+
logger.warn({ sizeBytes, maxSize: MAX_RESPONSE_SIZE, context, pass }, "Response exceeds size limit, truncating");
|
|
25
|
+
if (Array.isArray(current)) {
|
|
26
|
+
current = current.slice(0, Math.max(1, Math.floor(current.length * 0.5)));
|
|
27
|
+
continue;
|
|
24
28
|
}
|
|
25
|
-
if (typeof
|
|
26
|
-
const obj =
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
if (typeof current === "object" && current !== null) {
|
|
30
|
+
const obj = current;
|
|
31
|
+
let truncated = false;
|
|
32
|
+
for (const key of ["items", "results", "data", "rows", "tags", "triggers", "variables"]) {
|
|
33
|
+
if (Array.isArray(obj[key]) && obj[key].length > 1) {
|
|
29
34
|
obj[key] = obj[key].slice(0, Math.max(1, Math.floor(obj[key].length * 0.5)));
|
|
30
|
-
|
|
35
|
+
if ("count" in obj)
|
|
36
|
+
obj.count = obj[key].length;
|
|
37
|
+
if ("row_count" in obj)
|
|
38
|
+
obj.row_count = obj[key].length;
|
|
39
|
+
obj.truncated = true;
|
|
40
|
+
truncated = true;
|
|
41
|
+
break;
|
|
31
42
|
}
|
|
32
43
|
}
|
|
44
|
+
if (truncated)
|
|
45
|
+
continue;
|
|
33
46
|
}
|
|
47
|
+
break;
|
|
34
48
|
}
|
|
35
|
-
return
|
|
49
|
+
return current;
|
|
36
50
|
}
|
|
37
51
|
const backoff = new ExponentialBackoff({ initialDelay: 100, maxDelay: 5_000 });
|
|
38
|
-
const
|
|
39
|
-
const
|
|
52
|
+
const isTransient = handleWhen((err) => {
|
|
53
|
+
const msg = (err?.message || "").toLowerCase();
|
|
54
|
+
const code = err?.code || err?.status;
|
|
55
|
+
if (code === 401 || code === 403 || code === 7 || code === 16)
|
|
56
|
+
return false;
|
|
57
|
+
if (msg.includes("unauthenticated") || msg.includes("permission_denied") || msg.includes("invalid_grant"))
|
|
58
|
+
return false;
|
|
59
|
+
if (code === 429 || msg.includes("rate"))
|
|
60
|
+
return true;
|
|
61
|
+
if (code >= 400 && code < 500)
|
|
62
|
+
return false;
|
|
63
|
+
return true;
|
|
64
|
+
});
|
|
65
|
+
const retryPolicy = retry(isTransient, { maxAttempts: 3, backoff });
|
|
66
|
+
const circuitBreakerPolicy = circuitBreaker(isTransient, { halfOpenAfter: 60_000, breaker: new ConsecutiveBreaker(5) });
|
|
40
67
|
const timeoutPolicy = timeout(30_000, TimeoutStrategy.Cooperative);
|
|
41
68
|
const policy = wrap(timeoutPolicy, circuitBreakerPolicy, retryPolicy);
|
|
42
69
|
export async function withResilience(fn, operationName) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mcp-ga4",
|
|
3
3
|
"mcpName": "io.github.mharnett/ga4",
|
|
4
|
-
"version": "2.0.
|
|
4
|
+
"version": "2.0.4",
|
|
5
5
|
"description": "MCP server for Google Analytics 4 - query GA4 data with natural language via Claude.",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"bin": {
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"node": ">=18.0.0"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@google-analytics/data": "^5.2.1",
|
|
37
36
|
"@google-analytics/admin": "^9.0.1",
|
|
37
|
+
"@google-analytics/data": "^5.2.1",
|
|
38
38
|
"@modelcontextprotocol/sdk": "^0.5.0",
|
|
39
39
|
"cockatiel": "^3.2.1",
|
|
40
40
|
"google-auth-library": "^9.14.0",
|