mcp: avoid "null" when tools fail to return content

For some reason, this was a regression from v0.3.1, for some of our
examples. Fix it and address the existing TODO.
This commit is contained in:
Rob Findley 2025-09-05 20:48:32 +00:00 • committed by Robert Findley
commit 4656930de8

View file

@ -496,9 +496,13 @@ func (s *Server) callTool(ctx context.Context, req *CallToolRequest) (*CallToolR
Message: fmt.Sprintf("unknown tool %q", req.Params.Name),
}
}
// TODO: if handler returns nil content, it will serialize as null.
// Add a test and fix.
return st.handler(ctx, req)
res, err := st.handler(ctx, req)
if err == nil && res != nil && res.Content == nil {
res2 := *res
res2.Content = []Content{} // avoid "null"
res = &res2
}
return res, err
}
func (s *Server) listResources(_ context.Context, req *ListResourcesRequest) (*ListResourcesResult, error) {