Using one Magic Keyboard with two Macs
A Magic Keyboard can be paired with several Macs. It can only talk to one of them at a time. That is the whole problem, and it isn't a fault in your setup.
Why it only connects to one Mac
Apple's Magic Keyboard, Magic Mouse and Magic Trackpad, 2015 and later, will remember more than one Mac. What they won't do is hold more than one Bluetooth link at once. Whichever Mac has the live connection owns the peripheral, and the other Mac sees nothing, because from its side there is nothing there.
There's no key combination for this and no setting. macOS has never shipped a switch for it. So people invent one.
The three things people do instead
Re-pair by hand. Open System Settings, Bluetooth, on the Mac that currently has the keyboard. Disconnect it. Walk to the other Mac, connect it there. It works, and it takes about twenty seconds every single time you move between machines.
Buy a keyboard built for it. Some third-party keyboards are made to pair with more than one machine and switch between them on their own. They're good keyboards. They're also not a Magic Keyboard, and if you own a Magic Trackpad the problem is still sitting on your desk.
Keep a second set. Another keyboard, another mouse, another thing to charge. Fine if you have the space, and a lot of people quietly do this.
What Baton does about it
Baton is the switch macOS never shipped. It's a menu bar app. You click the Mac you want, and the peripherals you've ticked go there.
Underneath it's two moves. The Mac that currently owns the peripheral drops the Bluetooth bond, so macOS stops reconnecting to it. The Mac you picked then opens its own bond, or pairs from scratch if it has to. Magic devices use Just Works pairing, so nothing appears asking you to confirm a code.
If the second move fails, the Mac you started from takes the peripheral back and says so. You shouldn't end up with a keyboard attached to nothing.
The Macs find each other over Bonjour on your own network. They talk over TLS using a key derived from a twelve-character code you type in once, with PBKDF2 at 600,000 iterations. A Mac that doesn't have your code fails the handshake and gets ignored. None of that goes through a server, because there isn't one in the middle, and there's no account to make. The one time Baton needs the internet is to activate your licence.
What it doesn't do
It won't do the first pairing for you. Pair each peripheral with each Mac once, the normal way, in System Settings. Baton moves ownership after that. It doesn't create it.
Both Macs have to be on the same network, with Local Network permission granted in System Settings, Privacy & Security. Some guest and office networks block the discovery Baton relies on, and on those the Macs never find each other.
A released device stops listening eventually. If a peripheral ends up attached to no Mac at all, switch it off and on and choose Bring to This Mac.
Magic peripherals, 2015 or newer. Other Bluetooth keyboards and mice often work. They aren't supported, and I'd rather say that than let you find out after paying.
It isn't on the Mac App Store. Unpairing a Bluetooth device isn't something App Store apps are allowed to do, which is the entire reason this app can exist outside it. Baton is notarised by Apple and sold here instead.
Price
Nine dollars, once. There's a fourteen-day trial, and the trial only gates switching, so nothing is ever released on the way out. An expired trial can't strand your keyboard on a Mac you've walked away from.