Repository navigation
MCP: AUTO mode over STDIO, probing with server/discover (#1670) - #1729
Conversation
jlouvel
left a comment
There was a problem hiding this comment.
Round 1.
I reviewed only this PR's diff on top of feature/mcp-sdk-modern-client. The probe classification over stdio matches the 2026-07-28 Versioning and Compatibility rules and the HTTP path: a DiscoverResult naming 2026-07-28 or a recognized modern error means modern, -32022 reuses offersLegacy, and any other JSON-RPC error or a timeout (stdio only) means legacy. Transport failures such as EOF are not treated as legacy. The legacy refactor keeps the version order (versions.reversed(), as RestletMcpClientTransport does), and non-auto stdio still offers VERSION only. The tests are strong where it counts. The ignored-probe test would fail if the late answer reached the legacy transport, because correlate throws on an unsolicited id. The -32601 test proves LEGACY_VERSIONS is offered, since a VERSION-only offer would reject 2025-06-18.
Two findings, both about the stream's lifecycle:
- Medium:
detachwaits on the write lock, so closing a client over a server that has stopped reading stdin hangs before the stream is ever stopped. This is a regression for the plain legacy stdio client over the adapter's own stream. - Low:
stop()closes the adapter's own channel only if the client closed without throwing.
Review assisted by Claude Code.
113d5e6 to
181b19b
Compare
jlouvel
left a comment
There was a problem hiding this comment.
Round 2.
Both round-1 points hold in 181b19b:
McpStdioChannel.detachtakes only the channel's own monitor now, so a write stuck on a peer that stopped reading no longer holds it.aServerThatStopsReadingDoesNotHoldTheStoppins this: without the fix,stop()never gets pastdetach.McpClientAdapter.stop()closes the adapter's own channel in afinally. The same test pins this too: the legacy close throws there, so without thefinallythe stuck call would never end andcall.joinwould fail.
The fix adds no new race that I can find. A write that races detach already has its id in pending, so its id moves to abandoned. If that write then fails, its id stays in abandoned, which is bounded and cleared when the id is reused, so nothing is misrouted. The next sink's writes wait on writing, so frames never interleave. On the modern side, sdk.close() closes FrameOutput without its monitor, so it doesn't block behind the SDK's stuck writer either.
I also checked what round 1 may have missed:
McpModernStdioTransport: the stream views give one frame per line, end on close or on the channel's end, and keep a read failure distinct from EOF. The SDK writes each line in onewrite, so the output limit can't send a truncated frame.- Consistency with the current base: the SDK's stdio transport already fails open exchanges with
McpResponseLostExceptionand applies the round timeout itself, so the stdio side matches the HTTP transport, which has no exchange timeout of its own. - Probe classification over stdio matches the HTTP path, plus the timeout case.
StdioClientHelperownership and the docs inchanges.mdlook right.
One Low inline, about how stop() behaves in the case the fix targets.
Review assisted by Claude Code.
jlouvel
left a comment
There was a problem hiding this comment.
Round 3.
The round-2 point holds in b9168e1. stop() stops the adapter's own stream first, so a write stuck on a full pipe fails at once and writers.close() ends in time. A failure from the channel or from either client is kept, the first one thrown with the later ones suppressed. aServerThatStopsReadingDoesNotHoldTheStop now calls client::stop directly, so it fails if stop() throws.
I checked the new order for regressions:
- Legacy client after the stream has stopped:
closeGracefully->detachdoes nothing on a closed channel (no sink attached), and the writers end once the write fails. Over the adapter's own stream this is the order the owned transport used before this PR. - Modern client after the stream has stopped: the SDK's
StdioMcpClientTransport.close()fails open exchanges, disposes its writer, and closesFrameOutput, which closesFramesand wakes the SDK's reader intake(). It waits on nothing (no process to wait for), so it hits no error or delay. - HTTP path and
stdio:connector:ownChannelis null there, socloseOwnChannel()does nothing and the order is the same as before. stop()during or right after a failed start:start()already closed and clearedownChannel, andstartModern/probeclose the candidate.stop()finds nothing left to close and doesn't throw.
One Low inline, about a case the new order handles worse than the previous commit did. A final pass over the rest of the diff found nothing new.
Review assisted by Claude Code.
jlouvel
left a comment
There was a problem hiding this comment.
Round 4.
The round-3 point holds in b00dce5. stop() now closes the modern client first, then the adapter's own stream, then the legacy client. Every failure is kept: the first one is thrown and the later ones are added as suppressed. Closing the modern client calls frames.close(), which wakes a channel reader waiting in Frames.receive. So the stream's reader no longer runs out its join, and stop() no longer throws "MCP STDIO reader shutdown timed out".
The new order keeps the earlier fixes:
- Round 1 (detach without the writing lock): unchanged.
McpStdioChannel.detachtakes only the channel's monitor. - Round 2 (own stream stopped before the legacy close): it still holds, since
closeOwnChannel()runs beforelegacy.close(). With no modern client attached, the order is the same as in b9168e1, soaServerThatStopsReadingDoesNotHoldTheStopstill covers it. - Modern close with the SDK's writer stuck on a full pipe: I checked SDK 2.2.0's
StdioMcpClientTransport.close(). It fails the open exchanges, callswriter.dispose()(an interrupt, whichwriteFrameignores, and no wait), closesoutwithcloseQuietly, and disposes its schedulers. None of these wait.awaitExitruns only with a process, and there is none here.FrameOutput.close()isn't synchronized, so it doesn't wait behind the writer thread, which holdsFrameOutput's monitor andwriting. It callsFrames.close(), which takes only the monitor ofFrames, whichreceiveandtakerelease inwait(), and thendetach. So the modern close returns before the stream stops, andcloseOwnChannel()then ends the stuck write. The writer thread then holds no lock that the rest ofstop()needs. - The case of round 3:
failOpenExchangessignals the error while the reader thread is insidenext. The serialized sink queues that error and returns, andreaderScheduler.dispose()interrupts the progress handler that is blocking. Nothing in the close waits on that subscriber.
The test is strong. The SDK runs the progress handler on its reader thread, inside concatMap through deliverProgress. So a blocked handler stops take(), and 256 notifications fill the 64-frame queue. On the previous order, the reader stays in wait() after the stream stops, and the 30 s join fails the 10 s bound. Under load, the test isn't flaky: the 500 ms sleep only affects whether it would catch a regression, not whether it passes on this commit. With this order, stop() waits on nothing slow. Cleanup ends the fake server's sender, which may be blocked on the full pipe, by closing responses.sink().
A final pass over the rest of the diff found nothing new. Round 4 is clean.
Review assisted by Claude Code.
In auto mode, the MCP client now probes stdio servers with server/discover within the request timeout, over its own stream or a stdio: connector, and falls back to a legacy initialize offering the legacy revisions on the same stream when the server rejects the probe or leaves it unanswered. A late answer to the abandoned probe is dropped. The 2026-07-28 client, also when asked for explicitly, speaks over stdio through the SDK's stdio transport; the Tasks client stays HTTP only. A new McpStdioChannel owns the stream's reader and serialized writes, and hands frames to one sink at a time. The legacy stdio transport reads from it instead of owning its own loop, and StdioClientHelper keeps one channel per connector. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…m even if a client fails to close Co-Authored-By: Claude Opus 5.5 <[email protected]>
…very close failure Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ber's reader may wait on Co-Authored-By: Claude Opus 5.5 <[email protected]>
b00dce5 to
ebd074d
Compare
|



Last open part of #1670 (item 2 over stdio):
McpClientAdapterinautomode now probes stdio servers withserver/discoverinstead of always falling back to legacy, and the 2026-07-28 client works over stdio.Stacked on #1728 (base
feature/mcp-sdk-modern-client). Retarget to3.0once #1728 merges.What it does
StreamTransportor astdio:connector): sendsserver/discoverthrough the SDK's modern client withinrequestTimeout.DiscoverResultnaming 2026-07-28 → modern client.-32022→ legacy only ifdata.supportedoffers a legacy revision, else "No MCP revision supported by both..." (reusesisLegacy/offersLegacy).initializeruns on the same stream, offeringMcpProtocol.LEGACY_VERSIONSas HTTP auto does.MODERN_VERSIONover stdio now works (it threwUnsupportedOperationExceptionbefore). It discovers first, asstartModerndoes over HTTP.McpErasstays per HTTP origin. A stdio process or connector serves one client, so each start probes once. The class javadoc says so.getTasksClient()over stdio throwsUnsupportedOperationException("HTTP only for now"). Adapting it isn't trivial:McpTasksClientcallsMcpModernHttpTransport.senddirectly and numbers its own request ids from 1, which would collide with the SDK client's ids on a shared stdio stream (the SDK transport rejects duplicate ids).Design
internal/McpStdioChannel(new): one perStreamTransport. It owns the single reader (a virtual thread) and the serialized frame writes, and appliesmaxMessageBytes(setMaxOperationBytes(max+1), starting the stream once; later opens must use the same limit). Frames go to the attached sink, in order. Frames read while no sink is attached are queued (at most 64, oldest dropped) and delivered on attach. It records the ids of requests a sink wrote and left unanswered when it detached, and of requests it cancelled (notifications/cancelled), and drops later responses with those ids (at most 1024 remembered). EOF and read errors go to the attached sink, or to the next one.Pipefor the client that comes next. Under load this broke the legacy fallback in the tests, so it is part of the design now.internal/McpModernStdioTransport): the SDK'sStdioMcpClientTransport.builder(InputStream, OutputStream), withjsonMapper,inputMaxSize(maxMessageBytes)andshutdownTimeout, over stream views of the channel. Input is the attached frames, each followed by\n, and ends on detach or close. Output is split on\ninto channel frames, withinmaxMessageBytes. I kept the SDK transport rather than writing our ownMcpClientTransport: it already routes responses, progress andsubscriptions/listennotifications by id or token, and sendsnotifications/cancelled. Closing it closes its output, which detaches from the channel. The wrapper adds themaxConcurrentCallslimit on calls (listen streams excepted), as HTTP has.StdioMcpClientTransportis now a channel sink and no longer runs its own read loop. Its correlation, expiry, reject-server-requests logic and limits are unchanged. It takes the versions to offer (non-auto:VERSIONonly, as before), andowneddecides whether closing it closes the channel or only detaches. The public constructor over aStreamTransportused byMcpStdioTestCaseis kept.McpLegacyEdge.StdioTransportand theBorrowedwrapper are gone (all internal).StdioClientHelperkeeps one channel per connector instead of a legacy transport, so one adapter can probe and then bind either era on it. Stopping the helper still ends the stream. The non-autobindkeeps its behavior, including "limits fixed once connected".McpClientAdaptercloses its own stream's channel onstop(), or whenstart()fails, as the owned legacy transport did before.Tests
New
McpStdioAutoTestCase(pipes):stdio:connector against a RestletStdioServerHelper+McpServerAdapter→ modern, tool call and listing work, connector stays started afterstop().server/discoverwith-32601→ legacy, negotiated2025-06-18, tool call works.server/discover(2 s request timeout) → legacy, and its late answer to the probe, sent just before it answersinitialize, is dropped without breaking the session.-32022offering only2099-01-01→ fails with no shared revision, noinitializesent. Also-32022offering2025-06-18→ legacy.getModernClient()andcallTool,getTasksClient()→UnsupportedOperationException.initialize(offering2025-11-25, empty capabilities),notifications/initializedandtools/call. No secondserver/discover, and no message mentions 2026-07-28.McpModernClientTestCase.modernModeLimitschanges: a MODERNstdio:target on an HTTP connector now fails withIllegalStateException(no stdio helper), notUnsupportedOperationException. Existing stdio tests (McpStdioTestCase,McpModernStdioTestCase) are unchanged and pass../mvnw -B verify -P default,test -pl org.restlet.ext.mcp: 213 tests, 0 failures, BUILD SUCCESS (Spotless, JaCoCo coverage checks met, SpotBugs 0). A first full run failed only on themodernModeLimitsexpectation above, before I updated it; no flakes seen.Out of scope
autothe default (the issue's open question).Issue #1670 must be closed by hand once this reaches
3.0, which isn't the default branch.🤖 Generated with Claude Code