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