@@ -13,6 +13,8 @@ import * as Effect from "effect/Effect";
1313import * as Exit from "effect/Exit" ;
1414import * as Layer from "effect/Layer" ;
1515import * as Schema from "effect/Schema" ;
16+ import * as Sink from "effect/Sink" ;
17+ import * as Stream from "effect/Stream" ;
1618import { McpSchema , McpServer , Tool , Toolkit } from "effect/ai" ;
1719
1820import { OrchestratorProjectionError } from "../orchestration-v2/Orchestrator.ts" ;
@@ -113,8 +115,14 @@ const probe = {
113115} ;
114116const ProbeToolkit = Toolkit . make (
115117 Tool . make ( "reads" , probe ) ,
116- Tool . make ( "reads_as_caller" , probe ) ,
117- Tool . make ( "acts_as_caller" , probe ) ,
118+ Tool . make ( "reads_as_caller" , probe ) . annotate (
119+ McpSchema . EnabledWhen ,
120+ McpInvocationContext . hiddenFromOutsideClients ,
121+ ) ,
122+ Tool . make ( "acts_as_caller" , probe ) . annotate (
123+ McpSchema . EnabledWhen ,
124+ McpInvocationContext . hiddenFromOutsideClients ,
125+ ) ,
118126 Tool . make ( "writes" , probe ) ,
119127 Tool . make ( "writes_with_capability" , probe ) ,
120128 Tool . make ( "writes_threads" , {
@@ -254,15 +262,13 @@ it.effect.each([
254262 [ "writes_environment" , readOnlyClient , { } , "capability_denied" ] ,
255263
256264 [ "reads_as_caller" , ended , { } , "ran" ] ,
257- [ "reads_as_caller" , fullAccessClient , { } , "thread_credential_required" ] ,
258265 [ "acts_as_caller" , supervised , { } , "ran" ] ,
259266 [ "acts_as_caller" , ended , { } , "parent_not_active" ] ,
260267 [ "acts_as_caller" , background , { } , "ran" ] ,
261268 [ "acts_as_caller" , oldProviderBackground , { } , "parent_not_active" ] ,
262269 [ "acts_as_caller" , mismatchedProvider , { } , "parent_not_active" ] ,
263270 [ "acts_as_caller" , archivedBackground , { } , "parent_not_active" ] ,
264271 [ "acts_as_caller" , unknownOwnerBackground , { } , "parent_not_active" ] ,
265- [ "acts_as_caller" , fullAccessClient , { } , "thread_credential_required" ] ,
266272
267273 [ "writes" , supervised , { } , "ran" ] ,
268274 [ "writes" , ended , { } , "parent_not_active" ] ,
@@ -455,7 +461,12 @@ it.effect.each([
455461
456462it . effect ( "refuses a linked environment to a tool that acts as its calling thread" , ( ) =>
457463 Effect . gen ( function * ( ) {
458- const CallerToolkit = Toolkit . make ( Tool . make ( "acts_as_caller" , linkedProbe ) ) ;
464+ const CallerToolkit = Toolkit . make (
465+ Tool . make ( "acts_as_caller" , linkedProbe ) . annotate (
466+ McpSchema . EnabledWhen ,
467+ McpInvocationContext . hiddenFromOutsideClients ,
468+ ) ,
469+ ) ;
459470 const built = yield * Layer . build (
460471 McpToolAccess . HandlersLayer . layer (
461472 McpToolAccess . toLayer ( CallerToolkit , {
@@ -466,3 +477,49 @@ it.effect("refuses a linked environment to a tool that acts as its calling threa
466477 expect ( Exit . isFailure ( built ) && Cause . hasDies ( built . cause ) ) . toBe ( true ) ;
467478 } ) ,
468479) ;
480+
481+ // Discovery hides these tools. The handler must refuse on its own if that ever changes.
482+ it . effect . each ( [ "reads_as_caller" , "acts_as_caller" ] as const ) (
483+ "%s refuses an outside client behind discovery" ,
484+ ( tool ) =>
485+ Effect . gen ( function * ( ) {
486+ const built = yield * ProbeToolkit ;
487+ const { result } = yield * built
488+ . handle ( tool , { } )
489+ . pipe ( Stream . unwrap , Stream . run ( Sink . last ( ) ) , Effect . flatMap ( Effect . fromOption ) ) ;
490+ expect ( result ) . toMatchObject ( { code : "thread_credential_required" } ) ;
491+ } ) . pipe (
492+ Effect . provide ( McpToolAccess . HandlersLayer . layer ( layerProbeHandlers ) ) ,
493+ Effect . provideService ( McpInvocationContext . McpInvocationContext , fullAccessClient ) ,
494+ Effect . provide ( Layer . mock ( ThreadManagement . ThreadManagementService ) ( { } ) ) ,
495+ ) ,
496+ ) ;
497+
498+ it . effect . each ( [ "reads_as_caller" , "acts_as_caller" ] as const ) (
499+ "%s is not found by an outside client" ,
500+ ( tool ) =>
501+ McpServer . McpServer . pipe (
502+ Effect . flatMap ( ( server ) => server . callTool ( { name : tool , arguments : { } } ) ) ,
503+ Effect . flip ,
504+ Effect . map ( ( error ) => expect ( error . message ) . toContain ( `Tool '${ tool } ' not found` ) ) ,
505+ Effect . provideService ( McpInvocationContext . McpInvocationContext , fullAccessClient ) ,
506+ Effect . provideService ( McpSchema . McpServerClient , mcpClient ) ,
507+ Effect . provide ( probeServer ( Layer . mock ( ThreadManagement . ThreadManagementService ) ( { } ) ) ) ,
508+ ) ,
509+ ) ;
510+
511+ it . effect ( "refuses to build a thread-bound tool that outside clients would be offered" , ( ) =>
512+ Effect . gen ( function * ( ) {
513+ const CallerToolkit = Toolkit . make ( Tool . make ( "reads_as_caller" , probe ) ) ;
514+ const built = yield * Layer . build (
515+ McpToolAccess . HandlersLayer . layer (
516+ McpToolAccess . toLayer ( CallerToolkit , {
517+ reads_as_caller : McpToolAccess . readsAsCaller ( ( ) => ran ) ,
518+ } ) ,
519+ ) ,
520+ ) . pipe ( Effect . scoped , Effect . exit ) ;
521+ expect ( Exit . isFailure ( built ) && Cause . pretty ( built . cause ) ) . toContain (
522+ "reads_as_caller acts as its calling thread" ,
523+ ) ;
524+ } ) ,
525+ ) ;
0 commit comments