@@ -405,6 +405,11 @@ func fileResourceHandler(dir string) ResourceHandler {
405405// Run runs the server over the given transport, which must be persistent.
406406//
407407// Run blocks until the client terminates the connection.
408+ //
409+ // If tools have been added to the server before this call, then the server will
410+ // advertise the capability for tools, including the ability to send list-changed notifications.
411+ // If no tools have been added, the server will not have the tool capability.
412+ // The same goes for other features like prompts and resources.
408413func (s * Server ) Run (ctx context.Context , t Transport ) error {
409414 ss , err := s .Connect (ctx , t )
410415 if err != nil {
@@ -640,24 +645,31 @@ func (ss *ServerSession) initialize(ctx context.Context, params *InitializeParam
640645 version = v
641646 }
642647
648+ caps := & serverCapabilities {
649+ Completions : & completionCapabilities {},
650+ Logging : & loggingCapabilities {},
651+ }
652+ ss .server .mu .Lock ()
653+ hasTools := ss .server .tools .len () > 0
654+ hasPrompts := ss .server .prompts .len () > 0
655+ hasResources := ss .server .resources .len () > 0
656+ ss .server .mu .Unlock ()
657+ if hasTools {
658+ caps .Tools = & toolCapabilities {ListChanged : true }
659+ }
660+ if hasPrompts {
661+ caps .Prompts = & promptCapabilities {ListChanged : true }
662+ }
663+ if hasResources {
664+ caps .Resources = & resourceCapabilities {ListChanged : true }
665+ }
666+
643667 return & InitializeResult {
644668 // TODO(rfindley): alter behavior when falling back to an older version:
645669 // reject unsupported features.
646670 ProtocolVersion : version ,
647- Capabilities : & serverCapabilities {
648- Completions : & completionCapabilities {},
649- Prompts : & promptCapabilities {
650- ListChanged : true ,
651- },
652- Tools : & toolCapabilities {
653- ListChanged : true ,
654- },
655- Resources : & resourceCapabilities {
656- ListChanged : true ,
657- },
658- Logging : & loggingCapabilities {},
659- },
660- Instructions : ss .server .opts .Instructions ,
671+ Capabilities : caps ,
672+ Instructions : ss .server .opts .Instructions ,
661673 ServerInfo : & implementation {
662674 Name : ss .server .name ,
663675 Version : ss .server .version ,
0 commit comments