Skip to content

fix(mcp): fall back to legacy when a slow stdio server refuses initialize - #66595

Open
heunghingwan wants to merge 2 commits into
anomalyco:devfrom
heunghingwan:mcp-probe-timeout
Open

heunghingwan wants to merge 2 commits into
anomalyco:devfrom
heunghingwan:mcp-probe-timeout

Conversation

@heunghingwan

@heunghingwan heunghingwan commented Nov 26, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #66577

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Since #65282 an unset protocol probes a local server with server/discover and gives up after 5 s. The SDK then 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 refuses the initialize (Python mcp 2.3: "the initialize handshake is not accepted"). The stdio path only respawned on ConnectionClosed and EraNegotiationFailed, so the connect failed.

This adds UnsupportedProtocolVersionError to the errors that respawn the server once for the legacy handshake, where initialize arrives first. It is the stdio counterpart of what negotiate already does over HTTP: a probe timeout falls back to legacy on a fresh transport.

With pool: true the 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 protocol to "2026-07-28" still gets the modern protocol.

How did you verify your code works?

  • New tests, with a fixture that behaves like the Python server (the first request decides the connection's era). Each fails without its own change, with the error from the issue, and passes with it: one for the respawn, one for the pooled path.
  • Ran the server from the issue (Python mcp 2.3.0, 6 s start) through McpClient.connect with no protocol: fails on dev after 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:tsc and bun run lint are clean.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #66101

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread packages/core/test/mcp.test.ts Outdated
)
// 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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@heunghingwan heunghingwan Nov 26, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

heunghingwan added a commit to heunghingwan/opencode that referenced this pull request Nov 26, 2026
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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

heunghingwan added a commit to heunghingwan/opencode that referenced this pull request Nov 26, 2026
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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

heunghingwan added a commit to heunghingwan/opencode that referenced this pull request Nov 27, 2026
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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

heunghingwan added a commit to heunghingwan/opencode that referenced this pull request Nov 27, 2026
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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

heunghingwan added a commit to heunghingwan/opencode that referenced this pull request Nov 27, 2026
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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

heunghingwan added a commit to heunghingwan/opencode that referenced this pull request Nov 28, 2026
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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

heunghingwan added a commit to heunghingwan/opencode that referenced this pull request Nov 28, 2026
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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

heunghingwan added a commit to heunghingwan/opencode that referenced this pull request Nov 28, 2026
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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

heunghingwan added a commit to heunghingwan/opencode that referenced this pull request Nov 28, 2026
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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@heunghingwan

Copy link
Copy Markdown
Author

Rebased onto the rebuilt MCP test suite (#67018), since it removed test/mcp.test.ts. The fix in packages/core/src/mcp/client.ts is unchanged. The tests moved with it:

  • the respawn test is in test/mcp/quirks.test.ts, next to the stdio server that ignores the version probe;
  • the pooled test is in test/mcp/pool.test.ts;
  • the Python-shaped fixture is test/mcp/fixture/era-locked.ts, and eraLocked and releaseEraLocked are in test/mcp/harness.ts.

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, bun run script/test.ts test/mcp gives 145 pass, the full core suite 8055 pass, and typecheck:tsc and bun run lint are clean.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
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.

mcp: slow-starting local server fails under default protocol ("initialize handshake is not accepted")

1 participant