modelcontextprotocol / modelcontextprotocol/java-sdk

StdioServerTransportProvider should support a stdin-close callback without interrupting shutdown

オープン
#936 コメント 0 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

waiting for triage
主要言語
Java
スター
3.7k
フォーク
1.1k
平均マージ
1日 15時間
マージ済み PR(30日)
9

説明

For stdio-based MCP servers, stdin closing is effectively the client disconnect signal. This is especially important when an MCP server runs in a container: once the MCP client disconnects or closes stdin, the server should be able to trigger application shutdown and clean up background resources.

Today, StdioServerTransportProvider closes the MCP session when stdin reaches EOF, but it does not expose a public transport-level callback that applications can use to run shutdown logic.

Downstream, we had to vendor/copy StdioServerTransportProvider to add such a callback.

Current behavior

When the stdin read loop exits, the SDK transport does roughly this:

finally {
  isClosing.set(true);
  if (session != null) {
    session.close();
  }
  inboundSink.tryEmitComplete();
}

The session is closed, but the application has no direct hook to react to the stdio client disconnect.

There is also a subtle ordering issue. handleIncomingMessages() currently disposes the inbound scheduler from doOnTerminate():

this.inboundSink.asFlux()
  .flatMap(message -> session.handle(message))
  .doOnTerminate(() -> {
    this.outboundSink.tryEmitComplete();
    this.inboundScheduler.dispose();
  })
  .subscribe();

When stdin closes, the inbound read loop completes inboundSink. The termination callback can run on the same inbound thread. Disposing the scheduler there may call shutdownNow(), interrupting that same thread before downstream shutdown work has completed.

We observed this downstream: shutdown logic could run with the current thread interrupt flag already set, causing graceful shutdown to fail or exit early.

Expected behavior

StdioServerTransportProvider should allow applications to register an optional callback for stdin EOF / stdio client disconnect.

That callback should run:

  1. After the session is closed.
  2. After the inbound sink is completed.
  3. Before the inbound scheduler is disposed.
  4. Without the current thread being interrupted by scheduler disposal.
Proposed fix

Add an optional callback to StdioServerTransportProvider, for example as a Runnable:

public StdioServerTransportProvider(
    McpJsonMapper jsonMapper,
    InputStream inputStream,
    OutputStream outputStream,
    Runnable closeCallback
)

Or, if a reactive API is preferred:

Supplier<Mono<Void>> closeCallback

Then move inboundScheduler.dispose() out of handleIncomingMessages().doOnTerminate(...) and into the inbound read-loop finally, after the callback has completed.

The inbound read-loop cleanup would look conceptually like this:

finally {
  isClosing.set(true);
  if (session != null) {
    session.close();
  }
  inboundSink.tryEmitComplete();

  if (closeCallback != null) {
    closeCallback.run();
  }

  inboundScheduler.dispose();
}

handleIncomingMessages() should still complete the outbound sink, but should not dispose the inbound scheduler from the termination callback.

Suggested regression test

Add a test that:

  1. Creates a StdioServerTransportProvider with a piped input stream.
  2. Registers a callback.
  3. Starts the transport with a mock session.
  4. Closes the piped output stream to simulate stdin EOF.
  5. Verifies the callback runs.
  6. Verifies Thread.currentThread().isInterrupted() is false inside the callback.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

StdioServerTransportProviderから始め、特にstdinの読み取りループとhandleIncomingMessages()の終了処理を確認してください。次に、提案されたpiped-streamのリグレッションテストを追加し、sessionとinbound-sinkのクリーンアップ後、schedulerの破棄前にcallbackが実行され、callbackスレッドが割り込みを受けないことを確認してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
java
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
明確に書かれている
初心者へのやさしさ
68/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。