Skip to content

Commit 3e2ad09

Browse files
committed
fix: add onprogress callback to MCP tool calls for timeout renewal
- Add onprogress callback in convertMcpTool to enable progressToken injection - This allows MCP server heartbeat to reset request timeout - Fixes -32001 timeout errors on long-running tool calls (10-20 min) - Add unit test to verify onprogress handler is included in call options Closes anomalyco#14499
1 parent 3a416f6 commit 3e2ad09

2 files changed

Lines changed: 112 additions & 0 deletions

File tree

‎packages/opencode/src/mcp/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ export namespace MCP {
139139
},
140140
CallToolResultSchema,
141141
{
142+
onprogress: () => undefined,
142143
resetTimeoutOnProgress: true,
143144
timeout,
144145
},
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { test, expect, mock, beforeEach } from "bun:test"
2+
3+
const callToolOptions: Array<Record<string, unknown>> = []
4+
const listToolsCalls: Array<string> = []
5+
6+
mock.module("@modelcontextprotocol/sdk/client/streamableHttp.js", () => ({
7+
StreamableHTTPClientTransport: class MockStreamableHTTP {
8+
constructor(_url: URL, _options?: { authProvider?: unknown; requestInit?: RequestInit }) {}
9+
},
10+
}))
11+
12+
mock.module("@modelcontextprotocol/sdk/client/sse.js", () => ({
13+
SSEClientTransport: class MockSSE {
14+
constructor(_url: URL, _options?: { authProvider?: unknown; requestInit?: RequestInit }) {}
15+
},
16+
}))
17+
18+
mock.module("@modelcontextprotocol/sdk/client/index.js", () => ({
19+
Client: class MockClient {
20+
setNotificationHandler() {}
21+
async connect(_transport: unknown) {}
22+
async close() {}
23+
async listTools() {
24+
listToolsCalls.push("list")
25+
return {
26+
tools: [
27+
{
28+
name: "review",
29+
description: "mock review tool",
30+
inputSchema: {
31+
type: "object",
32+
properties: {},
33+
},
34+
},
35+
],
36+
}
37+
}
38+
async callTool(
39+
_params: unknown,
40+
_resultSchema: unknown,
41+
options?: Record<string, unknown>,
42+
) {
43+
callToolOptions.push(options ?? {})
44+
return {
45+
content: [
46+
{
47+
type: "text",
48+
text: "ok",
49+
},
50+
],
51+
}
52+
}
53+
},
54+
}))
55+
56+
beforeEach(() => {
57+
callToolOptions.length = 0
58+
listToolsCalls.length = 0
59+
})
60+
61+
const { MCP } = await import("../../src/mcp/index")
62+
const { Instance } = await import("../../src/project/instance")
63+
const { tmpdir } = await import("../fixture/fixture")
64+
65+
test("mcp callTool options should include progress handler and timeout reset", async () => {
66+
await using tmp = await tmpdir({
67+
init: async (dir) => {
68+
await Bun.write(
69+
`${dir}/opencode.json`,
70+
JSON.stringify({
71+
$schema: "https://opencode.ai/config.json",
72+
mcp: {
73+
"test-server": {
74+
type: "remote",
75+
url: "https://example.com/mcp",
76+
oauth: false,
77+
timeout: 45678,
78+
},
79+
},
80+
}),
81+
)
82+
},
83+
})
84+
85+
await Instance.provide({
86+
directory: tmp.path,
87+
fn: async () => {
88+
await MCP.add("test-server", {
89+
type: "remote",
90+
url: "https://example.com/mcp",
91+
oauth: false,
92+
timeout: 45678,
93+
}).catch(() => {})
94+
95+
const tools = await MCP.tools()
96+
const key = Object.keys(tools).find((item) => item.endsWith("_review"))
97+
expect(key).toBeDefined()
98+
99+
const tool = tools[key!]
100+
await (tool.execute as (args: unknown) => Promise<unknown>)({})
101+
102+
expect(listToolsCalls.length).toBeGreaterThan(0)
103+
expect(callToolOptions.length).toBe(1)
104+
expect(callToolOptions[0]["resetTimeoutOnProgress"]).toBe(true)
105+
expect(callToolOptions[0]["timeout"]).toBe(45678)
106+
expect(typeof callToolOptions[0]["onprogress"]).toBe("function")
107+
108+
await MCP.disconnect("test-server")
109+
},
110+
})
111+
})

0 commit comments

Comments
 (0)