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

Commit 716d1a0

Browse files
committed
Enhance DefaultMcpSession with request/response handling
- Add comprehensive documentation for MCP session components in MCP-SESSION.md - Add logging and error handling in McpAsyncClient - Update test configurations and dependencies - Add reactor-netty-http and spring-context test dependencies Resolves #6
1 parent 0ebb38c commit 716d1a0

File tree

8 files changed

+535
-32
lines changed

8 files changed

+535
-32
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
<slf4j-api.version>2.0.16</slf4j-api.version>
7676
<logback.version>1.5.12</logback.version>
7777
<jackson.version>2.18.2</jackson.version>
78-
<springframework.version>6.1.13</springframework.version>
78+
<springframework.version>6.2.1</springframework.version>
7979

8080
<!-- plugin versions -->
8181
<maven-compiler-plugin.version>3.11.0</maven-compiler-plugin.version>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# MCP Session Components
2+
3+
## Overview
4+
5+
The MCP (Model Context Protocol) session components implement the [MCP specification](https://spec.modelcontextprotocol.io/) for managing bidirectional JSON-RPC communication between clients and model servers.
6+
The implementation provides a robust foundation for request-response patterns and notification handling.
7+
8+
## Core Components
9+
10+
### McpSession Interface
11+
12+
The `McpSession` interface defines the contract for MCP communication with three primary operations:
13+
14+
1. Request-Response Communication
15+
```java
16+
<T> Mono<T> sendRequest(String method, Object requestParams, TypeReference<T> typeRef);
17+
```
18+
19+
2. Notification Handling
20+
```java
21+
Mono<Void> sendNotification(String method, Map<String, Object> params);
22+
```
23+
24+
3. Session Lifecycle Management
25+
```java
26+
Mono<Void> closeGracefully();
27+
```
28+
29+
### DefaultMcpSession Implementation
30+
31+
The `DefaultMcpSession` provides the concrete implementation of the MCP session with the following key features:
32+
33+
#### 1. Message Correlation
34+
- Uses unique request IDs combining session UUID prefix and atomic counter
35+
- Maintains thread-safe tracking of pending responses
36+
- Implements timeout management for requests
37+
38+
#### 2. Handler Registration
39+
```java
40+
public interface RequestHandler {
41+
Mono<Object> handle(Object params);
42+
}
43+
44+
public interface NotificationHandler {
45+
Mono<Void> handle(Object params);
46+
}
47+
```
48+
49+
#### 3. Transport Integration
50+
- Abstracts transport details through `McpTransport` interface
51+
- Supports different transport implementations (SSE, STDIO)
52+
- Manages message serialization using Jackson
53+
54+
## Implementation Details
55+
56+
- The session processes three types of messages: McpSchema.JSONRPCResponse, McpSchema.JSONRPCRequest, McpSchema.JSONRPCNotificaiton.
57+
- The implementation ensures thread safety through
58+
59+
### Error Handling
60+
61+
WIP
62+
63+
## Integration Guidelines
64+
65+
1. **Transport Selection**
66+
- Choose appropriate transport (SSE, STDIO) based on use case
67+
- Configure transport-specific parameters
68+
- Handle transport lifecycle properly
69+
70+
2. **Message Handling**
71+
- Register handlers before starting session
72+
- Use appropriate error codes from MCP specification
73+
- Implement proper request/response correlation
74+
75+
3. **Type Safety**
76+
- Use appropriate TypeReference for response deserialization
77+
- Validate request parameters
78+
- Handle type conversion errors

spring-ai-mcp-core/pom.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,30 @@
4444
<artifactId>reactor-core</artifactId>
4545
</dependency>
4646

47+
4748
<dependency>
4849
<groupId>org.springframework</groupId>
4950
<artifactId>spring-webflux</artifactId>
5051
<version>${springframework.version}</version>
5152
</dependency>
5253

54+
55+
<dependency>
56+
<groupId>io.projectreactor.netty</groupId>
57+
<artifactId>reactor-netty-http</artifactId>
58+
<scope>test</scope>
59+
</dependency>
60+
61+
<!-- The Spring Context is required due to the reactor-netty connector being dependant on
62+
the Spring Lifecycle, as discussed here:
63+
https://github.com/spring-projects/spring-framework/issues/31180 -->
64+
<dependency>
65+
<groupId>org.springframework</groupId>
66+
<artifactId>spring-context</artifactId>
67+
<version>${springframework.version}</version>
68+
<scope>test</scope>
69+
</dependency>
70+
5371
<dependency>
5472
<groupId>org.assertj</groupId>
5573
<artifactId>assertj-core</artifactId>
@@ -83,4 +101,4 @@
83101
</dependencies>
84102

85103

86-
</project>
104+
</project>

spring-ai-mcp-core/src/main/java/org/springframework/ai/mcp/client/McpAsyncClient.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import com.fasterxml.jackson.core.type.TypeReference;
2121
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
2224
import reactor.core.publisher.Mono;
2325

2426
import org.springframework.ai.mcp.spec.DefaultMcpSession;
@@ -36,6 +38,8 @@
3638
*/
3739
public class McpAsyncClient extends DefaultMcpSession {
3840

41+
private final static Logger logger = LoggerFactory.getLogger(McpAsyncClient.class);
42+
3943
private static TypeReference<Void> VOID_TYPE_REFERENCE = new TypeReference<>() {
4044
};
4145

@@ -86,6 +90,11 @@ public Mono<McpSchema.InitializeResult> initialize() {
8690
});
8791

8892
return result.flatMap(initializeResult -> {
93+
94+
logger.info("Server response with Protocol: {}, Capabilities: {}, Info: {} and Instructions {}",
95+
initializeResult.protocolVersion(), initializeResult.capabilities(), initializeResult.serverInfo(),
96+
initializeResult.instructions());
97+
8998
if (!McpSchema.LATEST_PROTOCOL_VERSION.equals(initializeResult.protocolVersion())) {
9099
return Mono.error(new McpError(
91100
"Unsupported protocol version from the server: " + initializeResult.protocolVersion()));

0 commit comments

Comments
 (0)