Skip to content

Commit 70e8a0f

Browse files
authored
fix(core): fall back to legacy when the MCP probe gets an HTTP 5xx (#66588)
1 parent dc390b6 commit 70e8a0f

3 files changed

Lines changed: 47 additions & 13 deletions

File tree

‎packages/core/src/mcp/client.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,12 +585,17 @@ export const connect = Effect.fnUntraced(function* (options: ConnectOptions) {
585585
})
586586
// Every attempt opens a fresh transport; no retry starts once the startup timeout has passed.
587587
const deadline = (yield* Clock.currentTimeMillis) + (timeout?.startup ?? DEFAULT_STARTUP_TIMEOUT)
588-
// Over HTTP the SDK treats an unanswered auto probe as an outage, but slow legacy servers may still initialize
589-
// within what is left of the startup timeout.
588+
// Over HTTP the SDK treats an unanswered auto probe as an outage and a 5xx answer as a failed connect, but slow
589+
// legacy servers may still initialize within what is left of the startup timeout, and many (Spring AI among
590+
// them) answer the unknown server/discover with a 500.
590591
const negotiate = (url: URL) =>
591592
open(url, protocol).pipe(
592593
Effect.catchIf(
593-
(error) => protocol === "auto" && error instanceof SdkError && error.code === SdkErrorCode.RequestTimeout,
594+
(error) =>
595+
protocol === "auto" &&
596+
error instanceof SdkError &&
597+
(error.code === SdkErrorCode.RequestTimeout ||
598+
(error instanceof SdkHttpError && error.code === SdkErrorCode.EraNegotiationFailed)),
594599
(error) =>
595600
Clock.currentTimeMillis.pipe(
596601
Effect.flatMap((now) => (now < deadline ? open(url, "legacy", deadline - now) : Effect.fail(error))),

‎packages/core/test/mcp.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,34 @@ test("falls back to legacy when a stdio server ignores the version probe", async
951951
expect(result.tools.map((tool) => tool.name)).toEqual(["echo"])
952952
})
953953

954+
testEffect(Layer.empty).live("falls back to legacy when a remote server answers the version probe with HTTP 500", () =>
955+
Effect.gen(function* () {
956+
const probes = { count: 0 }
957+
// Spring AI answers methods it has no handler for, server/discover included, with a 500.
958+
const server = yield* resourceServer({
959+
resources: false,
960+
respond: async (request) => {
961+
if (request.method !== "POST") return undefined
962+
const body: unknown = await request.clone().json()
963+
if (typeof body !== "object" || body === null || !("method" in body) || body.method !== "server/discover")
964+
return undefined
965+
probes.count += 1
966+
return new Response("Missing handler for request type: server/discover", { status: 500 })
967+
},
968+
})
969+
const connection = yield* connect(
970+
"resources",
971+
new ConfigMCP.Remote({ type: "remote", url: server.url, oauth: false }),
972+
import.meta.dir,
973+
)
974+
975+
expect(connection.modern).toBe(false)
976+
expect((yield* connection.tools()).map((tool) => tool.name)).toEqual(["echo"])
977+
expect(probes.count).toBe(1)
978+
expect(server.state.initializations).toBe(1)
979+
}),
980+
)
981+
954982
test("applies the configured MCP catalog timeout", async () => {
955983
const result = Effect.runPromise(
956984
Effect.scoped(
@@ -3285,8 +3313,9 @@ describe("MCP pool", () => {
32853313
})
32863314

32873315
// A server that is down, or that refuses this client, would refuse a private connection too.
3316+
// Each of the 3 attempts at a server that is down probes, then tries initialize in case it is legacy.
32883317
for (const [code, what, attempts] of [
3289-
[503, "was down", 3],
3318+
[503, "was down", 6],
32903319
[401, "rejected the credentials", 1],
32913320
[403, "refused access", 1],
32923321
] as const)

‎services/site/src/docs/content/mcp-servers.mdx‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Use configuration for other process options such as a working directory:
119119
| `disabled` | No | Prevents connection when `true`. Defaults to `false`. |
120120
| `codemode` | No | Set to `false` to expose tools directly instead of through Code Mode. Defaults to `true`. |
121121
| `timeout` | No | Per-server timeout overrides. |
122-
| `protocol` | No | `legacy` (default), `auto`, or `2026-07-28`. See [Protocol version](#protocol-version). |
122+
| `protocol` | No | `auto` (default), `legacy`, or `2026-07-28`. See [Protocol version](#protocol-version). |
123123
| `pool` | No | Share one connection across directories. Defaults to `false`. See [Shared connections](#shared-connections). |
124124

125125
Use `{env:NAME}` for environment substitution. Shell expressions such as `$NAME` are not expanded in JSON strings:
@@ -175,7 +175,7 @@ Use configuration for other options. Store secrets in environment variables rath
175175
| `disabled` | No | Prevents connection when `true`. Defaults to `false`. |
176176
| `codemode` | No | Set to `false` to expose tools directly instead of through Code Mode. Defaults to `true`. |
177177
| `timeout` | No | Per-server timeout overrides. |
178-
| `protocol` | No | `legacy` (default), `auto`, or `2026-07-28`. See [Protocol version](#protocol-version). |
178+
| `protocol` | No | `auto` (default), `legacy`, or `2026-07-28`. See [Protocol version](#protocol-version). |
179179
| `pool` | No | Share one connection across directories. Defaults to `false`. See [Shared connections](#shared-connections). |
180180

181181
Use `oauth: false` only when the server exclusively uses an API key or another header credential:
@@ -335,17 +335,17 @@ Timeouts are positive integer milliseconds. Set defaults under `mcp.timeout`; a
335335

336336
## Protocol version
337337

338-
OpenCode opens every server with the classic MCP `initialize` handshake by default. Set a server's `protocol` to talk to one built on the 2026-07-28 revision:
338+
OpenCode probes every server for the 2026-07-28 revision by default and falls back to the classic MCP `initialize` handshake when the server does not support it. Set a server's `protocol` to `legacy` to skip the probe:
339339

340340
```jsonc title="opencode.jsonc"
341341
{
342342
"$schema": "https://opencode.ai/config.json",
343343
"mcp": {
344344
"servers": {
345-
"modern": {
345+
"older": {
346346
"type": "remote",
347347
"url": "https://mcp.example.com/mcp",
348-
"protocol": "auto",
348+
"protocol": "legacy",
349349
},
350350
},
351351
},
@@ -354,11 +354,11 @@ OpenCode opens every server with the classic MCP `initialize` handshake by defau
354354

355355
| Value | Behavior |
356356
| --- | --- |
357-
| `legacy` | Default. Sends `initialize` and speaks protocol revisions up to 2025-11-25. |
358-
| `auto` | Probes with `server/discover` for the 2026-07-28 revision and falls back to `legacy` when the server does not support it. |
357+
| `auto` | Default. Probes with `server/discover` for the 2026-07-28 revision and falls back to `legacy` when the server does not support it, including when a remote server answers the probe with an HTTP 5xx error. |
358+
| `legacy` | Sends `initialize` and speaks protocol revisions up to 2025-11-25. |
359359
| `2026-07-28` | Requires the 2026-07-28 revision. The connection fails against older servers. |
360360

361-
Probing a local `legacy` server adds a short-lived extra process and can wait up to the startup timeout when the server ignores unknown requests, so keep `auto` scoped to servers that need it.
361+
Probing an older local server adds a short-lived extra process and can wait up to the startup timeout when the server ignores unknown requests, so set `legacy` on servers you know predate the 2026-07-28 revision.
362362

363363
## Shared connections
364364

@@ -379,7 +379,7 @@ One OpenCode server can have many directories open, and each one connects to eve
379379
}
380380
```
381381

382-
- Only connections on the 2026-07-28 revision are shared. With `pool` set, an unset `protocol` means `auto`. A server that answers with an older revision, or one set to `legacy`, keeps one connection per directory.
382+
- Only connections on the 2026-07-28 revision are shared. A server that answers with an older revision, or one set to `legacy`, keeps one connection per directory.
383383
- Directories share a connection only when everything that changes it matches: the URL, headers, OAuth settings and signed-in account for a remote server, and the command, working directory and environment for a local one. Timeouts and `protocol` must match too. The server's name doesn't need to, but sign-ins are stored per name, so an OAuth server is shared only under the same name.
384384
- A local server is shared only between directories on this machine, not ones in a workspace. Sharing it across projects needs a fixed `cwd`, such as `"~"`. Without one it runs in each project's folder, so each project gets its own and OpenCode logs a warning.
385385
- A shared connection has no roots. When a tool asks for input, the request appears in the directory that called it.

0 commit comments

Comments
 (0)