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

View file

@ -164,7 +164,7 @@ internal static class Program
{
if (request.Params?.Arguments is null || !request.Params.Arguments.TryGetValue("message", out var message))
{
throw new McpServerException("Missing required argument 'message'");
throw new McpException("Missing required argument 'message'");
}
return new CallToolResponse()
{
@ -177,7 +177,7 @@ internal static class Program
!request.Params.Arguments.TryGetValue("prompt", out var prompt) ||
!request.Params.Arguments.TryGetValue("maxTokens", out var maxTokens))
{
throw new McpServerException("Missing required arguments 'prompt' and 'maxTokens'");
throw new McpException("Missing required arguments 'prompt' and 'maxTokens'");
}
var sampleResult = await request.Server.RequestSamplingAsync(CreateRequestSamplingParams(prompt.ToString(), "sampleLLM", Convert.ToInt32(maxTokens.GetRawText())),
cancellationToken);
@ -189,7 +189,7 @@ internal static class Program
}
else
{
throw new McpServerException($"Unknown tool: {request.Params?.Name}");
throw new McpException($"Unknown tool: {request.Params?.Name}");
}
}
};
@ -283,7 +283,7 @@ internal static class Program
}
else
{
throw new McpServerException($"Unknown prompt: {request.Params?.Name}");
throw new McpException($"Unknown prompt: {request.Params?.Name}");
}
return Task.FromResult(new GetPromptResult()
@ -304,7 +304,7 @@ internal static class Program
{
if (request.Params?.Level is null)
{
throw new McpServerException("Missing required argument 'level'");
throw new McpException("Missing required argument 'level'");
}
_minimumLoggingLevel = request.Params.Level;
@ -384,9 +384,9 @@ internal static class Program
var startIndexAsString = Encoding.UTF8.GetString(Convert.FromBase64String(request.Params.Cursor));
startIndex = Convert.ToInt32(startIndexAsString);
}
catch
catch (Exception e)
{
throw new McpServerException("Invalid cursor");
throw new McpException("Invalid cursor.", e);
}
}
@ -408,7 +408,7 @@ internal static class Program
{
if (request.Params?.Uri is null)
{
throw new McpServerException("Missing required argument 'uri'");
throw new McpException("Missing required argument 'uri'");
}
if (request.Params.Uri.StartsWith("test://dynamic/resource/"))
@ -416,7 +416,7 @@ internal static class Program
var id = request.Params.Uri.Split('/').LastOrDefault();
if (string.IsNullOrEmpty(id))
{
throw new McpServerException("Invalid resource URI");
throw new McpException("Invalid resource URI");
}
return Task.FromResult(new ReadResourceResult()
{
@ -432,7 +432,7 @@ internal static class Program
}
ResourceContents contents = resourceContents.FirstOrDefault(r => r.Uri == request.Params.Uri)
?? throw new McpServerException("Resource not found");
?? throw new McpException("Resource not found");
return Task.FromResult(new ReadResourceResult()
{
@ -444,12 +444,12 @@ internal static class Program
{
if (request?.Params?.Uri is null)
{
throw new McpServerException("Missing required argument 'uri'");
throw new McpException("Missing required argument 'uri'");
}
if (!request.Params.Uri.StartsWith("test://static/resource/")
&& !request.Params.Uri.StartsWith("test://dynamic/resource/"))
{
throw new McpServerException("Invalid resource URI");
throw new McpException("Invalid resource URI");
}
_subscribedResources.TryAdd(request.Params.Uri, true);
@ -461,12 +461,12 @@ internal static class Program
{
if (request?.Params?.Uri is null)
{
throw new McpServerException("Missing required argument 'uri'");
throw new McpException("Missing required argument 'uri'");
}
if (!request.Params.Uri.StartsWith("test://static/resource/")
&& !request.Params.Uri.StartsWith("test://dynamic/resource/"))
{
throw new McpServerException("Invalid resource URI");
throw new McpException("Invalid resource URI");
}
_subscribedResources.Remove(request.Params.Uri, out _);
@ -511,7 +511,7 @@ internal static class Program
return Task.FromResult(new CompleteResult() { Completion = new() { Values = values, HasMore = false, Total = values.Length } });
}
throw new McpServerException($"Unknown reference type: {request.Params?.Ref.Type}");
throw new McpException($"Unknown reference type: {request.Params?.Ref.Type}");
};
}