Skip to content

Commit ddca6cc

Browse files
committed
Remove error codes - we don't use those
1 parent 389fb3d commit ddca6cc

4 files changed

Lines changed: 8 additions & 32 deletions

File tree

src/ModelContextProtocol/McpAuthorizationException.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class McpAuthorizationException : McpException
1313
/// Initializes a new instance of the <see cref="McpAuthorizationException"/> class.
1414
/// </summary>
1515
public McpAuthorizationException()
16-
: base("Authorization failed", McpErrorCode.Unauthorized)
16+
: base("Authorization failed", McpErrorCode.InvalidRequest)
1717
{
1818
}
1919

@@ -22,7 +22,7 @@ public McpAuthorizationException()
2222
/// </summary>
2323
/// <param name="message">The message that describes the error.</param>
2424
public McpAuthorizationException(string message)
25-
: base(message, McpErrorCode.Unauthorized)
25+
: base(message, McpErrorCode.InvalidRequest)
2626
{
2727
}
2828

@@ -32,37 +32,29 @@ public McpAuthorizationException(string message)
3232
/// <param name="message">The message that describes the error.</param>
3333
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
3434
public McpAuthorizationException(string message, Exception? innerException)
35-
: base(message, innerException, McpErrorCode.Unauthorized)
35+
: base(message, innerException, McpErrorCode.InvalidRequest)
3636
{
3737
}
3838

3939
/// <summary>
4040
/// Initializes a new instance of the <see cref="McpAuthorizationException"/> class with a specified error message and error code.
4141
/// </summary>
4242
/// <param name="message">The message that describes the error.</param>
43-
/// <param name="errorCode">The MCP error code. Should be either <see cref="McpErrorCode.Unauthorized"/> or <see cref="McpErrorCode.AuthenticationFailed"/>.</param>
43+
/// <param name="errorCode">The MCP error code. Should use one of the standard error codes.</param>
4444
public McpAuthorizationException(string message, McpErrorCode errorCode)
4545
: base(message, errorCode)
4646
{
47-
if (errorCode != McpErrorCode.Unauthorized && errorCode != McpErrorCode.AuthenticationFailed)
48-
{
49-
throw new ArgumentException($"Error code must be either {nameof(McpErrorCode.Unauthorized)} or {nameof(McpErrorCode.AuthenticationFailed)}", nameof(errorCode));
50-
}
5147
}
5248

5349
/// <summary>
5450
/// Initializes a new instance of the <see cref="McpAuthorizationException"/> class with a specified error message, inner exception, and error code.
5551
/// </summary>
5652
/// <param name="message">The message that describes the error.</param>
5753
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
58-
/// <param name="errorCode">The MCP error code. Should be either <see cref="McpErrorCode.Unauthorized"/> or <see cref="McpErrorCode.AuthenticationFailed"/>.</param>
54+
/// <param name="errorCode">The MCP error code. Should use one of the standard error codes.</param>
5955
public McpAuthorizationException(string message, Exception? innerException, McpErrorCode errorCode)
6056
: base(message, innerException, errorCode)
6157
{
62-
if (errorCode != McpErrorCode.Unauthorized && errorCode != McpErrorCode.AuthenticationFailed)
63-
{
64-
throw new ArgumentException($"Error code must be either {nameof(McpErrorCode.Unauthorized)} or {nameof(McpErrorCode.AuthenticationFailed)}", nameof(errorCode));
65-
}
6658
}
6759

6860
/// <summary>

src/ModelContextProtocol/McpErrorCode.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,4 @@ public enum McpErrorCode
4646
/// This error is used when the endpoint encounters an unexpected condition that prevents it from fulfilling the request.
4747
/// </remarks>
4848
InternalError = -32603,
49-
50-
/// <summary>
51-
/// Indicates that the client is not authorized to access the requested resource.
52-
/// </summary>
53-
/// <remarks>
54-
/// This error is returned when the client lacks the necessary credentials or permissions to access a resource.
55-
/// </remarks>
56-
Unauthorized = -32401,
57-
58-
/// <summary>
59-
/// Indicates that the authentication process failed.
60-
/// </summary>
61-
/// <remarks>
62-
/// This error is returned when the client provides invalid or expired credentials, or when the authentication flow fails.
63-
/// </remarks>
64-
AuthenticationFailed = -32402,
6549
}

src/ModelContextProtocol/Protocol/Auth/DefaultAuthorizationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public async Task<bool> HandleUnauthorizedResponseAsync(HttpResponseMessage resp
193193
_logger.LogError(ex, "Failed to complete authorization flow");
194194

195195
var authException = new McpAuthorizationException(
196-
$"Failed to complete authorization flow: {ex.Message}", ex, McpErrorCode.AuthenticationFailed);
196+
$"Failed to complete authorization flow: {ex.Message}", ex, McpErrorCode.InvalidRequest);
197197

198198
authException.ResourceUri = resourceMetadata.Resource;
199199
authException.AuthorizationServerUri = authServerUrl;

src/ModelContextProtocol/Protocol/Transport/SseClientTransport.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ public SseClientTransport(SseClientTransportOptions transportOptions, HttpClient
120120
if (!string.IsNullOrEmpty(error))
121121
{
122122
responseHtml = $"<html><body><h1>Authorization Failed</h1><p>Error: {error}</p></body></html>";
123-
authCodeTcs.SetException(new McpException($"Authorization failed: {error}", McpErrorCode.AuthenticationFailed));
123+
authCodeTcs.SetException(new McpException($"Authorization failed: {error}", McpErrorCode.InvalidRequest));
124124
}
125125
else if (string.IsNullOrEmpty(code))
126126
{
127127
responseHtml = "<html><body><h1>Authorization Failed</h1><p>No authorization code received.</p></body></html>";
128-
authCodeTcs.SetException(new McpException("No authorization code received", McpErrorCode.AuthenticationFailed));
128+
authCodeTcs.SetException(new McpException("No authorization code received", McpErrorCode.InvalidRequest));
129129
}
130130
else
131131
{

0 commit comments

Comments
 (0)