googleapis/mcp-toolbox · error
failed to unmarshal operation bytes: %w
Error message
failed to unmarshal operation bytes: %w
What it means
After successfully marshaling the completed operation to JSON bytes, GetOperations unmarshals those bytes into a generic any so the result can be returned as structured data. This error means the round-trip failed — the JSON bytes the tool itself just produced could not be parsed, which indicates a deep inconsistency (corrupt bytes, encoding issue) in the marshaled payload.
Source
Thrown at internal/sources/alloydbadmin/alloydbadmin.go:371
var opBytes []byte
opBytes, err = op.MarshalJSON()
if err != nil {
return nil, fmt.Errorf("could not marshal operation: %w", err)
}
if op.Response != nil {
var responseData map[string]any
if err := json.Unmarshal(op.Response, &responseData); err == nil && responseData != nil {
if msg, ok := generateAlloyDBConnectionMessage(responseData, connectionMessageTemplate); ok {
return msg, nil
}
}
}
var result any
if err := json.Unmarshal(opBytes, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal operation bytes: %w", err)
}
return result, nil
}
logger.DebugContext(ctx, fmt.Sprintf("Operation not complete, retrying in %v\n", delay))
}
return nil, nil
}
func generateAlloyDBConnectionMessage(responseData map[string]any, connectionMessageTemplate string) (string, bool) {
resourceName, ok := responseData["name"].(string)
if !ok {
return "", false
}
parts := strings.Split(resourceName, "/")
var project, region, cluster, instance string
// Expected format: projects/{project}/locations/{location}/clusters/{cluster}View on GitHub (pinned to 8cc6e09de2)
Solutions
- Retry the call; inspect the wrapped error for 'invalid character' or 'invalid UTF-8' clues.
- Log/dump opBytes (or a safe prefix) to see what failed to parse.
- Check the operation payload via `gcloud alloydb operations describe <op>` for non-UTF-8 or unusual metadata.
- Upgrade the AlloyDB Admin Go SDK to the latest version.
- If reproducible, file a bug against the SDK's operation MarshalJSON.
Defensive patterns
Strategy: retry
Validate before calling
// Ensure the operation payload is sane via a direct API read before round-tripping
op, err := adminClient.GetOperation(ctx, &adminpb.GetOperationRequest{Name: opName})
if err != nil { return err }
if _, err := json.Marshal(op); err != nil { return fmt.Errorf("operation payload not serializable: %w", err) } Try / catch
result, err := toolbox.GetOperations(ctx, opName)
var opErr *errLookup
if errors.As(err, &opErr) && strings.Contains(err.Error(), "failed to unmarshal operation bytes") {
// fall back to describing the operation directly via the Admin API
} Prevention
- Use latest SDK versions where MarshalJSON bugs are fixed
- Log opBytes safely (truncated) on failure
- Cross-check operation payloads with `gcloud alloydb operations describe`
- File SDK bugs when invalid-JSON marshaling is reproducible
When it happens
Trigger: Calling GetOperations on a DONE operation where json.Unmarshal(opBytes, &result) fails — practically only when MarshalJSON produced bytes that are not valid JSON (e.g. embedded invalid UTF-8 or non-standard JSON from the operation's MarshalJSON implementation).
Common situations: Operation payloads containing invalid UTF-8 strings or exotic values that marshal to non-standard JSON; SDK version bugs in the longrunning operation's MarshalJSON.
Understand the failure class
Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.
Related errors
- operation finished with error but could not marshal error ob
- could not marshal operation: %w
- failed to marshal payload: %w
- error marshalling message: %w
- failed to marshal result: %w
AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05).
Data as JSON: /api/errors/c64551e3d6824ffc.
Report an issue: GitHub.