Skip to content

Commit 6e66602

Browse files
committed
WIP: Get rid of initialization notification handler
Signed-off-by: Dariusz Jędrzejczyk <[email protected]>
1 parent d98b8c9 commit 6e66602

File tree

5 files changed

+29
-27
lines changed

5 files changed

+29
-27
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,8 @@ public class McpAsyncServer {
145145
Map<String, McpRequestHandler<?>> requestHandlers = prepareRequestHandlers();
146146
Map<String, McpNotificationHandler> notificationHandlers = prepareNotificationHandlers(features);
147147

148-
mcpTransportProvider.setSessionFactory(
149-
transport -> new McpServerSession(UUID.randomUUID().toString(), requestTimeout, transport,
150-
this::asyncInitializeRequestHandler, Mono::empty, requestHandlers, notificationHandlers));
148+
mcpTransportProvider.setSessionFactory(transport -> new McpServerSession(UUID.randomUUID().toString(),
149+
requestTimeout, transport, this::asyncInitializeRequestHandler, requestHandlers, notificationHandlers));
151150
}
152151

153152
McpAsyncServer(McpStreamableServerTransportProvider mcpTransportProvider, ObjectMapper objectMapper,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public McpStreamableServerSession.McpStreamableServerSessionInit startSession(
3333
McpSchema.InitializeRequest initializeRequest) {
3434
return new McpStreamableServerSession.McpStreamableServerSessionInit(
3535
new McpStreamableServerSession(UUID.randomUUID().toString(), initializeRequest.capabilities(),
36-
initializeRequest.clientInfo(), requestTimeout, Mono::empty, requestHandlers,
37-
notificationHandlers),
36+
initializeRequest.clientInfo(), requestTimeout, requestHandlers, notificationHandlers),
3837
this.initRequestHandler.handle(initializeRequest));
3938
}
4039

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ public class McpServerSession implements McpLoggableSession {
3838

3939
private final McpInitRequestHandler initRequestHandler;
4040

41-
private final InitNotificationHandler initNotificationHandler;
42-
4341
private final Map<String, McpRequestHandler<?>> requestHandlers;
4442

4543
private final Map<String, McpNotificationHandler> notificationHandlers;
@@ -62,6 +60,27 @@ public class McpServerSession implements McpLoggableSession {
6260

6361
private volatile McpSchema.LoggingLevel minLoggingLevel = McpSchema.LoggingLevel.INFO;
6462

63+
/**
64+
* Creates a new server session with the given parameters and the transport to use.
65+
* @param id session id
66+
* @param transport the transport to use
67+
* @param initHandler called when a
68+
* {@link io.modelcontextprotocol.spec.McpSchema.InitializeRequest} is received by the
69+
* server
70+
* @param requestHandlers map of request handlers to use
71+
* @param notificationHandlers map of notification handlers to use
72+
*/
73+
public McpServerSession(String id, Duration requestTimeout, McpServerTransport transport,
74+
McpInitRequestHandler initHandler, Map<String, McpRequestHandler<?>> requestHandlers,
75+
Map<String, McpNotificationHandler> notificationHandlers) {
76+
this.id = id;
77+
this.requestTimeout = requestTimeout;
78+
this.transport = transport;
79+
this.initRequestHandler = initHandler;
80+
this.requestHandlers = requestHandlers;
81+
this.notificationHandlers = notificationHandlers;
82+
}
83+
6584
/**
6685
* Creates a new server session with the given parameters and the transport to use.
6786
* @param id session id
@@ -74,7 +93,10 @@ public class McpServerSession implements McpLoggableSession {
7493
* received.
7594
* @param requestHandlers map of request handlers to use
7695
* @param notificationHandlers map of notification handlers to use
96+
* @deprecated Use
97+
* {@link #McpServerSession(String, Duration, McpServerTransport, McpInitRequestHandler, Map, Map)}
7798
*/
99+
@Deprecated
78100
public McpServerSession(String id, Duration requestTimeout, McpServerTransport transport,
79101
McpInitRequestHandler initHandler, InitNotificationHandler initNotificationHandler,
80102
Map<String, McpRequestHandler<?>> requestHandlers,
@@ -83,7 +105,6 @@ public McpServerSession(String id, Duration requestTimeout, McpServerTransport t
83105
this.requestTimeout = requestTimeout;
84106
this.transport = transport;
85107
this.initRequestHandler = initHandler;
86-
this.initNotificationHandler = initNotificationHandler;
87108
this.requestHandlers = requestHandlers;
88109
this.notificationHandlers = notificationHandlers;
89110
}
@@ -264,7 +285,6 @@ private Mono<Void> handleIncomingNotification(McpSchema.JSONRPCNotification noti
264285
// legacy SSE transport.
265286
exchangeSink.tryEmitValue(new McpAsyncServerExchange(this.id, this, clientCapabilities.get(),
266287
clientInfo.get(), McpTransportContext.EMPTY));
267-
return this.initNotificationHandler.handle();
268288
}
269289

270290
var handler = notificationHandlers.get(notification.method());

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ public class McpStreamableServerSession implements McpLoggableSession {
3232

3333
private final AtomicLong requestCounter = new AtomicLong(0);
3434

35-
private final InitNotificationHandler initNotificationHandler;
36-
3735
private final Map<String, McpRequestHandler<?>> requestHandlers;
3836

3937
private final Map<String, McpNotificationHandler> notificationHandlers;
@@ -58,15 +56,14 @@ public class McpStreamableServerSession implements McpLoggableSession {
5856

5957
public McpStreamableServerSession(String id, McpSchema.ClientCapabilities clientCapabilities,
6058
McpSchema.Implementation clientInfo, Duration requestTimeout,
61-
InitNotificationHandler initNotificationHandler, Map<String, McpRequestHandler<?>> requestHandlers,
59+
Map<String, McpRequestHandler<?>> requestHandlers,
6260
Map<String, McpNotificationHandler> notificationHandlers) {
6361
this.id = id;
6462
this.missingMcpTransportSession = new MissingMcpTransportSession(id);
6563
this.listeningStreamRef = new AtomicReference<>(this.missingMcpTransportSession);
6664
this.clientCapabilities.lazySet(clientCapabilities);
6765
this.clientInfo.lazySet(clientInfo);
6866
this.requestTimeout = requestTimeout;
69-
this.initNotificationHandler = initNotificationHandler;
7067
this.requestHandlers = requestHandlers;
7168
this.notificationHandlers = notificationHandlers;
7269
}
@@ -231,19 +228,6 @@ public interface InitRequestHandler {
231228

232229
}
233230

234-
/**
235-
* Notification handler for the initialization notification from the client.
236-
*/
237-
public interface InitNotificationHandler {
238-
239-
/**
240-
* Specifies an action to take upon successful initialization.
241-
* @return a Mono that will complete when the initialization is acted upon.
242-
*/
243-
Mono<Void> handle();
244-
245-
}
246-
247231
public interface Factory {
248232

249233
McpStreamableServerSessionInit startSession(McpSchema.InitializeRequest initializeRequest);

mcp/src/test/java/io/modelcontextprotocol/client/StdioMcpAsyncClientTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected McpClientTransport createMcpTransport() {
3737
}
3838

3939
protected Duration getInitializationTimeout() {
40-
return Duration.ofSeconds(10);
40+
return Duration.ofSeconds(20);
4141
}
4242

4343
}

0 commit comments

Comments
 (0)