fix(mcp): make client creation failure-safe (#31595)

This commit is contained in:
Aiden Cline 2026-06-09 23:24:41 -05:00 committed by GitHub
commit 91073360c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 65 additions and 25 deletions

View file

@ -8,6 +8,7 @@ import { testEffect } from "../lib/effect"
// Per-client state for controlling mock behavior
interface MockClientState {
capabilities: { tools?: object; prompts?: object; resources?: object }
capabilitiesShouldThrow: boolean
tools: Array<{ name: string; description?: string; inputSchema: object; outputSchema?: object }>
listToolsCalls: number
listPromptsCalls: number
@ -51,6 +52,7 @@ function getOrCreateClientState(name?: string): MockClientState {
if (!state) {
state = {
capabilities: { tools: {}, prompts: {}, resources: {} },
capabilitiesShouldThrow: false,
tools: [{ name: "test_tool", description: "A test tool", inputSchema: { type: "object", properties: {} } }],
listToolsCalls: 0,
listPromptsCalls: 0,
@ -155,6 +157,7 @@ void mock.module("@modelcontextprotocol/sdk/client/index.js", () => ({
}
getServerCapabilities() {
if (this._state?.capabilitiesShouldThrow) throw new Error("capability discovery failed")
return this._state?.capabilities
}
@ -566,6 +569,31 @@ it.instance(
},
)
it.instance(
"returns failed and closes the client when SDK initialization throws",
() =>
MCP.Service.use((mcp: MCPNS.Interface) =>
Effect.gen(function* () {
lastCreatedClientName = "defective-server"
const serverState = getOrCreateClientState("defective-server")
serverState.capabilitiesShouldThrow = true
const result = yield* mcp.add("defective-server", {
type: "local",
command: ["echo", "test"],
})
expect(statusName(result.status, "defective-server")).toBe("failed")
expect((yield* mcp.status())["defective-server"]).toEqual({
status: "failed",
error: "capability discovery failed",
})
expect(serverState.closed).toBe(true)
}),
),
{ config: { mcp: {} } },
)
it.instance(
"falls back when MCP output schema refs fail SDK tool discovery",
() =>