googleapis/mcp-toolbox · error

failed to marshal cluster to JSON: %w

Error message

failed to marshal cluster to JSON: %w

What it means

Thrown by GetCluster when protojson.Marshal fails to serialize the returned Cluster proto into JSON. This is rare because the input is a valid proto from a successful RPC; it typically indicates an unexpected/nil proto or an incompatibility between the dataprocpb version and the protojson behavior.

Source

Thrown at internal/sources/dataproc/dataproc.go:239

// GetCluster gets a single cluster.
func (s *Source) GetCluster(ctx context.Context, clusterName string) (any, error) {
	client := s.GetClusterControllerClient()

	req := &dataprocpb.GetClusterRequest{
		ProjectId:   s.Project,
		Region:      s.Region,
		ClusterName: clusterName,
	}

	clusterPb, err := client.GetCluster(ctx, req)
	if err != nil {
		return nil, fmt.Errorf("failed to get cluster: %w", err)
	}

	jsonBytes, err := protojson.Marshal(clusterPb)
	if err != nil {
		return nil, fmt.Errorf("failed to marshal cluster to JSON: %w", err)
	}

	var result map[string]any
	if err := json.Unmarshal(jsonBytes, &result); err != nil {
		return nil, fmt.Errorf("failed to unmarshal cluster JSON: %w", err)
	}

	consoleUrl := ClusterConsoleURLFromProto(clusterPb, s.Region)
	logsUrl := ClusterLogsURLFromProto(clusterPb, s.Region)

	wrappedResult := map[string]any{
		"consoleUrl": consoleUrl,
		"logsUrl":    logsUrl,
		"cluster":    result,
	}

	return wrappedResult, nil
}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Run `go mod tidy` and update cloud.google.com/go/dataproc and google.golang.org/protobuf to compatible versions.
  2. Check that GetCluster returned a non-nil proto before marshaling (defensive nil check).
  3. Report the issue with the full error if it reproduces with pinned, up-to-date dependencies.

Example fix

// before (no nil check)
clusterPb, err := client.GetCluster(ctx, req)
if err != nil { ... }
jsonBytes, err := protojson.Marshal(clusterPb)

// after (defensive nil check)
if clusterPb == nil {
    return nil, fmt.Errorf("received nil cluster proto")
}
jsonBytes, err := protojson.Marshal(clusterPb)
Defensive patterns

Strategy: try-catch

Validate before calling

null

Type guard

if clusterPb == nil {
    return errors.New("nil cluster proto returned by GetCluster")
}

Try / catch

cluster, err := src.GetCluster(ctx, name)
if err != nil && strings.Contains(err.Error(), "marshal cluster to JSON") {
    // serialization bug: pin/update protobuf + dataproc module versions, report upstream
    return err
}

Prevention

When it happens

Trigger: protojson.Marshal(clusterPb) receives a nil or invalidly-formed Cluster proto, or a mismatched generated proto type (version skew between cloud.google.com/go/dataproc module and google.golang.org/protobuf).

Common situations: Dependency version skew after a Go module upgrade; manually constructed sources in tests passing nil protos; corrupted in-memory state.

Related errors


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/f2200c3d031eff37. Report an issue: GitHub.