Skip to content

feat: Add ResourceLink #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions mcp/src/main/java/io/modelcontextprotocol/spec/McpSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -1473,8 +1473,9 @@ public record CompleteCompletion(
@JsonSubTypes({ @JsonSubTypes.Type(value = TextContent.class, name = "text"),
@JsonSubTypes.Type(value = ImageContent.class, name = "image"),
@JsonSubTypes.Type(value = AudioContent.class, name = "audio"),
@JsonSubTypes.Type(value = EmbeddedResource.class, name = "resource") })
public sealed interface Content permits TextContent, ImageContent, AudioContent, EmbeddedResource {
@JsonSubTypes.Type(value = EmbeddedResource.class, name = "resource"),
@JsonSubTypes.Type(value = ResourceLink.class, name = "resource_link") })
public sealed interface Content permits TextContent, ImageContent, AudioContent, EmbeddedResource, ResourceLink {

default String type() {
if (this instanceof TextContent) {
Expand All @@ -1489,6 +1490,9 @@ else if (this instanceof AudioContent) {
else if (this instanceof EmbeddedResource) {
return "resource";
}
else if (this instanceof ResourceLink) {
return "resource_link";
}
throw new IllegalArgumentException("Unknown content type: " + this);
}

Expand Down Expand Up @@ -1601,6 +1605,18 @@ public Double priority() {
}
}

@JsonInclude(JsonInclude.Include.NON_ABSENT)
@JsonIgnoreProperties(ignoreUnknown = true)
public record ResourceLink( // @formatter:off
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, the ResourceLink content is equivalent to the existing Resource type.
Unfortunately, Java doesn't allow record inheritance. Perhaps we should consider a common interface (not sure how to call it? Perhaps ResourceType ) that both Resource and ResourceLink would implement.

Also it will be useful to have Builder for the ResourceLink similar to the Resource's builder

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, I'll push an update

@JsonProperty("name") String name,
@JsonProperty("title") String title,
@JsonProperty("uri") String uri,
@JsonProperty("description") String description,
@JsonProperty("mimeType") String mimeType,
@JsonProperty("annotations") Annotations annotations,
@JsonProperty("size") Integer size) implements Annotated, Content { // @formatter:on
}

// ---------------------------
// Roots
// ---------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void testContentDeserializationWrongType() throws Exception {
{"type":"WRONG","text":"XXX"}""", McpSchema.TextContent.class))
.isInstanceOf(InvalidTypeIdException.class)
.hasMessageContaining(
"Could not resolve type id 'WRONG' as a subtype of `io.modelcontextprotocol.spec.McpSchema$TextContent`: known type ids = [audio, image, resource, text]");
"Could not resolve type id 'WRONG' as a subtype of `io.modelcontextprotocol.spec.McpSchema$TextContent`: known type ids = [audio, image, resource, resource_link, text]");
}

@Test
Expand Down Expand Up @@ -168,6 +168,36 @@ void testEmbeddedResourceWithBlobContentsDeserialization() throws Exception {
.isEqualTo("base64encodedblob");
}

@Test
void testResourceLink() throws Exception {
McpSchema.ResourceLink resourceLink = new McpSchema.ResourceLink("main.rs",
"Rust Software Application Main File", "file:///project/src/main.rs", "Primary application entry point",
"text/x-rust", null, null);
String value = mapper.writeValueAsString(resourceLink);

assertThatJson(value).when(Option.IGNORING_ARRAY_ORDER)
.when(Option.IGNORING_EXTRA_ARRAY_ITEMS)
.isObject()
.isEqualTo(
json("""
{"type":"resource_link","name":"main.rs","title":"Rust Software Application Main File","uri":"file:///project/src/main.rs","description":"Primary application entry point","mimeType":"text/x-rust"}"""));
}

@Test
void testResourceLinkDeserialization() throws Exception {
McpSchema.ResourceLink resourceLink = mapper.readValue(
"""
{"type":"resource_link","name":"main.rs","title":"Rust Software Application Main File","uri":"file:///project/src/main.rs","description":"Primary application entry point","mimeType":"text/x-rust"}""",
McpSchema.ResourceLink.class);
assertThat(resourceLink).isNotNull();
assertThat(resourceLink.type()).isEqualTo("resource_link");
assertThat(resourceLink.name()).isEqualTo("main.rs");
assertThat(resourceLink.title()).isEqualTo("Rust Software Application Main File");
assertThat(resourceLink.uri()).isEqualTo("file:///project/src/main.rs");
assertThat(resourceLink.description()).isEqualTo("Primary application entry point");
assertThat(resourceLink.mimeType()).isEqualTo("text/x-rust");
}

// JSON-RPC Message Types Tests

@Test
Expand Down