flipped-aurora/gin-vue-admin · error

序列化结果失败: %w

Error message

序列化结果失败: %w

What it means

json.MarshalIndent failure inside textResultWithJSON, the shared helper that turns a tool payload into an MCP CallToolResult text. Marshal of plain Go data from these tools practically only fails with unsupported types (channels, funcs, cyclic pointers, NaN/Inf floats).

Source

Thrown at server/mcp/result.go:13

package mcpTool

import (
	"encoding/json"
	"fmt"

	"github.com/mark3labs/mcp-go/mcp"
)

func textResultWithJSON(title string, payload any) (*mcp.CallToolResult, error) {
	resultJSON, err := json.MarshalIndent(payload, "", "  ")
	if err != nil {
		return nil, fmt.Errorf("序列化结果失败: %w", err)
	}

	text := string(resultJSON)
	if title != "" {
		text = fmt.Sprintf("%s\n\n%s", title, text)
	}

	return &mcp.CallToolResult{
		Content: []mcp.Content{
			mcp.TextContent{
				Type: "text",
				Text: text,
			},
		},
	}, nil
}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Find the offending field by marshaling the payload in isolation to reproduce the error
  2. Remove or replace non-serializable fields (channels, funcs, cycles) in result structs
  3. Add json tags and custom MarshalJSON for special types
  4. Sanitize float values (NaN/Inf) to 0 or null before building the payload

Example fix

// before
result := orgAssignChange{Before: currentDeptIDs, After: merged, Added: added, Callback: logFn} // func field
// after
result := orgAssignChange{Before: currentDeptIDs, After: merged, Added: added} // drop non-serializable field
Defensive patterns

Strategy: type-guard

Validate before calling

if err := json.Marshal(payload); err != nil {
    return fmt.Errorf("payload not serializable: %w", err)
}

Type guard

func jsonSafe(v any) bool {
    switch v.(type) {
    case chan struct{}, func(), unsafe.Pointer:
        return false
    }
    return !reflect.ValueOf(v).IsValid() || !hasCycle(reflect.ValueOf(v))
}

Try / catch

result, err := tool.Handle(ctx, args)
if err != nil {
    if strings.Contains(err.Error(), "序列化结果失败") || errors.Is(err, json.UnsupportedTypeError{}) || errors.Is(err, json.UnsupportedValueError{}) {
        // log the payload type; fix the result struct field causing it
    }
    return err
}

Prevention

When it happens

Trigger: Any Handle that builds its result payload with a value json cannot encode (e.g. a channel/func field, a cyclic reference, or math NaN/Inf in a float field) and then calls textResultWithJSON.

Common situations: Custom structs added to result types with non-serializable fields and no json tag handling; accidentally passing a raw func or nil-typed interface holding an unsupported value; injecting NaN from a division by zero.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/8272f72c23639392. Report an issue: GitHub.