modelcontextprotocol / modelcontextprotocol/java-sdk

StdioServerTransportProvider should support a stdin-close callback without interrupting shutdown

Aperta
#936 0 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

waiting for triage
Lingua principale
Java
Stelle
3.7k
Fork
1.1k
Merge medio
1g 15h
PR unite (30g)
9

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia da StdioServerTransportProvider, in particolare dal ciclo di lettura di stdin e dalla gestione della terminazione di handleIncomingMessages(). Aggiungi il test di regressione suggerito per piped-stream, quindi verifica che il callback venga eseguito dopo la pulizia di session e inbound-sink, prima della dismissione dello scheduler, e che il thread del callback non venga interrotto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
java
Ambito
backend
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Specificata chiaramente
Idoneità per principianti
68/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.