We could in principle keep hens as pets and treat them well, and then eat their eggs so what is the problem?
There's a large question of whether or not it's ethical to support the breeding of such an animal in the first place. Chickens are genetic freaks that have been bred to exaggerate certain proportions for meat consumption and to lay an obscene number of eggs relative to their body mass. So, not unlike like modern pure-bred show dogs that suffer health problems, there's something to be said for whether such a practice is ethical in the first place, or whether someone should pick a more reasonable pet.
Also, if you end up with a bunch of female hens with no male counterparts, that means the source is clearly selling them off to slaughter. To then get the hens from that source means that you're supporting deliberate animal cruelty.
However, to avoid all these issues, let us assume that you have picked up rescue hens.
There is a danger that lurks, which starts with first giving extra eggs away, and then "hey, well, let me pay you for the chicken feed at least for those eggs you gave me", and then one might think "oh if I get a few more chickens I can make more money"... and then you might end up back in a cramped animal agriculture scenario. Or if you think that giving away eggs is a gesture that makes you more friends and gives you social status, that ultimately you might end up sacrificing the chickens' wellbeing for your own.
It's important to ensure that, no matter what you're doing, you're really treating the animals as pets first and egg-layers on the side. If you find yourself treating animals more poorly than you would were they not laying eggs just because of their profitability, you might reconsider how you're handling them.
It goes without saying that this hypothetical isn't a defense of eating at McDonalds. Essentially no one eats only the eggs of their own ethical rescue hen and otherwise obstains from all other animal products on ethical grounds. And if you did, you wouldn't defend carnism.