55using ModelContextProtocol . Shared ;
66using ModelContextProtocol . Utils ;
77using ModelContextProtocol . Utils . Json ;
8+ using System . Diagnostics ;
89
910namespace ModelContextProtocol . Server ;
1011
@@ -44,49 +45,43 @@ public McpServer(ITransport transport, McpServerOptions options, ILoggerFactory?
4445 Services = serviceProvider ;
4546 _endpointName = $ "Server ({ options . ServerInfo ? . Name ?? DefaultImplementation . Name } { options . ServerInfo ? . Version ?? DefaultImplementation . Version } )";
4647
47- _toolsChangedDelegate = delegate
48- {
49- _ = SendMessageAsync ( new JsonRpcNotification ( )
50- {
51- Method = NotificationMethods . ToolListChangedNotification ,
52- } ) ;
53- } ;
54- _promptsChangedDelegate = delegate
55- {
56- _ = SendMessageAsync ( new JsonRpcNotification ( )
57- {
58- Method = NotificationMethods . PromptListChangedNotification ,
59- } ) ;
60- } ;
48+ // Configure all request handlers based on the supplied options.
49+ SetInitializeHandler ( options ) ;
50+ SetToolsHandler ( options ) ;
51+ SetPromptsHandler ( options ) ;
52+ SetResourcesHandler ( options ) ;
53+ SetSetLoggingLevelHandler ( options ) ;
54+ SetCompletionHandler ( options ) ;
55+ SetPingHandler ( ) ;
6156
62- NotificationHandlers . Add ( NotificationMethods . InitializedNotification , _ =>
57+ // Register any notification handlers that were provided.
58+ if ( options . Capabilities ? . NotificationHandlers is { } notificationHandlers )
6359 {
64- if ( ServerOptions . Capabilities ? . Tools ? . ToolCollection is { } tools )
65- {
66- tools . Changed += _toolsChangedDelegate ;
67- }
60+ NotificationHandlers . RegisterRange ( notificationHandlers ) ;
61+ }
6862
69- if ( ServerOptions . Capabilities ? . Prompts ? . PromptCollection is { } prompts )
63+ // Now that everything has been configured, subscribe to any necessary notifications.
64+ if ( ServerOptions . Capabilities ? . Tools ? . ToolCollection is { } tools )
65+ {
66+ _toolsChangedDelegate = delegate
7067 {
71- prompts . Changed += _promptsChangedDelegate ;
72- }
68+ _ = SendMessageAsync ( new JsonRpcNotification ( ) { Method = NotificationMethods . ToolListChangedNotification } ) ;
69+ } ;
7370
74- return Task . CompletedTask ;
75- } ) ;
71+ tools . Changed += _toolsChangedDelegate ;
72+ }
7673
77- if ( options . Capabilities ? . NotificationHandlers is { } notificationHandlers )
74+ if ( ServerOptions . Capabilities ? . Prompts ? . PromptCollection is { } prompts )
7875 {
79- NotificationHandlers . AddRange ( notificationHandlers ) ;
80- }
76+ _promptsChangedDelegate = delegate
77+ {
78+ _ = SendMessageAsync ( new JsonRpcNotification ( ) { Method = NotificationMethods . PromptListChangedNotification } ) ;
79+ } ;
8180
82- SetToolsHandler ( options ) ;
83- SetInitializeHandler ( options ) ;
84- SetCompletionHandler ( options ) ;
85- SetPingHandler ( ) ;
86- SetPromptsHandler ( options ) ;
87- SetResourcesHandler ( options ) ;
88- SetSetLoggingLevelHandler ( options ) ;
81+ prompts . Changed += _promptsChangedDelegate ;
82+ }
8983
84+ // And start the session.
9085 StartSession ( transport ) ;
9186 }
9287
@@ -129,12 +124,14 @@ public async Task RunAsync(CancellationToken cancellationToken = default)
129124
130125 public override async ValueTask DisposeUnsynchronizedAsync ( )
131126 {
132- if ( ServerOptions . Capabilities ? . Tools ? . ToolCollection is { } tools )
127+ if ( _toolsChangedDelegate is not null &&
128+ ServerOptions . Capabilities ? . Tools ? . ToolCollection is { } tools )
133129 {
134130 tools . Changed -= _toolsChangedDelegate ;
135131 }
136132
137- if ( ServerOptions . Capabilities ? . Prompts ? . PromptCollection is { } prompts )
133+ if ( _promptsChangedDelegate is not null &&
134+ ServerOptions . Capabilities ? . Prompts ? . PromptCollection is { } prompts )
138135 {
139136 prompts . Changed -= _promptsChangedDelegate ;
140137 }
@@ -179,8 +176,8 @@ private void SetCompletionHandler(McpServerOptions options)
179176 // This capability is not optional, so return an empty result if there is no handler.
180177 RequestHandlers . Set ( RequestMethods . CompletionComplete ,
181178 options . GetCompletionHandler is { } handler ?
182- ( request , ct ) => handler ( new ( this , request ) , ct ) :
183- ( request , ct ) => Task . FromResult ( new CompleteResult ( ) { Completion = new ( ) { Values = [ ] , Total = 0 , HasMore = false } } ) ,
179+ ( request , cancellationToken ) => handler ( new ( this , request ) , cancellationToken ) :
180+ ( request , cancellationToken ) => Task . FromResult ( new CompleteResult ( ) { Completion = new ( ) { Values = [ ] , Total = 0 , HasMore = false } } ) ,
184181 McpJsonUtilities . JsonContext . Default . CompleteRequestParams ,
185182 McpJsonUtilities . JsonContext . Default . CompleteResult ) ;
186183 }
@@ -205,20 +202,20 @@ private void SetResourcesHandler(McpServerOptions options)
205202
206203 RequestHandlers . Set (
207204 RequestMethods . ResourcesList ,
208- ( request , ct ) => listResourcesHandler ( new ( this , request ) , ct ) ,
205+ ( request , cancellationToken ) => listResourcesHandler ( new ( this , request ) , cancellationToken ) ,
209206 McpJsonUtilities . JsonContext . Default . ListResourcesRequestParams ,
210207 McpJsonUtilities . JsonContext . Default . ListResourcesResult ) ;
211208
212209 RequestHandlers . Set (
213210 RequestMethods . ResourcesRead ,
214- ( request , ct ) => readResourceHandler ( new ( this , request ) , ct ) ,
211+ ( request , cancellationToken ) => readResourceHandler ( new ( this , request ) , cancellationToken ) ,
215212 McpJsonUtilities . JsonContext . Default . ReadResourceRequestParams ,
216213 McpJsonUtilities . JsonContext . Default . ReadResourceResult ) ;
217214
218215 listResourceTemplatesHandler ??= ( static ( _ , _ ) => Task . FromResult ( new ListResourceTemplatesResult ( ) ) ) ;
219216 RequestHandlers . Set (
220217 RequestMethods . ResourcesTemplatesList ,
221- ( request , ct ) => listResourceTemplatesHandler ( new ( this , request ) , ct ) ,
218+ ( request , cancellationToken ) => listResourceTemplatesHandler ( new ( this , request ) , cancellationToken ) ,
222219 McpJsonUtilities . JsonContext . Default . ListResourceTemplatesRequestParams ,
223220 McpJsonUtilities . JsonContext . Default . ListResourceTemplatesResult ) ;
224221
@@ -236,13 +233,13 @@ private void SetResourcesHandler(McpServerOptions options)
236233
237234 RequestHandlers . Set (
238235 RequestMethods . ResourcesSubscribe ,
239- ( request , ct ) => subscribeHandler ( new ( this , request ) , ct ) ,
236+ ( request , cancellationToken ) => subscribeHandler ( new ( this , request ) , cancellationToken ) ,
240237 McpJsonUtilities . JsonContext . Default . SubscribeRequestParams ,
241238 McpJsonUtilities . JsonContext . Default . EmptyResult ) ;
242239
243240 RequestHandlers . Set (
244241 RequestMethods . ResourcesUnsubscribe ,
245- ( request , ct ) => unsubscribeHandler ( new ( this , request ) , ct ) ,
242+ ( request , cancellationToken ) => unsubscribeHandler ( new ( this , request ) , cancellationToken ) ,
246243 McpJsonUtilities . JsonContext . Default . UnsubscribeRequestParams ,
247244 McpJsonUtilities . JsonContext . Default . EmptyResult ) ;
248245 }
@@ -329,13 +326,13 @@ await originalListPromptsHandler(request, cancellationToken).ConfigureAwait(fals
329326
330327 RequestHandlers . Set (
331328 RequestMethods . PromptsList ,
332- ( request , ct ) => listPromptsHandler ( new ( this , request ) , ct ) ,
329+ ( request , cancellationToken ) => listPromptsHandler ( new ( this , request ) , cancellationToken ) ,
333330 McpJsonUtilities . JsonContext . Default . ListPromptsRequestParams ,
334331 McpJsonUtilities . JsonContext . Default . ListPromptsResult ) ;
335332
336333 RequestHandlers . Set (
337334 RequestMethods . PromptsGet ,
338- ( request , ct ) => getPromptHandler ( new ( this , request ) , ct ) ,
335+ ( request , cancellationToken ) => getPromptHandler ( new ( this , request ) , cancellationToken ) ,
339336 McpJsonUtilities . JsonContext . Default . GetPromptRequestParams ,
340337 McpJsonUtilities . JsonContext . Default . GetPromptResult ) ;
341338 }
@@ -422,13 +419,13 @@ await originalListToolsHandler(request, cancellationToken).ConfigureAwait(false)
422419
423420 RequestHandlers . Set (
424421 RequestMethods . ToolsList ,
425- ( request , ct ) => listToolsHandler ( new ( this , request ) , ct ) ,
422+ ( request , cancellationToken ) => listToolsHandler ( new ( this , request ) , cancellationToken ) ,
426423 McpJsonUtilities . JsonContext . Default . ListToolsRequestParams ,
427424 McpJsonUtilities . JsonContext . Default . ListToolsResult ) ;
428425
429426 RequestHandlers . Set (
430427 RequestMethods . ToolsCall ,
431- ( request , ct ) => callToolHandler ( new ( this , request ) , ct ) ,
428+ ( request , cancellationToken ) => callToolHandler ( new ( this , request ) , cancellationToken ) ,
432429 McpJsonUtilities . JsonContext . Default . CallToolRequestParams ,
433430 McpJsonUtilities . JsonContext . Default . CallToolResponse ) ;
434431 }
@@ -447,7 +444,7 @@ private void SetSetLoggingLevelHandler(McpServerOptions options)
447444
448445 RequestHandlers . Set (
449446 RequestMethods . LoggingSetLevel ,
450- ( request , ct ) => setLoggingLevelHandler ( new ( this , request ) , ct ) ,
447+ ( request , cancellationToken ) => setLoggingLevelHandler ( new ( this , request ) , cancellationToken ) ,
451448 McpJsonUtilities . JsonContext . Default . SetLevelRequestParams ,
452449 McpJsonUtilities . JsonContext . Default . EmptyResult ) ;
453450 }
0 commit comments