11import {
22 AuthAccessTokenType ,
33 AuthAccessWriteScope ,
4+ AuthMcpClientAccess ,
45 AuthOrchestrationOperateScope ,
56 AuthStandardClientScopes ,
67 type AuthAccessTokenResult ,
@@ -9,7 +10,6 @@ import {
910 type AuthClientSession ,
1011 type AuthCreatePairingCredentialInput ,
1112 type AuthEnvironmentScope ,
12- type AuthMcpClientAccess ,
1313 type AuthPairingLink ,
1414 type AuthPairingCredentialResult ,
1515 type AuthSessionId ,
@@ -48,6 +48,7 @@ import { DESKTOP_RENDERER_ORIGINS } from "../httpCors.ts";
4848import * as SqlitePersistence from "../persistence/Sqlite.ts" ;
4949import * as ManagedLimits from "../policy/managedLimits.ts" ;
5050import * as ManagedPolicy from "../policy/ManagedPolicy.ts" ;
51+ import { MCP_APPROVAL_SUBJECT_PREFIX } from "../persistence/AuthPairingLinks.ts" ;
5152import { requestUrl } from "../requestUrl.ts" ;
5253
5354const DEFAULT_SESSION_SUBJECT = "cli-issued-session" ;
@@ -488,6 +489,7 @@ export class EnvironmentAuth extends Context.Service<
488489 readonly subject ?: string ;
489490 readonly proofKeyThumbprint ?: string ;
490491 readonly purpose ?: "startup" ;
492+ readonly mcpApproval ?: AuthMcpClientAccess ;
491493 } ) => Effect . Effect <
492494 IssuedPairingLink ,
493495 ServerAuthInternalError | ManagedLimits . ManagedPolicyRefusedError
@@ -559,11 +561,17 @@ export class EnvironmentAuth extends Context.Service<
559561 } ,
560562 ServerAuthInternalError
561563 > ;
562- /** Spends a code covering all granted scopes. Rejects desktop grants and leaves refused proof-bound codes unspent. */
564+ /**
565+ * Spends a code covering all granted scopes and returns the level it approves. A level-bound
566+ * code must name `access`. Rejects desktop grants and leaves refused codes unspent.
567+ */
563568 readonly consumeMcpApprovalCode : (
564569 code : string ,
565570 access : AuthMcpClientAccess ,
566- ) => Effect . Effect < void , ServerAuthMcpApprovalCodeError | ServerAuthInternalError > ;
571+ ) => Effect . Effect <
572+ AuthMcpClientAccess ,
573+ ServerAuthMcpApprovalCodeError | ServerAuthInternalError
574+ > ;
567575 readonly authenticateBrowserSession : (
568576 request : HttpServerRequest . HttpServerRequest ,
569577 ) => Effect . Effect < AuthenticatedSession , ServerAuthCredentialError | ServerAuthInternalError > ;
@@ -918,6 +926,8 @@ export const make = Effect.gen(function* () {
918926 readonly proofKeyThumbprint ?: string ;
919927 readonly requestedScopes ?: ReadonlyArray < AuthEnvironmentScope > ;
920928 readonly requiredScopes ?: ReadonlyArray < AuthEnvironmentScope > ;
929+ /** Only an agent sign-in opts in to spending a code issued for it. */
930+ readonly mcpApproval ?: AuthMcpClientAccess ;
921931 } ,
922932 ) : Effect . Effect <
923933 ResolvedBootstrapGrant ,
@@ -1032,6 +1042,7 @@ export const make = Effect.gen(function* () {
10321042 ...( input ?. label ? { label : input . label } : { } ) ,
10331043 ...( input ?. proofKeyThumbprint ? { proofKeyThumbprint : input . proofKeyThumbprint } : { } ) ,
10341044 ...( input ?. purpose ? { purpose : input . purpose } : { } ) ,
1045+ ...( input ?. mcpApproval ? { mcpApproval : input . mcpApproval } : { } ) ,
10351046 } ) ;
10361047 return {
10371048 id : issued . id ,
@@ -1066,7 +1077,11 @@ export const make = Effect.gen(function* () {
10661077 INTERNAL_ADMINISTRATIVE_BOOTSTRAP_SUBJECT ,
10671078 ] ;
10681079 return pairingLinks
1069- . filter ( ( pairingLink ) => ! excludedSubjects . includes ( pairingLink . subject ) )
1080+ . filter (
1081+ ( pairingLink ) =>
1082+ ! excludedSubjects . includes ( pairingLink . subject ) &&
1083+ ! pairingLink . subject . startsWith ( MCP_APPROVAL_SUBJECT_PREFIX ) ,
1084+ )
10701085 . map ( ( link ) => ( { ...link , ...authScopeResponse ( link . scopes ) } ) )
10711086 . toSorted (
10721087 ( left , right ) => right . createdAt . epochMilliseconds - left . createdAt . epochMilliseconds ,
@@ -1132,11 +1147,17 @@ export const make = Effect.gen(function* () {
11321147
11331148 const issuePairingCredential : EnvironmentAuth [ "Service" ] [ "issuePairingCredential" ] = ( input ) =>
11341149 createPairingLink ( {
1135- scopes : input ?. scopes ?? AuthStandardClientScopes ,
1150+ scopes : input ?. access
1151+ ? mcpClientScopes ( input . access )
1152+ : ( input ?. scopes ?? AuthStandardClientScopes ) ,
11361153 subject : "one-time-token" ,
11371154 ...( input ?. label ? { label : input . label } : { } ) ,
1155+ ...( input ?. access ? { mcpApproval : input . access } : { } ) ,
11381156 } ) . pipe (
1139- Effect . map ( toPairingCredential ) ,
1157+ Effect . map ( ( issued ) => ( {
1158+ ...toPairingCredential ( issued ) ,
1159+ ...( input ?. access ? { access : input . access } : { } ) ,
1160+ } ) ) ,
11401161 Effect . withSpan ( "EnvironmentAuth.issuePairingCredential" ) ,
11411162 ) ;
11421163
@@ -1361,7 +1382,10 @@ export const make = Effect.gen(function* () {
13611382 code ,
13621383 access ,
13631384 ) =>
1364- resolveBootstrapGrant ( code . trim ( ) , { requiredScopes : mcpClientScopes ( access ) } ) . pipe (
1385+ resolveBootstrapGrant ( code . trim ( ) , {
1386+ requiredScopes : mcpClientScopes ( access ) ,
1387+ mcpApproval : access ,
1388+ } ) . pipe (
13651389 Effect . catchTags ( {
13661390 ServerAuthInvalidCredentialError : ( ) =>
13671391 Effect . fail ( new ServerAuthMcpApprovalCodeError ( { reason : "unknown_or_used" } ) ) ,
@@ -1371,7 +1395,7 @@ export const make = Effect.gen(function* () {
13711395 Effect . flatMap ( ( grant ) =>
13721396 grant . method !== "one-time-token" && grant . method !== "reusable-dev-token"
13731397 ? Effect . fail ( new ServerAuthMcpApprovalCodeError ( { reason : "not_a_pairing_code" } ) )
1374- : Effect . void ,
1398+ : Effect . succeed ( mcpApprovalLevel ( grant . subject ) ?? access ) ,
13751399 ) ,
13761400 Effect . withSpan ( "EnvironmentAuth.consumeMcpApprovalCode" ) ,
13771401 ) ;
@@ -1410,6 +1434,16 @@ export const make = Effect.gen(function* () {
14101434 } ) ;
14111435} ) ;
14121436
1437+ const isMcpClientAccess = Schema . is ( AuthMcpClientAccess ) ;
1438+
1439+ /** The level an approval code's subject names, or undefined for any other code. */
1440+ const mcpApprovalLevel = ( subject : string ) : AuthMcpClientAccess | undefined => {
1441+ const level = subject . startsWith ( MCP_APPROVAL_SUBJECT_PREFIX )
1442+ ? subject . slice ( MCP_APPROVAL_SUBJECT_PREFIX . length )
1443+ : undefined ;
1444+ return isMcpClientAccess ( level ) ? level : undefined ;
1445+ } ;
1446+
14131447/** Subjects of sessions and links a pairing link created, keyed by how the client paired. */
14141448const LINK_PAIRED_SUBJECTS = { link : "one-time-token" , connect : "cloud-connect" } as const ;
14151449
@@ -1440,8 +1474,11 @@ const revokePairedClientsThePolicyDisallows = Effect.fn(
14401474 session . client . ipAddress !== undefined &&
14411475 ! isLocalLoopbackHost ( session . client . ipAddress ) ) ,
14421476 ) ;
1443- const links = ( yield * stores . pairingLinks . listActive ( ) ) . filter ( ( link ) =>
1444- disallowed . has ( link . subject ) ,
1477+ const links = ( yield * stores . pairingLinks . listActive ( ) ) . filter (
1478+ ( link ) =>
1479+ disallowed . has ( link . subject ) ||
1480+ // Approval codes let an agent elsewhere sign in, as plain codes do.
1481+ ( ! remoteAllowed && link . subject . startsWith ( MCP_APPROVAL_SUBJECT_PREFIX ) ) ,
14451482 ) ;
14461483 yield * Effect . forEach ( sessions , ( session ) => stores . sessions . revoke ( session . sessionId ) , {
14471484 discard : true ,
0 commit comments