@@ -20,6 +20,7 @@ import {
2020 isAgentHarnessSessionStoreEntryProtected ,
2121} from "../sessions/agent-harness-session-key.js" ;
2222import {
23+ registerMcpLoopbackClientGrantRevocationListener ,
2324 resolveMcpLoopbackClientGrant ,
2425 revokeMcpLoopbackClientGrantsForRuntime ,
2526} from "./mcp-grant-store.js" ;
@@ -247,9 +248,22 @@ async function startMcpLoopbackServer(port = 0): Promise<{
247248 } ) ;
248249 } ) ;
249250 markMcpLoopbackRequestClassified ( cliRequestCaptureHandle ) ;
251+ const { boundGrantToken, boundCaptureKey } = auth ;
252+ const activeBoundGrant =
253+ boundGrantToken && boundCaptureKey
254+ ? resolveMcpLoopbackClientGrant ( {
255+ token : boundGrantToken ,
256+ runtimeOwnerToken : ownerToken ,
257+ captureKey : boundCaptureKey ,
258+ } )
259+ : undefined ;
260+ if ( boundGrantToken && ! activeBoundGrant ) {
261+ res . writeHead ( 401 , { "Content-Type" : "application/json" } ) ;
262+ res . end ( JSON . stringify ( { error : "unauthorized" } ) ) ;
263+ return ;
264+ }
250265 const cfg = getRuntimeConfig ( ) ;
251266 const requestContext = resolveMcpRequestContext ( req , cfg , auth ) ;
252- const { boundGrantToken, boundCaptureKey } = auth ;
253267 const authorizeToolCall =
254268 boundGrantToken && boundCaptureKey
255269 ? ( ) =>
@@ -300,15 +314,15 @@ async function startMcpLoopbackServer(port = 0): Promise<{
300314 cwd : requestContext . cwd ,
301315 modelProvider : requestContext . modelProvider ,
302316 modelId : requestContext . modelId ,
303- ...( auth . boundToolAuth
317+ ...( activeBoundGrant ?. toolAuth
304318 ? {
305- authProfileStore : auth . boundToolAuth . store ,
306- ...( auth . boundToolAuth . agentDir
307- ? { authProfileStoreAgentDir : auth . boundToolAuth . agentDir }
319+ authProfileStore : activeBoundGrant . toolAuth . store ,
320+ ...( activeBoundGrant . toolAuth . agentDir
321+ ? { authProfileStoreAgentDir : activeBoundGrant . toolAuth . agentDir }
308322 : { } ) ,
309- ...( boundGrantToken ? { authProfileStoreCacheKey : boundGrantToken } : { } ) ,
310323 }
311324 : { } ) ,
325+ ...( boundGrantToken ? { grantToken : boundGrantToken } : { } ) ,
312326 yieldContextCacheKey : yieldContext ?. cacheKey ,
313327 onYield : yieldContext ?. onYield ,
314328 messageProvider : requestContext . messageProvider ,
@@ -485,6 +499,11 @@ async function startMcpLoopbackServer(port = 0): Promise<{
485499 if ( ! address || typeof address === "string" ) {
486500 throw new Error ( "mcp loopback did not bind to a TCP port" ) ;
487501 }
502+ const unregisterGrantRevocation = registerMcpLoopbackClientGrantRevocationListener ( ( event ) => {
503+ if ( event . runtimeOwnerToken === ownerToken ) {
504+ toolCache . evictGrant ( event . token ) ;
505+ }
506+ } ) ;
488507 // Register tokens only after the TCP listener is live so clients never learn
489508 // a bearer token for a server that failed to bind.
490509 setActiveMcpLoopbackRuntime ( { port : address . port , ownerToken, nonOwnerToken } ) ;
@@ -497,6 +516,8 @@ async function startMcpLoopbackServer(port = 0): Promise<{
497516 // requests. A delayed old-server close cannot revoke a successor runtime.
498517 clearActiveMcpLoopbackRuntimeByOwnerToken ( ownerToken ) ;
499518 revokeMcpLoopbackClientGrantsForRuntime ( ownerToken ) ;
519+ unregisterGrantRevocation ( ) ;
520+ toolCache . clear ( ) ;
500521 return new Promise < void > ( ( resolve , reject ) => {
501522 httpServer . close ( ( error ) => {
502523 if ( ! error ) {
0 commit comments