Consolidate McpClientException/McpServerException into McpException (#209)

This commit is contained in:
Stephen Toub 2025-04-03 21:10:57 -04:00 • committed by GitHub
commit 8fcdf95785
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 163 additions and 197 deletions

View file

@ -25,7 +25,7 @@ internal sealed class McpServer : McpEndpoint, IMcpServer
/// Make sure to accurately reflect exactly what capabilities the server supports and does not support.</param>
/// <param name="loggerFactory">Logger factory to use for logging</param>
/// <param name="serviceProvider">Optional service provider to use for dependency injection</param>
/// <exception cref="McpServerException"></exception>
/// <exception cref="McpException">The server was incorrectly configured.</exception>
public McpServer(ITransport transport, McpServerOptions options, ILoggerFactory? loggerFactory, IServiceProvider? serviceProvider)
: base(loggerFactory)
{
@ -185,7 +185,7 @@ internal sealed class McpServer : McpEndpoint, IMcpServer
if ((listResourcesHandler is not { } && listResourceTemplatesHandler is not { }) ||
resourcesCapability.ReadResourceHandler is not { } readResourceHandler)
{
throw new McpServerException("Resources capability was enabled, but ListResources and/or ReadResource handlers were not specified.");
throw new McpException("Resources capability was enabled, but ListResources and/or ReadResource handlers were not specified.");
}
listResourcesHandler ??= (static (_, _) => Task.FromResult(new ListResourcesResult()));
@ -218,7 +218,7 @@ internal sealed class McpServer : McpEndpoint, IMcpServer
var unsubscribeHandler = resourcesCapability.UnsubscribeFromResourcesHandler;
if (subscribeHandler is null || unsubscribeHandler is null)
{
throw new McpServerException("Resources capability was enabled with subscribe support, but SubscribeToResources and/or UnsubscribeFromResources handlers were not specified.");
throw new McpException("Resources capability was enabled with subscribe support, but SubscribeToResources and/or UnsubscribeFromResources handlers were not specified.");
}
SetRequestHandler(
@ -243,7 +243,7 @@ internal sealed class McpServer : McpEndpoint, IMcpServer
if (listPromptsHandler is null != getPromptHandler is null)
{
throw new McpServerException("ListPrompts and GetPrompt handlers should be specified together.");
throw new McpException("ListPrompts and GetPrompt handlers should be specified together.");
}
// Handle prompts provided via DI.
@ -276,7 +276,7 @@ internal sealed class McpServer : McpEndpoint, IMcpServer
return originalGetPromptHandler(request, cancellationToken);
}
throw new McpServerException($"Unknown prompt '{request.Params?.Name}'");
throw new McpException($"Unknown prompt '{request.Params?.Name}'");
}
return prompt.GetAsync(request, cancellationToken);
@ -310,7 +310,7 @@ internal sealed class McpServer : McpEndpoint, IMcpServer
// Make sure the handlers are provided if the capability is enabled.
if (listPromptsHandler is null || getPromptHandler is null)
{
throw new McpServerException("ListPrompts and/or GetPrompt handlers were not specified but the Prompts capability was enabled.");
throw new McpException("ListPrompts and/or GetPrompt handlers were not specified but the Prompts capability was enabled.");
}
}
@ -336,7 +336,7 @@ internal sealed class McpServer : McpEndpoint, IMcpServer
if (listToolsHandler is null != callToolHandler is null)
{
throw new McpServerException("ListTools and CallTool handlers should be specified together.");
throw new McpException("ListTools and CallTool handlers should be specified together.");
}
// Handle tools provided via DI.
@ -369,7 +369,7 @@ internal sealed class McpServer : McpEndpoint, IMcpServer
return originalCallToolHandler(request, cancellationToken);
}
throw new McpServerException($"Unknown tool '{request.Params?.Name}'");
throw new McpException($"Unknown tool '{request.Params?.Name}'");
}
return tool.InvokeAsync(request, cancellationToken);
@ -403,7 +403,7 @@ internal sealed class McpServer : McpEndpoint, IMcpServer
// Make sure the handlers are provided if the capability is enabled.
if (listToolsHandler is null || callToolHandler is null)
{
throw new McpServerException("ListTools and/or CallTool handlers were not specified but the Tools capability was enabled.");
throw new McpException("ListTools and/or CallTool handlers were not specified but the Tools capability was enabled.");
}
}
@ -429,7 +429,7 @@ internal sealed class McpServer : McpEndpoint, IMcpServer
if (loggingCapability.SetLoggingLevelHandler is not { } setLoggingLevelHandler)
{
throw new McpServerException("Logging capability was enabled, but SetLoggingLevelHandler was not specified.");
throw new McpException("Logging capability was enabled, but SetLoggingLevelHandler was not specified.");
}
SetRequestHandler(

View file

@ -1,48 +0,0 @@
using ModelContextProtocol.Client;
namespace ModelContextProtocol.Server;
/// <summary>
/// Represents errors that occur in the MCP server.
/// </summary>
public class McpServerException : Exception
{
/// <summary>
/// Gets the error code if this exception was caused by a JSON-RPC error response.
/// </summary>
public int? ErrorCode { get; }
/// <summary>
/// Initializes a new instance of the <see cref="McpServerException"/> class with a specified error message.
/// </summary>
public McpServerException()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="McpServerException"/> class with a specified error message.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public McpServerException(string message) : base(message)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="McpServerException"/> class with a specified error message and error code.
/// </summary>
/// <param name="message">The message that describes the error.</param>
/// <param name="errorCode">The error code associated with the JSON-RPC error response.</param>
public McpServerException(string message, int errorCode) : base(message)
{
ErrorCode = errorCode;
}
/// <summary>
/// Initializes a new instance of the <see cref="McpClientException"/> class with a specified error message and a reference to the inner exception that is the cause of this exception.
/// </summary>
/// <param name="message">The message that describes the error.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
public McpServerException(string message, Exception innerException) : base(message, innerException)
{
}
}

View file

@ -41,7 +41,7 @@ public static class McpServerExtensions
/// <param name="server">The server issueing the request.</param>
/// <param name="messages">The messages to send as part of the request.</param>
/// <param name="options">The options to use for the request.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>A task containing the response from the client.</returns>
/// <exception cref="ArgumentNullException"><paramref name="server"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="messages"/> is <see langword="null"/>.</exception>