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
- Find the offending field by marshaling the payload in isolation to reproduce the error
- Remove or replace non-serializable fields (channels, funcs, cycles) in result structs
- Add json tags and custom MarshalJSON for special types
- 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
- Keep result structs free of chan/func/cyclic fields
- Add json tags to every result struct field
- Test Handle output serialization in unit tests, not only in production
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
- 参数错误:generatedFiles 必须是JSON字符串
- 解析执行计划失败: %v
- 解析执行计划失败: %v 请确保ExecutionPlan格式正确,参考工具描述中的结构体格式要求
- 解析generatedFiles失败: %v
- 序列化上游请求失败: %w
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/8272f72c23639392.
Report an issue: GitHub.