Elffuss BraidHub

kikocis/continue public

95c91a48de4e · 7.2 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.clearAllMocks();
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 tools: [],
115 status: "not-connected",
116 });
117 });
118 });
119
120 describe("connectClient", () => {
121 const options = {
122 name: "test-mcp",
123 id: "test-id",
124 transport: {
125 type: "stdio" as const,
126 command: "test-cmd",
127 args: [],
128 },
129 };
130
131 it("should connect successfully", async () => {
132 const conn = new MCPConnection(options);
133 const mockConnect = jest
134 .spyOn(Client.prototype, "connect")
135 .mockResolvedValue(undefined);
136 const mockGetServerCapabilities = jest
137 .spyOn(Client.prototype, "getServerCapabilities")
138 .mockReturnValue({
139 resources: {},
140 tools: {},
141 prompts: {},
142 });
143
144 const mockListResources = jest
145 .spyOn(Client.prototype, "listResources")
146 .mockResolvedValue({
147 resources: [{ name: "test-resource", uri: "test-uri" }],
148 });
149 const mockListTools = jest
150 .spyOn(Client.prototype, "listTools")
151 .mockResolvedValue({
152 tools: [
153 {
154 name: "test-tool",
155 inputSchema: {
156 type: "object",
157 },
158 },
159 ],
160 });
161 const mockListPrompts = jest
162 .spyOn(Client.prototype, "listPrompts")
163 .mockResolvedValue({ prompts: [{ name: "test-prompt" }] });
164
165 const abortController = new AbortController();
166 await conn.connectClient(false, abortController.signal);
167
168 expect(conn.status).toBe("connected");
169 expect(conn.resources).toHaveLength(1);
170 expect(conn.tools).toHaveLength(1);
171 expect(conn.prompts).toHaveLength(1);
172 expect(mockConnect).toHaveBeenCalled();
173 });
174
175 it("should handle custom connection timeout", async () => {
176 const conn = new MCPConnection({ ...options, timeout: 11 });
177 const mockConnect = jest
178 .spyOn(Client.prototype, "connect")
179 .mockImplementation(
180 () => new Promise((resolve) => setTimeout(resolve, 10)),
181 );
182
183 const abortController = new AbortController();
184 await conn.connectClient(false, abortController.signal);
185
186 expect(conn.status).toBe("connected");
187 expect(mockConnect).toHaveBeenCalled();
188 });
189
190 it("should handle connection timeout", async () => {
191 const conn = new MCPConnection({ ...options, timeout: 1 });
192 const mockConnect = jest
193 .spyOn(Client.prototype, "connect")
194 .mockImplementation(
195 () => new Promise((resolve) => setTimeout(resolve, 100)),
196 );
197
198 const abortController = new AbortController();
199 await conn.connectClient(false, abortController.signal);
200
201 expect(conn.status).toBe("error");
202 expect(conn.errors[0]).toContain("Failed to connect");
203 expect(mockConnect).toHaveBeenCalled();
204 });
205
206 it("should handle already connected state", async () => {
207 const conn = new MCPConnection(options);
208 conn.status = "connected";
209
210 const mockConnect = jest.spyOn(Client.prototype, "connect");
211 const abortController = new AbortController();
212
213 await conn.connectClient(false, abortController.signal);
214
215 expect(mockConnect).not.toHaveBeenCalled();
216 expect(conn.status).toBe("connected");
217 });
218
219 it("should handle transport errors", async () => {
220 const conn = new MCPConnection(options);
221 const mockConnect = jest
222 .spyOn(Client.prototype, "connect")
223 .mockRejectedValue(new Error("spawn test-cmd ENOENT"));
224
225 const abortController = new AbortController();
226 await conn.connectClient(false, abortController.signal);
227
228 expect(conn.status).toBe("error");
229 expect(conn.errors[0]).toContain('command "test-cmd" not found');
230 expect(mockConnect).toHaveBeenCalled();
231 });
232 });
233
234 describe.skip("actually connect to Filesystem MCP", () => {
235 it("should connect and include correct tools", async () => {
236 const conn = new MCPConnection({
237 id: "filesystem",
238 name: "Filesystem",
239 transport: {
240 type: "stdio" as const,
241 command: "npx",
242 args: ["-y", "@modelcontextprotocol/server-filesystem", "."],
243 },
244 });
245
246 try {
247 const abortController = new AbortController();
248 await conn.connectClient(false, abortController.signal);
249 expect(conn.status).toBe("connected");
250 } finally {
251 await conn.disconnect();
252 }
253 });
254 });
255});