GeezerVison is showing github.com — fix(core): fall back to legacy when the MCP probe gets an HTTP 5xx (#… · erickwill/opencode@70e8a0f
File treeExpand file treeCollapse file tree Open diff view settings services/site/src/docs/content
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/core/src/mcp/client.ts +8-3Lines changed: 8 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -585,12 +585,17 @@ export const connect = Effect.fnUntraced(function* (options: ConnectOptions) { | 585 | 585 | }) | 586 | 586 | // Every attempt opens a fresh transport; no retry starts once the startup timeout has passed. | 587 | 587 | 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. | 590 | 591 | const negotiate = (url: URL) => | 591 | 592 | open(url, protocol).pipe( | 592 | 593 | 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)), | 594 | 599 | (error) => | 595 | 600 | Clock.currentTimeMillis.pipe( | 596 | 601 | Effect.flatMap((now) => (now < deadline ? open(url, "legacy", deadline - now) : Effect.fail(error))), | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/mcp.test.ts +30-1Lines changed: 30 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -951,6 +951,34 @@ test("falls back to legacy when a stdio server ignores the version probe", async | 951 | 951 | expect(result.tools.map((tool) => tool.name)).toEqual(["echo"]) | 952 | 952 | }) | 953 | 953 |
| | 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 | + | 954 | 982 | test("applies the configured MCP catalog timeout", async () => { | 955 | 983 | const result = Effect.runPromise( | 956 | 984 | Effect.scoped( | @@ -3285,8 +3313,9 @@ describe("MCP pool", () => { | 3285 | 3313 | }) | 3286 | 3314 |
| 3287 | 3315 | // 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. | 3288 | 3317 | for (const [code, what, attempts] of [ | 3289 | | - [503, "was down", 3], | | 3318 | + [503, "was down", 6], | 3290 | 3319 | [401, "rejected the credentials", 1], | 3291 | 3320 | [403, "refused access", 1], | 3292 | 3321 | ] as const) | |
Collapse file Copy file name to clipboardExpand all lines: services/site/src/docs/content/mcp-servers.mdx +9-9Lines changed: 9 additions & 9 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -119,7 +119,7 @@ Use configuration for other process options such as a working directory: | 119 | 119 | | `disabled` | No | Prevents connection when `true`. Defaults to `false`. | | 120 | 120 | | `codemode` | No | Set to `false` to expose tools directly instead of through Code Mode. Defaults to `true`. | | 121 | 121 | | `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). | | 123 | 123 | | `pool` | No | Share one connection across directories. Defaults to `false`. See [Shared connections](#shared-connections). | | 124 | 124 |
| 125 | 125 | 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 | 175 | 175 | | `disabled` | No | Prevents connection when `true`. Defaults to `false`. | | 176 | 176 | | `codemode` | No | Set to `false` to expose tools directly instead of through Code Mode. Defaults to `true`. | | 177 | 177 | | `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). | | 179 | 179 | | `pool` | No | Share one connection across directories. Defaults to `false`. See [Shared connections](#shared-connections). | | 180 | 180 |
| 181 | 181 | 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 | 335 | 335 |
| 336 | 336 | ## Protocol version | 337 | 337 |
| 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: | 339 | 339 |
| 340 | 340 | ```jsonc title="opencode.jsonc" | 341 | 341 | { | 342 | 342 | "$schema": "https://opencode.ai/config.json", | 343 | 343 | "mcp": { | 344 | 344 | "servers": { | 345 | | - "modern": { | | 345 | + "older": { | 346 | 346 | "type": "remote", | 347 | 347 | "url": "https://mcp.example.com/mcp", | 348 | | - "protocol": "auto", | | 348 | + "protocol": "legacy", | 349 | 349 | }, | 350 | 350 | }, | 351 | 351 | }, | @@ -354,11 +354,11 @@ OpenCode opens every server with the classic MCP `initialize` handshake by defau | 354 | 354 |
| 355 | 355 | | Value | Behavior | | 356 | 356 | | --- | --- | | 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. | | 359 | 359 | | `2026-07-28` | Requires the 2026-07-28 revision. The connection fails against older servers. | | 360 | 360 |
| 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. | 362 | 362 |
| 363 | 363 | ## Shared connections | 364 | 364 |
| @@ -379,7 +379,7 @@ One OpenCode server can have many directories open, and each one connects to eve | 379 | 379 | } | 380 | 380 | ``` | 381 | 381 |
| 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. | 383 | 383 | - 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. | 384 | 384 | - 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. | 385 | 385 | - A shared connection has no roots. When a tool asks for input, the request appears in the directory that called it. | |
|