feat: Add builder pattern for McpError and mutate method for capabilities
- Add builder pattern to McpError for structured error creation with validation - Deprecate McpError(Object) constructor in favor of builder approach - Add mutate() method to server capabilities for creating modified copies Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
This commit is contained in:
parent
a14ef425d6
commit
1edd1b64db
2 changed files with 50 additions and 0 deletions
|
|
@ -5,6 +5,7 @@
|
|||
package io.modelcontextprotocol.spec;
|
||||
|
||||
import io.modelcontextprotocol.spec.McpSchema.JSONRPCResponse.JSONRPCError;
|
||||
import io.modelcontextprotocol.util.Assert;
|
||||
|
||||
public class McpError extends RuntimeException {
|
||||
|
||||
|
|
@ -15,6 +16,7 @@ public class McpError extends RuntimeException {
|
|||
this.jsonRpcError = jsonRpcError;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public McpError(Object error) {
|
||||
super(error.toString());
|
||||
}
|
||||
|
|
@ -23,4 +25,37 @@ public class McpError extends RuntimeException {
|
|||
return jsonRpcError;
|
||||
}
|
||||
|
||||
public static Builder builder(int errorCode) {
|
||||
return new Builder(errorCode);
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
||||
private final int code;
|
||||
|
||||
private String message;
|
||||
|
||||
private Object data;
|
||||
|
||||
private Builder(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public Builder message(String message) {
|
||||
this.message = message;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder data(Object data) {
|
||||
this.data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
public McpError build() {
|
||||
Assert.hasText(message, "message must not be empty");
|
||||
return new McpError(new JSONRPCError(code, message, data));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -523,6 +523,21 @@ public final class McpSchema {
|
|||
public record ToolCapabilities(@JsonProperty("listChanged") Boolean listChanged) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a mutated copy of this object with the specified changes.
|
||||
* @return A new Builder instance with the same values as this object.
|
||||
*/
|
||||
public Builder mutate() {
|
||||
var builder = new Builder();
|
||||
builder.completions = this.completions;
|
||||
builder.experimental = this.experimental;
|
||||
builder.logging = this.logging;
|
||||
builder.prompts = this.prompts;
|
||||
builder.resources = this.resources;
|
||||
builder.tools = this.tools;
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue