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:
parent
7f25221d42
commit
972519b9ea
1 changed files with 48 additions and 52 deletions
|
|
@ -38,66 +38,15 @@ Detailed UML class diagrams showing the relationships between components can be
|
||||||
|
|
||||||
## Usage
|
## 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
|
### Sync Client Example
|
||||||
|
|
||||||
```java
|
```java
|
||||||
// Create and initialize sync client
|
// Create and initialize sync client
|
||||||
McpSyncClient client = McpClient.sync(
|
ServerParameters params = ServerParameters.builder("npx")
|
||||||
new StdioServerTransport(params),
|
.args("-y", "@modelcontextprotocol/server-everything", "dir")
|
||||||
timeout
|
.build();
|
||||||
);
|
|
||||||
|
|
||||||
try {
|
try (McpSyncClient client = McpClient.sync(new StdioServerTransport(params))) {
|
||||||
// Initialize connection
|
// Initialize connection
|
||||||
McpSchema.InitializeResult initResult = client.initialize();
|
McpSchema.InitializeResult initResult = client.initialize();
|
||||||
|
|
||||||
|
|
@ -118,14 +67,61 @@ try {
|
||||||
// Prompt management
|
// Prompt management
|
||||||
ListPromptsResult prompts = client.listPrompts(null);
|
ListPromptsResult prompts = client.listPrompts(null);
|
||||||
GetPromptResult prompt = client.getPrompt(
|
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
|
## Architecture
|
||||||
|
|
||||||
The SDK follows a layered architecture:
|
The SDK follows a layered architecture:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue