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 all commits
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
94 changes: 91 additions & 3 deletions mcp/src/main/java/io/modelcontextprotocol/spec/McpSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,22 @@ public record Annotations( // @formatter:off
@JsonProperty("priority") Double priority) {
} // @formatter:on

public interface ResourceType {

String uri();

String name();

String description();

String mimeType();

Long size();

Annotations annotations();

}

/**
* A known resource that the server is capable of reading.
*
Expand All @@ -503,7 +519,7 @@ public record Resource( // @formatter:off
@JsonProperty("description") String description,
@JsonProperty("mimeType") String mimeType,
@JsonProperty("size") Long size,
@JsonProperty("annotations") Annotations annotations) implements Annotated {
@JsonProperty("annotations") Annotations annotations) implements Annotated, ResourceType {

/**
* @deprecated Only exists for backwards-compatibility purposes. Use
Expand Down Expand Up @@ -1473,8 +1489,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 +1506,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 +1621,74 @@ 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("uri") String uri,
@JsonProperty("description") String description,
@JsonProperty("mimeType") String mimeType,
@JsonProperty("annotations") Annotations annotations,
@JsonProperty("size") Long size) implements Annotated, Content, ResourceType { // @formatter:on

public static Builder builder() {
return new Builder();
}

public static class Builder {

private String name;

private String uri;

private String description;

private String mimeType;

private Annotations annotations;

private Long size;

public Builder name(String name) {
this.name = name;
return this;
}

public Builder uri(String uri) {
this.uri = uri;
return this;
}

public Builder description(String description) {
this.description = description;
return this;
}

public Builder mimeType(String mimeType) {
this.mimeType = mimeType;
return this;
}

public Builder annotations(Annotations annotations) {
this.annotations = annotations;
return this;
}

public Builder size(Long size) {
this.size = size;
return this;
}

public ResourceLink build() {
Assert.hasText(uri, "uri must not be empty");
Assert.hasText(name, "name must not be empty");

return new ResourceLink(name, uri, description, mimeType, annotations, size);
}

}
}

// ---------------------------
// Roots
// ---------------------------
Expand Down
39 changes: 33 additions & 6 deletions mcp/src/test/java/io/modelcontextprotocol/spec/McpSchemaTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
*/
package io.modelcontextprotocol.spec;

import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.json;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;

import io.modelcontextprotocol.spec.McpSchema.TextResourceContents;
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.json;
import net.javacrumbs.jsonunit.core.Option;

/**
Expand Down Expand Up @@ -60,7 +59,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 +167,34 @@ void testEmbeddedResourceWithBlobContentsDeserialization() throws Exception {
.isEqualTo("base64encodedblob");
}

@Test
void testResourceLink() throws Exception {
McpSchema.ResourceLink resourceLink = new McpSchema.ResourceLink("main.rs", "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","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","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.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