Repository navigation
Make the token refresh code reusable for vMCP and testable - #4117
Conversation
Introduce a reusable upstreamtoken.Service interface that encapsulates the upstream token lifecycle (read, validate expiry, refresh, dedup) behind a single GetValidTokens call. This is designed for reuse by vMCP, which needs the same refresh logic without depending on the middleware layer. InProcessService composes UpstreamTokenStorage + UpstreamTokenRefresher with singleflight deduplication to prevent concurrent refresh attempts from exhausting single-use refresh tokens. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Simplify the MiddlewareRunner interface from two storage-level accessors (GetUpstreamTokenStorage, GetUpstreamTokenRefresher) to a single service-level accessor (GetUpstreamTokenService). The upstreamswap middleware now consumes the Service interface directly, removing its responsibility for composing storage and refresher components. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Reorder Run() to initialize the embedded auth server before middleware creation, eliminating the need for lazy accessor with mutex and caching in GetUpstreamTokenService(). The service is now created eagerly and stored as a field, making the accessor a simple field read. Also deduplicate the createMinimalAuthServerConfig test helper, add UpstreamTokenRefresher to the go:generate directive, and update the middleware documentation to reflect the new interface method. Co-Authored-By: Claude Opus 4.6 <[email protected]>
When a request carries a tsid claim (confirming it expects upstream token injection), passing through with the original JWT would leak the auth server token to the backend. Return 503 instead of forwarding the request when the token service is nil. Co-Authored-By: Claude Opus 4.6 <[email protected]>
There was a problem hiding this comment.
Large PR Detected
This PR exceeds 1000 lines of changes and requires justification before it can be reviewed.
How to unblock this PR:
Add a section to your PR description with the following format:
## Large PR Justification
[Explain why this PR must be large, such as:]
- Generated code that cannot be split
- Large refactoring that must be atomic
- Multiple related changes that would break if separated
- Migration or data transformationAlternative:
Consider splitting this PR into smaller, focused changes (< 1000 lines each) for easier review and reduced risk.
See our Contributing Guidelines for more details.
This review will be automatically dismissed once you add the justification section.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4117 +/- ##
==========================================
+ Coverage 68.84% 68.87% +0.02%
==========================================
Files 458 459 +1
Lines 46274 46267 -7
==========================================
+ Hits 31859 31866 +7
+ Misses 11953 11941 -12
+ Partials 2462 2460 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
tgrunnagle
left a comment
There was a problem hiding this comment.
Couple non-blocking comments. Looks great!
I added the required paragraph but the nack didn't go away
Summary
This PR refactors the in-middleware refresh into an
UpstreamTokenServicethat transparently refreshes expired upstream access tokens on behalf of the user, so MCP sessions survive beyond the upstream IDP's token lifespan.Commits
Add upstreamtoken package for upstream token lifecycle — Introduces a reusable
upstreamtoken.Serviceinterface that encapsulates read → validate expiry → refresh → dedup behind a singleGetValidTokenscall. Designed for reuse by vMCP.Wire UpstreamTokenService into runner and middleware — Simplifies the
MiddlewareRunnerinterface from two storage-level accessors to a singleGetUpstreamTokenService. The upstreamswap middleware now consumes theServiceinterface directly instead of composing storage and refresher itself.Simplify runner startup with eager service initialization — Reorders
Run()to initialize the embedded auth server before middleware creation, eliminating the lazy accessor with mutex and caching. The service is now a simple field read.Fail closed when upstream token service is unavailable — When a request carries a
tsidclaim (meaning it expects upstream token injection), the previous behavior passed it through with the original JWT, leaking the auth server token to the backend. Now returns 503 instead.Tested end-to-end against a live Okta instance in a kind cluster. The ToolHive proxy was exposed via ngrok to avoid port-forward instability, and VS Code was used as the MCP client to exercise the real-world OAuth flow (dynamic client registration, PKCE, browser login).
After authenticating through Okta, the proxy successfully obtained upstream tokens including a refresh token. The Okta authorization server was configured with a 5-minute access token lifespan. Once the upstream AT expired, the next MCP tool call from VS Code triggered a transparent refresh — the
UpstreamTokenServicedetected the expiry, used singleflight-deduplicated refresh via Okta's token endpoint, stored the new tokens, and injected the fresh AT into the proxied request. The tool call succeeded with no 401 and no re-authentication prompt. Subsequent requests used the refreshed token with a full 5-minute TTL.Does this introduce a user-facing change?
No. This is transparent to MCP clients — sessions that previously broke after the upstream AT expired now continue working silently.
Special notes for reviewers
upstreamtokenpackage is intentionally designed as a standalone service (not embedded in middleware) so vMCP can reuse it for the same refresh logic without depending on the middleware layer.Fixes: #3914
Fixes: #3915
Fixes: #3916
Fixes: #3917
Large PR Justification