Skip to content

Commit 2b6f7b5

Browse files
mcp/server: add initial capability server option (#182)
This CL adds an option for servers to advertise support for features that have not been added yet. This includes Prompts, Tools, and Resources as these could be dynamically added after initialization. Fixes: #135.
1 parent a5aa370 commit 2b6f7b5

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

mcp/server.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ type ServerOptions struct {
6969
SubscribeHandler func(context.Context, *SubscribeParams) error
7070
// Function called when a client session unsubscribes from a resource.
7171
UnsubscribeHandler func(context.Context, *UnsubscribeParams) error
72+
// If true, advertises the prompts capability during initialization,
73+
// even if no prompts have been registered.
74+
HasPrompts bool
75+
// If true, advertises the resources capability during initialization,
76+
// even if no resources have been registered.
77+
HasResources bool
78+
// If true, advertises the tools capability during initialization,
79+
// even if no tools have been registered.
80+
HasTools bool
7281
}
7382

7483
// NewServer creates a new MCP server. The resulting server has no features:
@@ -229,16 +238,17 @@ func (s *Server) capabilities() *serverCapabilities {
229238
defer s.mu.Unlock()
230239

231240
caps := &serverCapabilities{
241+
// TODO(samthanawalla): check for completionHandler before advertising capability.
232242
Completions: &completionCapabilities{},
233243
Logging: &loggingCapabilities{},
234244
}
235-
if s.tools.len() > 0 {
245+
if s.opts.HasTools || s.tools.len() > 0 {
236246
caps.Tools = &toolCapabilities{ListChanged: true}
237247
}
238-
if s.prompts.len() > 0 {
248+
if s.opts.HasPrompts || s.prompts.len() > 0 {
239249
caps.Prompts = &promptCapabilities{ListChanged: true}
240250
}
241-
if s.resources.len() > 0 || s.resourceTemplates.len() > 0 {
251+
if s.opts.HasResources || s.resources.len() > 0 || s.resourceTemplates.len() > 0 {
242252
caps.Resources = &resourceCapabilities{ListChanged: true}
243253
if s.opts.SubscribeHandler != nil {
244254
caps.Resources.Subscribe = true

mcp/server_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,22 @@ func TestServerCapabilities(t *testing.T) {
333333
Tools: &toolCapabilities{ListChanged: true},
334334
},
335335
},
336+
{
337+
name: "With initial capabilities",
338+
configureServer: func(s *Server) {},
339+
serverOpts: ServerOptions{
340+
HasPrompts: true,
341+
HasResources: true,
342+
HasTools: true,
343+
},
344+
wantCapabilities: &serverCapabilities{
345+
Completions: &completionCapabilities{},
346+
Logging: &loggingCapabilities{},
347+
Prompts: &promptCapabilities{ListChanged: true},
348+
Resources: &resourceCapabilities{ListChanged: true},
349+
Tools: &toolCapabilities{ListChanged: true},
350+
},
351+
},
336352
}
337353

338354
for _, tc := range testCases {

0 commit comments

Comments
 (0)