@@ -38,6 +38,40 @@ Detailed UML class diagrams showing the relationships between components can be
3838
3939## Usage
4040
41+ ### Sync Client Example
42+
43+ ``` java
44+ // Create and initialize sync client
45+ ServerParameters params = ServerParameters . builder(" npx" )
46+ .args(" -y" , " @modelcontextprotocol/server-everything" , " dir" )
47+ .build();
48+
49+ try (McpSyncClient client = McpClient . sync(new StdioServerTransport (params))) {
50+ // Initialize connection
51+ McpSchema . InitializeResult initResult = client. initialize();
52+
53+ // List tools synchronously
54+ McpSchema . ListToolsResult tools = client. listTools(null );
55+
56+ // Call tool synchronously
57+ McpSchema . CallToolResult result = client. callTool(
58+ new McpSchema .CallToolRequest (" echo" , Map . of(" message" , " Hello!" ))
59+ );
60+
61+ // Resource management
62+ McpSchema . ListResourcesResult resources = client. listResources(null );
63+ McpSchema . ReadResourceResult resource = client. readResource(
64+ new McpSchema .ReadResourceRequest (" resource-uri" )
65+ );
66+
67+ // Prompt management
68+ ListPromptsResult prompts = client. listPrompts(null );
69+ GetPromptResult prompt = client. getPrompt(
70+ new McpSchema .GetPromptRequest (" prompt-id" , Map . of())
71+ );
72+ }
73+ ```
74+
4175### Async Client Example
4276
4377``` java
@@ -47,21 +81,19 @@ ServerParameters params = ServerParameters.builder("npx")
4781 .build();
4882
4983// Initialize the async client
50- Duration timeout = Duration . ofSeconds(10 );
5184McpAsyncClient client = McpClient . async(
52- new StdioServerTransport (params),
53- timeout
85+ new StdioServerTransport (params)
5486);
5587
5688// Initialize the connection
57- client. initialize()
58- . flatMap(result - > {
89+ var promptResult = client. initialize()
90+ flatMap(result - > {
5991 // Connection initialized
6092 return client. listTools(null );
6193 })
6294 .flatMap(tools - > {
6395 // Process tools
64- return client. callTool(new McpSchema .CallToolRequest (" echo" ,
96+ return client. callTool(new McpSchema .CallToolRequest (" echo" ,
6597 Map . of(" message" , " Hello MCP!" )));
6698 })
6799 .flatMap(result - > {
@@ -70,60 +102,24 @@ client.initialize()
70102 })
71103 .flatMap(prompts - > {
72104 // Process available prompts
73- return client. getPrompt(new McpSchema .GetPromptRequest (" prompt-id" ));
74- })
75- .subscribe(prompt - > {
76- // Handle prompt result
105+ return client. getPrompt(new McpSchema .GetPromptRequest (" prompt-id" , Map . of()));
77106 });
78107
108+ // Handle prompt result, e.g. by blocking on it
109+ McpSchema . GetPromptResult result = promptResult. block();
110+
79111// Resource management example
80- client. listResources(null )
112+ var resourcesResult = client. listResources(null )
81113 .flatMap(resources - > {
82114 // Subscribe to resource changes
83115 return client. subscribeResource(new McpSchema .SubscribeRequest (" resource-uri" ));
84- })
85- .subscribe();
86-
87- // Cleanup
88- client. closeGracefully(timeout). block();
89- ```
90-
91- ### Sync Client Example
92-
93- ``` java
94- // Create and initialize sync client
95- McpSyncClient client = McpClient . sync(
96- new StdioServerTransport (params),
97- timeout
98- );
99-
100- try {
101- // Initialize connection
102- McpSchema . InitializeResult initResult = client. initialize();
103-
104- // List tools synchronously
105- McpSchema . ListToolsResult tools = client. listTools(null );
116+ });
106117
107- // Call tool synchronously
108- McpSchema . CallToolResult result = client. callTool(
109- new McpSchema .CallToolRequest (" echo" , Map . of(" message" , " Hello!" ))
110- );
118+ // Handle resources result
119+ resourcesResult. block();
111120
112- // Resource management
113- McpSchema . ListResourcesResult resources = client. listResources(null );
114- McpSchema . ReadResourceResult resource = client. readResource(
115- new McpSchema .ReadResourceRequest (" resource-uri" )
116- );
117-
118- // Prompt management
119- ListPromptsResult prompts = client. listPrompts(null );
120- GetPromptResult prompt = client. getPrompt(
121- new McpSchema .GetPromptRequest (" prompt-id" )
122- );
123- } finally {
124- // Cleanup
125- client. close();
126- }
121+ // Cleanup
122+ client. closeGracefully(). block();
127123```
128124
129125## Architecture
0 commit comments