There are ways to use your iMac as a display for your MBP, but none of them will be the same as using a native display.
For some Late 2009 to Early 2014 iMacs, there was a wonderful feature called Target Display Mode, which the iMac was used as a external monitor to other Macs, via the Mini DisplayPort or Thunderbolt port, depending on the years.
Actually, the Late 2009 to Mid 2010 27” iMacs could be used for any device, Apple or non-Apple, as long as it was capable of outputting DisplayPort.
But, Apple removed the feature , so there is no true HW solution, AFAIK.
There is Luna Display, a third party solution, but not fully HW.
You can use Screen Sharing, it is built into MacOS. I use it all the time.
I have many Macs, some headless, and I control all of them via a single Mac with Screen Sharing.