Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit 49e0291

Browse files
committed
Update READEME
1 parent 7fa6d0c commit 49e0291

File tree

1 file changed

+88
-44
lines changed

1 file changed

+88
-44
lines changed

mcp-core/README.md

Lines changed: 88 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Model Context Protocol (MCP) Java SDK
22

3-
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.
3+
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.
44

55
## Overview
66

@@ -11,9 +11,10 @@ This SDK implements the Model Context Protocol, enabling seamless integration wi
1111
- Synchronous and Asynchronous client implementations
1212
- Standard MCP operations support:
1313
- Tool discovery and execution
14-
- Resource management
15-
- Message creation
16-
- Server initialization
14+
- Resource management and templates
15+
- Prompt handling and management
16+
- Resource subscription system
17+
- Server initialization and ping
1718
- Stdio-based server transport
1819
- Reactive programming support using Project Reactor
1920

@@ -43,53 +44,80 @@ ServerParameters params = ServerParameters.builder("npx")
4344
Duration timeout = Duration.ofSeconds(10);
4445
McpAsyncClient client = McpClient.async(
4546
new StdioServerTransport(params),
46-
timeout,
47-
new ObjectMapper()
47+
timeout
4848
);
4949

5050
// Initialize the connection
51-
client.initialize();
52-
53-
// List available tools
54-
client.listTools(null).subscribe(result -> {
55-
List<Tool> tools = result.tools();
56-
// Process tools...
57-
});
58-
59-
// Call a tool
60-
CallToolRequest request = new CallToolRequest(
61-
"echo",
62-
Map.of("message", "Hello MCP!")
63-
);
64-
client.callTool(request).subscribe(result -> {
65-
// Handle tool execution result...
66-
});
51+
client.initialize()
52+
.flatMap(result -> {
53+
// Connection initialized
54+
return client.listTools(null);
55+
})
56+
.flatMap(tools -> {
57+
// Process tools
58+
return client.callTool(new McpSchema.CallToolRequest("echo",
59+
Map.of("message", "Hello MCP!")));
60+
})
61+
.flatMap(result -> {
62+
// Handle tool result
63+
return client.listPrompts(null);
64+
})
65+
.flatMap(prompts -> {
66+
// Process available prompts
67+
return client.getPrompt(new McpSchema.GetPromptRequest("prompt-id"));
68+
})
69+
.subscribe(prompt -> {
70+
// Handle prompt result
71+
});
72+
73+
// Resource management example
74+
client.listResources(null)
75+
.flatMap(resources -> {
76+
// Subscribe to resource changes
77+
return client.subscribeResource(new McpSchema.SubscribeRequest("resource-uri"));
78+
})
79+
.subscribe();
6780

6881
// Cleanup
69-
client.close();
82+
client.closeGracefully(timeout).block();
7083
```
7184

7285
### Sync Client Example
7386

7487
```java
7588
// Create and initialize sync client
76-
McpClient syncClient = McpClient.sync(
89+
McpSyncClient client = McpClient.sync(
7790
new StdioServerTransport(params),
78-
timeout,
79-
new ObjectMapper()
91+
timeout
8092
);
8193

82-
// Initialize connection
83-
syncClient.initialize();
84-
85-
// List tools synchronously
86-
ListToolsResult tools = syncClient.listTools(null);
87-
88-
// Call tool synchronously
89-
CallToolResult result = syncClient.callTool("echo", Map.of("message", "Hello!"));
90-
91-
// Cleanup
92-
syncClient.close();
94+
try {
95+
// Initialize connection
96+
McpSchema.InitializeResult initResult = client.initialize();
97+
98+
// List tools synchronously
99+
McpSchema.ListToolsResult tools = client.listTools(null);
100+
101+
// Call tool synchronously
102+
McpSchema.CallToolResult result = client.callTool(
103+
new McpSchema.CallToolRequest("echo", Map.of("message", "Hello!"))
104+
);
105+
106+
// Resource management
107+
McpSchema.ListResourcesResult resources = client.listResources(null);
108+
McpSchema.ReadResourceResult resource = client.readResource(
109+
new McpSchema.ReadResourceRequest("resource-uri")
110+
);
111+
112+
// Prompt management
113+
ListPromptsResult prompts = client.listPrompts(null);
114+
GetPromptResult prompt = client.getPrompt(
115+
new McpSchema.GetPromptRequest("prompt-id")
116+
);
117+
} finally {
118+
// Cleanup
119+
client.close();
120+
}
93121
```
94122

95123
## Architecture
@@ -98,11 +126,13 @@ The SDK follows a layered architecture:
98126

99127
### Core Components
100128

101-
- **McpClient**: Main interface defining the synchronous operations
102-
- **McpAsyncClient**: Async implementation using Project Reactor
129+
- **McpClient**: Factory class for creating sync and async clients
130+
- **McpAsyncClient**: Primary async implementation using Project Reactor
103131
- **McpSyncClient**: Synchronous wrapper around the async client
132+
- **McpSession**: Core session interface defining communication patterns
104133
- **McpTransport**: Transport layer interface
105-
- **AbstracttMcpTransport**: Abstract transport implementation
134+
- **McpSchema**: Comprehensive protocol schema definitions
135+
- **AbstractMcpTransport**: Base transport implementation
106136
- **StdioServerTransport**: Stdio-based server communication
107137

108138
### Key Interactions
@@ -113,25 +143,39 @@ The SDK follows a layered architecture:
113143
- Protocol handshake
114144

115145
2. Message Flow
116-
- Request creation
146+
- JSON-RPC message creation
117147
- Transport layer handling
118148
- Response processing
149+
- Error handling
150+
151+
3. Resource Management
152+
- Resource listing and reading
153+
- Template management
154+
- Subscription handling
155+
- Change notifications
156+
157+
4. Prompt System
158+
- Prompt discovery
159+
- Prompt retrieval
160+
- Change notifications
119161

120-
3. Tool Execution
162+
5. Tool Execution
121163
- Tool discovery
122164
- Parameter validation
123165
- Execution handling
124166
- Result processing
125167

126168
## Error Handling
127169

128-
The SDK provides comprehensive error handling:
170+
The SDK provides comprehensive error handling through the McpError class:
129171

130172
- Transport-level errors
131173
- Protocol violations
132174
- Tool execution failures
175+
- Resource access errors
176+
- Subscription handling errors
177+
- Prompt management errors
133178
- Timeout handling
134-
- Resource management errors
135179

136180
## Contributing
137181

0 commit comments

Comments
 (0)