mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-26 23:28:43 +00:00
fix(core): fall back to legacy when the MCP probe gets an HTTP 5xx (#66588)
This commit is contained in:
parent
dc390b6a04
commit
70e8a0f6f3
3 changed files with 47 additions and 13 deletions
|
|
@ -585,12 +585,17 @@ export const connect = Effect.fnUntraced(function* (options: ConnectOptions) {
|
|||
})
|
||||
// Every attempt opens a fresh transport; no retry starts once the startup timeout has passed.
|
||||
const deadline = (yield* Clock.currentTimeMillis) + (timeout?.startup ?? DEFAULT_STARTUP_TIMEOUT)
|
||||
// Over HTTP the SDK treats an unanswered auto probe as an outage, but slow legacy servers may still initialize
|
||||
// within what is left of the startup timeout.
|
||||
// Over HTTP the SDK treats an unanswered auto probe as an outage and a 5xx answer as a failed connect, but slow
|
||||
// legacy servers may still initialize within what is left of the startup timeout, and many (Spring AI among
|
||||
// them) answer the unknown server/discover with a 500.
|
||||
const negotiate = (url: URL) =>
|
||||
open(url, protocol).pipe(
|
||||
Effect.catchIf(
|
||||
(error) => protocol === "auto" && error instanceof SdkError && error.code === SdkErrorCode.RequestTimeout,
|
||||
(error) =>
|
||||
protocol === "auto" &&
|
||||
error instanceof SdkError &&
|
||||
(error.code === SdkErrorCode.RequestTimeout ||
|
||||
(error instanceof SdkHttpError && error.code === SdkErrorCode.EraNegotiationFailed)),
|
||||
(error) =>
|
||||
Clock.currentTimeMillis.pipe(
|
||||
Effect.flatMap((now) => (now < deadline ? open(url, "legacy", deadline - now) : Effect.fail(error))),
|
||||
|
|
|
|||
|
|
@ -951,6 +951,34 @@ test("falls back to legacy when a stdio server ignores the version probe", async
|
|||
expect(result.tools.map((tool) => tool.name)).toEqual(["echo"])
|
||||
})
|
||||
|
||||
testEffect(Layer.empty).live("falls back to legacy when a remote server answers the version probe with HTTP 500", () =>
|
||||
Effect.gen(function* () {
|
||||
const probes = { count: 0 }
|
||||
// Spring AI answers methods it has no handler for, server/discover included, with a 500.
|
||||
const server = yield* resourceServer({
|
||||
resources: false,
|
||||
respond: async (request) => {
|
||||
if (request.method !== "POST") return undefined
|
||||
const body: unknown = await request.clone().json()
|
||||
if (typeof body !== "object" || body === null || !("method" in body) || body.method !== "server/discover")
|
||||
return undefined
|
||||
probes.count += 1
|
||||
return new Response("Missing handler for request type: server/discover", { status: 500 })
|
||||
},
|
||||
})
|
||||
const connection = yield* connect(
|
||||
"resources",
|
||||
new ConfigMCP.Remote({ type: "remote", url: server.url, oauth: false }),
|
||||
import.meta.dir,
|
||||
)
|
||||
|
||||
expect(connection.modern).toBe(false)
|
||||
expect((yield* connection.tools()).map((tool) => tool.name)).toEqual(["echo"])
|
||||
expect(probes.count).toBe(1)
|
||||
expect(server.state.initializations).toBe(1)
|
||||
}),
|
||||
)
|
||||
|
||||
test("applies the configured MCP catalog timeout", async () => {
|
||||
const result = Effect.runPromise(
|
||||
Effect.scoped(
|
||||
|
|
@ -3285,8 +3313,9 @@ describe("MCP pool", () => {
|
|||
})
|
||||
|
||||
// A server that is down, or that refuses this client, would refuse a private connection too.
|
||||
// Each of the 3 attempts at a server that is down probes, then tries initialize in case it is legacy.
|
||||
for (const [code, what, attempts] of [
|
||||
[503, "was down", 3],
|
||||
[503, "was down", 6],
|
||||
[401, "rejected the credentials", 1],
|
||||
[403, "refused access", 1],
|
||||
] as const)
|
||||
|
|
|
|||
|
|
@ -119,7 +119,7 @@ Use configuration for other process options such as a working directory:
|
|||
| `disabled` | No | Prevents connection when `true`. Defaults to `false`. |
|
||||
| `codemode` | No | Set to `false` to expose tools directly instead of through Code Mode. Defaults to `true`. |
|
||||
| `timeout` | No | Per-server timeout overrides. |
|
||||
| `protocol` | No | `legacy` (default), `auto`, or `2026-07-28`. See [Protocol version](#protocol-version). |
|
||||
| `protocol` | No | `auto` (default), `legacy`, or `2026-07-28`. See [Protocol version](#protocol-version). |
|
||||
| `pool` | No | Share one connection across directories. Defaults to `false`. See [Shared connections](#shared-connections). |
|
||||
|
||||
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
|
|||
| `disabled` | No | Prevents connection when `true`. Defaults to `false`. |
|
||||
| `codemode` | No | Set to `false` to expose tools directly instead of through Code Mode. Defaults to `true`. |
|
||||
| `timeout` | No | Per-server timeout overrides. |
|
||||
| `protocol` | No | `legacy` (default), `auto`, or `2026-07-28`. See [Protocol version](#protocol-version). |
|
||||
| `protocol` | No | `auto` (default), `legacy`, or `2026-07-28`. See [Protocol version](#protocol-version). |
|
||||
| `pool` | No | Share one connection across directories. Defaults to `false`. See [Shared connections](#shared-connections). |
|
||||
|
||||
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
|
|||
|
||||
## Protocol version
|
||||
|
||||
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:
|
||||
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:
|
||||
|
||||
```jsonc title="opencode.jsonc"
|
||||
{
|
||||
"$schema": "https://opencode.ai/config.json",
|
||||
"mcp": {
|
||||
"servers": {
|
||||
"modern": {
|
||||
"older": {
|
||||
"type": "remote",
|
||||
"url": "https://mcp.example.com/mcp",
|
||||
"protocol": "auto",
|
||||
"protocol": "legacy",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
@ -354,11 +354,11 @@ OpenCode opens every server with the classic MCP `initialize` handshake by defau
|
|||
|
||||
| Value | Behavior |
|
||||
| --- | --- |
|
||||
| `legacy` | Default. Sends `initialize` and speaks protocol revisions up to 2025-11-25. |
|
||||
| `auto` | Probes with `server/discover` for the 2026-07-28 revision and falls back to `legacy` when the server does not support it. |
|
||||
| `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. |
|
||||
| `legacy` | Sends `initialize` and speaks protocol revisions up to 2025-11-25. |
|
||||
| `2026-07-28` | Requires the 2026-07-28 revision. The connection fails against older servers. |
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
## Shared connections
|
||||
|
||||
|
|
@ -379,7 +379,7 @@ One OpenCode server can have many directories open, and each one connects to eve
|
|||
}
|
||||
```
|
||||
|
||||
- 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.
|
||||
- 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.
|
||||
- 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.
|
||||
- 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.
|
||||
- A shared connection has no roots. When a tool asks for input, the request appears in the directory that called it.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue