@@ -39,6 +39,7 @@ import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
3939import * as ServerConfig from "../config.ts" ;
4040import * as EnvironmentAuthPolicy from "./EnvironmentAuthPolicy.ts" ;
4141import * as PairingGrantStore from "./PairingGrantStore.ts" ;
42+ import { decodeMcpApprovalSubject } from "./mcpApprovalCode.ts" ;
4243import * as ServerSecretStore from "./ServerSecretStore.ts" ;
4344import * as SessionStore from "./SessionStore.ts" ;
4445import { REUSABLE_DEV_SESSION_EXPIRES_AT , resolveReusableDevAuth } from "./ReusableDevAuth.ts" ;
@@ -556,6 +557,7 @@ export class EnvironmentAuth extends Context.Service<
556557 readonly issueMcpClientSession : ( input : {
557558 readonly label : string ;
558559 readonly access : AuthMcpClientAccess ;
560+ readonly ttl ?: Duration . Duration ;
559561 readonly client : AuthClientMetadata ;
560562 } ) => Effect . Effect <
561563 { readonly token : string ; readonly expiresAt : DateTime . DateTime } ,
@@ -565,12 +567,16 @@ export class EnvironmentAuth extends Context.Service<
565567 * Spends a one-time pairing code as approval for an MCP client with the
566568 * given access; the code must hold every scope that access grants.
567569 * Proof-bound codes (T3 Connect) are refused without being spent, and
568- * desktop bootstrap grants never qualify.
570+ * desktop bootstrap grants never qualify. A reusable MCP approval code is
571+ * checked without being spent, and its session TTL is returned.
569572 */
570573 readonly consumeMcpApprovalCode : (
571574 code : string ,
572575 access : AuthMcpClientAccess ,
573- ) => Effect . Effect < void , ServerAuthMcpApprovalCodeError | ServerAuthInternalError > ;
576+ ) => Effect . Effect <
577+ { readonly sessionTtl ?: Duration . Duration } ,
578+ ServerAuthMcpApprovalCodeError | ServerAuthInternalError
579+ > ;
574580 readonly authenticateBrowserSession : (
575581 request : HttpServerRequest . HttpServerRequest ,
576582 ) => Effect . Effect < AuthenticatedSession , ServerAuthCredentialError | ServerAuthInternalError > ;
@@ -1287,7 +1293,7 @@ export const make = Effect.gen(function* () {
12871293 subject : MCP_CLIENT_SUBJECT ,
12881294 method : "bearer-access-token" ,
12891295 scopes : mcpClientScopes ( input . access ) ,
1290- ttl : MCP_CLIENT_SESSION_TTL ,
1296+ ttl : input . ttl ?? MCP_CLIENT_SESSION_TTL ,
12911297 ...( input . access === "read-only" ? { } : { runtimeModeCeiling : input . access } ) ,
12921298 client : { ...input . client , label : input . label , deviceType : "bot" } ,
12931299 } )
@@ -1313,11 +1319,36 @@ export const make = Effect.gen(function* () {
13131319 ) ;
13141320 } ;
13151321
1322+ // Fork: MCP approval goes through `useMcpApprovalCode`, which leaves a
1323+ // reusable MCP approval code unspent.
1324+ const resolveMcpApprovalGrant = (
1325+ credential : string ,
1326+ ) : Effect . Effect <
1327+ ResolvedBootstrapGrant ,
1328+ ServerAuthInvalidCredentialError | ServerAuthInternalError | ServerAuthScopeNotGrantedError
1329+ > =>
1330+ devAuth ?. matches ( credential )
1331+ ? resolveBootstrapGrant ( credential )
1332+ : bootstrapCredentials
1333+ . useMcpApprovalCode ( credential )
1334+ . pipe (
1335+ Effect . mapError ( ( cause ) =>
1336+ cause . _tag === "BootstrapCredentialScopeNotGrantedError"
1337+ ? new ServerAuthScopeNotGrantedError ( { } )
1338+ : toBootstrapExchangeError ( cause ) ,
1339+ ) ,
1340+ ) ;
1341+
1342+ const mcpApprovalSessionTtl = ( subject : string ) => {
1343+ const sessionTtl = decodeMcpApprovalSubject ( subject ) ?. sessionTtl ;
1344+ return sessionTtl === undefined ? { } : { sessionTtl } ;
1345+ } ;
1346+
13161347 const consumeMcpApprovalCode : EnvironmentAuth [ "Service" ] [ "consumeMcpApprovalCode" ] = (
13171348 code ,
13181349 access ,
13191350 ) =>
1320- resolveBootstrapGrant ( code . trim ( ) ) . pipe (
1351+ resolveMcpApprovalGrant ( code . trim ( ) ) . pipe (
13211352 Effect . catchTags ( {
13221353 ServerAuthInvalidCredentialError : ( ) =>
13231354 Effect . fail ( new ServerAuthMcpApprovalCodeError ( { reason : "unknown_or_used" } ) ) ,
@@ -1328,7 +1359,7 @@ export const make = Effect.gen(function* () {
13281359 grant . method !== "one-time-token" && grant . method !== "reusable-dev-token"
13291360 ? Effect . fail ( new ServerAuthMcpApprovalCodeError ( { reason : "not_a_pairing_code" } ) )
13301361 : mcpClientScopes ( access ) . every ( ( scope ) => grant . scopes . includes ( scope ) )
1331- ? Effect . void
1362+ ? Effect . succeed ( mcpApprovalSessionTtl ( grant . subject ) )
13321363 : Effect . fail ( new ServerAuthMcpApprovalCodeError ( { reason : "insufficient_scope" } ) ) ,
13331364 ) ,
13341365 Effect . withSpan ( "EnvironmentAuth.consumeMcpApprovalCode" ) ,
0 commit comments