Elffuss BraidHub

kikocis/continue public

81ce8cce6ac5 · 9.1 KiBRawPermalink
1import { Client } from "@modelcontextprotocol/sdk/client/index.js";
2import { beforeEach, describe, expect, it, vi } from "vitest";
3import MCPConnection from "./MCPConnection";
4import { StdioOptions } from "../../index.d.js";
5
6describe("MCPConnection", () => {
7 beforeEach(() => {
8 vi.restoreAllMocks();
9 });
10
11 describe("constructor", () => {
12 it("should create instance with stdio transport", () => {
13 const options = {
14 name: "test-mcp",
15 id: "test-id",
16 transport: {
17 type: "stdio" as const,
18 command: "test-cmd",
19 args: ["--test"],
20 env: { TEST: "true" },
21 },
22 };
23
24 const conn = new MCPConnection(options);
25 expect(conn).toBeInstanceOf(MCPConnection);
26 expect(conn.status).toBe("not-connected");
27 });
28
29 it("should create instance with stdio transport including cwd", () => {
30 const options = {
31 name: "test-mcp",
32 id: "test-id",
33 transport: {
34 type: "stdio" as const,
35 command: "test-cmd",
36 args: ["--test"],
37 env: { TEST: "true" },
38 cwd: "/path/to/working/directory",
39 },
40 };
41
42 const conn = new MCPConnection(options);
43 expect(conn).toBeInstanceOf(MCPConnection);
44 expect(conn.status).toBe("not-connected");
45 if (conn.options.transport.type === "stdio") {
46 expect(conn.options.transport.cwd).toBe("/path/to/working/directory");
47 }
48 });
49
50 it("should create instance with websocket transport", () => {
51 const options = {
52 name: "test-mcp",
53 id: "test-id",
54 transport: {
55 type: "websocket" as const,
56 url: "ws://test.com",
57 },
58 };
59
60 const conn = new MCPConnection(options);
61 expect(conn).toBeInstanceOf(MCPConnection);
62 expect(conn.status).toBe("not-connected");
63 });
64
65 it("should create instance with SSE transport", () => {
66 const options = {
67 name: "test-mcp",
68 id: "test-id",
69 transport: {
70 type: "sse" as const,
71 url: "http://test.com/events",
72 },
73 };
74
75 const conn = new MCPConnection(options);
76 expect(conn).toBeInstanceOf(MCPConnection);
77 expect(conn.status).toBe("not-connected");
78 });
79
80 it("should create instance with SSE transport and custom headers", () => {
81 const options = {
82 name: "test-mcp",
83 id: "test-id",
84 transport: {
85 type: "sse" as const,
86 url: "http://test.com/events",
87 requestOptions: {
88 headers: {
89 Authorization: "Bearer token123",
90 "X-Custom-Header": "custom-value",
91 },
92 },
93 },
94 };
95
96 const conn = new MCPConnection(options);
97 expect(conn).toBeInstanceOf(MCPConnection);
98 expect(conn.status).toBe("not-connected");
99 });
100
101 it("should throw on invalid transport type", () => {
102 const options = {
103 name: "test-mcp",
104 id: "test-id",
105 transport: {
106 type: "invalid",
107 } as any,
108 };
109
110 expect(() => new MCPConnection(options)).toThrow(
111 "Unsupported transport type: invalid",
112 );
113 });
114 });
115
116 describe("getStatus", () => {
117 it("should return current status", () => {
118 const options = {
119 name: "test-mcp",
120 id: "test-id",
121 transport: {
122 type: "stdio" as const,
123 command: "test",
124 args: [],
125 },
126 };
127
128 const conn = new MCPConnection(options);
129 const status = conn.getStatus();
130
131 expect(status).toEqual({
132 ...options,
133 errors: [],
134 prompts: [],
135 resources: [],
136 resourceTemplates: [],
137 tools: [],
138 status: "not-connected",
139 });
140 });
141 });
142
143 describe("connectClient", () => {
144 const options = {
145 name: "test-mcp",
146 id: "test-id",
147 transport: {
148 type: "stdio" as const,
149 command: "test-cmd",
150 args: [],
151 },
152 };
153
154 it("should connect successfully", async () => {
155 const conn = new MCPConnection(options);
156 const mockConnect = vi
157 .spyOn(Client.prototype, "connect")
158 .mockResolvedValue(undefined);
159 const mockGetServerCapabilities = vi
160 .spyOn(Client.prototype, "getServerCapabilities")
161 .mockReturnValue({
162 resources: {},
163 tools: {},
164 prompts: {},
165 });
166
167 const mockListResources = vi
168 .spyOn(Client.prototype, "listResources")
169 .mockResolvedValue({
170 resources: [{ name: "test-resource", uri: "test-uri" }],
171 });
172 const mockListTools = vi
173 .spyOn(Client.prototype, "listTools")
174 .mockResolvedValue({
175 tools: [
176 {
177 name: "test-tool",
178 inputSchema: {
179 type: "object",
180 },
181 },
182 ],
183 });
184 const mockListPrompts = vi
185 .spyOn(Client.prototype, "listPrompts")
186 .mockResolvedValue({ prompts: [{ name: "test-prompt" }] });
187
188 const abortController = new AbortController();
189 await conn.connectClient(false, abortController.signal);
190
191 expect(conn.status).toBe("connected");
192 expect(conn.resources).toHaveLength(1);
193 expect(conn.tools).toHaveLength(1);
194 expect(conn.prompts).toHaveLength(1);
195 expect(mockConnect).toHaveBeenCalled();
196 });
197
198 it("should handle custom connection timeout", async () => {
199 const conn = new MCPConnection({ ...options, timeout: 1500 });
200 const mockConnect = vi
201 .spyOn(Client.prototype, "connect")
202 .mockImplementation(
203 () => new Promise((resolve) => setTimeout(resolve, 1000)),
204 );
205
206 const abortController = new AbortController();
207 await conn.connectClient(false, abortController.signal);
208
209 expect(conn.status).toBe("connected");
210 expect(mockConnect).toHaveBeenCalled();
211 });
212
213 it("should handle connection timeout", async () => {
214 const conn = new MCPConnection({ ...options, timeout: 1 });
215 const mockConnect = vi
216 .spyOn(Client.prototype, "connect")
217 .mockImplementation(
218 () => new Promise((resolve) => setTimeout(resolve, 100)),
219 );
220
221 const abortController = new AbortController();
222 await conn.connectClient(false, abortController.signal);
223
224 expect(conn.status).toBe("error");
225 expect(conn.errors[0]).toContain("Failed to connect");
226 expect(mockConnect).toHaveBeenCalled();
227 });
228
229 it("should handle already connected state", async () => {
230 const conn = new MCPConnection(options);
231 conn.status = "connected";
232
233 const mockConnect = vi.spyOn(Client.prototype, "connect");
234 const abortController = new AbortController();
235
236 await conn.connectClient(false, abortController.signal);
237
238 expect(mockConnect).not.toHaveBeenCalled();
239 expect(conn.status).toBe("connected");
240 });
241
242 it("should handle transport errors", async () => {
243 const conn = new MCPConnection(options);
244 const mockConnect = vi
245 .spyOn(Client.prototype, "connect")
246 .mockRejectedValue(new Error("spawn test-cmd ENOENT"));
247
248 const abortController = new AbortController();
249 await conn.connectClient(false, abortController.signal);
250
251 expect(conn.status).toBe("error");
252 expect(conn.errors[0]).toContain('command "test-cmd" not found');
253 expect(mockConnect).toHaveBeenCalled();
254 });
255
256 it.skip("should include stderr output in error message when stdio command fails", async () => {
257 // Clear any existing mocks to ensure we get real behavior
258 vi.restoreAllMocks();
259
260 // Use a command that will definitely fail and produce stderr output
261 const failingOptions = {
262 name: "failing-mcp",
263 id: "failing-id",
264 transport: {
265 type: "stdio" as const,
266 command: "node",
267 args: [
268 "-e",
269 "console.error('Custom error message from stderr'); process.exit(1);",
270 ],
271 },
272 timeout: 5000, // Give enough time for the command to run and fail
273 };
274
275 const conn = new MCPConnection(failingOptions);
276 const abortController = new AbortController();
277
278 await conn.connectClient(false, abortController.signal);
279
280 expect(conn.status).toBe("error");
281 expect(conn.errors).toHaveLength(1);
282 expect(conn.errors[0]).toContain("Failed to connect");
283 expect(conn.errors[0]).toContain("Process output:");
284 expect(conn.errors[0]).toContain("STDERR:");
285 expect(conn.errors[0]).toContain("Custom error message from stderr");
286 });
287 });
288
289 describe.skip("actually connect to Filesystem MCP", () => {
290 it("should connect and include correct tools", async () => {
291 const conn = new MCPConnection({
292 id: "filesystem",
293 name: "Filesystem",
294 transport: {
295 type: "stdio" as const,
296 command: "npx",
297 args: ["-y", "@modelcontextprotocol/server-filesystem", "."],
298 },
299 });
300
301 try {
302 const abortController = new AbortController();
303 await conn.connectClient(false, abortController.signal);
304 expect(conn.status).toBe("connected");
305 } finally {
306 await conn.disconnect();
307 }
308 });
309 });
310});