11import { AuditAction , AuditResourceType } from '@sim/audit'
2- import { resolvePrincipalSubject } from '@sim/auth/principal'
2+ import { resolvePrincipalSubject , resolvePrincipalSubjectUserId } from '@sim/auth/principal'
33import { defineAuthorizedWorkspaceUseCase } from '@/lib/core/application'
44import { OrchestrationError } from '@/lib/core/orchestration/types'
55import { requireCredentialGroupCredentialAccess } from '@/lib/credential-groups/application/authorization'
@@ -22,6 +22,7 @@ import {
2222 loadMcpOperationAccess ,
2323 requireMcpOperationAccess ,
2424} from '@/lib/mcp/application/operation-access'
25+ import { createMcpToolPresentation } from '@/lib/mcp/application/presentation'
2526import {
2627 isMcpToolVisible ,
2728 matchesMcpAppOrigin ,
@@ -30,6 +31,7 @@ import {
3031import { mcpService } from '@/lib/mcp/service'
3132import type { McpTool , McpToolCall , McpToolSchema } from '@/lib/mcp/types'
3233import { assertWorkspaceCapability } from '@/lib/permission-groups/capability-assertions'
34+ import type { ResolvedSecretTraceProvenanceV1 } from '@/executor/utils/resolved-secret-trace-registry'
3335
3436export interface ExecuteManagedMcpToolInput {
3537 workspaceId : string
@@ -42,6 +44,7 @@ export interface ExecuteManagedMcpToolInput {
4244 signal ?: AbortSignal
4345 includePresentation ?: boolean
4446 appOrigin ?: { toolName : string ; resourceUri : string }
47+ onResolvedSecretTraceProvenance ?: ( provenance : ResolvedSecretTraceProvenanceV1 ) => void
4548}
4649
4750function requireToolSchema ( value : unknown ) : McpToolSchema {
@@ -78,6 +81,7 @@ export const executeManagedMcpToolUseCase = defineAuthorizedWorkspaceUseCase({
7881 } ,
7982 async execute ( { principal, input, context } ) : Promise < ExecuteMcpToolResult > {
8083 input . signal ?. throwIfAborted ( )
84+ const userId = resolvePrincipalSubjectUserId ( principal )
8185 const runtime = await loadManagedMcpRuntimeCredential ( context . credentialId , context . workspaceId )
8286 if (
8387 runtime . mcpServerId !== context . mcpServerId ||
@@ -103,7 +107,11 @@ export const executeManagedMcpToolUseCase = defineAuthorizedWorkspaceUseCase({
103107 loadProvider : ( ) => loadManagedMcpAuthProvider ( runtime . credentialId , runtime . workspaceId ) ,
104108 } ,
105109 input . signal ,
106- { requireComplete : true }
110+ {
111+ requireComplete : true ,
112+ provenanceScope : userId ? { userId, workspaceId : context . workspaceId } : undefined ,
113+ onResolvedSecretTraceProvenance : input . onResolvedSecretTraceProvenance ,
114+ }
107115 )
108116 await saveManagedMcpToolSnapshot (
109117 runtime . credentialId ,
@@ -160,6 +168,9 @@ export const executeManagedMcpToolUseCase = defineAuthorizedWorkspaceUseCase({
160168 credentialOperations . useManagedMcp . resourcePolicy
161169 )
162170 const providerResult = await mcpService . executeManagedMcpTool ( {
171+ userId,
172+ workspaceId : context . workspaceId ,
173+ onResolvedSecretTraceProvenance : input . onResolvedSecretTraceProvenance ,
163174 connectionId : runtime . credentialId ,
164175 serverId : runtime . mcpServerId ,
165176 scope : runtime . scope ,
@@ -171,8 +182,26 @@ export const executeManagedMcpToolUseCase = defineAuthorizedWorkspaceUseCase({
171182 } )
172183 input . signal ?. throwIfAborted ( )
173184 const result = transformToolResult ( providerResult )
174- if ( input . includePresentation )
175- result . presentation = { tool, result : providerResult , arguments : args }
185+ if ( input . includePresentation && userId )
186+ result . presentation = await createMcpToolPresentation (
187+ { tool, result : providerResult , arguments : args } ,
188+ ( uri , signal ) =>
189+ mcpService . readResource ( {
190+ serverId : current . mcpServerId ,
191+ workspaceId : context . workspaceId ,
192+ userId,
193+ uri,
194+ signal,
195+ onResolvedSecretTraceProvenance : input . onResolvedSecretTraceProvenance ,
196+ managed : {
197+ connectionId : context . credentialId ,
198+ scope : current . scope ,
199+ loadAuthProvider : ( ) =>
200+ loadManagedMcpAuthProvider ( context . credentialId , context . workspaceId ) ,
201+ } ,
202+ } ) ,
203+ input . signal
204+ )
176205 return result
177206 } ,
178207 projectAudit : ( { input, context } ) => ( {
0 commit comments