Skip to content

Commit 99943aa

Browse files
committed
feat: add optional size property to Resource with builder
1 parent b701a36 commit 99943aa

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,9 @@ public record Annotations( // @formatter:off
482482
* by clients to improve the LLM's understanding of available resources. It can be
483483
* thought of like a "hint" to the model.
484484
* @param mimeType The MIME type of this resource, if known.
485+
* @param size The size of the raw resource content, in bytes (i.e., before base64
486+
* encoding or any tokenization), if known. This can be used by Hosts to display file
487+
* sizes and estimate context window usage.
485488
* @param annotations Optional annotations for the client. The client can use
486489
* annotations to inform how objects are used or displayed.
487490
*/
@@ -492,7 +495,64 @@ public record Resource( // @formatter:off
492495
@JsonProperty("name") String name,
493496
@JsonProperty("description") String description,
494497
@JsonProperty("mimeType") String mimeType,
498+
@JsonProperty("size") Long size,
495499
@JsonProperty("annotations") Annotations annotations) implements Annotated {
500+
501+
/**
502+
* @deprecated Only exists for backwards-compatibility purposes. Use
503+
* {@link Resource#builder()} instead.
504+
*/
505+
@Deprecated
506+
public Resource(String uri, String name, String description, String mimeType, Annotations annotations) {
507+
this(uri, name, description, mimeType, null, annotations);
508+
}
509+
510+
public static Builder builder() {
511+
return new Builder();
512+
}
513+
514+
public static class Builder {
515+
private String uri;
516+
private String name;
517+
private String description;
518+
private String mimeType;
519+
private Long size;
520+
private Annotations annotations;
521+
522+
public Builder uri(String uri) {
523+
this.uri = uri;
524+
return this;
525+
}
526+
527+
public Builder name(String name) {
528+
this.name = name;
529+
return this;
530+
}
531+
532+
public Builder description(String description) {
533+
this.description = description;
534+
return this;
535+
}
536+
537+
public Builder mimeType(String mimeType) {
538+
this.mimeType = mimeType;
539+
return this;
540+
}
541+
542+
public Builder size(Long size) {
543+
this.size = size;
544+
return this;
545+
}
546+
547+
public Builder annotations(Annotations annotations) {
548+
this.annotations = annotations;
549+
return this;
550+
}
551+
552+
public Resource build() {
553+
return new Resource(uri, name, description, mimeType, size, annotations);
554+
}
555+
}
496556
} // @formatter:on
497557

498558
/**

mcp/src/test/java/io/modelcontextprotocol/spec/McpSchemaTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,29 @@ void testResource() throws Exception {
295295
{"uri":"resource://test","name":"Test Resource","description":"A test resource","mimeType":"text/plain","annotations":{"audience":["user","assistant"],"priority":0.8}}"""));
296296
}
297297

298+
@Test
299+
void testResourceBuilder() throws Exception {
300+
McpSchema.Annotations annotations = new McpSchema.Annotations(
301+
Arrays.asList(McpSchema.Role.USER, McpSchema.Role.ASSISTANT), 0.8);
302+
303+
McpSchema.Resource resource = McpSchema.Resource.builder()
304+
.uri("resource://test")
305+
.name("Test Resource")
306+
.description("A test resource")
307+
.mimeType("text/plain")
308+
.size(256L)
309+
.annotations(annotations)
310+
.build();
311+
312+
String value = mapper.writeValueAsString(resource);
313+
assertThatJson(value).when(Option.IGNORING_ARRAY_ORDER)
314+
.when(Option.IGNORING_EXTRA_ARRAY_ITEMS)
315+
.isObject()
316+
.isEqualTo(
317+
json("""
318+
{"uri":"resource://test","name":"Test Resource","description":"A test resource","mimeType":"text/plain","size":256,"annotations":{"audience":["user","assistant"],"priority":0.8}}"""));
319+
}
320+
298321
@Test
299322
void testResourceTemplate() throws Exception {
300323
McpSchema.Annotations annotations = new McpSchema.Annotations(Arrays.asList(McpSchema.Role.USER), 0.5);

0 commit comments

Comments
 (0)