mcp: preserve existing Content in SetError (#864)

SetError now only populates the Content field when it has not already
been populated. This allows callers to provide a user-friendly message
while still recording the underlying error for middleware inspection
via GetError.
The len check (rather than nil check) ensures both nil and empty-slice
Content are treated as "not yet populated."
  Fixes #858
This commit is contained in:
Ravish Gupta 2026-04-03 11:15:24 -07:00 • committed by GitHub
commit 34a19dff46
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2228,4 +2228,45 @@ func TestToolErrorMiddleware(t *testing.T) {
}
}
func TestSetErrorPreservesContent(t *testing.T) {
for _, tt := range []struct {
name string
content []Content
err error
wantContent string
}{
{
name: "nil content",
err: errors.New("internal failure"),
wantContent: "internal failure",
},
{
name: "empty slice content",
content: []Content{},
err: errors.New("internal failure"),
wantContent: "internal failure",
},
{
name: "existing content preserved",
content: []Content{&TextContent{Text: "user-friendly msg"}},
err: errors.New("db timeout"),
wantContent: "user-friendly msg",
},
} {
t.Run(tt.name, func(t *testing.T) {
res := CallToolResult{Content: tt.content}
res.SetError(tt.err)
if !res.IsError {
t.Fatal("want IsError=true")
}
if got := res.Content[0].(*TextContent).Text; got != tt.wantContent {
t.Errorf("Content text = %q, want %q", got, tt.wantContent)
}
if got := res.GetError(); got != tt.err {
t.Errorf("GetError() = %v, want %v", got, tt.err)
}
})
}
}
var ctrCmpOpts = []cmp.Option{cmp.AllowUnexported(CallToolResult{})}