Update READEME
This commit is contained in:
parent
7fa6d0c56a
commit
49e0291611
1 changed files with 88 additions and 44 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
# Model Context Protocol (MCP) Java SDK
|
# Model Context Protocol (MCP) Java SDK
|
||||||
|
|
||||||
A Java implementation of the [Model Context Protocol](https://modelcontextprotocol.io/docs/concepts/architecture) specification, providing both synchronous and asynchronous clients for MCP server interactions.
|
A Java implementation of the [Model Context Protocol](https://modelcontextprotocol.org/docs/concepts/architecture) specification, providing both synchronous and asynchronous clients for MCP server interactions.
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
|
|
@ -11,9 +11,10 @@ This SDK implements the Model Context Protocol, enabling seamless integration wi
|
||||||
- Synchronous and Asynchronous client implementations
|
- Synchronous and Asynchronous client implementations
|
||||||
- Standard MCP operations support:
|
- Standard MCP operations support:
|
||||||
- Tool discovery and execution
|
- Tool discovery and execution
|
||||||
- Resource management
|
- Resource management and templates
|
||||||
- Message creation
|
- Prompt handling and management
|
||||||
- Server initialization
|
- Resource subscription system
|
||||||
|
- Server initialization and ping
|
||||||
- Stdio-based server transport
|
- Stdio-based server transport
|
||||||
- Reactive programming support using Project Reactor
|
- Reactive programming support using Project Reactor
|
||||||
|
|
||||||
|
|
@ -43,53 +44,80 @@ ServerParameters params = ServerParameters.builder("npx")
|
||||||
Duration timeout = Duration.ofSeconds(10);
|
Duration timeout = Duration.ofSeconds(10);
|
||||||
McpAsyncClient client = McpClient.async(
|
McpAsyncClient client = McpClient.async(
|
||||||
new StdioServerTransport(params),
|
new StdioServerTransport(params),
|
||||||
timeout,
|
timeout
|
||||||
new ObjectMapper()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Initialize the connection
|
// Initialize the connection
|
||||||
client.initialize();
|
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
|
||||||
|
});
|
||||||
|
|
||||||
// List available tools
|
// Resource management example
|
||||||
client.listTools(null).subscribe(result -> {
|
client.listResources(null)
|
||||||
List<Tool> tools = result.tools();
|
.flatMap(resources -> {
|
||||||
// Process tools...
|
// Subscribe to resource changes
|
||||||
});
|
return client.subscribeResource(new McpSchema.SubscribeRequest("resource-uri"));
|
||||||
|
})
|
||||||
// Call a tool
|
.subscribe();
|
||||||
CallToolRequest request = new CallToolRequest(
|
|
||||||
"echo",
|
|
||||||
Map.of("message", "Hello MCP!")
|
|
||||||
);
|
|
||||||
client.callTool(request).subscribe(result -> {
|
|
||||||
// Handle tool execution result...
|
|
||||||
});
|
|
||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
client.close();
|
client.closeGracefully(timeout).block();
|
||||||
```
|
```
|
||||||
|
|
||||||
### Sync Client Example
|
### Sync Client Example
|
||||||
|
|
||||||
```java
|
```java
|
||||||
// Create and initialize sync client
|
// Create and initialize sync client
|
||||||
McpClient syncClient = McpClient.sync(
|
McpSyncClient client = McpClient.sync(
|
||||||
new StdioServerTransport(params),
|
new StdioServerTransport(params),
|
||||||
timeout,
|
timeout
|
||||||
new ObjectMapper()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Initialize connection
|
try {
|
||||||
syncClient.initialize();
|
// Initialize connection
|
||||||
|
McpSchema.InitializeResult initResult = client.initialize();
|
||||||
|
|
||||||
// List tools synchronously
|
// List tools synchronously
|
||||||
ListToolsResult tools = syncClient.listTools(null);
|
McpSchema.ListToolsResult tools = client.listTools(null);
|
||||||
|
|
||||||
// Call tool synchronously
|
// Call tool synchronously
|
||||||
CallToolResult result = syncClient.callTool("echo", Map.of("message", "Hello!"));
|
McpSchema.CallToolResult result = client.callTool(
|
||||||
|
new McpSchema.CallToolRequest("echo", Map.of("message", "Hello!"))
|
||||||
|
);
|
||||||
|
|
||||||
// Cleanup
|
// Resource management
|
||||||
syncClient.close();
|
McpSchema.ListResourcesResult resources = client.listResources(null);
|
||||||
|
McpSchema.ReadResourceResult resource = client.readResource(
|
||||||
|
new McpSchema.ReadResourceRequest("resource-uri")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Prompt management
|
||||||
|
ListPromptsResult prompts = client.listPrompts(null);
|
||||||
|
GetPromptResult prompt = client.getPrompt(
|
||||||
|
new McpSchema.GetPromptRequest("prompt-id")
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
// Cleanup
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Architecture
|
## Architecture
|
||||||
|
|
@ -98,11 +126,13 @@ The SDK follows a layered architecture:
|
||||||
|
|
||||||
### Core Components
|
### Core Components
|
||||||
|
|
||||||
- **McpClient**: Main interface defining the synchronous operations
|
- **McpClient**: Factory class for creating sync and async clients
|
||||||
- **McpAsyncClient**: Async implementation using Project Reactor
|
- **McpAsyncClient**: Primary async implementation using Project Reactor
|
||||||
- **McpSyncClient**: Synchronous wrapper around the async client
|
- **McpSyncClient**: Synchronous wrapper around the async client
|
||||||
|
- **McpSession**: Core session interface defining communication patterns
|
||||||
- **McpTransport**: Transport layer interface
|
- **McpTransport**: Transport layer interface
|
||||||
- **AbstracttMcpTransport**: Abstract transport implementation
|
- **McpSchema**: Comprehensive protocol schema definitions
|
||||||
|
- **AbstractMcpTransport**: Base transport implementation
|
||||||
- **StdioServerTransport**: Stdio-based server communication
|
- **StdioServerTransport**: Stdio-based server communication
|
||||||
|
|
||||||
### Key Interactions
|
### Key Interactions
|
||||||
|
|
@ -113,11 +143,23 @@ The SDK follows a layered architecture:
|
||||||
- Protocol handshake
|
- Protocol handshake
|
||||||
|
|
||||||
2. Message Flow
|
2. Message Flow
|
||||||
- Request creation
|
- JSON-RPC message creation
|
||||||
- Transport layer handling
|
- Transport layer handling
|
||||||
- Response processing
|
- Response processing
|
||||||
|
- Error handling
|
||||||
|
|
||||||
3. Tool Execution
|
3. Resource Management
|
||||||
|
- Resource listing and reading
|
||||||
|
- Template management
|
||||||
|
- Subscription handling
|
||||||
|
- Change notifications
|
||||||
|
|
||||||
|
4. Prompt System
|
||||||
|
- Prompt discovery
|
||||||
|
- Prompt retrieval
|
||||||
|
- Change notifications
|
||||||
|
|
||||||
|
5. Tool Execution
|
||||||
- Tool discovery
|
- Tool discovery
|
||||||
- Parameter validation
|
- Parameter validation
|
||||||
- Execution handling
|
- Execution handling
|
||||||
|
|
@ -125,13 +167,15 @@ The SDK follows a layered architecture:
|
||||||
|
|
||||||
## Error Handling
|
## Error Handling
|
||||||
|
|
||||||
The SDK provides comprehensive error handling:
|
The SDK provides comprehensive error handling through the McpError class:
|
||||||
|
|
||||||
- Transport-level errors
|
- Transport-level errors
|
||||||
- Protocol violations
|
- Protocol violations
|
||||||
- Tool execution failures
|
- Tool execution failures
|
||||||
|
- Resource access errors
|
||||||
|
- Subscription handling errors
|
||||||
|
- Prompt management errors
|
||||||
- Timeout handling
|
- Timeout handling
|
||||||
- Resource management errors
|
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue