Refactor the mcp-core module

- The core module has been refactored to remove unnecessary abstraction
  between the SSE transport and the STDIO transport.
- Tests have been improved and no longer complain about multiple
  subscriptions to single-subscriber Sink.
- SSE transport layer is now assuming non-blocking execution and no longer
  makes thread hops when not needed.
- Decouple ObjectMapper and error handling from AbstractMcpTransport
  and remove later.

Additional improvements:

- Rename transport classes to better reflect client-side role (*ServerTransport -> *ClientTransport)
- Introduce fluent builder pattern for client configuration
- Simplify DefaultMcpSession by removing ObjectMapper dependency
- Move unmarshal responsibility to transport layer
- Update documentation and diagrams to reflect new architecture
- Clean up test implementations
This commit is contained in:
Dariusz Jędrzejczyk 2024-12-17 18:10:46 +01:00 • committed by Christian Tzolov
commit 7a98a935af
4 changed files with 560 additions and 761 deletions

View file

@ -2,7 +2,7 @@
## Overview
The `SseServerTransport` class implements the Model Context Protocol (MCP) HTTP with SSE transport specification, providing a bidirectional communication channel between clients and servers. This implementation is part of the Spring AI MCP Core library and follows the [MCP HTTP with SSE Transport Specification](https://spec.modelcontextprotocol.io/specification/basic/transports/#http-with-sse).
The `SseClientTransport` class implements the Model Context Protocol (MCP) HTTP with SSE transport specification, providing a bidirectional communication channel between clients and servers. This implementation is part of the Spring AI MCP Core library and follows the [MCP HTTP with SSE Transport Specification](https://spec.modelcontextprotocol.io/specification/basic/transports/#http-with-sse).
## Key Features
@ -53,7 +53,7 @@ WebClient.Builder webClientBuilder = WebClient.builder()
.baseUrl("http://localhost:3001");
// Initialize transport
SseServerTransport transport = new SseServerTransport(webClientBuilder);
SseClientTransport transport = new SseClientTransport(webClientBuilder);
// Create a JSON-RPC request
JSONRPCRequest request = new JSONRPCRequest(
@ -141,4 +141,4 @@ The implementation includes comprehensive tests covering:
- Shutdown procedures
- Edge cases and failure scenarios
For detailed test examples, see `SseServerTransportTests.java`.
For detailed test examples, see `SseClientTransportTests.java`.

View file

@ -1,6 +1,6 @@
# StdioServerTransport
# StdioClientTransport
The `StdioServerTransport` class implements the [MCP Stdio Transport](https://spec.modelcontextprotocol.io/specification/basic/transports/#stdio) specification, providing communication with MCP servers over standard input/output streams.
The `StdioClientTransport` class implements the [MCP Stdio Transport](https://spec.modelcontextprotocol.io/specification/basic/transports/#stdio) specification, providing communication with MCP servers over standard input/output streams.
## Overview
@ -84,7 +84,7 @@ ServerParameters params = ServerParameters.builder()
.build();
// Create transport
StdioServerTransport transport = new StdioServerTransport(params);
StdioClientTransport transport = new StdioClientTransport(params);
// Start transport
transport.start();

View file

@ -1,25 +1,18 @@
@startuml Core Components
interface McpTransport {
+void start()
+Mono<Void> connect(Function<Mono<JSONRPCMessage>,
Mono<JSONRPCMessage>> handler)
+Mono<Void> sendMessage()
+void close()
+Mono<Void> closeGracefully()
+void setInboudMessageHandler()
+void setInboundErrorHandler()
+Mono<Void> sendMessage()
}
abstract class AbstractMcpTransport {
#ObjectMapper objectMapper
#getInboundSink()
#getOutboundSink()
#getErrorSink()
}
interface McpSession {
+<T> Mono<T> sendRequest()
+Mono<Void> sendNotification()
+<T> Mono<T> sendRequest(String method, Object requestParams, TypeReference<T> typeRef)
+Mono<Void> sendNotification(String method, Map<String, Object> params)
+Mono<Void> closeGracefully()
+close()
}
class DefaultMcpSession {
@ -52,12 +45,12 @@ class McpSyncClient {
+GetPromptResult getPrompt()
}
class StdioServerTransport {
class StdioClientTransport {
-Process serverProcess
-ServerParameters parameters
}
class SseServerTransport {
class SseClientTransport {
-WebClient webClient
}
@ -74,11 +67,10 @@ class McpError {
-String message
}
McpTransport <|.. AbstractMcpTransport
AbstractMcpTransport <|-- StdioServerTransport
AbstractMcpTransport <|-- SseServerTransport
McpTransport <|-- StdioClientTransport
McpTransport <|-- SseClientTransport
McpSession <|-- DefaultMcpSession
DefaultMcpSession <|-- McpAsyncClient
DefaultMcpSession --o McpAsyncClient
McpClient ..> McpAsyncClient : creates
McpClient ..> McpSyncClient : creates
McpSyncClient o-- McpAsyncClient

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 28 KiB

Before After
Before After