Skip to content

Commit 1edd1b6

Browse files
committed
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 <[email protected]>
1 parent a14ef42 commit 1edd1b6

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

mcp/src/main/java/io/modelcontextprotocol/spec/McpError.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package io.modelcontextprotocol.spec;
66

77
import io.modelcontextprotocol.spec.McpSchema.JSONRPCResponse.JSONRPCError;
8+
import io.modelcontextprotocol.util.Assert;
89

910
public class McpError extends RuntimeException {
1011

@@ -15,6 +16,7 @@ public McpError(JSONRPCError jsonRpcError) {
1516
this.jsonRpcError = jsonRpcError;
1617
}
1718

19+
@Deprecated
1820
public McpError(Object error) {
1921
super(error.toString());
2022
}
@@ -23,4 +25,37 @@ public JSONRPCError getJsonRpcError() {
2325
return jsonRpcError;
2426
}
2527

28+
public static Builder builder(int errorCode) {
29+
return new Builder(errorCode);
30+
}
31+
32+
public static class Builder {
33+
34+
private final int code;
35+
36+
private String message;
37+
38+
private Object data;
39+
40+
private Builder(int code) {
41+
this.code = code;
42+
}
43+
44+
public Builder message(String message) {
45+
this.message = message;
46+
return this;
47+
}
48+
49+
public Builder data(Object data) {
50+
this.data = data;
51+
return this;
52+
}
53+
54+
public McpError build() {
55+
Assert.hasText(message, "message must not be empty");
56+
return new McpError(new JSONRPCError(code, message, data));
57+
}
58+
59+
}
60+
2661
}

mcp/src/main/java/io/modelcontextprotocol/spec/McpSchema.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,21 @@ public record ResourceCapabilities(@JsonProperty("subscribe") Boolean subscribe,
523523
public record ToolCapabilities(@JsonProperty("listChanged") Boolean listChanged) {
524524
}
525525

526+
/**
527+
* Create a mutated copy of this object with the specified changes.
528+
* @return A new Builder instance with the same values as this object.
529+
*/
530+
public Builder mutate() {
531+
var builder = new Builder();
532+
builder.completions = this.completions;
533+
builder.experimental = this.experimental;
534+
builder.logging = this.logging;
535+
builder.prompts = this.prompts;
536+
builder.resources = this.resources;
537+
builder.tools = this.tools;
538+
return builder;
539+
}
540+
526541
public static Builder builder() {
527542
return new Builder();
528543
}

0 commit comments

Comments
 (0)