Skip to content

Commit 7a98a93

Browse files
chemicLtzolov
authored andcommitted
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
1 parent 8b2ce26 commit 7a98a93

30 files changed

+560
-761
lines changed

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
# Spring AI MCP
22

3-
Java SDK for the Model Context Protocol (MCP), providing seamless integration between Java and Spring applications and MCP-compliant AI models and tools.
3+
Java SDK for the Model Context Protocol (MCP), providing seamless integration between Java and Spring applications and MCP-compliant AI resources and tools.
44

55
## Overview
66

7-
Spring AI MCP is an experimental project that provides Java and Spring Framework integration for the [Model Context Protocol](https://modelcontextprotocol.org/docs/concepts/architecture). It enables Spring applications to interact with AI models and tools through a standardized interface, supporting both synchronous and asynchronous communication patterns.
7+
Spring AI MCP is an experimental project that provides Java and Spring Framework integration for the [Model Context Protocol](https://modelcontextprotocol.org/docs/concepts/architecture). It enables Java applications to interact with AI models and tools through a standardized interface, supporting both synchronous and asynchronous communication patterns.
88

99
<img src="spring-ai-mcp-architecture.jpg" width="600">
1010

1111
## Modules
1212

1313
The project consists of two main modules:
1414

15-
### [spring-ai-mcp-core](./spring-ai-mcp-core/README.md)
15+
### [mcp-core](./spring-ai-mcp-core/README.md)
1616

1717
The core module provides a Java implementation of the Model Context Protocol specification. It includes:
1818
- Synchronous and asynchronous client implementations
19-
- Standard MCP operations support (tool discovery, resource management, prompt handling)
20-
- Stdio-based server transport
21-
- Reactive programming support using Project Reactor
19+
- Standard MCP operations support (tool discovery, resource management, prompt handling). Support for request and notificaiotn handling.
20+
- [Stdio](https://spec.modelcontextprotocol.io/specification/basic/transports/#stdio) and [SSE](https://spec.modelcontextprotocol.io/specification/basic/transports/#http-with-sse) transport implementations.
21+
- [Find more](./spring-ai-mcp-core/README.md).
2222

23-
[find more](./spring-ai-mcp-core/README.md)
24-
25-
### [spring-ai-mcp-spring](./spring-ai-mcp-spring/README.md)
23+
### [mcp-spring](./spring-ai-mcp-spring/README.md)
2624

2725
The Spring integration module provides Spring-specific functionality:
2826
- Integration with Spring AI's function calling system
2927
- Spring-friendly abstractions for MCP clients
30-
- Automatic conversion between JSON and Java objects for tool arguments
28+
- Auto-configurations (WIP)
3129

3230
## Requirements
3331

spring-ai-mcp-core/README.md

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ This SDK implements the Model Context Protocol, enabling seamless integration wi
2020
- Multiple transport implementations:
2121
- Stdio-based transport for process-based communication
2222
- SSE-based transport for HTTP streaming
23-
- Reactive programming support using Project Reactor
2423
- Configurable request timeouts
2524
- Customizable JSON serialization/deserialization
2625

@@ -32,43 +31,37 @@ Add the following dependency to your Maven project:
3231
<dependency>
3332
<groupId>org.springframework.experimental</groupId>
3433
<artifactId>spring-ai-mcp-core</artifactId>
35-
<version>0.1.0</version>
34+
<version>0.2.0-SNAPSHOT</version>
3635
</dependency>
3736
```
3837

39-
## Documentation
40-
41-
Detailed UML class diagrams showing the relationships between components can be found in [docs/class-diagrams.puml](docs/class-diagrams.puml). The diagrams include:
42-
- Core Components: Shows the main interfaces, classes, and their relationships
43-
- Message Flow: Illustrates the message and resource type hierarchies
44-
4538
## Usage
4639

4740
### Transport Layer Options
4841

4942
The SDK provides two transport implementations:
5043

51-
#### StdioServerTransport
44+
#### StdioClientTransport
5245
Standard I/O based transport for process-based communication with MCP servers:
5346

5447
```java
5548
ServerParameters params = ServerParameters.builder("npx")
5649
.args("-y", "@modelcontextprotocol/server-everything", "dir")
5750
.build();
58-
McpTransport transport = new StdioServerTransport(params);
51+
McpTransport transport = new StdioClientTransport(params);
5952
```
6053

61-
#### SseServerTransport
54+
#### SseClientTransport
6255
Server-Sent Events (SSE) based transport following the MCP HTTP with SSE transport specification:
6356

6457
```java
6558
WebClient.Builder webClientBuilder = WebClient.builder()
6659
.baseUrl("http://your-mcp-server");
67-
McpTransport transport = new SseServerTransport(webClientBuilder);
60+
McpTransport transport = new SseClientTransport(webClientBuilder);
6861

6962
// Or with custom ObjectMapper
7063
ObjectMapper mapper = new ObjectMapper();
71-
McpTransport transport = new SseServerTransport(webClientBuilder, mapper);
64+
McpTransport transport = new SseClientTransport(webClientBuilder, mapper);
7265
```
7366

7467
The SSE transport provides:
@@ -87,7 +80,7 @@ ServerParameters params = ServerParameters.builder("npx")
8780
.args("-y", "@modelcontextprotocol/server-everything", "dir")
8881
.build();
8982

90-
try (McpSyncClient client = McpClient.sync(new StdioServerTransport(params))) {
83+
try (McpSyncClient client = McpClient.sync(new StdioClientTransport(params))) {
9184
// Initialize connection with protocol version and capabilities
9285
McpSchema.InitializeResult initResult = client.initialize();
9386

@@ -123,7 +116,7 @@ ServerParameters params = ServerParameters.builder("npx")
123116

124117
// Initialize async client with custom timeout and object mapper
125118
McpAsyncClient client = McpClient.async(
126-
new StdioServerTransport(params),
119+
new StdioClientTransport(params),
127120
Duration.ofSeconds(30),
128121
new ObjectMapper()
129122
);
@@ -172,11 +165,11 @@ The SDK follows a layered architecture with clear separation of concerns:
172165
- **McpAsyncClient**: Primary async implementation using Project Reactor for non-blocking operations
173166
- **McpSyncClient**: Synchronous wrapper around the async client for blocking operations
174167
- **McpSession**: Core session interface defining communication patterns
175-
- **McpTransport**: Transport layer interface for server communication
168+
- **McpTransport**: Transport layer interface for client/server communication
176169
- **McpSchema**: Comprehensive protocol schema definitions
177-
- **DefaultMcpSession**: Base implementation of the session management
178-
- **StdioServerTransport**: Standard I/O based server communication
179-
- **SseServerTransport**: HTTP-based transport using Server-Sent Events for bidirectional communication
170+
- **DefaultMcpSession**: Default implementation of the session management
171+
- **StdioClientTransport**: Standard I/O based client to server communication
172+
- **SseClientTransport**: HTTP-based transport using Server-Sent Events for bidirectional client-sever communication
180173

181174
<img src="docs/spring-ai-mcp-uml-classdiagram.svg" width="600"/>
182175

spring-ai-mcp-core/docs/CLIENT.SSE.TRANSPORT.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Overview
44

5-
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).
5+
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).
66

77
## Key Features
88

@@ -53,7 +53,7 @@ WebClient.Builder webClientBuilder = WebClient.builder()
5353
.baseUrl("http://localhost:3001");
5454

5555
// Initialize transport
56-
SseServerTransport transport = new SseServerTransport(webClientBuilder);
56+
SseClientTransport transport = new SseClientTransport(webClientBuilder);
5757

5858
// Create a JSON-RPC request
5959
JSONRPCRequest request = new JSONRPCRequest(
@@ -141,4 +141,4 @@ The implementation includes comprehensive tests covering:
141141
- Shutdown procedures
142142
- Edge cases and failure scenarios
143143

144-
For detailed test examples, see `SseServerTransportTests.java`.
144+
For detailed test examples, see `SseClientTransportTests.java`.

spring-ai-mcp-core/docs/CLIENT.STDIO.TRANSPORT.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# StdioServerTransport
1+
# StdioClientTransport
22

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

55
## Overview
66

@@ -84,7 +84,7 @@ ServerParameters params = ServerParameters.builder()
8484
.build();
8585

8686
// Create transport
87-
StdioServerTransport transport = new StdioServerTransport(params);
87+
StdioClientTransport transport = new StdioClientTransport(params);
8888

8989
// Start transport
9090
transport.start();

spring-ai-mcp-core/docs/class-diagrams.puml

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
@startuml Core Components
22

33
interface McpTransport {
4-
+void start()
4+
+Mono<Void> connect(Function<Mono<JSONRPCMessage>,
5+
Mono<JSONRPCMessage>> handler)
6+
+Mono<Void> sendMessage()
57
+void close()
68
+Mono<Void> closeGracefully()
7-
+void setInboudMessageHandler()
8-
+void setInboundErrorHandler()
9-
+Mono<Void> sendMessage()
10-
}
11-
12-
abstract class AbstractMcpTransport {
13-
#ObjectMapper objectMapper
14-
#getInboundSink()
15-
#getOutboundSink()
16-
#getErrorSink()
179
}
1810

1911
interface McpSession {
20-
+<T> Mono<T> sendRequest()
21-
+Mono<Void> sendNotification()
12+
+<T> Mono<T> sendRequest(String method, Object requestParams, TypeReference<T> typeRef)
13+
+Mono<Void> sendNotification(String method, Map<String, Object> params)
2214
+Mono<Void> closeGracefully()
15+
+close()
2316
}
2417

2518
class DefaultMcpSession {
@@ -52,12 +45,12 @@ class McpSyncClient {
5245
+GetPromptResult getPrompt()
5346
}
5447

55-
class StdioServerTransport {
48+
class StdioClientTransport {
5649
-Process serverProcess
5750
-ServerParameters parameters
5851
}
5952

60-
class SseServerTransport {
53+
class SseClientTransport {
6154
-WebClient webClient
6255
}
6356

@@ -74,11 +67,10 @@ class McpError {
7467
-String message
7568
}
7669

77-
McpTransport <|.. AbstractMcpTransport
78-
AbstractMcpTransport <|-- StdioServerTransport
79-
AbstractMcpTransport <|-- SseServerTransport
70+
McpTransport <|-- StdioClientTransport
71+
McpTransport <|-- SseClientTransport
8072
McpSession <|-- DefaultMcpSession
81-
DefaultMcpSession <|-- McpAsyncClient
73+
DefaultMcpSession --o McpAsyncClient
8274
McpClient ..> McpAsyncClient : creates
8375
McpClient ..> McpSyncClient : creates
8476
McpSyncClient o-- McpAsyncClient

spring-ai-mcp-core/docs/spring-ai-mcp-uml-classdiagram.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)