@@ -77,7 +77,7 @@ public class StdioServerTransport implements McpTransport {
77
77
* Creates a new StdioServerTransport with a default ObjectMapper and System streams.
78
78
*/
79
79
public StdioServerTransport () {
80
- this (new ObjectMapper (), System . in , System . out );
80
+ this (new ObjectMapper ());
81
81
}
82
82
83
83
/**
@@ -86,26 +86,15 @@ public StdioServerTransport() {
86
86
* @param objectMapper The ObjectMapper to use for JSON serialization/deserialization
87
87
*/
88
88
public StdioServerTransport (ObjectMapper objectMapper ) {
89
- this (objectMapper , System .in , System .out );
90
- }
91
89
92
- /**
93
- * Creates a new StdioServerTransport with the specified ObjectMapper and streams.
94
- * @param objectMapper The ObjectMapper to use for JSON serialization/deserialization
95
- * @param inputStream The input stream to read from
96
- * @param outputStream The output stream to write to
97
- */
98
- public StdioServerTransport (ObjectMapper objectMapper , InputStream inputStream , OutputStream outputStream ) {
99
90
Assert .notNull (objectMapper , "The ObjectMapper can not be null" );
100
- Assert .notNull (inputStream , "The InputStream can not be null" );
101
- Assert .notNull (outputStream , "The OutputStream can not be null" );
102
91
103
92
this .inboundSink = Sinks .many ().unicast ().onBackpressureBuffer ();
104
93
this .outboundSink = Sinks .many ().unicast ().onBackpressureBuffer ();
105
94
106
95
this .objectMapper = objectMapper ;
107
- this .inputStream = inputStream ;
108
- this .outputStream = outputStream ;
96
+ this .inputStream = System . in ;
97
+ this .outputStream = System . out ;
109
98
110
99
// Use bounded schedulers for better resource management
111
100
this .inboundScheduler = Schedulers .newBoundedElastic (1 , 1 , "inbound" );
@@ -248,50 +237,31 @@ public Mono<Void> closeGracefully() {
248
237
return Mono .fromRunnable (() -> {
249
238
isClosing = true ;
250
239
logger .debug ("Initiating graceful shutdown" );
251
- })
252
- // .then(Mono.defer(() -> {
253
- // // First complete the sinks to stop processing
254
- // inboundSink.tryEmitComplete();
255
- // outboundSink.tryEmitComplete();
256
- // return Mono.delay(Duration.ofMillis(100));
257
- // }))
258
- .then (Mono .fromRunnable (() -> {
259
- try {
260
- // Dispose schedulers first
261
- inboundScheduler .dispose ();
262
- outboundScheduler .dispose ();
263
-
264
- // Wait for schedulers to terminate
265
- if (!inboundScheduler .isDisposed ()) {
266
- inboundScheduler .disposeGracefully ().block (Duration .ofSeconds (5 ));
267
- }
268
- if (!outboundScheduler .isDisposed ()) {
269
- outboundScheduler .disposeGracefully ().block (Duration .ofSeconds (5 ));
270
- }
271
-
272
- // Only after schedulers are disposed, close the streams
273
- try {
274
- if (inputStream != System .in ) {
275
- inputStream .close ();
276
- }
277
- if (outputStream != System .out ) {
278
- outputStream .flush ();
279
- outputStream .close ();
280
- }
281
- }
282
- catch (IOException e ) {
283
- // Log but don't throw since we're shutting down
284
- logger .debug ("Error closing streams during shutdown" , e );
285
- }
286
-
287
- logger .info ("Graceful shutdown completed" );
240
+ }).then (Mono .defer (() -> {
241
+ // First complete the sinks to stop processing
242
+ inboundSink .tryEmitComplete ();
243
+ outboundSink .tryEmitComplete ();
244
+ return Mono .delay (Duration .ofMillis (100 ));
245
+ })).then (Mono .fromRunnable (() -> {
246
+ try {
247
+ // Dispose schedulers first
248
+ inboundScheduler .dispose ();
249
+ outboundScheduler .dispose ();
250
+
251
+ // Wait for schedulers to terminate
252
+ if (!inboundScheduler .isDisposed ()) {
253
+ inboundScheduler .disposeGracefully ().block (Duration .ofSeconds (5 ));
288
254
}
289
- catch ( Exception e ) {
290
- logger . error ( "Error during graceful shutdown" , e );
255
+ if (! outboundScheduler . isDisposed () ) {
256
+ outboundScheduler . disposeGracefully (). block ( Duration . ofSeconds ( 5 ) );
291
257
}
292
- }))
293
- .then ()
294
- .subscribeOn (Schedulers .boundedElastic ());
258
+
259
+ logger .info ("Graceful shutdown completed" );
260
+ }
261
+ catch (Exception e ) {
262
+ logger .error ("Error during graceful shutdown" , e );
263
+ }
264
+ })).then ().subscribeOn (Schedulers .boundedElastic ());
295
265
}
296
266
297
267
@ Override
0 commit comments