@@ -140,6 +140,7 @@ interface CreateResult {
140140 mcpClient ?: MCPClient
141141 status : Status
142142 defs ?: MCPToolDef [ ]
143+ instructions ?: string
143144}
144145
145146interface AuthResult {
@@ -155,11 +156,19 @@ interface State {
155156 status : Record < string , Status >
156157 clients : Record < string , MCPClient >
157158 defs : Record < string , MCPToolDef [ ] >
159+ instructions : Record < string , string >
160+ }
161+
162+ export interface ServerInstructions {
163+ name : string
164+ instructions : string
165+ tools : string [ ]
158166}
159167
160168export interface Interface {
161169 readonly status : ( ) => Effect . Effect < Record < string , Status > >
162170 readonly clients : ( ) => Effect . Effect < Record < string , MCPClient > >
171+ readonly instructions : ( ) => Effect . Effect < ServerInstructions [ ] >
163172 readonly tools : ( ) => Effect . Effect < Record < string , Tool > >
164173 readonly prompts : ( ) => Effect . Effect < Record < string , PromptInfo & { client : string } > >
165174 readonly resources : ( clientName ?: string ) => Effect . Effect < Record < string , ResourceInfo & { client : string } > >
@@ -383,7 +392,7 @@ export const layer = Layer.effect(
383392 if ( ! listed ) {
384393 return yield * Effect . fail ( new Error ( "Failed to get tools" ) )
385394 }
386- return { mcpClient, status, defs : listed } satisfies CreateResult
395+ return { mcpClient, status, defs : listed , instructions : mcpClient . getInstructions ( ) ?. trim ( ) } satisfies CreateResult
387396 } ) . pipe (
388397 Effect . catchCause ( ( cause ) =>
389398 Effect . tryPromise ( ( ) => mcpClient . close ( ) ) . pipe ( Effect . ignore , Effect . andThen ( Effect . failCause ( cause ) ) ) ,
@@ -430,6 +439,7 @@ export const layer = Layer.effect(
430439 if ( s . clients [ name ] !== client ) return
431440 delete s . clients [ name ]
432441 delete s . defs [ name ]
442+ delete s . instructions [ name ]
433443 s . status [ name ] = { status : "failed" , error : "Connection closed" }
434444 bridge . fork (
435445 Effect . logWarning ( "MCP connection closed" , { server : name } ) . pipe (
@@ -484,6 +494,7 @@ export const layer = Layer.effect(
484494 status : { } ,
485495 clients : { } ,
486496 defs : { } ,
497+ instructions : { } ,
487498 }
488499
489500 yield * Effect . forEach (
@@ -505,6 +516,7 @@ export const layer = Layer.effect(
505516 if ( result . mcpClient ) {
506517 s . clients [ key ] = result . mcpClient
507518 s . defs [ key ] = result . defs !
519+ if ( result . instructions ) s . instructions [ key ] = result . instructions
508520 watch ( s , key , result . mcpClient , bridge , mcp . timeout )
509521 }
510522 } ) ,
@@ -516,6 +528,7 @@ export const layer = Layer.effect(
516528 const clients = Object . values ( s . clients )
517529 s . clients = { }
518530 s . defs = { }
531+ s . instructions = { }
519532 yield * Effect . forEach (
520533 clients ,
521534 ( client ) =>
@@ -545,6 +558,7 @@ export const layer = Layer.effect(
545558 const client = s . clients [ name ]
546559 delete s . clients [ name ]
547560 delete s . defs [ name ]
561+ delete s . instructions [ name ]
548562 if ( ! client ) return Effect . void
549563 return Effect . tryPromise ( ( ) => client . close ( ) ) . pipe ( Effect . ignore )
550564 }
@@ -554,13 +568,16 @@ export const layer = Layer.effect(
554568 name : string ,
555569 client : MCPClient ,
556570 listed : MCPToolDef [ ] ,
571+ instructions : string | undefined ,
557572 timeout ?: number ,
558573 ) {
559574 const bridge = yield * EffectBridge . make ( )
560575 const previous = s . clients [ name ]
561576 s . status [ name ] = { status : "connected" }
562577 s . clients [ name ] = client
563578 s . defs [ name ] = listed
579+ if ( instructions ) s . instructions [ name ] = instructions
580+ else delete s . instructions [ name ]
564581 watch ( s , name , client , bridge , timeout )
565582 if ( previous ) yield * Effect . tryPromise ( ( ) => previous . close ( ) ) . pipe ( Effect . ignore )
566583 return s . status [ name ]
@@ -590,6 +607,18 @@ export const layer = Layer.effect(
590607 return s . clients
591608 } )
592609
610+ const instructions = Effect . fn ( "MCP.instructions" ) ( function * ( ) {
611+ const s = yield * InstanceState . get ( state )
612+ return Object . entries ( s . instructions )
613+ . filter ( ( [ name ] ) => s . status [ name ] ?. status === "connected" )
614+ . sort ( ( [ a ] , [ b ] ) => a . localeCompare ( b ) )
615+ . map ( ( [ name , item ] ) => ( {
616+ name,
617+ instructions : item ,
618+ tools : ( s . defs [ name ] ?? [ ] ) . map ( ( tool ) => McpCatalog . toolName ( name , tool . name ) ) ,
619+ } ) )
620+ } )
621+
593622 const createAndStore = Effect . fn ( "MCP.createAndStore" ) ( function * ( name : string , mcp : ConfigMCPV1 . Info ) {
594623 const s = yield * InstanceState . get ( state )
595624 const result = yield * create ( name , mcp )
@@ -601,7 +630,7 @@ export const layer = Layer.effect(
601630 return result . status
602631 }
603632
604- return yield * storeClient ( s , name , result . mcpClient , result . defs ! , mcp . timeout )
633+ return yield * storeClient ( s , name , result . mcpClient , result . defs ! , result . instructions , mcp . timeout )
605634 } )
606635
607636 const add = Effect . fn ( "MCP.add" ) ( function * ( name : string , mcp : ConfigMCPV1 . Info ) {
@@ -647,7 +676,7 @@ export const layer = Layer.effect(
647676 }
648677 const timeout = requestTimeout ( s , clientName , mcpConfig , defaultTimeout )
649678 for ( const mcpTool of listed ) {
650- const key = McpCatalog . sanitize ( clientName ) + "_" + McpCatalog . sanitize ( mcpTool . name )
679+ const key = McpCatalog . toolName ( clientName , mcpTool . name )
651680 result [ key ] = McpCatalog . convertTool ( mcpTool , client , timeout )
652681 }
653682 }
@@ -855,7 +884,7 @@ export const layer = Layer.effect(
855884
856885 const s = yield * InstanceState . get ( state )
857886 yield * auth . clearOAuthState ( mcpName )
858- return yield * storeClient ( s , mcpName , client , listed , mcpConfig . timeout )
887+ return yield * storeClient ( s , mcpName , client , listed , client . getInstructions ( ) ?. trim ( ) , mcpConfig . timeout )
859888 }
860889
861890 const callbackPromise = McpOAuthCallback . waitForCallback ( result . oauthState , mcpName )
@@ -942,6 +971,7 @@ export const layer = Layer.effect(
942971 return Service . of ( {
943972 status,
944973 clients,
974+ instructions,
945975 tools,
946976 prompts,
947977 resources,
0 commit comments