Repository navigation
Conversation
The MCP stdio transport in spec revision 2025-06-18 and later carries newline-delimited JSON messages; readFrame/writeFrame still spoke the older Content-Length header framing, so NDJSON clients (the reference Python and TypeScript SDKs) hung waiting for a header line that never arrived (rest-sh#388). Additionally, when stdout is not a terminal the CLI wraps it in a 64KB buffered writer flushed only on exit. Plugin stdout-data relayed by the command plugin handler was never flushed in between, so even a framing-compatible client received no response until the process exited. Flush after each relayed stdout chunk. Tests updated to NDJSON, with coverage for blank lines, oversized messages, and trailing messages without a newline.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #388. Alternative to #394 that also covers the second half of the bug.
Problem
restish mcp servehangs with MCP clients that speak the current stdio transport. Two independent bugs, both required:Content-Lengthheader frames (spec revision 2024-11-05), while the current spec (2025-06-18 and later) defines stdio messages as newline-delimited JSON. Both reference SDKs (Pythonmcp, TypeScript) send NDJSON, soreadFramewaits forever for aContent-Lengthheader that never comes.bufio.Writerflushed only on exit (internal/cli/cli.go,cascadingFlushWriter). Plugin stdout data relayed incommand_plugin_handlers.gois never flushed in between, so even a framing-compatible client receives no response until the process exits. Measured: with a Content-Length client on stock v2.3.0, theinitializeresponse arrives only after stdin EOF. The flush suggested in restish mcp serve hangs with NDJSON stdio MCP clients #388's body is therefore mandatory, not optional — and it must happen in the parent relay (internal/cli/command_plugin_handlers.go), not only in the plugin-side writer.Changes
cmd/restish-mcp/server.go:readFramenow reads one NDJSON message per call (blank lines skipped, 64MiB message cap preserved, trailing message without newline accepted);writeFrameemits<json>\n. Header-specific constants removed.internal/cli/command_plugin_handlers.go: flush stdout after relaying eachstdout-datamessage from a passthrough plugin.cmd/restish-mcp/mcp_test.goline-limit/framing-error cases incl. blank-line and no-trailing-newline coverage;internal/cli/mcp_plugin_test.goframe helpers).Verification
go build ./...,go test ./...(unit) andgo test -tags integration ./internal/cli/ -run TestMCPpass locally.mcp2.0.0) as client, serving three registered APIs (171 tools):initialize→ ~0.5s (previously: infinite hang)tools/list→ all tools discoveredtools/call(HTTP-delegated) → all OK, ~0.15s each after warmupOne observation for follow-up (not addressed here): a single
fatal error: all goroutines are asleep - deadlock!was seen once right after a cold firsttools/callwhose delegated HTTP request took ~4.6s (TLS setup). Not reproduced across 25+ subsequent calls, but the plugin's idle wait after a slowDo()round trip may deserve a stress test.Note: three pre-existing test failures in
internal/cli(TestRetrySucceedsAfterTransientFailures,TestRetryAfterHeaderRespected,TestAPIRetryMaxWaitCapsRetryAfter, all "exit code 5") reproduce identically on unmodifiedmain(79c3d88) on my machine — unrelated to this change.