forked from microsoft/mcp-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_example_java.java
More file actions
113 lines (94 loc) · 4.11 KB
/
client_example_java.java
File metadata and controls
113 lines (94 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.microsoft.mcp.sample.client;
import java.util.Map;
import org.springframework.web.reactive.function.client.WebClient;
import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.client.transport.WebFluxSseClientTransport;
import io.modelcontextprotocol.spec.McpClientTransport;
import io.modelcontextprotocol.spec.McpSchema.CallToolRequest;
import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
import io.modelcontextprotocol.spec.McpSchema.ListToolsResult;
/**
* Complete Java MCP Client Example
*
* This client demonstrates how to:
* 1. Connect to an MCP server using SSE transport
* 2. List available tools
* 3. Call various calculator tools
* 4. Handle responses from the server
*/
public class SDKClient {
public static void main(String[] args) {
// Create SSE transport pointing to the MCP server
var transport = new WebFluxSseClientTransport(
WebClient.builder().baseUrl("http://localhost:8080")
);
// Create and run the client
new SDKClient(transport).run();
}
private final McpClientTransport transport;
public SDKClient(McpClientTransport transport) {
this.transport = transport;
}
public void run() {
try {
// Create synchronous MCP client
var client = McpClient.sync(this.transport).build();
// Initialize the connection
client.initialize();
System.out.println("✅ Connected to MCP server successfully!");
// Verify connection with ping
client.ping();
System.out.println("✅ Server ping successful!");
// List available tools
ListToolsResult toolsList = client.listTools();
System.out.println("\n📋 Available Tools:");
toolsList.tools().forEach(tool -> {
System.out.println(" - " + tool.name() + ": " + tool.description());
});
// Test calculator operations
System.out.println("\n🧮 Testing Calculator Operations:");
// Addition
CallToolResult resultAdd = client.callTool(
new CallToolRequest("add", Map.of("a", 5.0, "b", 3.0))
);
System.out.println("Add 5 + 3 = " + extractResult(resultAdd));
// Subtraction
CallToolResult resultSubtract = client.callTool(
new CallToolRequest("subtract", Map.of("a", 10.0, "b", 4.0))
);
System.out.println("Subtract 10 - 4 = " + extractResult(resultSubtract));
// Multiplication
CallToolResult resultMultiply = client.callTool(
new CallToolRequest("multiply", Map.of("a", 6.0, "b", 7.0))
);
System.out.println("Multiply 6 × 7 = " + extractResult(resultMultiply));
// Division
CallToolResult resultDivide = client.callTool(
new CallToolRequest("divide", Map.of("a", 20.0, "b", 4.0))
);
System.out.println("Divide 20 ÷ 4 = " + extractResult(resultDivide));
// Help
CallToolResult resultHelp = client.callTool(
new CallToolRequest("help", Map.of())
);
System.out.println("\n📖 Help Information:");
System.out.println(extractResult(resultHelp));
System.out.println("\n✨ Client operations completed successfully!");
} catch (Exception e) {
System.err.println("❌ Error running MCP client: " + e.getMessage());
e.printStackTrace();
}
}
/**
* Extract the text result from a CallToolResult
*/
private String extractResult(CallToolResult result) {
if (result != null && result.content() != null && !result.content().isEmpty()) {
var firstContent = result.content().get(0);
if (firstContent.text() != null) {
return firstContent.text();
}
}
return "No result";
}
}