@@ -280,6 +280,7 @@ function resourceMcpLayer(
280280 subscribe ?: Bus . Interface [ "subscribe" ]
281281 environment ?: Layer . Layer < Environment . Service >
282282 published ?: string [ ]
283+ beforeRegistration ?: ( service : Mcp . Interface ) => Effect . Effect < void >
283284 } ,
284285) {
285286 const directory = AbsolutePath . make ( import . meta. dir )
@@ -288,6 +289,7 @@ function resourceMcpLayer(
288289 Effect . gen ( function * ( ) {
289290 const bus = yield * Bus . Service
290291 const mcp = yield * Mcp . Service
292+ if ( overrides ?. beforeRegistration ) yield * overrides . beforeRegistration ( mcp )
291293 yield * State . batch (
292294 Effect . gen ( function * ( ) {
293295 yield * ConfigMcpPlugin . register ( bus . subscribe ( ) )
@@ -587,6 +589,43 @@ testEffect(Layer.empty).effect("does not spawn MCP for an unused location or its
587589 } ) ,
588590)
589591
592+ testEffect ( Layer . empty ) . effect ( "starts first MCP registrations after activation on empty config" , ( ) =>
593+ Effect . gen ( function * ( ) {
594+ const counts = { spawned : 0 , closed : 0 }
595+ yield * Effect . gen ( function * ( ) {
596+ const service = yield * Mcp . Service
597+ yield * advance ( ( ) => counts . spawned === 1 )
598+ yield * Effect . all ( Array . from ( { length : 12 } , ( ) => service . start ( ) ) , { concurrency : "unbounded" } )
599+ expect ( ( yield * service . servers ( ) ) [ 0 ] ?. status . status ) . toBe ( "connected" )
600+ expect ( counts . spawned ) . toBe ( 1 )
601+ yield * TestClock . adjust ( "61 seconds" )
602+ yield * advance ( ( ) => counts . closed === 1 )
603+ expect ( ( yield * service . servers ( ) ) [ 0 ] ?. status . status ) . toBe ( "idle" )
604+ } ) . pipe (
605+ Effect . provide (
606+ resourceMcpLayer (
607+ new ConfigMCP . Local ( {
608+ type : "local" ,
609+ command : [ process . execPath , path . join ( import . meta. dir , "fixture/mcp-prompts.ts" ) ] ,
610+ } ) ,
611+ undefined ,
612+ undefined ,
613+ {
614+ environment : countedEnvironment ( counts ) ,
615+ beforeRegistration : ( service ) =>
616+ Effect . gen ( function * ( ) {
617+ expect ( yield * service . servers ( ) ) . toEqual ( [ ] )
618+ yield * service . start ( )
619+ expect ( counts . spawned ) . toBe ( 0 )
620+ } ) ,
621+ } ,
622+ ) ,
623+ ) ,
624+ )
625+ expect ( counts ) . toEqual ( { spawned : 1 , closed : 1 } )
626+ } ) ,
627+ )
628+
590629testEffect ( Layer . empty ) . effect (
591630 "releases discovery-only MCP scopes, caches metadata, and reacquires for real work" ,
592631 ( ) =>
0 commit comments