Repository navigation
fix(mcp): fall back to legacy when a slow stdio server refuses initialize - #66595
heunghingwan wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Looks good. I reproduced the failure on the base with the issue's script (real Python mcp 2.3.0, 6 s start, protocol unset) and confirmed it connects with this change. The pinned-protocol and no-delay cases still connect. I also tried pool: true with the same slow server: it fails on the base and connects with the PR. Both new tests fail without the change. mcp.test.ts passes, except for one descendant-termination test that also fails on the base. typecheck:tsc is clean.
Public API is unchanged. Only internal connect and fallback behaviour changes.
A note for maintainers, not a blocker: answered: true applies to every UnsupportedProtocolVersionError. A pooled server that hits it (including a slow-starting one) is marked unpoolable for the process and connects per Location from then on. That seems an acceptable trade-off for this case.
One timing point on the first new test, inline.
Related: #66101
| ) | ||
| // Released once the probe has timed out and before that initialize does, so the server reads the probe | ||
| // first, as a slow start makes it. | ||
| await Bun.sleep(2_100) |
There was a problem hiding this comment.
This sleep starts when connect is called, not when the server is spawned, so the release has to land in a roughly 600 ms window (after the 1.5 s probe timeout, before the in-place initialize times out at 3 s). On a loaded CI runner that could be flaky. The pool test anchors its timer on the PID file being written. Doing the same here would make it sturdier.
There was a problem hiding this comment.
The PID-anchored release is now releaseEraLocked in test/mcp/harness.ts, used by both slow-server tests. When #67018 rebuilt the MCP test suite I ported the tests to it (test/mcp/quirks.test.ts and test/mcp/pool.test.ts), which folded the former anchor commit into the first one. The helper still waits for the fixture's first process, then 2.1 s: at least 0.6 s after the probe gives up, and 0.9 s minus the fixture's own boot time before the initialize sent in place times out. Each test still fails without its own change.
There was a problem hiding this comment.
Thanks, checked 7203a64. The new releaseEraLocked waits for the fixture's first PID before starting the 2.1 s timer, so the release now lines up with when the server starts and not with when connect is called. That resolves my concern. The source fix is unchanged, so I have nothing further on this.
The review of anomalyco#66595 noted that the first new test timed its release from the connect call rather than from the server's start, so the gap before the probe's own timer begins depends on how fast the process is spawned. The pool test already anchored on the PID file the fixture writes. Both tests now share releaseEraLocked, which waits for that file and then 2.1 s: at least 0.6 s after the probe gives up, and 0.9 s before the initialize sent in place times out.
There was a problem hiding this comment.
The timing point is addressed. Both slow-server tests now start their release timer from the fixture's PID file. They pass at the new head and typecheck:tsc is clean. The source is unchanged since my last review, so the earlier end-to-end check still applies. Public API is unchanged. Nothing new to raise.
The review of anomalyco#66595 noted that the first new test timed its release from the connect call rather than from the server's start, so the gap before the probe's own timer begins depends on how fast the process is spawned. The pool test already anchored on the PID file the fixture writes. Both tests now share releaseEraLocked, which waits for that file and then 2.1 s: at least 0.6 s after the probe gives up, and 0.9 s before the initialize sent in place times out.
7203a64 to
c80bc34
Compare
There was a problem hiding this comment.
Rebased onto current dev with no changes to the fix itself; it sits cleanly next to the HTTP 5xx fallback from #66588. mcp.test.ts passes on the new head, except for one descendant-termination test that also fails on the old base. The PID-file timing in the slow-server test is still in place. Public API is unchanged. Nothing new to raise.
c80bc34 to
2724121
Compare
The review of anomalyco#66595 noted that the first new test timed its release from the connect call rather than from the server's start, so the gap before the probe's own timer begins depends on how fast the process is spawned. The pool test already anchored on the PID file the fixture writes. Both tests now share releaseEraLocked, which waits for that file and then 2.1 s: at least 0.6 s after the probe gives up, and 0.9 s before the initialize sent in place times out.
There was a problem hiding this comment.
Rebased onto current dev with no changes to the fix. mcp.test.ts passes on the new head, except for one descendant-termination test that also fails on the old base. The PID-file timing in the slow-server tests is still in place. Public API is unchanged. Nothing new to raise.
2724121 to
02a4db9
Compare
The review of anomalyco#66595 noted that the first new test timed its release from the connect call rather than from the server's start, so the gap before the probe's own timer begins depends on how fast the process is spawned. The pool test already anchored on the PID file the fixture writes. Both tests now share releaseEraLocked, which waits for that file and then 2.1 s: at least 0.6 s after the probe gives up, and 0.9 s before the initialize sent in place times out.
There was a problem hiding this comment.
Rebased onto current dev with no changes to the fix; it sits cleanly next to the MCP changes from #66636 and #66685. mcp.test.ts passes on the new head, except for one descendant-termination test that also fails on the old base. typecheck:tsc is clean. The PID-file timing in the slow-server tests is still in place. Public API is unchanged. Nothing new to raise.
02a4db9 to
073a912
Compare
The review of anomalyco#66595 noted that the first new test timed its release from the connect call rather than from the server's start, so the gap before the probe's own timer begins depends on how fast the process is spawned. The pool test already anchored on the PID file the fixture writes. Both tests now share releaseEraLocked, which waits for that file and then 2.1 s: at least 0.6 s after the probe gives up, and 0.9 s before the initialize sent in place times out.
There was a problem hiding this comment.
Rebased onto current dev with no changes to the fix. The pool refactor in #66870 keeps the behaviour the pooled fix relies on: a connect failure marked answered still falls back to the Location's own connection. mcp.test.ts passes on the new head, including both slow-server tests, except for one descendant-termination test that also fails on the old base. typecheck:tsc is clean. Public API is unchanged. Nothing new to raise.
The review of anomalyco#66595 noted that the first new test timed its release from the connect call rather than from the server's start, so the gap before the probe's own timer begins depends on how fast the process is spawned. The pool test already anchored on the PID file the fixture writes. Both tests now share releaseEraLocked, which waits for that file and then 2.1 s: at least 0.6 s after the probe gives up, and 0.9 s before the initialize sent in place times out.
073a912 to
22fbf67
Compare
There was a problem hiding this comment.
Rebased onto current dev with no changes to the fix. It still lines up with the error-type move in #66915 and the pool rework in #66914: ConnectError.answered is still read, and an answered failure still falls back to the Location's own connection. mcp.test.ts passes on the new head, including both slow-server tests, except for one descendant-termination test that also fails on the old base. typecheck:tsc is clean. Public API is unchanged. Nothing new to raise.
The review of anomalyco#66595 noted that the first new test timed its release from the connect call rather than from the server's start, so the gap before the probe's own timer begins depends on how fast the process is spawned. The pool test already anchored on the PID file the fixture writes. Both tests now share releaseEraLocked, which waits for that file and then 2.1 s: at least 0.6 s after the probe gives up, and 0.9 s before the initialize sent in place times out.
22fbf67 to
31efd43
Compare
There was a problem hiding this comment.
Rebased onto current dev with no changes to the fix. It still lines up with the teardown rework in #66923: a failed connect still reports answered, and the pool still falls back to the Location's own connection when it's set. mcp.test.ts passes on the new head, including both slow-server tests, except for one descendant-termination test that also fails on the old base. typecheck:tsc is clean. Public API is unchanged. Nothing new to raise.
The review of anomalyco#66595 noted that the first new test timed its release from the connect call rather than from the server's start, so the gap before the probe's own timer begins depends on how fast the process is spawned. The pool test already anchored on the PID file the fixture writes. Both tests now share releaseEraLocked, which waits for that file and then 2.1 s: at least 0.6 s after the probe gives up, and 0.9 s before the initialize sent in place times out.
31efd43 to
c4c5cc3
Compare
There was a problem hiding this comment.
Rebased onto current dev with no changes to the fix. It still lines up with the config parsing change in #66931: local servers still default to auto, and an answered connect failure still falls back to the Location's own connection. mcp.test.ts passes on the new head, including both slow-server tests, except for one descendant-termination test that also fails on the old base. typecheck:tsc is clean. Public API is unchanged. Nothing new to raise.
c4c5cc3 to
17c6f84
Compare
The review of anomalyco#66595 noted that the first new test timed its release from the connect call rather than from the server's start, so the gap before the probe's own timer begins depends on how fast the process is spawned. The pool test already anchored on the PID file the fixture writes. Both tests now share releaseEraLocked, which waits for that file and then 2.1 s: at least 0.6 s after the probe gives up, and 0.9 s before the initialize sent in place times out.
There was a problem hiding this comment.
Rebased onto current dev with no changes to the fix. The new MCP changes on dev (#67013 event parsing, #66932 OAuth) are outside the connect, fallback and pool code this relies on. mcp.test.ts passes on the new head, except for one descendant-termination test that also fails on the old base. typecheck:tsc is clean. Public API is unchanged. Nothing new to raise.
17c6f84 to
0778aca
Compare
|
Rebased onto the rebuilt MCP test suite (#67018), since it removed
The commit that anchored the release on the PID file is folded into the first one, so the PR now has two commits. Each test still fails without its own change. On the rebased head, |
There was a problem hiding this comment.
Rebased onto the new MCP test suite (#67018) with the fix unchanged. The two regression tests and the era-locked fixture were ported to test/mcp/ (quirks.test.ts and pool.test.ts). Their release timer still starts from the fixture's PID file. Both pass with the change and fail without it. test/mcp passes, except for one stdio descendant-kill test that fails the same way without this change. typecheck:tsc is clean. Public API is unchanged. Nothing new to raise.
0778aca to
2eaea21
Compare
There was a problem hiding this comment.
Rebased onto current dev with no changes to the fix. #67036 changed how the pool gets its local host but left the answered fallback this relies on alone. test/mcp passes on the new head, including both slow-server tests, except for one stdio descendant-kill test that also fails without this change. typecheck:tsc is clean. Public API is unchanged. Nothing new to raise.
2eaea21 to
88f6b3a
Compare
There was a problem hiding this comment.
Rebased onto current dev with no changes to the fix. #67164 changed how pooled event streams are scoped but left the answered fallback this relies on alone. test/mcp passes on the new head, including both slow-server tests, except for one stdio descendant-kill test that also fails without this change. typecheck:tsc is clean. Public API is unchanged. Nothing new to raise.
…lize Since anomalyco#65282 a local server with no protocol gets a 5 s server/discover probe, after which the SDK sends the legacy initialize on the same connection. A server that is still starting reads the probe late, which moves that connection to 2026-07-28, and then refuses the initialize (Python mcp 2.3: "the initialize handshake is not accepted"). The stdio path respawned only on ConnectionClosed and EraNegotiationFailed, so the connect failed whenever the start outlived the probe. Respawn once for the legacy handshake on UnsupportedProtocolVersionError as well, so that initialize arrives first. This is what negotiate already does over HTTP after a probe timeout. A slow server now connects on the legacy protocol; pinning protocol keeps the modern one. The fixture is shaped like the Python server: the first request decides the connection's era, and it starts once the test releases it. Addresses anomalyco#66577
The pool moves a server to the Location's own connection only when the shared attempt failed while the server was reached (ConnectError's answered). The UnsupportedProtocolVersionError branch never set it, so a pooled server whose in-place initialize was refused stayed failed, with the respawn from the previous commit in place or not. The error is a refusal the server sent, and its message already points at other connect options, so report it as answered. The pool then stops sharing that server, and the Location's own connection connects: on 2026-07-28 once the server is up, or through a legacy respawn while it is still slow.
88f6b3a to
3799b06
Compare
There was a problem hiding this comment.
Rebased onto current dev with no changes to the fix. The new MCP changes on dev (#67216 OAuth, #67205 a test harness cleanup) are outside the code this relies on. test/mcp passes on the new head, including both slow-server tests, except for one stdio descendant-kill test that also fails without this change. typecheck:tsc is clean. Public API is unchanged. Nothing new to raise.
Issue for this PR
Closes #66577
Type of change
What does this PR do?
Since #65282 an unset
protocolprobes a local server withserver/discoverand gives up after 5 s. The SDK then sends the legacyinitializeon the same connection. A server that is still starting reads the probe late, which moves that connection to 2026-07-28, and refuses theinitialize(Pythonmcp2.3: "the initialize handshake is not accepted"). The stdio path only respawned onConnectionClosedandEraNegotiationFailed, so the connect failed.This adds
UnsupportedProtocolVersionErrorto the errors that respawn the server once for the legacy handshake, whereinitializearrives first. It is the stdio counterpart of whatnegotiatealready does over HTTP: a probe timeout falls back to legacy on a fresh transport.With
pool: truethe shared connection never goes legacy, so it has no respawn to fall back on. The pool retries on the Location's own connection only when the failure says the server was reached, and this error did not say so. It does now.A slow server now connects on the legacy protocol, the same as a slow server over HTTP. Pinning
protocolto"2026-07-28"still gets the modern protocol.How did you verify your code works?
mcp2.3.0, 6 s start) throughMcpClient.connectwith noprotocol: fails ondevafter 7 s, connects with the change.packages/core:bun test test/mcp.test.ts(103 pass) and the full core suite (7967 pass);bun run typecheck:tscandbun run lintare clean.Screenshots / recordings
Not a UI change.
Checklist
Added by the opencode bot (AI-generated).
Related: #66101