@@ -63,29 +63,45 @@ func NewClient(impl *Implementation, opts *ClientOptions) *Client {
6363type ClientOptions struct {
6464 // CreateMessageHandler handles incoming requests for sampling/createMessage.
6565 //
66- // Setting CreateMessageHandler to a non-nil value causes the client to
67- // advertise the sampling capability.
66+ // Setting CreateMessageHandler to a non-nil value automatically causes the
67+ // client to advertise the sampling capability, with default value
68+ // &SamplingCapabilities{}. If [ClientOptions.Capabilities] is set and has a
69+ // non nil value for [ClientCapabilities.Sampling], that value overrides the
70+ // inferred capability.
6871 CreateMessageHandler func (context.Context , * CreateMessageRequest ) (* CreateMessageResult , error )
6972 // ElicitationHandler handles incoming requests for elicitation/create.
7073 //
71- // Setting ElicitationHandler to a non-nil value causes the client to
72- // advertise the elicitation capability.
74+ // Setting ElicitationHandler to a non-nil value automatically causes the
75+ // client to advertise the elicitation capability, with default value
76+ // &ElicitationCapabilities{}. If [ClientOptions.Capabilities] is set and has
77+ // a non nil value for [ClientCapabilities.ELicitattion], that value
78+ // overrides the inferred capability.
7379 ElicitationHandler func (context.Context , * ElicitRequest ) (* ElicitResult , error )
74-
75- // Capabilities configures the client's default capabilities.
76- // If non-nil, this overrides the default capabilities (which includes
77- // only roots with listChanged). Features like sampling and elicitation
78- // are automatically added to the capabilities when their handlers are set.
80+ // Capabilities optionally configures the client's initial capabilities,
81+ // before any capabilities are inferred from other configuration.
7982 //
80- // Set to an empty &ClientCapabilities{} to disable all default capabilities,
81- // including roots.
82- // For example, to enable sampling but disable roots:
83+ // If Capabilities is nil, the initial client capabilities defaults to
84+ // {"roots":{"listChanged":true}}, for historical reasons. Setting
85+ // Capabilities to a non-nil value overrides this default. As a special case,
86+ // to work around #607, Capabilities.Roots is ignored: set
87+ // Capabilities.RootsV2 to configure the roots capability. This allows the
88+ // "roots" capability to be disabled entirely.
8389 //
84- // Capabilities: &ClientCapabilities{
85- // Sampling: &SamplingCapabilities{},
86- // }
90+ // For example:
91+ // - To disable the "roots" capability, use &ClientCapabilities{}
92+ // - To configure "roots", but disable "listChanged" notifications, use
93+ // &ClientCapabilities{RootsV2:&RootCapabilities{}}.
8794 //
88- // To configure elicitation modes:
95+ // # Interaction with capability inference
96+ //
97+ // Sampling and elicitation capabilities are automatically added when their
98+ // corresponding handlers are set, with the default value described at
99+ // [ClientOptions.CreateMessageHandler] and
100+ // [ClientOptions.ElicitationHandler]. If the Sampling or Elicitation fields
101+ // are set in the Capabilities field, their values overrides the default
102+ // inferred value.
103+ //
104+ // For example, to to configure elicitation modes:
89105 //
90106 // Capabilities: &ClientCapabilities{
91107 // Elicitation: &ElicitationCapabilities{
@@ -94,7 +110,6 @@ type ClientOptions struct {
94110 // },
95111 // }
96112 Capabilities * ClientCapabilities
97-
98113 // ElicitationCompleteHandler handles incoming notifications for notifications/elicitation/complete.
99114 ElicitationCompleteHandler func (context.Context , * ElicitationCompleteNotificationRequest )
100115 // Handlers for notifications from the server.
@@ -150,7 +165,7 @@ type ClientSessionOptions struct {
150165 protocolVersion string
151166}
152167
153- func (c * Client ) capabilities () * ClientCapabilities {
168+ func (c * Client ) capabilities (protocolVersion string ) * ClientCapabilities {
154169 // Start with user-provided capabilities as defaults, or use SDK defaults.
155170 var caps * ClientCapabilities
156171 if c .opts .Capabilities != nil {
@@ -181,9 +196,11 @@ func (c *Client) capabilities() *ClientCapabilities {
181196 // Augment with elicitation capability if handler is set.
182197 if c .opts .ElicitationHandler != nil {
183198 if caps .Elicitation == nil {
184- // Default to form elicitation for backward compatibility.
185- caps .Elicitation = & ElicitationCapabilities {
186- Form : & FormElicitationCapabilities {},
199+ caps .Elicitation = & ElicitationCapabilities {}
200+ // Form elicitation was added in 2025-11-25; for older versions,
201+ // {} is treated the same as {"form":{}}.
202+ if protocolVersion >= protocolVersion20251125 {
203+ caps .Elicitation .Form = & FormElicitationCapabilities {}
187204 }
188205 }
189206 }
@@ -210,7 +227,7 @@ func (c *Client) Connect(ctx context.Context, t Transport, opts *ClientSessionOp
210227 params := & InitializeParams {
211228 ProtocolVersion : protocolVersion ,
212229 ClientInfo : c .impl ,
213- Capabilities : c .capabilities (),
230+ Capabilities : c .capabilities (protocolVersion ),
214231 }
215232 req := & InitializeRequest {Session : cs , Params : params }
216233 res , err := handleSend [* InitializeResult ](ctx , methodInitialize , req )
0 commit comments