@@ -53,27 +53,30 @@ public class McpServerSession implements McpSession {
53
53
54
54
private final AtomicInteger state = new AtomicInteger (STATE_UNINITIALIZED );
55
55
56
+ private final Duration requestTimeout ;
57
+
56
58
/**
57
59
* Creates a new server session with the given parameters and the transport to use.
58
60
* @param id session id
59
61
* @param transport the transport to use
60
62
* @param initHandler called when a
61
63
* {@link io.modelcontextprotocol.spec.McpSchema.InitializeRequest} is received by the
62
64
* server
63
- * @param initNotificationHandler called when a
64
- * {@link McpSchema.METHOD_NOTIFICATION_INITIALIZED} is received.
65
+ * @param initNotificationHandler called when a {@link McpSchema.METHOD_INITIALIZE }
66
+ * is received.
65
67
* @param requestHandlers map of request handlers to use
66
68
* @param notificationHandlers map of notification handlers to use
67
69
*/
68
70
public McpServerSession (String id , McpServerTransport transport , InitRequestHandler initHandler ,
69
71
InitNotificationHandler initNotificationHandler , Map <String , RequestHandler <?>> requestHandlers ,
70
- Map <String , NotificationHandler > notificationHandlers ) {
72
+ Map <String , NotificationHandler > notificationHandlers , Duration requestTimeout ) {
71
73
this .id = id ;
72
74
this .transport = transport ;
73
75
this .initRequestHandler = initHandler ;
74
76
this .initNotificationHandler = initNotificationHandler ;
75
77
this .requestHandlers = requestHandlers ;
76
78
this .notificationHandlers = notificationHandlers ;
79
+ this .requestTimeout = requestTimeout ;
77
80
}
78
81
79
82
/**
@@ -116,7 +119,7 @@ public <T> Mono<T> sendRequest(String method, Object requestParams, TypeReferenc
116
119
this .pendingResponses .remove (requestId );
117
120
sink .error (error );
118
121
});
119
- }).timeout (Duration . ofSeconds ( 10 ) ).handle ((jsonRpcResponse , sink ) -> {
122
+ }).timeout (requestTimeout ).handle ((jsonRpcResponse , sink ) -> {
120
123
if (jsonRpcResponse .error () != null ) {
121
124
sink .error (new McpError (jsonRpcResponse .error ()));
122
125
}
@@ -197,6 +200,7 @@ private Mono<McpSchema.JSONRPCResponse> handleIncomingRequest(McpSchema.JSONRPCR
197
200
return Mono .defer (() -> {
198
201
Mono <?> resultMono ;
199
202
if (McpSchema .METHOD_INITIALIZE .equals (request .method ())) {
203
+
200
204
// TODO handle situation where already initialized!
201
205
McpSchema .InitializeRequest initializeRequest = transport .unmarshalFrom (request .params (),
202
206
new TypeReference <McpSchema .InitializeRequest >() {
@@ -254,13 +258,11 @@ record MethodNotFoundError(String method, String message, Object data) {
254
258
}
255
259
256
260
static MethodNotFoundError getMethodNotFoundError (String method ) {
257
- switch (method ) {
258
- case McpSchema .METHOD_ROOTS_LIST :
259
- return new MethodNotFoundError (method , "Roots not supported" ,
260
- Map .of ("reason" , "Client does not have roots capability" ));
261
- default :
262
- return new MethodNotFoundError (method , "Method not found: " + method , null );
261
+ if (method .equals (McpSchema .METHOD_ROOTS_LIST )) {
262
+ return new MethodNotFoundError (method , "Roots not supported" ,
263
+ Map .of ("reason" , "Client does not have roots capability" ));
263
264
}
265
+ return new MethodNotFoundError (method , "Method not found: " + method , null );
264
266
}
265
267
266
268
@ Override
0 commit comments