{"record":{"id":"fdc3357954c81347","repo":"googleapis/mcp-toolbox","slug":"failed-to-marshal-result-w","errorCode":null,"errorMessage":"failed to marshal result: %w","messagePattern":"failed to marshal result: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/internal/invoke/command.go","lineNumber":161,"sourceCode":"\t\treturn errMsg\n\t}\n\tif requiresAuth {\n\t\terrMsg := fmt.Errorf(\"client authorization is not supported\")\n\t\topts.Logger.ErrorContext(ctx, errMsg.Error())\n\t\treturn errMsg\n\t}\n\n\tresult, err := tool.Invoke(ctx, src, parsedParams, \"\")\n\tif err != nil {\n\t\terrMsg := fmt.Errorf(\"tool execution failed: %w\", err)\n\t\topts.Logger.ErrorContext(ctx, errMsg.Error())\n\t\treturn errMsg\n\t}\n\n\t// Print Result\n\toutput, err := json.MarshalIndent(result, \"\", \"  \")\n\tif err != nil {\n\t\terrMsg := fmt.Errorf(\"failed to marshal result: %w\", err)\n\t\topts.Logger.ErrorContext(ctx, errMsg.Error())\n\t\treturn errMsg\n\t}\n\tfmt.Fprintln(opts.IOStreams.Out, string(output))\n\n\treturn nil\n}\n","sourceCodeStart":143,"sourceCodeEnd":169,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/cmd/internal/invoke/command.go#L143-L169","documentation":"After a successful tool run, runInvoke pretty-prints the result with json.MarshalIndent. This error fires if the tool's returned result cannot be serialized to JSON — typically a result containing values Go's encoding/json cannot handle (channels, funcs, cyclic structures, or unsupported custom types) (cmd/internal/invoke/command.go:158-164).","triggerScenarios":"`toolbox invoke <tool>` where the invoked tool kind returns a result object that fails JSON marshaling: a custom/primitive tool returning unserializable data (e.g. non-UTF8 bytes mishandled, maps with non-string keys of unsupported types, cycles).","commonSituations":"Using a custom tool (via the SDK) whose Invoke returns a struct with unexported cyclic pointers or non-JSON-safe types; a tool returning map[customType]any results; rarely, driver row values wrapped in unsupported types.","solutions":["Inspect the tool implementation's Invoke return value and ensure it contains only JSON-serializable types.","For custom tools, convert unsupported fields (time, big numbers, byte slices) to strings or json.RawMessage before returning.","If it's a built-in tool, report/upgrade: this indicates a bug in that tool kind's result shape; try a newer toolbox version.","File an issue with the tool kind and result data if a stock tool reproduces it."],"exampleFix":"// before (custom tool)\nfunc (t *myTool) Invoke(...) (any, error) {\n    return map[string]any{\"created\": someCustomTimeType{}}, nil // unserializable\n}\n// after\nfunc (t *myTool) Invoke(...) (any, error) {\n    return map[string]any{\"created\": time.Now().UTC().Format(time.RFC3339)}, nil\n}","handlingStrategy":"type-guard","validationCode":"// Ensure a tool result is JSON-marshalable before relying on CLI output:\nfunc isJSONSafe(v any) bool {\n    _, err := json.Marshal(v)\n    return err == nil\n}","typeGuard":"func isJSONSerializable(v any) (string, bool) {\n    b, err := json.Marshal(v)\n    if err != nil { return \"\", false }\n    return string(b), true\n}","tryCatchPattern":"// When scripting around the CLI, treat any non-zero exit as marshal/other failure and fall back to raw server invocation:\nout, err := exec.Command(\"toolbox\", \"invoke\", tool, params).CombinedOutput()\nif err != nil {\n    log.Fatalf(\"invoke failed (possibly non-JSON-serializable result): %s\", out)\n}","preventionTips":["In custom tool Invoke implementations, return only JSON-safe types (string, number, bool, slice, map[string]any, struct).","Avoid cycles and non-string map keys in returned data.","Prefer json.RawMessage for pre-encoded payloads.","Update toolbox if a stock tool kind reproduces the error — it's likely a serialization bug."],"tags":["cli","json","serialization"],"backgroundTag":"json-marshal-failed","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}