← all posts site ↗

fixing whoop oauth token expiration

the whoop mcp server kept dying every few days and forcing me to re-authenticate through a browser. the root cause had multiple layers. whoop access tokens are intentionally short-lived at about one hour. permanent access requires the oauth offline scope plus a refresh token, but the existing auth flow wasn't requesting offline. so every re-auth would overwrite the long-lived token with one that couldn't refresh, and it would die again within an hour.

there were two whoop mcp processes running, the gateway and dashboard, both sharing the same token file. whoop rotates refresh tokens on each use, meaning you only get one valid refresh per token. both processes would hit expiry at roughly the same time, attempt refresh using the same old token, and one would invalidate the other. then both would be dead and demand browser auth again. they also fought over the callback port, causing address already in use errors where the wrong process would receive the oauth callback and the one waiting for it got nothing.

the fixes were adding offline to every authorization request so new tokens can actually refresh, implementing locked atomic token writes so two mcp instances can't corrupt each other's rotated refresh tokens, adding refresh coordination so a losing worker uses the newly saved access token instead of trying to refresh with a dead one, and changing the startup so the callback server only binds the port when authorization is actually initiated. the callback url also had to match exactly what was registered in the whoop developer portal, which was port eighteen nine three two, not eight thousand like the stale env file was sending. one clean re-auth and it should survive weeks now instead of days.