Skip to content

Commit c8c08dd

Browse files
committed
Add legacy constructors for backwards compatibility in McpSchema record types
As a followup of #372 introduce new constructors to several record types in McpSchema (Resource, ResourceTemplate, Prompt, PromptArgument, Tool, PromptReference, and ResourceLink) to improve backwards compatibility with previous code versions. Signed-off-by: Christian Tzolov <[email protected]>
1 parent 901175e commit c8c08dd

File tree

1 file changed

+62
-15
lines changed

1 file changed

+62
-15
lines changed

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

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,16 @@ public record Resource( // @formatter:off
578578
@JsonProperty("size") Long size,
579579
@JsonProperty("annotations") Annotations annotations) implements Annotated, ResourceContent {// @formatter:on
580580

581+
/**
582+
* @deprecated Only exists for backwards-compatibility purposes. Use
583+
* {@link Resource#builder()} instead.
584+
*/
585+
@Deprecated
586+
public Resource(String uri, String name, String description, String mimeType, Long size,
587+
Annotations annotations) {
588+
this(uri, name, null, description, mimeType, null, annotations);
589+
}
590+
581591
/**
582592
* @deprecated Only exists for backwards-compatibility purposes. Use
583593
* {@link Resource#builder()} instead.
@@ -677,8 +687,13 @@ public record ResourceTemplate( // @formatter:off
677687
@JsonProperty("title") String title,
678688
@JsonProperty("description") String description,
679689
@JsonProperty("mimeType") String mimeType,
680-
@JsonProperty("annotations") Annotations annotations) implements Annotated, BaseMetadata {
681-
} // @formatter:on
690+
@JsonProperty("annotations") Annotations annotations) implements Annotated, BaseMetadata {// @formatter:on
691+
692+
public ResourceTemplate(String uriTemplate, String name, String description, String mimeType,
693+
Annotations annotations) {
694+
this(uriTemplate, name, null, description, mimeType, annotations);
695+
}
696+
}
682697

683698
@JsonInclude(JsonInclude.Include.NON_ABSENT)
684699
@JsonIgnoreProperties(ignoreUnknown = true)
@@ -802,8 +817,12 @@ public record Prompt( // @formatter:off
802817
@JsonProperty("name") String name,
803818
@JsonProperty("title") String title,
804819
@JsonProperty("description") String description,
805-
@JsonProperty("arguments") List<PromptArgument> arguments) implements BaseMetadata {
806-
} // @formatter:on
820+
@JsonProperty("arguments") List<PromptArgument> arguments) implements BaseMetadata { // @formatter:on
821+
822+
public Prompt(String name, String description, List<PromptArgument> arguments) {
823+
this(name, null, description, arguments != null ? arguments : new ArrayList<>());
824+
}
825+
}
807826

808827
/**
809828
* Describes an argument that a prompt can accept.
@@ -819,8 +838,12 @@ public record PromptArgument( // @formatter:off
819838
@JsonProperty("name") String name,
820839
@JsonProperty("title") String title,
821840
@JsonProperty("description") String description,
822-
@JsonProperty("required") Boolean required) implements BaseMetadata {
823-
}// @formatter:on
841+
@JsonProperty("required") Boolean required) implements BaseMetadata {// @formatter:on
842+
843+
public PromptArgument(String name, String description, Boolean required) {
844+
this(name, null, description, required);
845+
}
846+
}
824847

825848
/**
826849
* Describes a message returned as part of a prompt.
@@ -946,6 +969,15 @@ public record Tool( // @formatter:off
946969
@JsonProperty("inputSchema") JsonSchema inputSchema,
947970
@JsonProperty("annotations") ToolAnnotations annotations) implements BaseMetadata { // @formatter:on
948971

972+
/**
973+
* @deprecated Only exists for backwards-compatibility purposes. Use
974+
* {@link Tool#builder()} instead.
975+
*/
976+
@Deprecated
977+
public Tool(String name, String description, JsonSchema inputSchema, ToolAnnotations annotations) {
978+
this(name, null, description, inputSchema, annotations);
979+
}
980+
949981
public Tool(String name, String description, String schema) {
950982
this(name, null, description, parseSchema(schema), null);
951983
}
@@ -1688,17 +1720,21 @@ public sealed interface CompleteReference permits PromptReference, ResourceRefer
16881720
public record PromptReference(// @formatter:off
16891721
@JsonProperty("type") String type,
16901722
@JsonProperty("name") String name,
1691-
@JsonProperty("title") String title ) implements McpSchema.CompleteReference, BaseMetadata {
1723+
@JsonProperty("title") String title ) implements McpSchema.CompleteReference, BaseMetadata { // @formatter:on
16921724

1693-
public PromptReference(String name) {
1694-
this("ref/prompt", name, null);
1695-
}
1725+
public PromptReference(String type, String name) {
1726+
this(type, name, null);
1727+
}
16961728

1697-
@Override
1698-
public String identifier() {
1699-
return name();
1700-
}
1701-
}// @formatter:on
1729+
public PromptReference(String name) {
1730+
this("ref/prompt", name, null);
1731+
}
1732+
1733+
@Override
1734+
public String identifier() {
1735+
return name();
1736+
}
1737+
}
17021738

17031739
@JsonInclude(JsonInclude.Include.NON_ABSENT)
17041740
@JsonIgnoreProperties(ignoreUnknown = true)
@@ -1911,6 +1947,17 @@ public record ResourceLink( // @formatter:off
19111947
@JsonProperty("size") Long size,
19121948
@JsonProperty("annotations") Annotations annotations) implements Annotated, Content, ResourceContent { // @formatter:on
19131949

1950+
/**
1951+
* @deprecated Only exists for backwards-compatibility purposes. Use
1952+
* {@link ResourceLink#ResourceLink(String, String, String, String, String, Long, Annotations)}
1953+
* instead.
1954+
*/
1955+
@Deprecated
1956+
public ResourceLink(String name, String uri, String description, String mimeType, Long size,
1957+
Annotations annotations) {
1958+
this(name, null, uri, description, mimeType, size, annotations);
1959+
}
1960+
19141961
public static Builder builder() {
19151962
return new Builder();
19161963
}

0 commit comments

Comments
 (0)