fix(google-analytics): fix input validation and align output schemas#475
Open
yuriassuncx wants to merge 3 commits into
Open
fix(google-analytics): fix input validation and align output schemas#475yuriassuncx wants to merge 3 commits into
yuriassuncx wants to merge 3 commits into
Conversation
…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>
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 flattens the custom dimensions/metrics output.Bug Fixes
Migration
{ response: ... }). Removeresponse.from callers.getCustomDimensionsAndMetricsnow returns{ customDimensions, customMetrics }.runReport,runRealtimeReport,runFunnelReport.Written for commit fd42de0. Summary will update on new commits.