@@ -18,14 +18,19 @@ export interface McpCredentialRequest {
1818
1919export interface McpIssuedCredential {
2020 readonly config : McpProviderSession . McpProviderSessionConfig ;
21- readonly expiresAt : number ;
2221}
2322
2423export interface McpSessionRegistryShape {
2524 readonly issue : ( request : McpCredentialRequest ) => Effect . Effect < McpIssuedCredential > ;
2625 readonly resolve : (
2726 rawToken : string ,
2827 ) => Effect . Effect < McpInvocationContext . McpInvocationScope | undefined > ;
28+ /**
29+ * Records a sign of life for every credential bound to `threadId`. Provider
30+ * turns call this so that a session which is plainly alive keeps its
31+ * credential even when it goes a long time without touching an MCP tool.
32+ */
33+ readonly touch : ( threadId : ThreadId ) => Effect . Effect < void > ;
2934 readonly revokeProviderSession : ( providerSessionId : string ) => Effect . Effect < void > ;
3035 readonly revokeThread : ( threadId : ThreadId ) => Effect . Effect < void > ;
3136 readonly revokeAll : Effect . Effect < void > ;
@@ -39,21 +44,33 @@ export class McpSessionRegistry extends Context.Service<
3944interface CredentialRecord {
4045 readonly tokenHash : string ;
4146 readonly scope : McpInvocationContext . McpInvocationScope ;
42- readonly lastUsedAt : number ;
47+ readonly lastAliveAt : number ;
4348}
4449
4550interface RegistryState {
4651 readonly records : ReadonlyMap < string , CredentialRecord > ;
4752}
4853
4954export interface McpSessionRegistryOptions {
50- readonly idleTimeoutMs ?: number ;
51- readonly maximumLifetimeMs ?: number ;
55+ readonly livenessWindowMs ?: number ;
5256 readonly now ?: ( ) => number ;
5357}
5458
55- const DEFAULT_IDLE_TIMEOUT_MS = 30 * 60 * 1_000 ;
56- const DEFAULT_MAXIMUM_LIFETIME_MS = 8 * 60 * 60 * 1_000 ;
59+ /**
60+ * How long a credential outlives the last sign of life from its provider
61+ * session.
62+ *
63+ * Liveness is refreshed both by MCP traffic and by `touch` on every provider
64+ * turn, so a session that is still doing work never expires no matter how long
65+ * it goes between browser tool calls. This window therefore only bounds
66+ * credentials whose session died without a clean stop — the normal paths
67+ * (`stopSession`, `stopAll`) revoke eagerly and do not wait for it.
68+ *
69+ * The bound matters because `/mcp` is mounted outside the environment auth
70+ * stack and is reachable on whatever host the server binds to, so this token is
71+ * the only thing guarding the preview toolkit on a remote-reachable server.
72+ */
73+ const DEFAULT_LIVENESS_WINDOW_MS = 24 * 60 * 60 * 1_000 ;
5774
5875const bytesToHex = ( bytes : Uint8Array ) : string =>
5976 Array . from ( bytes , ( byte ) => byte . toString ( 16 ) . padStart ( 2 , "0" ) ) . join ( "" ) ;
@@ -80,8 +97,7 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
8097 const httpServer = yield * HttpServer . HttpServer ;
8198 const state = yield * SynchronizedRef . make < RegistryState > ( { records : new Map ( ) } ) ;
8299 const currentTimeMillis = options . now ? Effect . sync ( options . now ) : Clock . currentTimeMillis ;
83- const idleTimeoutMs = options . idleTimeoutMs ?? DEFAULT_IDLE_TIMEOUT_MS ;
84- const maximumLifetimeMs = options . maximumLifetimeMs ?? DEFAULT_MAXIMUM_LIFETIME_MS ;
100+ const livenessWindowMs = options . livenessWindowMs ?? DEFAULT_LIVENESS_WINDOW_MS ;
85101 const endpoint =
86102 httpServer . address . _tag === "TcpAddress"
87103 ? `http://${ getHttpMcpEndpointHost ( httpServer . address . hostname ) } :${ httpServer . address . port } /mcp`
@@ -92,11 +108,10 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
92108 . digest ( "SHA-256" , new TextEncoder ( ) . encode ( token ) )
93109 . pipe ( Effect . map ( bytesToHex ) , Effect . orDie ) ;
94110
95- const pruneExpired = ( records : ReadonlyMap < string , CredentialRecord > , timestamp : number ) => {
111+ const pruneDead = ( records : ReadonlyMap < string , CredentialRecord > , timestamp : number ) => {
96112 const next = new Map (
97113 Array . from ( records ) . filter (
98- ( [ , record ] ) =>
99- timestamp <= record . scope . expiresAt && timestamp - record . lastUsedAt <= idleTimeoutMs ,
114+ ( [ , record ] ) => timestamp - record . lastAliveAt <= livenessWindowMs ,
100115 ) ,
101116 ) ;
102117 return next . size === records . size ? records : next ;
@@ -108,19 +123,17 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
108123 const providerSessionId = yield * crypto . randomUUIDv4 . pipe ( Effect . orDie ) ;
109124 const rawToken = yield * crypto . randomBytes ( 32 ) . pipe ( Effect . map ( tokenFromBytes ) , Effect . orDie ) ;
110125 const tokenHash = yield * hashToken ( rawToken ) ;
111- const expiresAt = issuedAt + maximumLifetimeMs ;
112126 const scope : McpInvocationContext . McpInvocationScope = {
113127 environmentId,
114128 threadId : ThreadId . make ( request . threadId ) ,
115129 providerSessionId,
116130 providerInstanceId : ProviderInstanceId . make ( request . providerInstanceId ) ,
117131 capabilities : new Set ( [ "preview" ] ) ,
118132 issuedAt,
119- expiresAt,
120133 } ;
121134 yield * SynchronizedRef . update ( state , ( { records } ) => {
122- const next = new Map ( pruneExpired ( records , issuedAt ) ) ;
123- next . set ( tokenHash , { tokenHash, scope, lastUsedAt : issuedAt } ) ;
135+ const next = new Map ( pruneDead ( records , issuedAt ) ) ;
136+ next . set ( tokenHash , { tokenHash, scope, lastAliveAt : issuedAt } ) ;
124137 return { records : next } ;
125138 } ) ;
126139 return {
@@ -132,7 +145,6 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
132145 endpoint,
133146 authorizationHeader : `Bearer ${ rawToken } ` ,
134147 } ,
135- expiresAt,
136148 } ;
137149 } ,
138150 ) ;
@@ -143,16 +155,32 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
143155 const tokenHash = yield * hashToken ( rawToken ) ;
144156 const timestamp = yield * currentTimeMillis ;
145157 return yield * SynchronizedRef . modify ( state , ( { records } ) => {
146- const current = pruneExpired ( records , timestamp ) ;
158+ const current = pruneDead ( records , timestamp ) ;
147159 const record = current . get ( tokenHash ) ;
148160 if ( ! record ) return [ undefined , { records : current } ] as const ;
149161 const next = new Map ( current ) ;
150- next . set ( tokenHash , { ...record , lastUsedAt : timestamp } ) ;
162+ next . set ( tokenHash , { ...record , lastAliveAt : timestamp } ) ;
151163 return [ record . scope , { records : next } ] as const ;
152164 } ) ;
153165 } ,
154166 ) ;
155167
168+ const touch : McpSessionRegistryShape [ "touch" ] = Effect . fn ( "McpSessionRegistry.touch" ) (
169+ function * ( threadId ) {
170+ const timestamp = yield * currentTimeMillis ;
171+ yield * SynchronizedRef . update ( state , ( { records } ) => {
172+ const current = pruneDead ( records , timestamp ) ;
173+ const next = new Map ( current ) ;
174+ for ( const [ tokenHash , record ] of current ) {
175+ if ( record . scope . threadId === threadId ) {
176+ next . set ( tokenHash , { ...record , lastAliveAt : timestamp } ) ;
177+ }
178+ }
179+ return { records : next } ;
180+ } ) ;
181+ } ,
182+ ) ;
183+
156184 const revokeWhere = ( predicate : ( record : CredentialRecord ) => boolean ) =>
157185 SynchronizedRef . update ( state , ( { records } ) => ( {
158186 records : new Map ( Array . from ( records ) . filter ( ( [ , record ] ) => ! predicate ( record ) ) ) ,
@@ -161,6 +189,7 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
161189 return McpSessionRegistry . of ( {
162190 issue,
163191 resolve,
192+ touch,
164193 revokeProviderSession : Effect . fn ( "McpSessionRegistry.revokeProviderSession" ) (
165194 function * ( providerSessionId ) {
166195 yield * revokeWhere ( ( record ) => record . scope . providerSessionId === providerSessionId ) ;
@@ -202,6 +231,13 @@ export const issueActiveMcpCredential = (
202231 . pipe ( Effect . andThen ( activeMcpSessionRegistry . issue ( request ) ) )
203232 : Effect . sync ( ( ) : McpIssuedCredential | undefined => undefined ) ;
204233
234+ /**
235+ * Refreshes the liveness of a thread's MCP credential. Called on every provider
236+ * turn so an active session is never mistaken for an abandoned one.
237+ */
238+ export const touchActiveMcpThread = ( threadId : ThreadId ) : Effect . Effect < void > =>
239+ activeMcpSessionRegistry ? activeMcpSessionRegistry . touch ( threadId ) : Effect . void ;
240+
205241export const revokeActiveMcpThread = ( threadId : ThreadId ) : Effect . Effect < void > =>
206242 activeMcpSessionRegistry ? activeMcpSessionRegistry . revokeThread ( threadId ) : Effect . void ;
207243
0 commit comments