Clean up (use of) McpException and McpTransportException (#321)
* Clean up (use of) McpException * Replace McpTransportException with InvalidOperationException * Add cancellation test
This commit is contained in:
parent
d65ddce268
commit
7d77be68cf
1 changed files with 231 additions and 219 deletions
|
|
@ -157,13 +157,13 @@ public class Program
|
|||
{
|
||||
if (request.Params is null)
|
||||
{
|
||||
throw new McpException("Missing required parameter 'name'");
|
||||
throw new McpException("Missing required parameter 'name'", McpErrorCode.InvalidParams);
|
||||
}
|
||||
if (request.Params.Name == "echo")
|
||||
{
|
||||
if (request.Params.Arguments is null || !request.Params.Arguments.TryGetValue("message", out var message))
|
||||
{
|
||||
throw new McpException("Missing required argument 'message'");
|
||||
throw new McpException("Missing required argument 'message'", McpErrorCode.InvalidParams);
|
||||
}
|
||||
return new CallToolResponse()
|
||||
{
|
||||
|
|
@ -176,7 +176,7 @@ public class Program
|
|||
!request.Params.Arguments.TryGetValue("prompt", out var prompt) ||
|
||||
!request.Params.Arguments.TryGetValue("maxTokens", out var maxTokens))
|
||||
{
|
||||
throw new McpException("Missing required arguments 'prompt' and 'maxTokens'");
|
||||
throw new McpException("Missing required arguments 'prompt' and 'maxTokens'", McpErrorCode.InvalidParams);
|
||||
}
|
||||
var sampleResult = await request.Server.RequestSamplingAsync(CreateRequestSamplingParams(prompt.ToString(), "sampleLLM", Convert.ToInt32(maxTokens.ToString())),
|
||||
cancellationToken);
|
||||
|
|
@ -188,7 +188,7 @@ public class Program
|
|||
}
|
||||
else
|
||||
{
|
||||
throw new McpException($"Unknown tool: {request.Params.Name}");
|
||||
throw new McpException($"Unknown tool: '{request.Params.Name}'", McpErrorCode.InvalidParams);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
@ -220,9 +220,9 @@ public class Program
|
|||
var startIndexAsString = Encoding.UTF8.GetString(Convert.FromBase64String(requestParams.Cursor));
|
||||
startIndex = Convert.ToInt32(startIndexAsString);
|
||||
}
|
||||
catch
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new McpException("Invalid cursor");
|
||||
throw new McpException($"Invalid cursor: '{requestParams.Cursor}'", e, McpErrorCode.InvalidParams);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -244,7 +244,7 @@ public class Program
|
|||
{
|
||||
if (request.Params?.Uri is null)
|
||||
{
|
||||
throw new McpException("Missing required argument 'uri'");
|
||||
throw new McpException("Missing required argument 'uri'", McpErrorCode.InvalidParams);
|
||||
}
|
||||
|
||||
if (request.Params.Uri.StartsWith("test://dynamic/resource/"))
|
||||
|
|
@ -252,7 +252,7 @@ public class Program
|
|||
var id = request.Params.Uri.Split('/').LastOrDefault();
|
||||
if (string.IsNullOrEmpty(id))
|
||||
{
|
||||
throw new McpException("Invalid resource URI");
|
||||
throw new McpException($"Invalid resource URI: '{request.Params.Uri}'", McpErrorCode.InvalidParams);
|
||||
}
|
||||
|
||||
return new ReadResourceResult()
|
||||
|
|
@ -269,7 +269,7 @@ public class Program
|
|||
}
|
||||
|
||||
ResourceContents? contents = resourceContents.FirstOrDefault(r => r.Uri == request.Params.Uri) ??
|
||||
throw new McpException("Resource not found");
|
||||
throw new McpException($"Resource not found: '{request.Params.Uri}'", McpErrorCode.InvalidParams);
|
||||
|
||||
return new ReadResourceResult()
|
||||
{
|
||||
|
|
@ -316,7 +316,7 @@ public class Program
|
|||
{
|
||||
if (request.Params is null)
|
||||
{
|
||||
throw new McpException("Missing required parameter 'name'");
|
||||
throw new McpException("Missing required parameter 'name'", McpErrorCode.InvalidParams);
|
||||
}
|
||||
List<PromptMessage> messages = new();
|
||||
if (request.Params.Name == "simple_prompt")
|
||||
|
|
@ -366,7 +366,7 @@ public class Program
|
|||
}
|
||||
else
|
||||
{
|
||||
throw new McpException($"Unknown prompt: {request.Params.Name}");
|
||||
throw new McpException($"Unknown prompt: {request.Params.Name}", McpErrorCode.InvalidParams);
|
||||
}
|
||||
|
||||
return new GetPromptResult()
|
||||
|
|
|
|||
Loading…
Reference in a new issue