Skip to content

Conversation

@ArtemYurov
Copy link

Problem:

  • MCP server disconnects after ~60 seconds of inactivity
  • fread() blocks and returns false when no data available
  • This triggers "Failed to read from STDIN" error and breaks the listening loop

Solution (Variant 3):

  • Explicitly set stream_set_blocking(true) for STDIN
  • Keep feof() check for EOF detection
  • Simplest code among all variants

Testing:
✅ Tested for 150+ seconds without disconnections
✅ Compatible with macOS and other UNIX systems
✅ Simplest implementation

Changes:

  • Added stream_set_blocking($this->input, true) before the listening loop
  • Keep while (!\feof($this->input) && !$this->closing) condition
  • Fixed $deferred->resolve() to pass null argument (line 129)

Limitations:
❌ Graceful shutdown does not work properly
❌ Cannot check $this->closing flag during fread() blocking ❌ Server will not exit until data arrives on STDIN ❌ Not recommended for production use due to shutdown issues

Comparison to other variants:

  • Simplest code but sacrifices graceful shutdown capability
  • Use Variant 1 (stream_select + feof) for production instead

…m_set_blocking)

Problem:
- MCP server disconnects after ~60 seconds of inactivity
- fread() blocks and returns false when no data available
- This triggers "Failed to read from STDIN" error and breaks the listening loop

Solution (Variant 3):
- Explicitly set stream_set_blocking(true) for STDIN
- Keep feof() check for EOF detection
- Simplest code among all variants

Testing:
✅ Tested for 150+ seconds without disconnections
✅ Compatible with macOS and other UNIX systems
✅ Simplest implementation

Changes:
- Added stream_set_blocking($this->input, true) before the listening loop
- Keep while (!\feof($this->input) && !$this->closing) condition
- Fixed $deferred->resolve() to pass null argument (line 129)

Limitations:
❌ Graceful shutdown does not work properly
❌ Cannot check $this->closing flag during fread() blocking
❌ Server will not exit until data arrives on STDIN
❌ Not recommended for production use due to shutdown issues

Comparison to other variants:
- Simplest code but sacrifices graceful shutdown capability
- Use Variant 1 (stream_select + feof) for production instead
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant