refactor: use builder pattern for CallToolResult and Resource (#652)

* use builder pattern for CallToolResult
* use builder pattern for Resource
* simplify lambda expressions in tests
This commit is contained in:
codeboyzhou 2025-11-21 20:44:29 +08:00 • committed by GitHub
commit 7e950ebb6c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 405 additions and 202 deletions

View file

@ -7,7 +7,6 @@ package io.modelcontextprotocol.server;
import java.time.Duration;
import java.util.List;
import io.modelcontextprotocol.spec.McpError;
import io.modelcontextprotocol.spec.McpSchema;
import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
import io.modelcontextprotocol.spec.McpSchema.GetPromptResult;
@ -86,7 +85,7 @@ public abstract class AbstractMcpAsyncServerTests {
void testImmediateClose() {
var mcpAsyncServer = prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build();
assertThatCode(() -> mcpAsyncServer.close()).doesNotThrowAnyException();
assertThatCode(mcpAsyncServer::close).doesNotThrowAnyException();
}
// ---------------------------------------
@ -104,8 +103,9 @@ public abstract class AbstractMcpAsyncServerTests {
.capabilities(ServerCapabilities.builder().tools(true).build())
.build();
StepVerifier.create(mcpAsyncServer.addTool(new McpServerFeatures.AsyncToolSpecification(newTool,
(exchange, args) -> Mono.just(new CallToolResult(List.of(), false)))))
StepVerifier
.create(mcpAsyncServer.addTool(new McpServerFeatures.AsyncToolSpecification(newTool,
(exchange, args) -> Mono.just(CallToolResult.builder().content(List.of()).isError(false).build()))))
.verifyComplete();
assertThatCode(() -> mcpAsyncServer.closeGracefully().block(Duration.ofSeconds(10))).doesNotThrowAnyException();
@ -125,7 +125,8 @@ public abstract class AbstractMcpAsyncServerTests {
StepVerifier.create(mcpAsyncServer.addTool(McpServerFeatures.AsyncToolSpecification.builder()
.tool(newTool)
.callHandler((exchange, request) -> Mono.just(new CallToolResult(List.of(), false)))
.callHandler((exchange, request) -> Mono
.just(CallToolResult.builder().content(List.of()).isError(false).build()))
.build())).verifyComplete();
assertThatCode(() -> mcpAsyncServer.closeGracefully().block(Duration.ofSeconds(10))).doesNotThrowAnyException();
@ -142,11 +143,13 @@ public abstract class AbstractMcpAsyncServerTests {
var mcpAsyncServer = prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().tools(true).build())
.tool(duplicateTool, (exchange, args) -> Mono.just(new CallToolResult(List.of(), false)))
.tool(duplicateTool,
(exchange, args) -> Mono.just(CallToolResult.builder().content(List.of()).isError(false).build()))
.build();
StepVerifier.create(mcpAsyncServer.addTool(new McpServerFeatures.AsyncToolSpecification(duplicateTool,
(exchange, args) -> Mono.just(new CallToolResult(List.of(), false)))))
StepVerifier
.create(mcpAsyncServer.addTool(new McpServerFeatures.AsyncToolSpecification(duplicateTool,
(exchange, args) -> Mono.just(CallToolResult.builder().content(List.of()).isError(false).build()))))
.verifyComplete();
assertThatCode(() -> mcpAsyncServer.closeGracefully().block(Duration.ofSeconds(10))).doesNotThrowAnyException();
@ -162,12 +165,15 @@ public abstract class AbstractMcpAsyncServerTests {
var mcpAsyncServer = prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().tools(true).build())
.toolCall(duplicateTool, (exchange, request) -> Mono.just(new CallToolResult(List.of(), false)))
.toolCall(duplicateTool,
(exchange, request) -> Mono
.just(CallToolResult.builder().content(List.of()).isError(false).build()))
.build();
StepVerifier.create(mcpAsyncServer.addTool(McpServerFeatures.AsyncToolSpecification.builder()
.tool(duplicateTool)
.callHandler((exchange, request) -> Mono.just(new CallToolResult(List.of(), false)))
.callHandler((exchange, request) -> Mono
.just(CallToolResult.builder().content(List.of()).isError(false).build()))
.build())).verifyComplete();
assertThatCode(() -> mcpAsyncServer.closeGracefully().block(Duration.ofSeconds(10))).doesNotThrowAnyException();
@ -183,8 +189,12 @@ public abstract class AbstractMcpAsyncServerTests {
assertThatThrownBy(() -> prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().tools(true).build())
.toolCall(duplicateTool, (exchange, request) -> Mono.just(new CallToolResult(List.of(), false)))
.toolCall(duplicateTool, (exchange, request) -> Mono.just(new CallToolResult(List.of(), false))) // Duplicate!
.toolCall(duplicateTool,
(exchange, request) -> Mono
.just(CallToolResult.builder().content(List.of()).isError(false).build()))
.toolCall(duplicateTool,
(exchange, request) -> Mono
.just(CallToolResult.builder().content(List.of()).isError(false).build())) // Duplicate!
.build()).isInstanceOf(IllegalArgumentException.class)
.hasMessage("Tool with name 'duplicate-build-toolcall' is already registered.");
}
@ -200,11 +210,13 @@ public abstract class AbstractMcpAsyncServerTests {
List<McpServerFeatures.AsyncToolSpecification> specs = List.of(
McpServerFeatures.AsyncToolSpecification.builder()
.tool(duplicateTool)
.callHandler((exchange, request) -> Mono.just(new CallToolResult(List.of(), false)))
.callHandler((exchange, request) -> Mono
.just(CallToolResult.builder().content(List.of()).isError(false).build()))
.build(),
McpServerFeatures.AsyncToolSpecification.builder()
.tool(duplicateTool)
.callHandler((exchange, request) -> Mono.just(new CallToolResult(List.of(), false)))
.callHandler((exchange, request) -> Mono
.just(CallToolResult.builder().content(List.of()).isError(false).build()))
.build() // Duplicate!
);
@ -227,11 +239,13 @@ public abstract class AbstractMcpAsyncServerTests {
.capabilities(ServerCapabilities.builder().tools(true).build())
.tools(McpServerFeatures.AsyncToolSpecification.builder()
.tool(duplicateTool)
.callHandler((exchange, request) -> Mono.just(new CallToolResult(List.of(), false)))
.callHandler((exchange, request) -> Mono
.just(CallToolResult.builder().content(List.of()).isError(false).build()))
.build(),
McpServerFeatures.AsyncToolSpecification.builder()
.tool(duplicateTool)
.callHandler((exchange, request) -> Mono.just(new CallToolResult(List.of(), false)))
.callHandler((exchange, request) -> Mono
.just(CallToolResult.builder().content(List.of()).isError(false).build()))
.build() // Duplicate!
)
.build()).isInstanceOf(IllegalArgumentException.class)
@ -248,7 +262,9 @@ public abstract class AbstractMcpAsyncServerTests {
var mcpAsyncServer = prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().tools(true).build())
.toolCall(too, (exchange, request) -> Mono.just(new CallToolResult(List.of(), false)))
.toolCall(too,
(exchange, request) -> Mono
.just(CallToolResult.builder().content(List.of()).isError(false).build()))
.build();
StepVerifier.create(mcpAsyncServer.removeTool(TEST_TOOL_NAME)).verifyComplete();
@ -277,7 +293,8 @@ public abstract class AbstractMcpAsyncServerTests {
var mcpAsyncServer = prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().tools(true).build())
.toolCall(too, (exchange, args) -> Mono.just(new CallToolResult(List.of(), false)))
.toolCall(too,
(exchange, args) -> Mono.just(CallToolResult.builder().content(List.of()).isError(false).build()))
.build();
StepVerifier.create(mcpAsyncServer.notifyToolsListChanged()).verifyComplete();
@ -316,8 +333,13 @@ public abstract class AbstractMcpAsyncServerTests {
.capabilities(ServerCapabilities.builder().resources(true, false).build())
.build();
Resource resource = new Resource(TEST_RESOURCE_URI, "Test Resource", "text/plain", "Test resource description",
null);
Resource resource = Resource.builder()
.uri(TEST_RESOURCE_URI)
.name("Test Resource")
.title("Test Resource")
.mimeType("text/plain")
.description("Test resource description")
.build();
McpServerFeatures.AsyncResourceSpecification specification = new McpServerFeatures.AsyncResourceSpecification(
resource, (exchange, req) -> Mono.just(new ReadResourceResult(List.of())));
@ -345,8 +367,13 @@ public abstract class AbstractMcpAsyncServerTests {
// Create a server without resource capabilities
McpAsyncServer serverWithoutResources = prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build();
Resource resource = new Resource(TEST_RESOURCE_URI, "Test Resource", "text/plain", "Test resource description",
null);
Resource resource = Resource.builder()
.uri(TEST_RESOURCE_URI)
.name("Test Resource")
.title("Test Resource")
.mimeType("text/plain")
.description("Test resource description")
.build();
McpServerFeatures.AsyncResourceSpecification specification = new McpServerFeatures.AsyncResourceSpecification(
resource, (exchange, req) -> Mono.just(new ReadResourceResult(List.of())));
@ -373,8 +400,13 @@ public abstract class AbstractMcpAsyncServerTests {
.capabilities(ServerCapabilities.builder().resources(true, false).build())
.build();
Resource resource = new Resource(TEST_RESOURCE_URI, "Test Resource", "text/plain", "Test resource description",
null);
Resource resource = Resource.builder()
.uri(TEST_RESOURCE_URI)
.name("Test Resource")
.title("Test Resource")
.mimeType("text/plain")
.description("Test resource description")
.build();
McpServerFeatures.AsyncResourceSpecification specification = new McpServerFeatures.AsyncResourceSpecification(
resource, (exchange, req) -> Mono.just(new ReadResourceResult(List.of())));
@ -392,8 +424,13 @@ public abstract class AbstractMcpAsyncServerTests {
.capabilities(ServerCapabilities.builder().resources(true, false).build())
.build();
Resource resource = new Resource(TEST_RESOURCE_URI, "Test Resource", "text/plain", "Test resource description",
null);
Resource resource = Resource.builder()
.uri(TEST_RESOURCE_URI)
.name("Test Resource")
.title("Test Resource")
.mimeType("text/plain")
.description("Test resource description")
.build();
McpServerFeatures.AsyncResourceSpecification specification = new McpServerFeatures.AsyncResourceSpecification(
resource, (exchange, req) -> Mono.just(new ReadResourceResult(List.of())));

View file

@ -851,7 +851,7 @@ public abstract class AbstractMcpClientServerIntegrationTests {
@ParameterizedTest(name = "{0} : {displayName} ")
@MethodSource("clientsForTesting")
void testToolCallSuccessWithTranportContextExtraction(String clientType) {
void testToolCallSuccessWithTransportContextExtraction(String clientType) {
var clientBuilder = clientBuilders.get(clientType);
@ -1077,7 +1077,10 @@ public abstract class AbstractMcpClientServerIntegrationTests {
.logger("test-logger")
.data("Another error message")
.build()))
.thenReturn(new CallToolResult("Logging test completed", false));
.thenReturn(CallToolResult.builder()
.content(List.of(new McpSchema.TextContent("Logging test completed")))
.isError(false)
.build());
//@formatter:on
})
.build();
@ -1172,7 +1175,10 @@ public abstract class AbstractMcpClientServerIntegrationTests {
0.0, 1.0, "Another processing started")))
.then(exchange.progressNotification(
new McpSchema.ProgressNotification(progressToken, 1.0, 1.0, "Processing completed")))
.thenReturn(new CallToolResult(("Progress test completed"), false));
.thenReturn(CallToolResult.builder()
.content(List.of(new McpSchema.TextContent("Progress test completed")))
.isError(false)
.build());
})
.build();
@ -1326,7 +1332,10 @@ public abstract class AbstractMcpClientServerIntegrationTests {
assertThat(result).isNotNull();
}).then(Mono.fromCallable(() -> {
executionOrder.set(executionOrder.get() + "3");
return new CallToolResult("Async ping test completed", false);
return CallToolResult.builder()
.content(List.of(new McpSchema.TextContent("Async ping test completed")))
.isError(false)
.build();
}));
})
.build();

View file

@ -6,7 +6,6 @@ package io.modelcontextprotocol.server;
import java.util.List;
import io.modelcontextprotocol.spec.McpError;
import io.modelcontextprotocol.spec.McpSchema;
import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
import io.modelcontextprotocol.spec.McpSchema.GetPromptResult;
@ -78,14 +77,14 @@ public abstract class AbstractMcpSyncServerTests {
void testGracefulShutdown() {
var mcpSyncServer = prepareSyncServerBuilder().serverInfo("test-server", "1.0.0").build();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
void testImmediateClose() {
var mcpSyncServer = prepareSyncServerBuilder().serverInfo("test-server", "1.0.0").build();
assertThatCode(() -> mcpSyncServer.close()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::close).doesNotThrowAnyException();
}
@Test
@ -94,7 +93,7 @@ public abstract class AbstractMcpSyncServerTests {
assertThat(mcpSyncServer.getAsyncServer()).isNotNull();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
// ---------------------------------------
@ -114,10 +113,10 @@ public abstract class AbstractMcpSyncServerTests {
.inputSchema(EMPTY_JSON_SCHEMA)
.build();
assertThatCode(() -> mcpSyncServer.addTool(new McpServerFeatures.SyncToolSpecification(newTool,
(exchange, args) -> new CallToolResult(List.of(), false))))
(exchange, args) -> CallToolResult.builder().content(List.of()).isError(false).build())))
.doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
@ -134,10 +133,10 @@ public abstract class AbstractMcpSyncServerTests {
assertThatCode(() -> mcpSyncServer.addTool(McpServerFeatures.SyncToolSpecification.builder()
.tool(newTool)
.callHandler((exchange, request) -> new CallToolResult(List.of(), false))
.callHandler((exchange, request) -> CallToolResult.builder().content(List.of()).isError(false).build())
.build())).doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
@ -151,14 +150,14 @@ public abstract class AbstractMcpSyncServerTests {
var mcpSyncServer = prepareSyncServerBuilder().serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().tools(true).build())
.tool(duplicateTool, (exchange, args) -> new CallToolResult(List.of(), false))
.tool(duplicateTool, (exchange, args) -> CallToolResult.builder().content(List.of()).isError(false).build())
.build();
assertThatCode(() -> mcpSyncServer.addTool(new McpServerFeatures.SyncToolSpecification(duplicateTool,
(exchange, args) -> new CallToolResult(List.of(), false))))
(exchange, args) -> CallToolResult.builder().content(List.of()).isError(false).build())))
.doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
@ -171,15 +170,16 @@ public abstract class AbstractMcpSyncServerTests {
var mcpSyncServer = prepareSyncServerBuilder().serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().tools(true).build())
.toolCall(duplicateTool, (exchange, request) -> new CallToolResult(List.of(), false))
.toolCall(duplicateTool,
(exchange, request) -> CallToolResult.builder().content(List.of()).isError(false).build())
.build();
assertThatCode(() -> mcpSyncServer.addTool(McpServerFeatures.SyncToolSpecification.builder()
.tool(duplicateTool)
.callHandler((exchange, request) -> new CallToolResult(List.of(), false))
.callHandler((exchange, request) -> CallToolResult.builder().content(List.of()).isError(false).build())
.build())).doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
@ -192,8 +192,10 @@ public abstract class AbstractMcpSyncServerTests {
assertThatThrownBy(() -> prepareSyncServerBuilder().serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().tools(true).build())
.toolCall(duplicateTool, (exchange, request) -> new CallToolResult(List.of(), false))
.toolCall(duplicateTool, (exchange, request) -> new CallToolResult(List.of(), false)) // Duplicate!
.toolCall(duplicateTool,
(exchange, request) -> CallToolResult.builder().content(List.of()).isError(false).build())
.toolCall(duplicateTool,
(exchange, request) -> CallToolResult.builder().content(List.of()).isError(false).build()) // Duplicate!
.build()).isInstanceOf(IllegalArgumentException.class)
.hasMessage("Tool with name 'duplicate-build-toolcall' is already registered.");
}
@ -208,11 +210,13 @@ public abstract class AbstractMcpSyncServerTests {
List<McpServerFeatures.SyncToolSpecification> specs = List.of(
McpServerFeatures.SyncToolSpecification.builder()
.tool(duplicateTool)
.callHandler((exchange, request) -> new CallToolResult(List.of(), false))
.callHandler(
(exchange, request) -> CallToolResult.builder().content(List.of()).isError(false).build())
.build(),
McpServerFeatures.SyncToolSpecification.builder()
.tool(duplicateTool)
.callHandler((exchange, request) -> new CallToolResult(List.of(), false))
.callHandler(
(exchange, request) -> CallToolResult.builder().content(List.of()).isError(false).build())
.build() // Duplicate!
);
@ -235,11 +239,12 @@ public abstract class AbstractMcpSyncServerTests {
.capabilities(ServerCapabilities.builder().tools(true).build())
.tools(McpServerFeatures.SyncToolSpecification.builder()
.tool(duplicateTool)
.callHandler((exchange, request) -> new CallToolResult(List.of(), false))
.callHandler((exchange, request) -> CallToolResult.builder().content(List.of()).isError(false).build())
.build(),
McpServerFeatures.SyncToolSpecification.builder()
.tool(duplicateTool)
.callHandler((exchange, request) -> new CallToolResult(List.of(), false))
.callHandler((exchange,
request) -> CallToolResult.builder().content(List.of()).isError(false).build())
.build() // Duplicate!
)
.build()).isInstanceOf(IllegalArgumentException.class)
@ -256,12 +261,12 @@ public abstract class AbstractMcpSyncServerTests {
var mcpSyncServer = prepareSyncServerBuilder().serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().tools(true).build())
.toolCall(tool, (exchange, args) -> new CallToolResult(List.of(), false))
.toolCall(tool, (exchange, args) -> CallToolResult.builder().content(List.of()).isError(false).build())
.build();
assertThatCode(() -> mcpSyncServer.removeTool(TEST_TOOL_NAME)).doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
@ -272,16 +277,16 @@ public abstract class AbstractMcpSyncServerTests {
assertThatCode(() -> mcpSyncServer.removeTool("nonexistent-tool")).doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
void testNotifyToolsListChanged() {
var mcpSyncServer = prepareSyncServerBuilder().serverInfo("test-server", "1.0.0").build();
assertThatCode(() -> mcpSyncServer.notifyToolsListChanged()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::notifyToolsListChanged).doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
// ---------------------------------------
@ -292,9 +297,9 @@ public abstract class AbstractMcpSyncServerTests {
void testNotifyResourcesListChanged() {
var mcpSyncServer = prepareSyncServerBuilder().serverInfo("test-server", "1.0.0").build();
assertThatCode(() -> mcpSyncServer.notifyResourcesListChanged()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::notifyResourcesListChanged).doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
@ -305,7 +310,7 @@ public abstract class AbstractMcpSyncServerTests {
.notifyResourcesUpdated(new McpSchema.ResourcesUpdatedNotification(TEST_RESOURCE_URI)))
.doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
@ -314,14 +319,18 @@ public abstract class AbstractMcpSyncServerTests {
.capabilities(ServerCapabilities.builder().resources(true, false).build())
.build();
Resource resource = new Resource(TEST_RESOURCE_URI, "Test Resource", "text/plain", "Test resource description",
null);
Resource resource = Resource.builder()
.uri(TEST_RESOURCE_URI)
.name("Test Resource")
.mimeType("text/plain")
.description("Test resource description")
.build();
McpServerFeatures.SyncResourceSpecification specification = new McpServerFeatures.SyncResourceSpecification(
resource, (exchange, req) -> new ReadResourceResult(List.of()));
assertThatCode(() -> mcpSyncServer.addResource(specification)).doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
@ -334,15 +343,19 @@ public abstract class AbstractMcpSyncServerTests {
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Resource must not be null");
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
void testAddResourceWithoutCapability() {
var serverWithoutResources = prepareSyncServerBuilder().serverInfo("test-server", "1.0.0").build();
Resource resource = new Resource(TEST_RESOURCE_URI, "Test Resource", "text/plain", "Test resource description",
null);
Resource resource = Resource.builder()
.uri(TEST_RESOURCE_URI)
.name("Test Resource")
.mimeType("text/plain")
.description("Test resource description")
.build();
McpServerFeatures.SyncResourceSpecification specification = new McpServerFeatures.SyncResourceSpecification(
resource, (exchange, req) -> new ReadResourceResult(List.of()));
@ -366,8 +379,12 @@ public abstract class AbstractMcpSyncServerTests {
.capabilities(ServerCapabilities.builder().resources(true, false).build())
.build();
Resource resource = new Resource(TEST_RESOURCE_URI, "Test Resource", "text/plain", "Test resource description",
null);
Resource resource = Resource.builder()
.uri(TEST_RESOURCE_URI)
.name("Test Resource")
.mimeType("text/plain")
.description("Test resource description")
.build();
McpServerFeatures.SyncResourceSpecification specification = new McpServerFeatures.SyncResourceSpecification(
resource, (exchange, req) -> new ReadResourceResult(List.of()));
@ -377,7 +394,7 @@ public abstract class AbstractMcpSyncServerTests {
assertThat(resources).hasSize(1);
assertThat(resources.get(0).uri()).isEqualTo(TEST_RESOURCE_URI);
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
@ -386,15 +403,19 @@ public abstract class AbstractMcpSyncServerTests {
.capabilities(ServerCapabilities.builder().resources(true, false).build())
.build();
Resource resource = new Resource(TEST_RESOURCE_URI, "Test Resource", "text/plain", "Test resource description",
null);
Resource resource = Resource.builder()
.uri(TEST_RESOURCE_URI)
.name("Test Resource")
.mimeType("text/plain")
.description("Test resource description")
.build();
McpServerFeatures.SyncResourceSpecification specification = new McpServerFeatures.SyncResourceSpecification(
resource, (exchange, req) -> new ReadResourceResult(List.of()));
mcpSyncServer.addResource(specification);
assertThatCode(() -> mcpSyncServer.removeResource(TEST_RESOURCE_URI)).doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
@ -407,7 +428,7 @@ public abstract class AbstractMcpSyncServerTests {
// as per the new implementation that just logs a warning
assertThatCode(() -> mcpSyncServer.removeResource("nonexistent://resource")).doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
// ---------------------------------------
@ -432,7 +453,7 @@ public abstract class AbstractMcpSyncServerTests {
assertThatCode(() -> mcpSyncServer.addResourceTemplate(specification)).doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
@ -474,7 +495,7 @@ public abstract class AbstractMcpSyncServerTests {
assertThatCode(() -> mcpSyncServer.removeResourceTemplate("test://template/{id}")).doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
@ -496,7 +517,7 @@ public abstract class AbstractMcpSyncServerTests {
assertThatCode(() -> mcpSyncServer.removeResourceTemplate("nonexistent://template/{id}"))
.doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
@ -520,7 +541,7 @@ public abstract class AbstractMcpSyncServerTests {
assertThat(templates).isNotNull();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
// ---------------------------------------
@ -531,9 +552,9 @@ public abstract class AbstractMcpSyncServerTests {
void testNotifyPromptsListChanged() {
var mcpSyncServer = prepareSyncServerBuilder().serverInfo("test-server", "1.0.0").build();
assertThatCode(() -> mcpSyncServer.notifyPromptsListChanged()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::notifyPromptsListChanged).doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
@ -584,7 +605,7 @@ public abstract class AbstractMcpSyncServerTests {
assertThatCode(() -> mcpSyncServer.removePrompt(TEST_PROMPT_NAME)).doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
@Test
@ -595,7 +616,7 @@ public abstract class AbstractMcpSyncServerTests {
assertThatCode(() -> mcpSyncServer.removePrompt("nonexistent://template/{id}")).doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(mcpSyncServer::closeGracefully).doesNotThrowAnyException();
}
// ---------------------------------------
@ -617,7 +638,7 @@ public abstract class AbstractMcpSyncServerTests {
}))
.build();
assertThat(singleConsumerServer).isNotNull();
assertThatCode(() -> singleConsumerServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(singleConsumerServer::closeGracefully).doesNotThrowAnyException();
onClose();
// Test with multiple consumers
@ -633,7 +654,7 @@ public abstract class AbstractMcpSyncServerTests {
.build();
assertThat(multipleConsumersServer).isNotNull();
assertThatCode(() -> multipleConsumersServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(multipleConsumersServer::closeGracefully).doesNotThrowAnyException();
onClose();
// Test error handling
@ -644,14 +665,14 @@ public abstract class AbstractMcpSyncServerTests {
.build();
assertThat(errorHandlingServer).isNotNull();
assertThatCode(() -> errorHandlingServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(errorHandlingServer::closeGracefully).doesNotThrowAnyException();
onClose();
// Test without consumers
var noConsumersServer = prepareSyncServerBuilder().serverInfo("test-server", "1.0.0").build();
assertThat(noConsumersServer).isNotNull();
assertThatCode(() -> noConsumersServer.closeGracefully()).doesNotThrowAnyException();
assertThatCode(noConsumersServer::closeGracefully).doesNotThrowAnyException();
}
}

View file

@ -40,7 +40,7 @@ class AsyncToolSpecificationBuilderTest {
McpServerFeatures.AsyncToolSpecification specification = McpServerFeatures.AsyncToolSpecification.builder()
.tool(tool)
.callHandler((exchange, request) -> Mono
.just(new CallToolResult(List.of(new TextContent("Test result")), false)))
.just(CallToolResult.builder().content(List.of(new TextContent("Test result"))).isError(false).build()))
.build();
assertThat(specification).isNotNull();
@ -52,7 +52,8 @@ class AsyncToolSpecificationBuilderTest {
@Test
void builderShouldThrowExceptionWhenToolIsNull() {
assertThatThrownBy(() -> McpServerFeatures.AsyncToolSpecification.builder()
.callHandler((exchange, request) -> Mono.just(new CallToolResult(List.of(), false)))
.callHandler((exchange, request) -> Mono
.just(CallToolResult.builder().content(List.of()).isError(false).build()))
.build()).isInstanceOf(IllegalArgumentException.class).hasMessage("Tool must not be null");
}
@ -80,7 +81,8 @@ class AsyncToolSpecificationBuilderTest {
// Then - verify method chaining returns the same builder instance
assertThat(builder.tool(tool)).isSameAs(builder);
assertThat(builder.callHandler((exchange, request) -> Mono.just(new CallToolResult(List.of(), false))))
assertThat(builder.callHandler(
(exchange, request) -> Mono.just(CallToolResult.builder().content(List.of()).isError(false).build())))
.isSameAs(builder);
}
@ -96,7 +98,10 @@ class AsyncToolSpecificationBuilderTest {
McpServerFeatures.AsyncToolSpecification specification = McpServerFeatures.AsyncToolSpecification.builder()
.tool(tool)
.callHandler((exchange, request) -> {
return Mono.just(new CallToolResult(List.of(new TextContent(expectedResult)), false));
return Mono.just(CallToolResult.builder()
.content(List.of(new TextContent(expectedResult)))
.isError(false)
.build());
})
.build();
@ -124,8 +129,11 @@ class AsyncToolSpecificationBuilderTest {
// Test the deprecated constructor that takes a 'call' function
McpServerFeatures.AsyncToolSpecification specification = new McpServerFeatures.AsyncToolSpecification(tool,
(exchange, arguments) -> Mono
.just(new CallToolResult(List.of(new TextContent(expectedResult)), false)));
(exchange,
arguments) -> Mono.just(CallToolResult.builder()
.content(List.of(new TextContent(expectedResult)))
.isError(false)
.build()));
assertThat(specification).isNotNull();
assertThat(specification.tool()).isEqualTo(tool);
@ -169,7 +177,10 @@ class AsyncToolSpecificationBuilderTest {
// Create a sync tool specification
McpServerFeatures.SyncToolSpecification syncSpec = McpServerFeatures.SyncToolSpecification.builder()
.tool(tool)
.callHandler((exchange, request) -> new CallToolResult(List.of(new TextContent(expectedResult)), false))
.callHandler((exchange, request) -> CallToolResult.builder()
.content(List.of(new TextContent(expectedResult)))
.isError(false)
.build())
.build();
// Convert to async using fromSync
@ -209,7 +220,10 @@ class AsyncToolSpecificationBuilderTest {
// Create a sync tool specification using the deprecated constructor
McpServerFeatures.SyncToolSpecification syncSpec = new McpServerFeatures.SyncToolSpecification(tool,
(exchange, arguments) -> new CallToolResult(List.of(new TextContent(expectedResult)), false));
(exchange, arguments) -> CallToolResult.builder()
.content(List.of(new TextContent(expectedResult)))
.isError(false)
.build());
// Convert to async using fromSync
McpServerFeatures.AsyncToolSpecification asyncSpec = McpServerFeatures.AsyncToolSpecification

View file

@ -114,7 +114,10 @@ class HttpServletStatelessIntegrationTests {
var clientBuilder = clientBuilders.get(clientType);
var callResponse = new CallToolResult(List.of(new McpSchema.TextContent("CALL RESPONSE")), null);
var callResponse = CallToolResult.builder()
.content(List.of(new McpSchema.TextContent("CALL RESPONSE")))
.isError(false)
.build();
McpStatelessServerFeatures.SyncToolSpecification tool1 = new McpStatelessServerFeatures.SyncToolSpecification(
Tool.builder().name("tool1").title("tool1 description").inputSchema(EMPTY_JSON_SCHEMA).build(),
(transportContext, request) -> {

View file

@ -32,7 +32,10 @@ class SyncToolSpecificationBuilderTest {
McpServerFeatures.SyncToolSpecification specification = McpServerFeatures.SyncToolSpecification.builder()
.tool(tool)
.callHandler((exchange, request) -> new CallToolResult(List.of(new TextContent("Test result")), false))
.callHandler((exchange, request) -> CallToolResult.builder()
.content(List.of(new TextContent("Test result")))
.isError(false)
.build())
.build();
assertThat(specification).isNotNull();
@ -44,7 +47,7 @@ class SyncToolSpecificationBuilderTest {
@Test
void builderShouldThrowExceptionWhenToolIsNull() {
assertThatThrownBy(() -> McpServerFeatures.SyncToolSpecification.builder()
.callHandler((exchange, request) -> new CallToolResult(List.of(), false))
.callHandler((exchange, request) -> CallToolResult.builder().content(List.of()).isError(false).build())
.build()).isInstanceOf(IllegalArgumentException.class).hasMessage("Tool must not be null");
}
@ -64,7 +67,9 @@ class SyncToolSpecificationBuilderTest {
// Then - verify method chaining returns the same builder instance
assertThat(builder.tool(tool)).isSameAs(builder);
assertThat(builder.callHandler((exchange, request) -> new CallToolResult(List.of(), false))).isSameAs(builder);
assertThat(builder
.callHandler((exchange, request) -> CallToolResult.builder().content(List.of()).isError(false).build()))
.isSameAs(builder);
}
@Test
@ -80,7 +85,10 @@ class SyncToolSpecificationBuilderTest {
.tool(tool)
.callHandler((exchange, request) -> {
// Simple test implementation
return new CallToolResult(List.of(new TextContent(expectedResult)), false);
return CallToolResult.builder()
.content(List.of(new TextContent(expectedResult)))
.isError(false)
.build();
})
.build();