|
| 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