fix(stdio): drain accepted responses before EOF shutdown

Closed
AC
2mo ago
+90-5

Fixes #2678

Summary

Ensure that accepted stdio requests complete and their JSON-RPC responses are
written before the server shuts down after stdin reaches EOF.

Motivation and Context

When a stdio server receives JSONL input through shell redirection, stdin may
reach EOF while one or more accepted request handlers are still running. The
current shutdown path can cancel those handlers before their responses are
flushed to stdout.

As a result, the final tool-call responses may disappear silently, even though
the requests were accepted and processed.

This change adds a bounded graceful-shutdown drain for active requests. The
default behavior remains unchanged for transports that do not opt into the
drain window.

How Has This Been Tested?

  • Reproduced the issue with file-redirected stdin.
  • Tested multiple in-flight requests with an artificial delay.
  • Verified that all accepted request IDs are emitted before process exit.
  • Added regression coverage for stdin EOF during active request handling.
  • Verified that the shutdown wait remains bounded if a handler does not finish.

Environment:

  • Python 3.12
  • MCP Python SDK 1.27.x
  • stdio transport
  • Linux/macOS

Breaking Changes

No breaking changes. The graceful-shutdown drain is bounded and opt-in. Existing
behavior for other transports remains unchanged.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature which would cause existing functionality to change)
  • Documentation update

Checklist

  • I am assigned to the linked issue (or it is labeled help wanted, or I'm a maintainer)
  • I have disclosed any AI assistance and can explain the change in my own words
  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

The implementation is available in:

https://github.com/ace-trump-tech/python-sdk/tree/fix/stdio-eof-drain