11import { UrlElicitationRequiredError } from "@modelcontextprotocol/server" ;
22import { RuntimeEnvironmentType } from "@trigger.dev/database" ;
33import type { RbacResource } from "@trigger.dev/plugins" ;
4- import { buildJwtAbility , CAPLESS_USER_ACTOR_SCOPES , type RbacAbility } from "@trigger.dev/rbac" ;
4+ import {
5+ buildJwtAbility ,
6+ CAPLESS_USER_ACTOR_SCOPES ,
7+ type RbacAbility ,
8+ withActionAliases ,
9+ } from "@trigger.dev/rbac" ;
510import { logger } from "~/services/logger.server" ;
611import { resolveAndRecheckUserActorClaims } from "~/services/personalAccessToken.server" ;
712import { MCP_SCOPES , mcpResource } from "~/services/oauth/metadata.server" ;
@@ -89,8 +94,8 @@ const errorText = (text: string): McpToolResult => ({
8994 isError : true ,
9095} ) ;
9196
92- const requiredScopes = ( auth : { action : string ; subject : string } ) => [
93- `${ auth . action } :${ auth . subject } ` ,
97+ const requiredScopes = ( auth : { action : string ; subject : string ; scope ?: string } ) => [
98+ auth . scope ?? `${ auth . action } :${ auth . subject } ` ,
9499] ;
95100
96101export function missingScopes (
@@ -101,9 +106,10 @@ export function missingScopes(
101106 if ( "public" in auth || identity . kind !== "uat" ) return [ ] ;
102107 const cap = identity . cap ?? CAPLESS_USER_ACTOR_SCOPES ;
103108 const scope = requiredScopes ( auth ) [ 0 ] ;
109+ const ability = withActionAliases ( buildJwtAbility ( cap ) ) ;
104110 const allowed = resource
105- ? buildJwtAbility ( cap ) . can ( auth . action , resource )
106- : buildJwtAbility ( cap ) . can ( auth . action , { type : auth . subject } ) ||
111+ ? ability . can ( auth . action , resource )
112+ : ability . can ( auth . action , { type : auth . subject } ) ||
107113 cap . some ( ( s ) => s . startsWith ( `${ scope } :` ) ) ;
108114 return allowed ? [ ] : [ scope ] ;
109115}
@@ -307,6 +313,10 @@ export function withMcpToolAuth<TArgs>(
307313 typeof approval === "object"
308314 ? await approval . precheck ?.( args , authorization . ability , subject )
309315 : undefined ;
316+ if ( precheck && typeof precheck === "object" && "refuse" in precheck ) {
317+ outcome = "forbidden" ;
318+ return errorText ( precheck . refuse ) ;
319+ }
310320 if ( precheck !== undefined ) {
311321 const named = precheck === "forbidden" ? null : namedPermission ( precheck ) ;
312322 if ( ! named && precheck !== "forbidden" ) {
0 commit comments