Elffuss BraidHub

kikocis/continue public

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