11import crypto from "node:crypto" ;
2+ import type { AuthProfileStore } from "../agents/auth-profiles/types.js" ;
23import type { ExecElevatedDefaults } from "../agents/bash-tools.exec-types.js" ;
34import type { ExecPolicyOverrides , ExecSessionDefaults } from "../agents/exec-defaults.js" ;
45import type { ScheduledToolPolicyContext } from "../agents/scheduled-tool-policy.js" ;
@@ -79,11 +80,24 @@ interface McpLoopbackClientGrant {
7980 readonly context : McpLoopbackRequestContext ;
8081}
8182
83+ type McpLoopbackToolAuth = {
84+ agentDir ?: string ;
85+ store : AuthProfileStore ;
86+ } ;
87+
8288type StoredMcpLoopbackClientGrant = McpLoopbackClientGrant & {
8389 runtimeOwnerToken : string ;
8490 activeCaptureKey ?: string ;
91+ toolAuth ?: McpLoopbackToolAuth ;
92+ } ;
93+
94+ type McpLoopbackClientGrantRevocation = {
95+ token : string ;
96+ runtimeOwnerToken : string ;
8597} ;
8698
99+ const clientGrantRevocationListeners = new Set < ( event : McpLoopbackClientGrantRevocation ) => void > ( ) ;
100+
87101const DEFAULT_TTL_MS = 60 * 60 * 1000 ; // 1h
88102const MAX_TTL_MS = 12 * 60 * 60 * 1000 ;
89103
@@ -171,6 +185,7 @@ function sweepExpiredAttachGrants(nowMs: number = Date.now()): number {
171185export function mintMcpLoopbackClientGrant ( params : {
172186 context : McpLoopbackRequestContext ;
173187 runtimeOwnerToken : string ;
188+ toolAuth ?: McpLoopbackToolAuth ;
174189} ) : McpLoopbackClientGrant {
175190 const sessionKey = params . context . sessionKey . trim ( ) ;
176191 if ( ! sessionKey ) {
@@ -184,6 +199,7 @@ export function mintMcpLoopbackClientGrant(params: {
184199 token : crypto . randomBytes ( 32 ) . toString ( "hex" ) ,
185200 context : structuredClone ( { ...params . context , sessionKey } ) ,
186201 runtimeOwnerToken,
202+ ...( params . toolAuth ? { toolAuth : structuredClone ( params . toolAuth ) } : { } ) ,
187203 } ;
188204 clientGrantsByToken . set ( grant . token , grant ) ;
189205 return structuredClone ( {
@@ -233,7 +249,13 @@ export function resolveMcpLoopbackClientGrant(params: {
233249 token : string ;
234250 runtimeOwnerToken : string ;
235251 captureKey : string ;
236- } ) : { context : McpLoopbackRequestContext ; captureKey : string } | undefined {
252+ } ) :
253+ | {
254+ context : McpLoopbackRequestContext ;
255+ captureKey : string ;
256+ toolAuth ?: McpLoopbackToolAuth ;
257+ }
258+ | undefined {
237259 const grant = clientGrantsByToken . get ( params . token ) ;
238260 if (
239261 ! grant ||
@@ -243,19 +265,41 @@ export function resolveMcpLoopbackClientGrant(params: {
243265 ) {
244266 return undefined ;
245267 }
246- return structuredClone ( { context : grant . context , captureKey : grant . activeCaptureKey } ) ;
268+ // Cached tools and OAuth refreshes must share the prepared store for this
269+ // grant; cloning on each request would discard refreshed credentials.
270+ return {
271+ context : structuredClone ( grant . context ) ,
272+ captureKey : grant . activeCaptureKey ,
273+ ...( grant . toolAuth ? { toolAuth : grant . toolAuth } : { } ) ,
274+ } ;
275+ }
276+
277+ /** Registers cleanup tied to the exact lifetime of loopback client grants. */
278+ export function registerMcpLoopbackClientGrantRevocationListener (
279+ listener : ( event : McpLoopbackClientGrantRevocation ) => void ,
280+ ) : ( ) => void {
281+ clientGrantRevocationListeners . add ( listener ) ;
282+ return ( ) => clientGrantRevocationListeners . delete ( listener ) ;
247283}
248284
249285export function revokeMcpLoopbackClientGrant ( token : string ) : boolean {
250- return clientGrantsByToken . delete ( token ) ;
286+ const grant = clientGrantsByToken . get ( token ) ;
287+ if ( ! grant || ! clientGrantsByToken . delete ( token ) ) {
288+ return false ;
289+ }
290+ // Revocation must also release server-owned projections whose closures retain
291+ // this grant's prepared credentials.
292+ for ( const listener of clientGrantRevocationListeners ) {
293+ listener ( { token, runtimeOwnerToken : grant . runtimeOwnerToken } ) ;
294+ }
295+ return true ;
251296}
252297
253298export function revokeMcpLoopbackClientGrantsForRuntime ( runtimeOwnerToken : string ) : number {
254299 let removed = 0 ;
255300 for ( const [ token , grant ] of clientGrantsByToken ) {
256301 if ( grant . runtimeOwnerToken === runtimeOwnerToken ) {
257- clientGrantsByToken . delete ( token ) ;
258- removed += 1 ;
302+ removed += revokeMcpLoopbackClientGrant ( token ) ? 1 : 0 ;
259303 }
260304 }
261305 return removed ;
0 commit comments