Detach the keyboard, shut the Surface down completely, wait 30 seconds, reattach the keyboard, and boot. Then check that the keyboard types, which proves the connection is good.
Plug the Surface into mains power while testing. The tray may not charge the pen reliably when the Surface is running on battery only.
Clean the contacts. Wipe the keyboard’s connector pins and the pen tray with a dry, lint-free cloth. Dust, a case, or a screen protector edge can block the tray’s wireless charging coil.
Seat the pen correctly. It should snap in flat and centered, with its magnet seated, and stay there for several hours. A fully discharged Slim Pen 2 can take a long time to wake, so leave it overnight (8 to 12 hours) before judging.
Update firmware. Install everything in Windows Update, including the Surface firmware and the keyboard firmware. Open the Surface app to check for keyboard and pen updates. If you have a Flex Keyboard, make sure the keyboard itself is charged and attached, since its tray runs on the keyboard’s own battery.
Use a different keyboard. If a friend or colleague has a compatible Surface keyboard, put the pens in that tray overnight. If the pens charge there, your keyboard is the fault. If they still don’t, the pens are the problem.
If the pens charge on another keyboard, you have a clear hardware fault and can take your keyboard to Microsoft or an authorized service center for a warranty check or replacement. Three months of failure is a good case for a claim. If the pens still don’t charge on another keyboard, both pens are dead, which would be unusual but is worth raising with Microsoft.