googleapis/mcp-toolbox · error
could not marshal operation: %w
Error message
could not marshal operation: %w
What it means
GetOperations polls a long-running AlloyDB Admin operation and, once complete, serializes the operation object to JSON via op.MarshalJSON(). This error wraps any failure during that serialization. It means the completed operation object could not be converted to its JSON representation, so GetOperations cannot return a result.
Source
Thrown at internal/sources/alloydbadmin/alloydbadmin.go:357
op, err := service.Projects.Locations.Operations.Get(name).Do()
if err != nil {
logger.DebugContext(ctx, fmt.Sprintf("error getting operation: %s, retrying in %v\n", err, delay))
} else {
if op.Done {
if op.Error != nil {
var errorBytes []byte
errorBytes, err = json.Marshal(op.Error)
if err != nil {
return nil, fmt.Errorf("operation finished with error but could not marshal error object: %w", err)
}
return nil, fmt.Errorf("operation finished with error: %s", string(errorBytes))
}
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))View on GitHub (pinned to 8cc6e09de2)
Solutions
- Retry GetOperations; transient serialization of a malformed fetch is unlikely but the underlying operation may have been fetched incompletely — re-fetch and retry.
- Inspect the wrapped err (%w) to identify which field failed to marshal.
- Check the AlloyDB Admin API operation directly (gcloud alloydb operations describe) to see if the operation payload is malformed.
- Upgrade google.golang.org/alloydb/admin/apiv1alpha (or related) client libraries to the latest version.
- File an issue with the operation name if the API returns a payload the SDK cannot serialize.
Example fix
// before
opBytes, err := op.MarshalJSON()
if err != nil { return nil, fmt.Errorf("could not marshal operation: %w", err) }
// after
opBytes, err := op.MarshalJSON()
if err != nil {
logger.DebugContext(ctx, fmt.Sprintf("operation %s failed to marshal: %v", opName, err))
return nil, fmt.Errorf("could not marshal operation: %w", err)
} Defensive patterns
Strategy: retry
Validate before calling
// Prefetch and inspect the operation via the Admin API before calling GetOperations
op, err := adminClient.GetOperation(ctx, &adminpb.GetOperationRequest{Name: opName})
if err != nil { return err }
if !op.Done { return fmt.Errorf("operation %s not done yet", opName) } Try / catch
result, err := toolbox.GetOperations(ctx, opName)
if err != nil {
if strings.Contains(err.Error(), "could not marshal operation") {
// inspect op via gcloud / retry once
}
return fmt.Errorf("get operations failed: %w", err)
} Prevention
- Keep the AlloyDB Admin Go SDK up to date
- Log the operation name on failure for support/debugging
- Validate the operation reaches DONE without error before processing
- Pin and test SDK versions against your AlloyDB API revision
When it happens
Trigger: Calling GetOperations on an AlloyDB Admin operation whose LRO struct, once fetched in DONE state, fails MarshalJSON — e.g. an operation response/metadata payload containing types the generated client cannot serialize (unexpected field types or nil-unsafe marshaling).
Common situations: Unusual operation payloads returned by the AlloyDB Admin API (error operations with atypical metadata), or bugs/mismatches between the generated longrunning client version and the actual API response shape.
Understand the failure class
Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.
Related errors
- operation finished with error but could not marshal error ob
- failed to unmarshal operation bytes: %w
- operation finished with error: %s
- unable to create pool: %w
- failed to initialize AlloyDB source in read-only mode: 'allo
AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05).
Data as JSON: /api/errors/f5c2d485c465120c.
Report an issue: GitHub.