Skip to content

Commit 796012b

Browse files
committed
Convert client code to sync
Signed-off-by: Dariusz Jędrzejczyk <dariusz.jedrzejczyk@broadcom.com>
1 parent 0b4060b commit 796012b

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

conformance-tests/client-jdk-http-client/src/main/java/io/modelcontextprotocol/conformance/client/ConformanceJdkClientMcpClient.java

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import java.time.Duration;
44

5-
import io.modelcontextprotocol.client.McpAsyncClient;
65
import io.modelcontextprotocol.client.McpClient;
6+
import io.modelcontextprotocol.client.McpSyncClient;
77
import io.modelcontextprotocol.client.transport.HttpClientStreamableHttpTransport;
88
import io.modelcontextprotocol.spec.McpSchema;
99

@@ -74,12 +74,12 @@ public static void main(String[] args) {
7474
/**
7575
* Helper method to create and configure an MCP client with transport.
7676
* @param serverUrl the URL of the MCP server
77-
* @return configured McpAsyncClient instance
77+
* @return configured McpSyncClient instance
7878
*/
79-
private static McpAsyncClient createClient(String serverUrl) {
79+
private static McpSyncClient createClient(String serverUrl) {
8080
HttpClientStreamableHttpTransport transport = HttpClientStreamableHttpTransport.builder(serverUrl).build();
8181

82-
return McpClient.async(transport)
82+
return McpClient.sync(transport)
8383
.clientInfo(new McpSchema.Implementation("test-client", "1.0.0"))
8484
.requestTimeout(Duration.ofSeconds(30))
8585
.build();
@@ -88,19 +88,19 @@ private static McpAsyncClient createClient(String serverUrl) {
8888
/**
8989
* Helper method to create and configure an MCP client with elicitation support.
9090
* @param serverUrl the URL of the MCP server
91-
* @return configured McpAsyncClient instance with elicitation handler
91+
* @return configured McpSyncClient instance with elicitation handler
9292
*/
93-
private static McpAsyncClient createClientWithElicitation(String serverUrl) {
93+
private static McpSyncClient createClientWithElicitation(String serverUrl) {
9494
HttpClientStreamableHttpTransport transport = HttpClientStreamableHttpTransport.builder(serverUrl).build();
9595

9696
// Build client capabilities with elicitation support
9797
var capabilities = McpSchema.ClientCapabilities.builder().elicitation().build();
9898

99-
return McpClient.async(transport)
99+
return McpClient.sync(transport)
100100
.clientInfo(new McpSchema.Implementation("test-client", "1.0.0"))
101101
.requestTimeout(Duration.ofSeconds(30))
102102
.capabilities(capabilities)
103-
.elicitation(request -> reactor.core.publisher.Mono.fromCallable(() -> {
103+
.elicitation(request -> {
104104
// Apply default values from the schema to create the content
105105
var content = new java.util.HashMap<String, Object>();
106106
var schema = request.requestedSchema();
@@ -121,7 +121,7 @@ private static McpAsyncClient createClientWithElicitation(String serverUrl) {
121121

122122
// Return accept action with the defaults applied
123123
return new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, content, null);
124-
}))
124+
})
125125
.build();
126126
}
127127

@@ -131,11 +131,11 @@ private static McpAsyncClient createClientWithElicitation(String serverUrl) {
131131
* @throws Exception if any error occurs during execution
132132
*/
133133
private static void runInitializeScenario(String serverUrl) throws Exception {
134-
McpAsyncClient client = createClient(serverUrl);
134+
McpSyncClient client = createClient(serverUrl);
135135

136136
try {
137137
// Initialize client
138-
client.initialize().block();
138+
client.initialize();
139139

140140
System.out.println("Successfully connected to MCP server");
141141
}
@@ -152,16 +152,16 @@ private static void runInitializeScenario(String serverUrl) throws Exception {
152152
* @throws Exception if any error occurs during execution
153153
*/
154154
private static void runToolsCallScenario(String serverUrl) throws Exception {
155-
McpAsyncClient client = createClient(serverUrl);
155+
McpSyncClient client = createClient(serverUrl);
156156

157157
try {
158158
// Initialize client
159-
client.initialize().block();
159+
client.initialize();
160160

161161
System.out.println("Successfully connected to MCP server");
162162

163163
// List available tools
164-
McpSchema.ListToolsResult toolsResult = client.listTools().block();
164+
McpSchema.ListToolsResult toolsResult = client.listTools();
165165
System.out.println("Successfully listed tools");
166166

167167
// Call the add_numbers tool if it exists
@@ -174,8 +174,7 @@ private static void runToolsCallScenario(String serverUrl) throws Exception {
174174
arguments.put("b", 3);
175175

176176
McpSchema.CallToolResult result = client
177-
.callTool(new McpSchema.CallToolRequest("add_numbers", arguments))
178-
.block();
177+
.callTool(new McpSchema.CallToolRequest("add_numbers", arguments));
179178

180179
System.out.println("Successfully called add_numbers tool");
181180
if (result != null && result.content() != null) {
@@ -200,16 +199,16 @@ private static void runToolsCallScenario(String serverUrl) throws Exception {
200199
* @throws Exception if any error occurs during execution
201200
*/
202201
private static void runElicitationDefaultsScenario(String serverUrl) throws Exception {
203-
McpAsyncClient client = createClientWithElicitation(serverUrl);
202+
McpSyncClient client = createClientWithElicitation(serverUrl);
204203

205204
try {
206205
// Initialize client
207-
client.initialize().block();
206+
client.initialize();
208207

209208
System.out.println("Successfully connected to MCP server");
210209

211210
// List available tools
212-
McpSchema.ListToolsResult toolsResult = client.listTools().block();
211+
McpSchema.ListToolsResult toolsResult = client.listTools();
213212
System.out.println("Successfully listed tools");
214213

215214
// Call the test_client_elicitation_defaults tool if it exists
@@ -220,8 +219,7 @@ private static void runElicitationDefaultsScenario(String serverUrl) throws Exce
220219
var arguments = new java.util.HashMap<String, Object>();
221220

222221
McpSchema.CallToolResult result = client
223-
.callTool(new McpSchema.CallToolRequest("test_client_elicitation_defaults", arguments))
224-
.block();
222+
.callTool(new McpSchema.CallToolRequest("test_client_elicitation_defaults", arguments));
225223

226224
System.out.println("Successfully called test_client_elicitation_defaults tool");
227225
if (result != null && result.content() != null) {
@@ -246,16 +244,16 @@ private static void runElicitationDefaultsScenario(String serverUrl) throws Exce
246244
* @throws Exception if any error occurs during execution
247245
*/
248246
private static void runSSERetryScenario(String serverUrl) throws Exception {
249-
McpAsyncClient client = createClient(serverUrl);
247+
McpSyncClient client = createClient(serverUrl);
250248

251249
try {
252250
// Initialize client
253-
client.initialize().block();
251+
client.initialize();
254252

255253
System.out.println("Successfully connected to MCP server");
256254

257255
// List available tools
258-
McpSchema.ListToolsResult toolsResult = client.listTools().block();
256+
McpSchema.ListToolsResult toolsResult = client.listTools();
259257
System.out.println("Successfully listed tools");
260258

261259
// Call the test_reconnection tool if it exists
@@ -267,8 +265,7 @@ private static void runSSERetryScenario(String serverUrl) throws Exception {
267265
var arguments = new java.util.HashMap<String, Object>();
268266

269267
McpSchema.CallToolResult result = client
270-
.callTool(new McpSchema.CallToolRequest("test_reconnection", arguments))
271-
.block();
268+
.callTool(new McpSchema.CallToolRequest("test_reconnection", arguments));
272269

273270
System.out.println("Successfully called test_reconnection tool");
274271
if (result != null && result.content() != null) {

0 commit comments

Comments
 (0)