@@ -196,7 +196,9 @@ private void SetCompletionHandler(McpServerOptions options)
196196 }
197197
198198 var completeHandler = completionsCapability . CompleteHandler ??
199- throw new McpException ( "Completions capability was enabled, but Complete handler was not specified." ) ;
199+ throw new InvalidOperationException (
200+ $ "{ nameof ( ServerCapabilities ) } .{ nameof ( ServerCapabilities . Completions ) } was enabled, " +
201+ $ "but { nameof ( CompletionsCapability . CompleteHandler ) } was not specified.") ;
200202
201203 // This capability is not optional, so return an empty result if there is no handler.
202204 RequestHandlers . Set (
@@ -219,7 +221,9 @@ private void SetResourcesHandler(McpServerOptions options)
219221 if ( ( listResourcesHandler is not { } && listResourceTemplatesHandler is not { } ) ||
220222 resourcesCapability . ReadResourceHandler is not { } readResourceHandler )
221223 {
222- throw new McpException ( "Resources capability was enabled, but ListResources and/or ReadResource handlers were not specified." ) ;
224+ throw new InvalidOperationException (
225+ $ "{ nameof ( ServerCapabilities ) } .{ nameof ( ServerCapabilities . Resources ) } was enabled, " +
226+ $ "but { nameof ( ResourcesCapability . ListResourcesHandler ) } or { nameof ( ResourcesCapability . ReadResourceHandler ) } was not specified.") ;
223227 }
224228
225229 listResourcesHandler ??= static async ( _ , _ ) => new ListResourcesResult ( ) ;
@@ -252,7 +256,9 @@ private void SetResourcesHandler(McpServerOptions options)
252256 var unsubscribeHandler = resourcesCapability . UnsubscribeFromResourcesHandler ;
253257 if ( subscribeHandler is null || unsubscribeHandler is null )
254258 {
255- throw new McpException ( "Resources capability was enabled with subscribe support, but SubscribeToResources and/or UnsubscribeFromResources handlers were not specified." ) ;
259+ throw new InvalidOperationException (
260+ $ "{ nameof ( ServerCapabilities ) } .{ nameof ( ServerCapabilities . Resources ) } .{ nameof ( ResourcesCapability . Subscribe ) } is set, " +
261+ $ "but { nameof ( ResourcesCapability . SubscribeToResourcesHandler ) } or { nameof ( ResourcesCapability . UnsubscribeFromResourcesHandler ) } was not specified.") ;
256262 }
257263
258264 RequestHandlers . Set (
@@ -277,7 +283,10 @@ private void SetPromptsHandler(McpServerOptions options)
277283
278284 if ( listPromptsHandler is null != getPromptHandler is null )
279285 {
280- throw new McpException ( "ListPrompts and GetPrompt handlers should be specified together." ) ;
286+ throw new InvalidOperationException (
287+ $ "{ nameof ( PromptsCapability ) } .{ nameof ( promptsCapability . ListPromptsHandler ) } or " +
288+ $ "{ nameof ( PromptsCapability ) } .{ nameof ( promptsCapability . GetPromptHandler ) } was specified without the other. " +
289+ $ "Both or neither must be provided.") ;
281290 }
282291
283292 // Handle prompts provided via DI.
@@ -310,7 +319,7 @@ await originalListPromptsHandler(request, cancellationToken).ConfigureAwait(fals
310319 return originalGetPromptHandler ( request , cancellationToken ) ;
311320 }
312321
313- throw new McpException ( $ "Unknown prompt '{ request . Params ? . Name } '") ;
322+ throw new McpException ( $ "Unknown prompt: '{ request . Params ? . Name } '", McpErrorCode . InvalidParams ) ;
314323 }
315324
316325 return prompt . GetAsync ( request , cancellationToken ) ;
@@ -344,7 +353,9 @@ await originalListPromptsHandler(request, cancellationToken).ConfigureAwait(fals
344353 // Make sure the handlers are provided if the capability is enabled.
345354 if ( listPromptsHandler is null || getPromptHandler is null )
346355 {
347- throw new McpException ( "ListPrompts and/or GetPrompt handlers were not specified but the Prompts capability was enabled." ) ;
356+ throw new InvalidOperationException (
357+ $ "{ nameof ( ServerCapabilities ) } .{ nameof ( ServerCapabilities . Prompts ) } was enabled, " +
358+ $ "but { nameof ( PromptsCapability . ListPromptsHandler ) } or { nameof ( PromptsCapability . GetPromptHandler ) } was not specified.") ;
348359 }
349360 }
350361
@@ -370,7 +381,10 @@ private void SetToolsHandler(McpServerOptions options)
370381
371382 if ( listToolsHandler is null != callToolHandler is null )
372383 {
373- throw new McpException ( "ListTools and CallTool handlers should be specified together." ) ;
384+ throw new InvalidOperationException (
385+ $ "{ nameof ( ToolsCapability ) } .{ nameof ( ToolsCapability . ListToolsHandler ) } or " +
386+ $ "{ nameof ( ToolsCapability ) } .{ nameof ( ToolsCapability . CallToolHandler ) } was specified without the other. " +
387+ $ "Both or neither must be provided.") ;
374388 }
375389
376390 // Handle tools provided via DI.
@@ -403,7 +417,7 @@ await originalListToolsHandler(request, cancellationToken).ConfigureAwait(false)
403417 return originalCallToolHandler ( request , cancellationToken ) ;
404418 }
405419
406- throw new McpException ( $ "Unknown tool '{ request . Params ? . Name } '") ;
420+ throw new McpException ( $ "Unknown tool: '{ request . Params ? . Name } '", McpErrorCode . InvalidParams ) ;
407421 }
408422
409423 return tool . InvokeAsync ( request , cancellationToken ) ;
@@ -437,7 +451,9 @@ await originalListToolsHandler(request, cancellationToken).ConfigureAwait(false)
437451 // Make sure the handlers are provided if the capability is enabled.
438452 if ( listToolsHandler is null || callToolHandler is null )
439453 {
440- throw new McpException ( "ListTools and/or CallTool handlers were not specified but the Tools capability was enabled." ) ;
454+ throw new InvalidOperationException (
455+ $ "{ nameof ( ServerCapabilities ) } .{ nameof ( ServerCapabilities . Tools ) } was enabled, " +
456+ $ "but { nameof ( ToolsCapability . ListToolsHandler ) } or { nameof ( ToolsCapability . CallToolHandler ) } was not specified.") ;
441457 }
442458 }
443459
0 commit comments