Skip to content

Commit cef7aac

Browse files
committed
WIP: Add session ID to exchange
Signed-off-by: Dariusz Jędrzejczyk <[email protected]>
1 parent a3ddb75 commit cef7aac

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

mcp/src/main/java/io/modelcontextprotocol/server/McpAsyncServerExchange.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
*/
2828
public class McpAsyncServerExchange {
2929

30+
private final String sessionId;
31+
3032
private final McpSession session;
3133

3234
private final McpSchema.ClientCapabilities clientCapabilities;
@@ -55,13 +57,16 @@ public class McpAsyncServerExchange {
5557
* @param clientCapabilities The client capabilities that define the supported
5658
* features and functionality.
5759
* @param clientInfo The client implementation information.
60+
* @deprecated Use
61+
* {@link #McpAsyncServerExchange(String, McpSession, McpSchema.ClientCapabilities, McpSchema.Implementation, McpTransportContext)}
5862
*/
63+
@Deprecated
5964
public McpAsyncServerExchange(McpSession session, McpSchema.ClientCapabilities clientCapabilities,
6065
McpSchema.Implementation clientInfo) {
6166
this.session = session;
6267
this.clientCapabilities = clientCapabilities;
6368
this.clientInfo = clientInfo;
64-
this.transportContext = new DefaultMcpTransportContext();
69+
this.transportContext = McpTransportContext.EMPTY;
6570
}
6671

6772
/**
@@ -73,8 +78,9 @@ public McpAsyncServerExchange(McpSession session, McpSchema.ClientCapabilities c
7378
* transport
7479
* @param clientInfo The client implementation information.
7580
*/
76-
public McpAsyncServerExchange(McpSession session, McpSchema.ClientCapabilities clientCapabilities,
81+
public McpAsyncServerExchange(String sessionId, McpSession session, McpSchema.ClientCapabilities clientCapabilities,
7782
McpSchema.Implementation clientInfo, McpTransportContext transportContext) {
83+
this.sessionId = sessionId;
7884
this.session = session;
7985
this.clientCapabilities = clientCapabilities;
8086
this.clientInfo = clientInfo;
@@ -101,6 +107,10 @@ public McpTransportContext transportContext() {
101107
return this.transportContext;
102108
}
103109

110+
public String sessionId() {
111+
return this.sessionId;
112+
}
113+
104114
/**
105115
* Create a new message using the sampling capabilities of the client. The Model
106116
* Context Protocol (MCP) provides a standardized way for servers to request LLM

mcp/src/main/java/io/modelcontextprotocol/server/McpSyncServerExchange.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public McpSyncServerExchange(McpAsyncServerExchange exchange) {
2828
this.exchange = exchange;
2929
}
3030

31+
public String sessionId() {
32+
return this.exchange.sessionId();
33+
}
34+
3135
/**
3236
* Get the client capabilities that define the supported features and functionality.
3337
* @return The client capabilities

mcp/src/main/java/io/modelcontextprotocol/spec/McpServerSession.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ private Mono<Void> handleIncomingNotification(McpSchema.JSONRPCNotification noti
246246
return Mono.defer(() -> {
247247
if (McpSchema.METHOD_NOTIFICATION_INITIALIZED.equals(notification.method())) {
248248
this.state.lazySet(STATE_INITIALIZED);
249-
exchangeSink.tryEmitValue(new McpAsyncServerExchange(this, clientCapabilities.get(), clientInfo.get()));
249+
exchangeSink.tryEmitValue(new McpAsyncServerExchange(this.id, this, clientCapabilities.get(),
250+
clientInfo.get(), McpTransportContext.EMPTY));
250251
return this.initNotificationHandler.handle();
251252
}
252253

mcp/src/main/java/io/modelcontextprotocol/spec/McpStreamableServerSession.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public Mono<Void> responseStream(McpSchema.JSONRPCRequest jsonrpcRequest, McpSer
118118
error.message(), error.data())));
119119
}
120120
return requestHandler
121-
.handle(new McpAsyncServerExchange(stream, clientCapabilities.get(), clientInfo.get(),
121+
.handle(new McpAsyncServerExchange(this.id, stream, clientCapabilities.get(), clientInfo.get(),
122122
transportContext), jsonrpcRequest.params())
123123
.map(result -> new McpSchema.JSONRPCResponse(McpSchema.JSONRPC_VERSION, jsonrpcRequest.id(), result,
124124
null))
@@ -137,7 +137,7 @@ public Mono<Void> accept(McpSchema.JSONRPCNotification notification) {
137137
}
138138
McpStreamableServerSessionStream listeningStream = this.listeningStreamRef.get();
139139
return notificationHandler.handle(
140-
new McpAsyncServerExchange(
140+
new McpAsyncServerExchange(this.id,
141141
listeningStream != null ? listeningStream : MissingMcpTransportSession.INSTANCE,
142142
this.clientCapabilities.get(), this.clientInfo.get(), transportContext),
143143
notification.params());

0 commit comments

Comments
 (0)