Repository navigation
Accept a call's messages after its token expires - #7339
Conversation
A call-bound token's exp now limits only when a call can start. A tool call that outlives its token keeps, on its session and while it is open and younger than the maximum call duration, its cancellation, JSON-RPC responses such as elicitation answers, and GET and DELETE. Nothing else is accepted after exp. The token validator gets an opt-in option, WithExpiredTokenAdmission, that checks an expired JWT again on everything but exp, bounds its age, and asks a callback. callbinding.Binding is shared by the validator and the vMCP's call-binding check: the server takes it as CallBinding and the auth factory through WithCallBinding. Admitted identities are marked, get no upstream tokens, are refused any call by the core and the authorization gate, and are audited with admitted_after_expiry and the call's operation ID. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Refuse a CallBinding that callbinding.New did not build, log the admitter's record failures at DEBUG, document that a stream opened after exp outlives the call, and bound the elicitation test's wait. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closing a Legacy call's POST returns its request but not its dispatch, and a go-sdk client closes the POST before it cancels. The call now stays open while its dispatch runs on the replica, so the cancellation, before or after exp, still reaches it; the last dispatch ends the call. Binding now owns opening and ending calls (OpenCall, EndCall, BeginDispatch) instead of exposing its record and returned set. Core admission refuses identities admitted after expiry, so both gates and dispatch hold the backstop. A request that crosses exp goes to the after-exp path. Docs cover the library API, the audit keys and the validator option. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7339 +/- ##
==========================================
+ Coverage 82.60% 82.71% +0.10%
==========================================
Files 943 946 +3
Lines 96943 97298 +355
==========================================
+ Hits 80082 80479 +397
+ Misses 16856 16814 -42
Partials 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-Authored-By: Claude Opus 5.5 <[email protected]>
jhrozek
left a comment
There was a problem hiding this comment.
I read through the after-exp path and want to raise one thing about how it ships, not about how it's built.
Accepting an expired JWT goes against RFC 7519 section 4.1.4, RFC 9068 section 4, and the MCP authorization text that says an expired token gets a 401. I think the problem in "vMCP: accept an access token for the one call its authorization_details names, once" is real, so I'm not arguing to drop it. But as written it is on for every thv vmcp serve with OIDC incoming auth, there is no switch, and the window is a fixed 30 minutes (DefaultMaxCallDuration, pkg/vmcp/callbinding/binding.go:63) whatever the token's own lifetime was. For a token that lives about 30 seconds, and a client that needs about a minute to answer an elicitation, that is a 30x extension the issuer never chose.
What I'd suggest: default off, with an operator list of (issuer, client_id, grace). An empty list means strict exp.
TokenValidatorOption(shared.go:177) returns a no-op option when the list is empty, so exp stays strict by construction andWithCallBinding(incoming.go:283) needs no change.- Match on the client_id claim only.
callbinding.ClientIDalso falls back to azp (binding.go:198), so this needs its own read. Note that client_id names the client the token was issued to, not the one presenting it, and on an authorization server shared by several clients it limits which tokens get the window, not which clients can be issued a call entry. That part is for the AS grant hook. - Check the list in
AdmitExpiredToken(shared.go:204), afterb.Token(...), and again inserveAfterExpiry(pkg/vmcp/server/call_binding.go:407), since the validator and the server are wired separately. Use the entry's grace instead ofmaxCallDurationfor the window; the validator'smaxAgecan stay as the ceiling. - Leave the pre-exp dispatch tracking (
BeginDispatch/EndCall) alone.
Done looks like: with no list configured, a call-bound token past exp gets 401 on every request; with a matching entry, today's behaviour, bounded by that entry's grace. serve.go:396 builds the binding with no operator input today, so this is the first knob. The config path is yours to pick.
Sender-constraining these tokens is planned, tracked as Bind gateway access tokens to the caller's key (DPoP or mTLS). Once it exists, I'd expect after-exp admission to require the proof as well, and the list is where that would be switched on.
jhrozek
left a comment
There was a problem hiding this comment.
Four small things, inline. The first one will start failing on a fixed date in January, so it's worth fixing in this PR. The rest tighten behaviour that is easy to miss.
After-exp admission was on for every vMCP with OIDC incoming auth, with a
fixed 30-minute window whatever the token's own lifetime. It is now off by
default: once a call-bound token expires, every request with it gets 401.
Operators opt in with incomingAuth.expiredTokenAdmission (on a
VirtualMCPServer, spec.incomingAuth.expiredTokenAdmission), a list of
{issuer, clientId, grace} entries matched against the token's iss and
client_id claims (azp is not read). A matching token's call messages are
accepted for the entry's grace after exp, still bounded by the call being
open and by the maximum call duration. Without an entry the validator
option is auth.WithStrictExpiry, so exp is strict by construction; the
call-binding middleware checks the list and grace again, as the two are
wired separately. The vMCP, callbinding.New and the operator's converter
refuse an empty issuer or client, a grace that is not positive or exceeds
the maximum call duration, and duplicate entries.
The after-exp DELETE test now expects the DELETE to cancel the open call,
as main does since sessions cancel their open calls on delete.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
- A list-call token has no messages after exp even when the record could not mark the list call ended: the binding's callback and the call-binding check now refuse a list entry by its type, for GET and DELETE as for a response. - The stream-open audit event of a GET admitted after exp carries admitted_after_expiry and operation_id, as other events do. - The admission unit test gives its in-memory record the clock it gives the admission check, so its fixed times never fall behind the real clock. - docs/authz.md says the messages are accepted until the call ends (its request has returned and its dispatch has finished) or has run for the maximum call duration. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks, adopted in 868c4aa and narrowed to Go only in 3b0c118, per the follow-up review. Admission after
New tests:
The existing tests that rely on admission now configure an entry. I also merged main (#7338, #7341). The DELETE-after- 🤖 Addressed by Claude Code |
jhrozek
left a comment
There was a problem hiding this comment.
The opt-in and the fixes from the last round look right to me, thanks for turning them around so fast. Four things inline: one is a request to pull back the CRD field, the others are a go-sdk behaviour I'd like a second look at, a test that can pass without cancelling, and a docs sentence.
- Remove spec.incomingAuth.expiredTokenAdmission from the CRD and the operator's converter, and incomingAuth.expiredTokenAdmission from the vMCP config, so vmcp serve and the operator stay strict with no way to opt in. A Go embedder sets callbinding.Config.ExpiredTokenAdmission, validated by callbinding.New and checked by the validator and the call-binding check as before. - Within the grace, a matching token on the session of a tool call that has ended, or reached the maximum call duration, is answered as before exp and never dispatched: its cancellation and a JSON-RPC response get 202, a GET or DELETE 403 no_open_call. A 401 there made go-sdk clients fail the connection over a cancellation that raced the call's end. The validator's callback now admits such a request on the call's own session so the call-binding check can answer it. - The chain cancellation test fails if the backend does not see the cancellation before the call's own deadline. - Docs: admission is a Go embedder option; admitted requests load no upstream tokens, so an upstream logout does not stop an open call's messages within the grace; an upgrade table for the embedder API; the call-binding settings are callbinding.Config fields. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
jhrozek
left a comment
There was a problem hiding this comment.
Thanks, all four are addressed: the CRD field and config key are gone, an ended call's cancel or response now gets a 202 and its GET or DELETE a 403 no_open_call, the cancel test fails when the backend never sees the cancellation, and the docs say what an admitted request skips.
One nit, not worth another CI run: in docs/arch/vmcp-library.md the sentence starting "List a client only once the authorization servers..." sits on the last row of the new before/after table, so it renders as a third cell. It needs a blank line before it. Could you fix that as a drive-by in your next PR?
Summary
A call-bound access token (an RFC 9396
authorization_detailsentry of typehttps://toolhive.dev/mcp_tool_call) may start one call before itsexp. A tool call can run longer than its token lives. Today, every message the client sends for that call afterexpgets the token validator's 401. The client then cannot cancel the call, answer an elicitation, reconnect the stream that carries the call's messages, or end its session. Issue #7062 asks that, for such tokens,explimit only when a call can start.This PR implements that last open piece of the issue as an opt-in for Go embedders only.
vmcp serveand the operator keepexpstrict and have no setting to change it.Strict by default.
expstays strict, as RFC 7519 section 4.1.4, RFC 9068 section 4 and the MCP authorization text require. Once a call-bound token expires, every request with it gets 401, including the call's own cancellation, replies, GET and DELETE.Go-only opt-in. An embedder sets
callbinding.Config.ExpiredTokenAdmission, a list of{Issuer, ClientID, Grace}entries:issandclient_idclaims equal itsIssuerandClientIDexactly. Theazpclaim is not read.Grace, measured from the token'sexp. A 30-second token with a one-minute grace gets one minute, not 30. The maximum call duration (callbinding.Config.MaxCallDuration, 30 minutes by default) stays the upper limit.callbinding.Newrefuses an emptyIssuerorClientID, aGracethat is not positive or exceeds the maximum call duration, and two entries for the same issuer and client.What a matching token may send after
exp, within its grace, on the session of the tool call it started, while the call is open:notifications/cancelledfor the call's JSON-RPC ID;Everything else gets 401: a new call, a list,
initialize,ping, a cancellation for another ID, or another session. A list-call token and a Modern call (which has no session) have no messages afterexp.Once the call has ended, or has reached the maximum call duration, a matching token within its grace is answered on the call's session as before
exp, and nothing is dispatched:no_open_callwith a JSON-RPC error.A 401 there would make a go-sdk v1.8.0 client fail its connection over a cancellation that merely raced the call's end. That client also fails the connection on a JSON-RPC error that has no
id(which is what a notification would get), so a cancellation must get 202.pkg/authgets two validator options:WithExpiredTokenAdmission(maxAge, admit), opt-in. When a JWT fails validation because of itsexpalone, it checks the token again on everything else (signature,iss,aud,nbf, subject restriction). It then requiresexpto be less thanmaxAgein the past and asks the callback. Opaque tokens never qualify. An admitted identity is marked (Identity.AdmittedAfterExpiry) and gets no upstream tokens. So a logout or a revoked upstream session does not stop an open call's cancellation, replies, GET or DELETE until the grace ends.WithStrictExpiry(), which states the default.MCPServer proxies set neither.
Wiring:
callbinding.New(cfg)builds a*callbinding.Binding, which the vMCP's call-binding check and the token validator share. The server takes it asCallBinding. The incoming auth factory takes it through a new optional argument,factory.WithCallBinding(b), which gives the validatorBinding.TokenValidatorOption().ExpiredTokenAdmissionentry, that option isauth.WithStrictExpiry(), soexpis strict by construction.vmcpcommand wires theBindingbut sets no list. The wiring is explicit, not context-based, because of.claude/rules/vmcp-anti-patterns.mdrule 1.Serverefuses aBindingthatNewdid not build.Checks in three places:
issandclient_idmatch a list entry within that entry's grace; itsissandjtihave a call in this replica's record; and the request names that call's session. It never reads the request body.token_expired.authorizeToolCall,authorizeResourceRead,authorizePromptGet) refuses marked identities. This covers the pre-dispatch gates on both revisions and dispatch.A Legacy call stays open while it dispatches. This applies whether or not the list is set. go-sdk keeps a Legacy tool call running after the client closes its POST, and a go-sdk client closes the POST before it sends
notifications/cancelled. Before this PR, the call was marked ended as soon as the POST returned, so that cancellation gotno_open_calland could not reach the running call. Now the Legacy dispatch marks itself running (Binding.BeginDispatch), and the request's return defers to it (Binding.EndCall). The last dispatch ends the call. Enforce list-call tokens at the vMCP #7325 deferred this case to this change.Audit: an admitted request's event carries
metadata.extra.admitted_after_expiryand the entry'soperation_id. This includes thesse_connectionstream-open event of a GET.Docs: updated
docs/arch/10,docs/arch/vmcp-library.md(with an embedder upgrade table),docs/authz.mdanddocs/middleware.md.Closes #7062
Type of change
Test plan
task test): race runs of./pkg/auth/... ./pkg/vmcp/... ./pkg/audit/... ./pkg/mcp/toolcalldetails/... ./cmd/thv-operator/pkg/vmcpconfig/... ./cmd/thv-operator/api/... ./cmd/thv-operator/controllers/...pass (71 packages)task lint-fix):task lintreports 0 issues;task docsproduces no changes; the operator's generated files are unchanged frommainWhat the new tests cover:
pkg/auth/token_expired_admission_test.go:issoraud, futurenbf, subject restriction, missingexp,expolder thanmaxAge, or an opaque token.WithStrictExpiryafterWithExpiredTokenAdmissionis strict.pkg/vmcp/callbinding/shared_test.go:NewandValidateExpiredTokenAdmission.AdmitExpiredToken's cases: no list, issuer or client not listed,azp-only client, the grace boundary, list calls (including one whose end the record missed), ended calls on and off the call's session, a Modern call, another session, a failing record, and a zeroBinding. Its record runs on the admission check's clock, so the fixed times never go stale.WithinExpiredTokenGrace.EndCalldeferring to running dispatches.pkg/vmcp/server/call_binding_expiry_test.go: a stand-in validator calls the realBinding.AdmitExpiredToken.expgets 401.azp-only client, gets 401.exp, only the open call's own messages pass.no_open_callfor a GET or DELETE, and 401 for anything else. Nothing is dispatched. Another session and past the grace still get 401.expstops the call, and a DELETE afterexpcancels it.exp. A replica sharing a record that missed a list call's end still refuses its session's GET and DELETE.expcarries both fields.pkg/vmcp/server/call_binding_idjag_expiry_integration_test.go: real AS-1 and AS-2 with 2-second access tokens, in front of a vMCP whose production OIDC validator shares the server'sBinding.expis strict.no_open_calland a repeated cancellation gets 202.expis received on a stream opened afterexp, and answered.pkg/vmcp/core/core_checks_test.goandpkg/audit/auditor_test.go.Manual end-to-end run on separate processes. AS-1 and AS-2 run on
pkg/authserver/runnerwith the call type registered, and AS-2 issues 10-second access tokens. Two vMCP replicas sit in front of two real MCP backends whose tools outlive the token. The full check list is in the run report.vmcp serveat this head:exp: the call's cancellation, a reply, GET and DELETE get the validator's 401, as do a list-call token's GET, DELETE and response. A cancellation beforeexpreaches the backend.incomingAuth.expiredTokenAdmissionis refused at startup:field expiredTokenAdmission not found in type config.IncomingAuthConfig.ExpiredTokenAdmissionset in Go (Grace: 1m), within the grace:exp, the question arrives, the answer gets 202, and the call completes.pingand a cancellation for another ID get 401. The other replica gets 401.StreamableClientTransportconnection: within the grace, its cancellation and a response for an ended call are accepted, and the connection stays usable. Past the grace, the cancellation gets 401 and go-sdk fails the connection.clientIdor issuer entries (401), audit fields on admitted requests including thesse_connectionevent,denial_reason: token_expiredon call-binding refusals, and refusal of invalid lists at startup.Changes
pkg/auth/token.go,pkg/auth/identity.goWithExpiredTokenAdmission,WithStrictExpiry,ExpiredToken,ExpiredTokenAdmitter;Identity.AdmittedAfterExpiry; no upstream tokens for admitted identitiespkg/vmcp/callbinding/admission.goExpiredTokenAdmission,ValidateExpiredTokenAdmission,WithinExpiredTokenGrace,AdmitsExpiredTokenspkg/vmcp/callbinding/shared.go,binding.goBindingandNew;AdmitExpiredToken,TokenValidatorOption(strict without a list),OpenCall,EndCall,BeginDispatch;Config.ExpiredTokenAdmissionpkg/vmcp/server/call_binding.goexppath (serveAfterExpiry,serveEndedCallAfterExpiry); a Legacy call stays open while it dispatches; unwired-validator WARNpkg/vmcp/server/serve_handlers.gotools/calldispatch marks itself runningpkg/vmcp/server/serve.go,server.goCallBindingis now a*callbinding.Binding;Serverefuses oneNewdid not buildpkg/vmcp/auth/factory/incoming.goWithCallBindingoption forNewIncomingAuthMiddlewarepkg/vmcp/cli/serve.goBindingfor the validator and the server, with no admission listpkg/vmcp/core/core_checks.go,core_calls.gopkg/audit/auditor.go,mcp_events.goadmitted_after_expiryandoperation_idmetadata, also on stream-open eventsdocs/...*_test.goDoes this introduce a user-facing change?
For
vmcp serveand operator users, only in one place:exp: a cancellation sent after the client closed the call's POST used to get 403no_open_callwhile the call kept running. It now reaches the call.expdoes not change. It still gets 401.For Go embedders:
ServerConfig.CallBinding: callbinding.Config{...}b, err := callbinding.New(callbinding.Config{...}), thenServerConfig.CallBinding: bfactory.NewIncomingAuthMiddleware(ctx, cfg, ...)factory.NewIncomingAuthMiddleware(ctx, cfg, ..., factory.WithCallBinding(b))with the sameb. The trailing argument is variadic, so existing call sites compile.CallBindingfield has not been in a release yet.expsetscallbinding.Config.ExpiredTokenAdmission.admitted_after_expiry: trueandoperation_id.Special notes for reviewers
client_idonly: an entry reads theclient_idclaim alone, notcallbinding.ClientID, which falls back toazp.client_idnames the client the token was issued to, not the one presenting it. The list limits which tokens get the grace; which clients can be issued a call entry belongs to the authorization server's grant policy.expis expected to require the proof as well. The list is where that would be switched on.iddoesn't decode. A standalone GET keeps the connection on a non-SSE answer.Closeignores the DELETE status.expwith no open call still gets 403 with a nullid, which go-sdk treats as a broken connection. That predates this PR.exp. This PR admits them for a matching token, on the open call's session only. The vMCP sends a backend's elicitation on the session's standalone GET stream, so a client whose stream dropped needs GET to receive the question. DELETE starts nothing, and since Cancel a session's open calls when its client deletes it #7341 it cancels the session's open calls. A stream is checked only when it opens.exp.expgets 401. That does not widen the single-use gap (vMCP: one single-use record shared by all replicas for call-bound access tokens #7261).exp.pkg/authoptions could be split out if reviewers prefer.🤖 Generated with Claude Code