fix(google-analytics): fix input validation and align output schemas#475
Merged
Conversation
yuriassuncx
enabled auto-merge (squash)
June 24, 2026 20:50
…with other Google MCPs
- Add `fromJson` preprocessor (z.preprocess) to handle MCP clients that
serialize arrays and objects as JSON strings instead of native types.
Fixes validation errors for dateRanges, dimensions, metrics,
dimensionFilter, metricFilter, orderBys, limit, offset, and funnelSteps.
- Remove the `response` wrapper from all output schemas so tools return
data directly (e.g. `{ rows: [...] }` instead of `{ response: { rows: [...] } }`),
consistent with google-search-console and google-tag-manager patterns.
- Flatten `getCustomDimensionsAndMetrics` output from
`{ dimensions: { customDimensions: [...] }, metrics: { customMetrics: [...] } }`
to `{ customDimensions: [...], customMetrics: [...] }`.
- Fix code formatting: remove unnecessary blank lines between chained
Zod method calls across all tool files.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ull strings - Apply fromJson() to returnPropertyQuota (all 3 report tools) and FunnelStepSchema.isDirectlyFollowedBy so MCP clients that serialize booleans as JSON strings (e.g. "true") pass validation, consistent with the existing fix for arrays, objects, and numbers. - In fromJson, treat JSON-parsed null as undefined so optional fields that receive the string "null" degrade gracefully instead of failing Zod validation. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…eprocessor
MCP clients that send null explicitly for absent optional fields (e.g.
{"dimensionFilter": null}) were getting a hard Zod type error instead of
the field being treated as absent. Add an explicit null check so both
native null and the JSON string "null" are treated as undefined.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
… schemas with GA4 API - fromJson(schema).optional() didn't accept null/"null" for optional fields because ZodOptional only short-circuits on undefined before the preprocessor runs; moved .optional() inside fromJson(...) so the coercion to undefined is honored by the resulting schema. - Fixed PropertyQuota's tokensPerProjectPerDay -> tokensPerProjectPerHour (wrong field name silently dropped real quota data). - Added missing fields per the GA4 REST API: PropertySummary.canEdit, ReportingDataAnnotation.annotationDateRange, and ResponseMetaData.dataLossFromOtherRow/emptyReason. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
yuriassuncx
force-pushed
the
fix/google-analytics-input-validation
branch
from
July 9, 2026 14:54
fd42de0 to
532d1c4
Compare
…hemas.ts The input-validation fix accidentally dropped JSDoc comments documenting resource-name formats and enum values for GA4 API fields. Restore them — they're useful reference for anyone reading the schema, independent of the response-wrapper removal. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
JonasJesus42
approved these changes
Jul 9, 2026
2 tasks
yuriassuncx
added a commit
that referenced
this pull request
Jul 9, 2026
Revert "fix(google-analytics): fix input validation and align output schemas with other Google MCPs (#475)"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
fromJsonpreprocessor (z.preprocess) inreports.tsthat transparently parses JSON strings before Zod validation. This fixes all 6 reported errors:dateRanges,dimensions,metrics,dimensionFilter,metricFilter,orderBys,orderBys,limit, andoffset.{ response: ... }wrapper from all output schemas — all tools now return data directly (e.g.{ rows: [...] }instead of{ response: { rows: [...] } }), consistent withgoogle-search-consoleandgoogle-tag-managerpatterns.getCustomDimensionsAndMetricsoutput — from{ dimensions: { customDimensions: [...] }, metrics: { customMetrics: [...] } }to{ customDimensions: [...], customMetrics: [...] }.Root cause
The validation error was:
```
Invalid input: expected array, received string (dateRanges, dimensions, metrics, orderBys)
Invalid input: expected record, received string (dimensionFilter)
Invalid input: expected number, received string (limit)
```
The fix uses
z.preprocessto attemptJSON.parseon string inputs before Zod validates them. If parsing fails, the original value is passed through and Zod produces a clear type error.Test plan
run-reportwith native JSON arrays — should work as beforerun-reportwith JSON-stringified arrays — should now pass validationget-account-summariesoutput is now{ accountSummaries: [...] }(noresponsewrapper)run-reportoutput is now{ rows: [...], dimensionHeaders: [...], ... }(noresponsewrapper)get-custom-dimensions-and-metricsoutput is{ customDimensions: [...], customMetrics: [...] }(flat)🤖 Generated with Claude Code
Summary by cubic
Fixes GA4 tool input validation across MCP clients (handles JSON‑stringified arrays/objects/booleans and both "null" strings and native nulls) and updates all outputs to return top‑level data without a response wrapper, matching
google-search-consoleandgoogle-tag-manager. Also aligns schemas with the GA4 API, flattens the custom dimensions/metrics output, and restores field documentation comments.Bug Fixes
PropertySummary.canEdit,ReportingDataAnnotation.annotationDateRange, andResponseMetaData.dataLossFromOtherRow/emptyReason.schemas.ts.Migration
{ response: ... }). Removeresponse.from callers.getCustomDimensionsAndMetricsnow returns{ customDimensions, customMetrics }.runReport,runRealtimeReport,runFunnelReport.Written for commit 4923302. Summary will update on new commits.