MCPcopy Create free account
hub / github.com/DerekYRC/mini-claude-code / McpClient

Class McpClient

src/main/java/org/miniclaudecode/mcp/McpClient.java:16–58  ·  view source on GitHub ↗

教学版 MCP client。 真实 MCP client 会通过 transport 发送 tools/list 和 tools/call; 这里用内存 handler 模拟外部 server,突出“发现工具”和“调用工具”两件事。

Source from the content-addressed store, hash-verified

14 * 这里用内存 handler 模拟外部 server,突出“发现工具”和“调用工具”两件事。
15 */
16public class McpClient {
17
18 public interface Handler {
19
20 String handle(JSONObject input);
21 }
22
23 private final String name;
24
25 private List<McpToolDefinition> tools = Collections.emptyList();
26
27 private Map<String, Handler> handlers = new LinkedHashMap<>();
28
29 public McpClient(String name) {
30 this.name = name;
31 }
32
33 public void register(List<McpToolDefinition> tools, Map<String, Handler> handlers) {
34 this.tools = tools == null ? Collections.emptyList() : tools;
35 this.handlers = handlers == null ? new LinkedHashMap<>() : handlers;
36 }
37
38 public String callTool(String toolName, JSONObject input) {
39 Handler handler = handlers.get(toolName);
40 if (handler == null) {
41 return "MCP error: unknown tool '" + toolName + "'";
42 }
43 try {
44 return handler.handle(input == null ? new JSONObject() : input);
45 }
46 catch (Exception e) {
47 return "MCP error: " + e.getMessage();
48 }
49 }
50
51 public String getName() {
52 return name;
53 }
54
55 public List<McpToolDefinition> getTools() {
56 return tools;
57 }
58}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected