@@ -407,6 +407,11 @@ func fileResourceHandler(dir string) ResourceHandler {
407407//
408408// Run blocks until the client terminates the connection or the provided
409409// context is cancelled. If the context is cancelled, Run closes the connection.
410+ //
411+ // If tools have been added to the server before this call, then the server will
412+ // advertise the capability for tools, including the ability to send list-changed notifications.
413+ // If no tools have been added, the server will not have the tool capability.
414+ // The same goes for other features like prompts and resources.
410415func (s * Server ) Run (ctx context.Context , t Transport ) error {
411416 ss , err := s .Connect (ctx , t )
412417 if err != nil {
@@ -655,24 +660,31 @@ func (ss *ServerSession) initialize(ctx context.Context, params *InitializeParam
655660 version = latestProtocolVersion
656661 }
657662
663+ caps := & serverCapabilities {
664+ Completions : & completionCapabilities {},
665+ Logging : & loggingCapabilities {},
666+ }
667+ ss .server .mu .Lock ()
668+ hasTools := ss .server .tools .len () > 0
669+ hasPrompts := ss .server .prompts .len () > 0
670+ hasResources := ss .server .resources .len () > 0
671+ ss .server .mu .Unlock ()
672+ if hasTools {
673+ caps .Tools = & toolCapabilities {ListChanged : true }
674+ }
675+ if hasPrompts {
676+ caps .Prompts = & promptCapabilities {ListChanged : true }
677+ }
678+ if hasResources {
679+ caps .Resources = & resourceCapabilities {ListChanged : true }
680+ }
681+
658682 return & InitializeResult {
659683 // TODO(rfindley): alter behavior when falling back to an older version:
660684 // reject unsupported features.
661685 ProtocolVersion : version ,
662- Capabilities : & serverCapabilities {
663- Completions : & completionCapabilities {},
664- Prompts : & promptCapabilities {
665- ListChanged : true ,
666- },
667- Tools : & toolCapabilities {
668- ListChanged : true ,
669- },
670- Resources : & resourceCapabilities {
671- ListChanged : true ,
672- },
673- Logging : & loggingCapabilities {},
674- },
675- Instructions : ss .server .opts .Instructions ,
686+ Capabilities : caps ,
687+ Instructions : ss .server .opts .Instructions ,
676688 ServerInfo : & implementation {
677689 Name : ss .server .name ,
678690 Version : ss .server .version ,
0 commit comments