Improve examples in README.md (#2)

* Improve AsyncMcpClient example in README.md

* Move McpSyncClient example above the async variant and improve the code
This commit is contained in:
Dariusz Jędrzejczyk 2024-12-12 14:34:03 +01:00 • committed by GitHub
commit 972519b9ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -38,66 +38,15 @@ Detailed UML class diagrams showing the relationships between components can be
## Usage
### Async Client Example
```java
// Create server parameters
ServerParameters params = ServerParameters.builder("npx")
.args("-y", "@modelcontextprotocol/server-everything", "dir")
.build();
// Initialize the async client
Duration timeout = Duration.ofSeconds(10);
McpAsyncClient client = McpClient.async(
new StdioServerTransport(params),
timeout
);
// Initialize the connection
client.initialize()
.flatMap(result -> {
// Connection initialized
return client.listTools(null);
})
.flatMap(tools -> {
// Process tools
return client.callTool(new McpSchema.CallToolRequest("echo",
Map.of("message", "Hello MCP!")));
})
.flatMap(result -> {
// Handle tool result
return client.listPrompts(null);
})
.flatMap(prompts -> {
// Process available prompts
return client.getPrompt(new McpSchema.GetPromptRequest("prompt-id"));
})
.subscribe(prompt -> {
// Handle prompt result
});
// Resource management example
client.listResources(null)
.flatMap(resources -> {
// Subscribe to resource changes
return client.subscribeResource(new McpSchema.SubscribeRequest("resource-uri"));
})
.subscribe();
// Cleanup
client.closeGracefully(timeout).block();
```
### Sync Client Example
```java
// Create and initialize sync client
McpSyncClient client = McpClient.sync(
new StdioServerTransport(params),
timeout
);
ServerParameters params = ServerParameters.builder("npx")
.args("-y", "@modelcontextprotocol/server-everything", "dir")
.build();
try {
try (McpSyncClient client = McpClient.sync(new StdioServerTransport(params))) {
// Initialize connection
McpSchema.InitializeResult initResult = client.initialize();
@ -118,14 +67,61 @@ try {
// Prompt management
ListPromptsResult prompts = client.listPrompts(null);
GetPromptResult prompt = client.getPrompt(
new McpSchema.GetPromptRequest("prompt-id")
new McpSchema.GetPromptRequest("prompt-id", Map.of())
);
} finally {
// Cleanup
client.close();
}
```
### Async Client Example
```java
// Create server parameters
ServerParameters params = ServerParameters.builder("npx")
.args("-y", "@modelcontextprotocol/server-everything", "dir")
.build();
// Initialize the async client
McpAsyncClient client = McpClient.async(
new StdioServerTransport(params)
);
// Initialize the connection
var promptResult = client.initialize()
flatMap(result -> {
// Connection initialized
return client.listTools(null);
})
.flatMap(tools -> {
// Process tools
return client.callTool(new McpSchema.CallToolRequest("echo",
Map.of("message", "Hello MCP!")));
})
.flatMap(result -> {
// Handle tool result
return client.listPrompts(null);
})
.flatMap(prompts -> {
// Process available prompts
return client.getPrompt(new McpSchema.GetPromptRequest("prompt-id", Map.of()));
});
// Handle prompt result, e.g. by blocking on it
McpSchema.GetPromptResult result = promptResult.block();
// Resource management example
var resourcesResult = client.listResources(null)
.flatMap(resources -> {
// Subscribe to resource changes
return client.subscribeResource(new McpSchema.SubscribeRequest("resource-uri"));
});
// Handle resources result
resourcesResult.block();
// Cleanup
client.closeGracefully().block();
```
## Architecture
The SDK follows a layered architecture: