@@ -163,27 +163,39 @@ type toolCapabilities struct {
163
163
// WithResourceCapabilities configures resource-related server capabilities
164
164
func WithResourceCapabilities (subscribe , listChanged bool ) ServerOption {
165
165
return func (s * MCPServer ) {
166
- s .capabilities .resources = & resourceCapabilities {
167
- subscribe : subscribe ,
168
- listChanged : listChanged ,
166
+ if ! subscribe && ! listChanged {
167
+ s .capabilities .resources = nil
168
+ } else {
169
+ s .capabilities .resources = & resourceCapabilities {
170
+ subscribe : subscribe ,
171
+ listChanged : listChanged ,
172
+ }
169
173
}
170
174
}
171
175
}
172
176
173
177
// WithPromptCapabilities configures prompt-related server capabilities
174
178
func WithPromptCapabilities (listChanged bool ) ServerOption {
175
179
return func (s * MCPServer ) {
176
- s .capabilities .prompts = & promptCapabilities {
177
- listChanged : listChanged ,
180
+ if ! listChanged {
181
+ s .capabilities .prompts = nil
182
+ } else {
183
+ s .capabilities .prompts = & promptCapabilities {
184
+ listChanged : listChanged ,
185
+ }
178
186
}
179
187
}
180
188
}
181
189
182
190
// WithToolCapabilities configures tool-related server capabilities
183
191
func WithToolCapabilities (listChanged bool ) ServerOption {
184
192
return func (s * MCPServer ) {
185
- s .capabilities .tools = & toolCapabilities {
186
- listChanged : listChanged ,
193
+ if ! listChanged {
194
+ s .capabilities .tools = nil
195
+ } else {
196
+ s .capabilities .tools = & toolCapabilities {
197
+ listChanged : listChanged ,
198
+ }
187
199
}
188
200
}
189
201
}
@@ -211,9 +223,9 @@ func NewMCPServer(
211
223
notificationHandlers : make (map [string ]NotificationHandlerFunc ),
212
224
notifications : make (chan ServerNotification , 100 ),
213
225
capabilities : serverCapabilities {
214
- tools : & toolCapabilities {} ,
215
- resources : & resourceCapabilities {} ,
216
- prompts : & promptCapabilities {} ,
226
+ tools : nil ,
227
+ resources : nil ,
228
+ prompts : nil ,
217
229
logging : false ,
218
230
},
219
231
}
@@ -325,7 +337,7 @@ func (s *MCPServer) HandleMessage(
325
337
}
326
338
return s .handleListResourceTemplates (ctx , baseMessage .ID , request )
327
339
case "resources/read" :
328
- if ! s .capabilities .resources . listChanged {
340
+ if s .capabilities .resources == nil {
329
341
return createErrorResponse (
330
342
baseMessage .ID ,
331
343
mcp .METHOD_NOT_FOUND ,
@@ -359,7 +371,7 @@ func (s *MCPServer) HandleMessage(
359
371
}
360
372
return s .handleListPrompts (ctx , baseMessage .ID , request )
361
373
case "prompts/get" :
362
- if ! s .capabilities .prompts . listChanged {
374
+ if s .capabilities .prompts == nil {
363
375
return createErrorResponse (
364
376
baseMessage .ID ,
365
377
mcp .METHOD_NOT_FOUND ,
@@ -376,7 +388,7 @@ func (s *MCPServer) HandleMessage(
376
388
}
377
389
return s .handleGetPrompt (ctx , baseMessage .ID , request )
378
390
case "tools/list" :
379
- if len ( s . tools ) == 0 {
391
+ if s . capabilities . tools == nil {
380
392
return createErrorResponse (
381
393
baseMessage .ID ,
382
394
mcp .METHOD_NOT_FOUND ,
@@ -393,7 +405,7 @@ func (s *MCPServer) HandleMessage(
393
405
}
394
406
return s .handleListTools (ctx , baseMessage .ID , request )
395
407
case "tools/call" :
396
- if ! s .capabilities .tools . listChanged || len ( s . tools ) == 0 {
408
+ if s .capabilities .tools == nil {
397
409
return createErrorResponse (
398
410
baseMessage .ID ,
399
411
mcp .METHOD_NOT_FOUND ,
@@ -424,7 +436,7 @@ func (s *MCPServer) AddResource(
424
436
handler ResourceHandlerFunc ,
425
437
) {
426
438
if s .capabilities .resources == nil {
427
- panic ( "Resource capabilities not enabled" )
439
+ s . capabilities . resources = & resourceCapabilities {}
428
440
}
429
441
s .mu .Lock ()
430
442
defer s .mu .Unlock ()
@@ -440,7 +452,7 @@ func (s *MCPServer) AddResourceTemplate(
440
452
handler ResourceTemplateHandlerFunc ,
441
453
) {
442
454
if s .capabilities .resources == nil {
443
- panic ( "Resource capabilities not enabled" )
455
+ s . capabilities . resources = & resourceCapabilities {}
444
456
}
445
457
s .mu .Lock ()
446
458
defer s .mu .Unlock ()
@@ -453,7 +465,7 @@ func (s *MCPServer) AddResourceTemplate(
453
465
// AddPrompt registers a new prompt handler with the given name
454
466
func (s * MCPServer ) AddPrompt (prompt mcp.Prompt , handler PromptHandlerFunc ) {
455
467
if s .capabilities .prompts == nil {
456
- panic ( "Prompt capabilities not enabled" )
468
+ s . capabilities . prompts = & promptCapabilities {}
457
469
}
458
470
s .mu .Lock ()
459
471
defer s .mu .Unlock ()
@@ -468,6 +480,9 @@ func (s *MCPServer) AddTool(tool mcp.Tool, handler ToolHandlerFunc) {
468
480
469
481
// AddTools registers multiple tools at once
470
482
func (s * MCPServer ) AddTools (tools ... ServerTool ) {
483
+ if s .capabilities .tools == nil {
484
+ s .capabilities .tools = & toolCapabilities {}
485
+ }
471
486
s .mu .Lock ()
472
487
for _ , entry := range tools {
473
488
s .tools [entry .Tool .Name ] = entry
@@ -525,24 +540,33 @@ func (s *MCPServer) handleInitialize(
525
540
) mcp.JSONRPCMessage {
526
541
capabilities := mcp.ServerCapabilities {}
527
542
528
- capabilities .Resources = & struct {
529
- Subscribe bool `json:"subscribe,omitempty"`
530
- ListChanged bool `json:"listChanged,omitempty"`
531
- }{
532
- Subscribe : s .capabilities .resources .subscribe ,
533
- ListChanged : s .capabilities .resources .listChanged ,
543
+ // Only add resource capabilities if they're configured
544
+ if s .capabilities .resources != nil {
545
+ capabilities .Resources = & struct {
546
+ Subscribe bool `json:"subscribe,omitempty"`
547
+ ListChanged bool `json:"listChanged,omitempty"`
548
+ }{
549
+ Subscribe : s .capabilities .resources .subscribe ,
550
+ ListChanged : s .capabilities .resources .listChanged ,
551
+ }
534
552
}
535
553
536
- capabilities .Prompts = & struct {
537
- ListChanged bool `json:"listChanged,omitempty"`
538
- }{
539
- ListChanged : s .capabilities .prompts .listChanged ,
554
+ // Only add prompt capabilities if they're configured
555
+ if s .capabilities .prompts != nil {
556
+ capabilities .Prompts = & struct {
557
+ ListChanged bool `json:"listChanged,omitempty"`
558
+ }{
559
+ ListChanged : s .capabilities .prompts .listChanged ,
560
+ }
540
561
}
541
562
542
- capabilities .Tools = & struct {
543
- ListChanged bool `json:"listChanged,omitempty"`
544
- }{
545
- ListChanged : s .capabilities .tools .listChanged ,
563
+ // Only add tool capabilities if they're configured
564
+ if s .capabilities .tools != nil {
565
+ capabilities .Tools = & struct {
566
+ ListChanged bool `json:"listChanged,omitempty"`
567
+ }{
568
+ ListChanged : s .capabilities .tools .listChanged ,
569
+ }
546
570
}
547
571
548
572
if s .capabilities .logging {
0 commit comments