@@ -52,6 +52,18 @@ type ServerTool struct {
52
52
Handler ToolHandlerFunc
53
53
}
54
54
55
+ // ServerPrompt combines a Prompt with its handler function.
56
+ type ServerPrompt struct {
57
+ Prompt mcp.Prompt
58
+ Handler PromptHandlerFunc
59
+ }
60
+
61
+ // ServerResource combines a Resource with its handler function.
62
+ type ServerResource struct {
63
+ Resource mcp.Resource
64
+ Handler ResourceHandlerFunc
65
+ }
66
+
55
67
// serverKey is the context key for storing the server instance
56
68
type serverKey struct {}
57
69
@@ -305,28 +317,16 @@ func NewMCPServer(
305
317
return s
306
318
}
307
319
308
- // AddResource registers a new resource and its handler
309
- func (s * MCPServer ) AddResource (
310
- resource mcp.Resource ,
311
- handler ResourceHandlerFunc ,
312
- ) {
313
- s .capabilitiesMu .RLock ()
314
- if s .capabilities .resources == nil {
315
- s .capabilitiesMu .RUnlock ()
316
-
317
- s .capabilitiesMu .Lock ()
318
- if s .capabilities .resources == nil {
319
- s .capabilities .resources = & resourceCapabilities {}
320
- }
321
- s .capabilitiesMu .Unlock ()
322
- } else {
323
- s .capabilitiesMu .RUnlock ()
324
- }
320
+ // AddResources registers multiple resources at once
321
+ func (s * MCPServer ) AddResources (resources ... ServerResource ) {
322
+ s .implicitlyRegisterResourceCapabilities ()
325
323
326
324
s .resourcesMu .Lock ()
327
- s .resources [resource .URI ] = resourceEntry {
328
- resource : resource ,
329
- handler : handler ,
325
+ for _ , entry := range resources {
326
+ s .resources [entry .Resource .URI ] = resourceEntry {
327
+ resource : entry .Resource ,
328
+ handler : entry .Handler ,
329
+ }
330
330
}
331
331
s .resourcesMu .Unlock ()
332
332
@@ -337,6 +337,14 @@ func (s *MCPServer) AddResource(
337
337
}
338
338
}
339
339
340
+ // AddResource registers a new resource and its handler
341
+ func (s * MCPServer ) AddResource (
342
+ resource mcp.Resource ,
343
+ handler ResourceHandlerFunc ,
344
+ ) {
345
+ s .AddResources (ServerResource {Resource : resource , Handler : handler })
346
+ }
347
+
340
348
// RemoveResource removes a resource from the server
341
349
func (s * MCPServer ) RemoveResource (uri string ) {
342
350
s .resourcesMu .Lock ()
@@ -357,18 +365,7 @@ func (s *MCPServer) AddResourceTemplate(
357
365
template mcp.ResourceTemplate ,
358
366
handler ResourceTemplateHandlerFunc ,
359
367
) {
360
- s .capabilitiesMu .RLock ()
361
- if s .capabilities .resources == nil {
362
- s .capabilitiesMu .RUnlock ()
363
-
364
- s .capabilitiesMu .Lock ()
365
- if s .capabilities .resources == nil {
366
- s .capabilities .resources = & resourceCapabilities {}
367
- }
368
- s .capabilitiesMu .Unlock ()
369
- } else {
370
- s .capabilitiesMu .RUnlock ()
371
- }
368
+ s .implicitlyRegisterResourceCapabilities ()
372
369
373
370
s .resourcesMu .Lock ()
374
371
s .resourceTemplates [template .URITemplate .Raw ()] = resourceTemplateEntry {
@@ -384,8 +381,8 @@ func (s *MCPServer) AddResourceTemplate(
384
381
}
385
382
}
386
383
387
- // AddPrompt registers a new prompt handler with the given name
388
- func (s * MCPServer ) AddPrompt ( prompt mcp. Prompt , handler PromptHandlerFunc ) {
384
+ // AddPrompts registers multiple prompts at once
385
+ func (s * MCPServer ) AddPrompts ( prompts ... ServerPrompt ) {
389
386
s .capabilitiesMu .RLock ()
390
387
if s .capabilities .prompts == nil {
391
388
s .capabilitiesMu .RUnlock ()
@@ -400,8 +397,10 @@ func (s *MCPServer) AddPrompt(prompt mcp.Prompt, handler PromptHandlerFunc) {
400
397
}
401
398
402
399
s .promptsMu .Lock ()
403
- s .prompts [prompt .Name ] = prompt
404
- s .promptHandlers [prompt .Name ] = handler
400
+ for _ , entry := range prompts {
401
+ s .prompts [entry .Prompt .Name ] = entry .Prompt
402
+ s .promptHandlers [entry .Prompt .Name ] = entry .Handler
403
+ }
405
404
s .promptsMu .Unlock ()
406
405
407
406
// When the list of available prompts changes, servers that declared the listChanged capability SHOULD send a notification.
@@ -411,6 +410,11 @@ func (s *MCPServer) AddPrompt(prompt mcp.Prompt, handler PromptHandlerFunc) {
411
410
}
412
411
}
413
412
413
+ // AddPrompt registers a new prompt handler with the given name
414
+ func (s * MCPServer ) AddPrompt (prompt mcp.Prompt , handler PromptHandlerFunc ) {
415
+ s .AddPrompts (ServerPrompt {Prompt : prompt , Handler : handler })
416
+ }
417
+
414
418
// DeletePrompts removes prompts from the server
415
419
func (s * MCPServer ) DeletePrompts (names ... string ) {
416
420
s .promptsMu .Lock ()
@@ -456,6 +460,21 @@ func (s *MCPServer) implicitlyRegisterToolCapabilities() {
456
460
}
457
461
}
458
462
463
+ func (s * MCPServer ) implicitlyRegisterResourceCapabilities () {
464
+ s .capabilitiesMu .RLock ()
465
+ if s .capabilities .resources == nil {
466
+ s .capabilitiesMu .RUnlock ()
467
+
468
+ s .capabilitiesMu .Lock ()
469
+ if s .capabilities .resources == nil {
470
+ s .capabilities .resources = & resourceCapabilities {}
471
+ }
472
+ s .capabilitiesMu .Unlock ()
473
+ } else {
474
+ s .capabilitiesMu .RUnlock ()
475
+ }
476
+ }
477
+
459
478
// AddTools registers multiple tools at once
460
479
func (s * MCPServer ) AddTools (tools ... ServerTool ) {
461
480
s .implicitlyRegisterToolCapabilities ()
0 commit comments