Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/opencode/src/mcp/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export function defs(clientName: string, client: Client, timeout?: number) {
)
}

export function convertTool(mcpTool: MCPToolDef, client: Client, timeout?: number): Tool {
// `client` is resolved per call so a server reconnect between build and call uses the live client.
export function convertTool(mcpTool: MCPToolDef, client: () => Client, timeout?: number): Tool {
const inputSchema: JSONSchema7 = {
...(mcpTool.inputSchema as JSONSchema7),
type: "object",
Expand All @@ -54,7 +55,7 @@ export function convertTool(mcpTool: MCPToolDef, client: Client, timeout?: numbe
description: mcpTool.description ?? "",
inputSchema: jsonSchema(inputSchema),
execute: async (args: unknown, options) => {
const result = await client.callTool(
const result = await client().callTool(
{
name: mcpTool.name,
arguments: (args || {}) as Record<string, unknown>,
Expand Down
10 changes: 9 additions & 1 deletion packages/opencode/src/mcp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export interface ServerInstructions {
export interface McpTool {
/** Shared cached definition; consumers must copy rather than mutate it. */
readonly def: MCPToolDef
/** The server's current client, resolved on each read; capture it only at the moment of the call. */
readonly client: MCPClient
readonly timeout?: number
}
Expand Down Expand Up @@ -736,7 +737,14 @@ const layer = Layer.effect(
}
const timeout = requestTimeout(s, clientName, mcpConfig, defaultTimeout)
for (const def of listed) {
result[McpCatalog.toolName(clientName, def.name)] = { def, client, timeout }
result[McpCatalog.toolName(clientName, def.name)] = {
def,
// Read at call time: a reconnect while the call awaits approval closes the client captured here.
get client() {
return s.clients[clientName] ?? client
},
timeout,
}
}
}
return result
Expand Down
2 changes: 1 addition & 1 deletion packages/opencode/src/session/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: {
if (flags.experimentalCodeMode) return tools

for (const [key, entry] of Object.entries(yield* mcp.tools())) {
const item = McpCatalog.convertTool(entry.def, entry.client, entry.timeout)
const item = McpCatalog.convertTool(entry.def, () => entry.client, entry.timeout)
const execute = item.execute
if (!execute) continue

Expand Down
17 changes: 15 additions & 2 deletions packages/opencode/test/mcp/catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe("McpCatalog.convertTool", () => {
test("preserves content when structuredContent is also present", async () => {
const content = [{ type: "image" as const, mimeType: "image/png", data: "AAAA" }]
const structuredContent = { image: { mimeType: "image/png", data: "AAAA" } }
const converted = McpCatalog.convertTool(mcpTool(), clientReturning({ content, structuredContent }))
const converted = McpCatalog.convertTool(mcpTool(), () => clientReturning({ content, structuredContent }))

const output = await converted.execute?.({}, options)

Expand All @@ -39,7 +39,7 @@ describe("McpCatalog.convertTool", () => {

test("falls back to structuredContent only when content is absent", async () => {
const structuredContent = { results: [{ title: "one" }] }
const converted = McpCatalog.convertTool(mcpTool(), clientReturning({ content: [], structuredContent }))
const converted = McpCatalog.convertTool(mcpTool(), () => clientReturning({ content: [], structuredContent }))

const output = await converted.execute?.({}, options)

Expand All @@ -48,6 +48,19 @@ describe("McpCatalog.convertTool", () => {
content: [{ type: "text", text: JSON.stringify(structuredContent) }],
})
})

test("calls the client current at execute time, not the one at build time", async () => {
const content = [{ type: "text" as const, text: "live" }]
let client = {
callTool: async () => {
throw new Error("Not connected")
},
} as unknown as Client
const converted = McpCatalog.convertTool(mcpTool(), () => client)
client = clientReturning({ content })

expect(await converted.execute?.({}, options)).toMatchObject({ content })
})
})

test("preserves output schema validation across paginated tool discovery", async () => {
Expand Down
17 changes: 17 additions & 0 deletions packages/opencode/test/mcp/lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,23 @@ it.instance("disconnect removes protocol data and reconnect establishes a new se
}),
)

it.instance("a tools() entry taken before a reconnect calls through the new client", () =>
Effect.gen(function* () {
const server = yield* lifecycleServer()
const mcp = yield* MCP.Service
yield* mcp.add("swap-server", remote(server.url))
// A tool call holds this entry while it waits for approval; a reconnect closes the old client meanwhile.
const entry = (yield* mcp.tools())["swap-server_test_tool"]!

yield* Effect.promise(server.restart)
yield* mcp.connect("swap-server")

const listed = yield* Effect.promise(() => entry.client.listTools())
expect(listed.tools.map((tool) => tool.name)).toEqual(["test_tool"])
expect(entry.client).toBe((yield* mcp.clients())["swap-server"]!)
}),
)

it.instance("add() closes the old protocol session when replacing a server", () =>
Effect.gen(function* () {
const first = yield* lifecycleServer()
Expand Down
Loading