fix(mcp): renew an API-key session's JWT when it ages out

Tool handlers run in the session task created at initialize and inherit
its context; contextvars set by later requests never reach them. In a
long-lived API-key session (the documented setup — keys never expire)
the minted 24h JWT eventually expired while the transport kept
authenticating, so every tool call failed with 401.

Remember the raw API key at initialize; when the session's JWT has aged
out, take the current one from the per-request API-key cache (every
tools/call POST refreshes it) or re-mint synchronously. JWT logins are
unchanged: the client's own credential expiring is the client's
business.
This commit is contained in:
yueguobin committed 2026-11-01 23:09:38 +08:00
1 parent 68c8c0f1a0
commit b184ea4c70
2 files changed
+61 -4

No files matched your search

+3
View File
@@ -86,6 +86,9 @@ Generates a fresh 24h JWT → stored in ContextVar for the session
Tool handlers run in the session task created by that request → they use this JWT
↓
Later requests re-validate the credential (result cached ~5 min → no repeated bcrypt)
↓
Once the session's JWT ages past its 24h lifetime, tool handlers renew it
from the API key automatically — long-lived sessions keep working
```
### Concurrency