Skip to content

Commit 33a25a1

Browse files
committed
chore: make Resource.uri and Resource.name required
1 parent eae7fd6 commit 33a25a1

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,14 @@ public Builder annotations(Annotations annotations) {
550550
}
551551

552552
public Resource build() {
553+
if (uri == null) {
554+
throw new IllegalStateException("uri cannot be null");
555+
}
556+
557+
if (name == null) {
558+
throw new IllegalStateException("name cannot be null");
559+
}
560+
553561
return new Resource(uri, name, description, mimeType, size, annotations);
554562
}
555563
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,36 @@ void testResourceBuilder() throws Exception {
318318
{"uri":"resource://test","name":"Test Resource","description":"A test resource","mimeType":"text/plain","size":256,"annotations":{"audience":["user","assistant"],"priority":0.8}}"""));
319319
}
320320

321+
@Test
322+
void testResourceBuilderUriRequired() {
323+
McpSchema.Annotations annotations = new McpSchema.Annotations(
324+
Arrays.asList(McpSchema.Role.USER, McpSchema.Role.ASSISTANT), 0.8);
325+
326+
McpSchema.Resource.Builder resourceBuilder = McpSchema.Resource.builder()
327+
.name("Test Resource")
328+
.description("A test resource")
329+
.mimeType("text/plain")
330+
.size(256L)
331+
.annotations(annotations);
332+
333+
assertThatThrownBy(resourceBuilder::build).isInstanceOf(IllegalStateException.class);
334+
}
335+
336+
@Test
337+
void testResourceBuilderNameRequired() {
338+
McpSchema.Annotations annotations = new McpSchema.Annotations(
339+
Arrays.asList(McpSchema.Role.USER, McpSchema.Role.ASSISTANT), 0.8);
340+
341+
McpSchema.Resource.Builder resourceBuilder = McpSchema.Resource.builder()
342+
.uri("resource://test")
343+
.description("A test resource")
344+
.mimeType("text/plain")
345+
.size(256L)
346+
.annotations(annotations);
347+
348+
assertThatThrownBy(resourceBuilder::build).isInstanceOf(IllegalStateException.class);
349+
}
350+
321351
@Test
322352
void testResourceTemplate() throws Exception {
323353
McpSchema.Annotations annotations = new McpSchema.Annotations(Arrays.asList(McpSchema.Role.USER), 0.5);

0 commit comments

Comments
 (0)