| 1 | import { AssistantConfig } from "@continuedev/sdk"; |
| 2 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | |
| 4 | import { MCPService } from "./MCPService.js"; |
| 5 | |
| 6 | // Mock the MCP SDK |
| 7 | const mockClient = { |
| 8 | connect: vi.fn(), |
| 9 | getServerCapabilities: vi.fn(() => ({ prompts: true, tools: true })), |
| 10 | listPrompts: vi.fn(() => Promise.resolve({ prompts: [] })), |
| 11 | listTools: vi.fn(() => Promise.resolve({ tools: [] })), |
| 12 | callTool: vi.fn(() => Promise.resolve({})), |
| 13 | close: vi.fn(() => Promise.resolve()), |
| 14 | }; |
| 15 | |
| 16 | vi.mock("@modelcontextprotocol/sdk/client/index.js", () => ({ |
| 17 | Client: vi.fn(() => mockClient), |
| 18 | })); |
| 19 | |
| 20 | vi.mock("@modelcontextprotocol/sdk/client/stdio.js", () => ({ |
| 21 | StdioClientTransport: vi.fn(), |
| 22 | })); |
| 23 | |
| 24 | vi.mock("@modelcontextprotocol/sdk/client/sse.js", () => ({ |
| 25 | SSEClientTransport: vi.fn(), |
| 26 | })); |
| 27 | |
| 28 | vi.mock("@modelcontextprotocol/sdk/client/streamableHttp.js", () => ({ |
| 29 | StreamableHTTPClientTransport: vi.fn(), |
| 30 | })); |
| 31 | |
| 32 | describe("MCPService", () => { |
| 33 | let mcpService: MCPService; |
| 34 | let mockAssistant: AssistantConfig; |
| 35 | |
| 36 | beforeEach(() => { |
| 37 | mcpService = new MCPService(); |
| 38 | mockAssistant = { |
| 39 | name: "test-assistant", |
| 40 | version: "1.0.0", |
| 41 | mcpServers: [ |
| 42 | { |
| 43 | name: "test-server", |
| 44 | command: "npx", |
| 45 | args: ["-v"], |
| 46 | env: { TEST: "value" }, |
| 47 | }, |
| 48 | ], |
| 49 | } as AssistantConfig; |
| 50 | }); |
| 51 | |
| 52 | afterEach(async () => { |
| 53 | vi.clearAllMocks(); |
| 54 | await mcpService.cleanup(); |
| 55 | }); |
| 56 | |
| 57 | describe("initialization", () => { |
| 58 | it("should initialize with empty state", () => { |
| 59 | const state = mcpService.getState(); |
| 60 | expect(mcpService.isReady()).toBe(false); |
| 61 | expect(state.connections).toEqual([]); |
| 62 | expect(state.tools.length).toBe(0); |
| 63 | expect(state.prompts.length).toBe(0); |
| 64 | }); |
| 65 | |
| 66 | it("should initialize with no servers", async () => { |
| 67 | const emptyAssistant = { |
| 68 | name: "empty", |
| 69 | version: "1.0.0", |
| 70 | } as AssistantConfig; |
| 71 | const state = await mcpService.initialize(emptyAssistant); |
| 72 | |
| 73 | expect(mcpService.isReady()).toBe(true); |
| 74 | expect(state.connections).toEqual([]); |
| 75 | expect(state.tools.length).toBe(0); |
| 76 | expect(state.prompts.length).toBe(0); |
| 77 | }); |
| 78 | |
| 79 | it("should initialize and return immediately even with server configs", async () => { |
| 80 | const state = await mcpService.initialize(mockAssistant); |
| 81 | |
| 82 | // Should return immediately with ready state |
| 83 | expect(mcpService.isReady()).toBe(true); |
| 84 | expect(state.mcpService).toBe(mcpService); |
| 85 | }); |
| 86 | }); |
| 87 | |
| 88 | describe("service management", () => { |
| 89 | beforeEach(async () => { |
| 90 | await mcpService.initialize(mockAssistant); |
| 91 | }); |
| 92 | |
| 93 | it("should get overall status", async () => { |
| 94 | const firstStatus = mcpService.getOverallStatus(); |
| 95 | expect(firstStatus.status).toBe("connected"); |
| 96 | expect(firstStatus.hasWarnings).toBe(false); |
| 97 | await mcpService.cleanup(); |
| 98 | const secondStatus = mcpService.getOverallStatus(); |
| 99 | expect(secondStatus.status).toBe("idle"); |
| 100 | }); |
| 101 | }); |
| 102 | |
| 103 | describe("server management", () => { |
| 104 | beforeEach(async () => { |
| 105 | await mcpService.initialize(mockAssistant); |
| 106 | }); |
| 107 | |
| 108 | it("should handle restart all servers", async () => { |
| 109 | await expect(mcpService.restartAllServers()).resolves.not.toThrow(); |
| 110 | }); |
| 111 | |
| 112 | it("should handle stop all servers", async () => { |
| 113 | await expect(mcpService.shutdownConnections()).resolves.not.toThrow(); |
| 114 | }); |
| 115 | |
| 116 | it("should handle server not found error", async () => { |
| 117 | await expect(mcpService.restartServer("nonexistent")).rejects.toThrow( |
| 118 | "Server nonexistent not found in configuration", |
| 119 | ); |
| 120 | }); |
| 121 | |
| 122 | it("should handle stop individual server", async () => { |
| 123 | await expect(mcpService.stopServer("test-server")).resolves.not.toThrow(); |
| 124 | }); |
| 125 | }); |
| 126 | |
| 127 | describe("tools and prompts", () => { |
| 128 | beforeEach(async () => { |
| 129 | await mcpService.initialize(mockAssistant); |
| 130 | }); |
| 131 | |
| 132 | it("should return empty arrays for tools and prompts initially", () => { |
| 133 | expect(Array.isArray(mcpService.getState().tools)).toBe(true); |
| 134 | expect(Array.isArray(mcpService.getState().tools)).toBe(true); |
| 135 | }); |
| 136 | |
| 137 | it("should throw error for unknown tool", async () => { |
| 138 | await expect(mcpService.runTool("unknown-tool", {})).rejects.toThrow( |
| 139 | "Tool unknown-tool not found", |
| 140 | ); |
| 141 | }); |
| 142 | }); |
| 143 | |
| 144 | describe("shutdown", () => { |
| 145 | it("should handle shutdown gracefully", async () => { |
| 146 | await mcpService.initialize(mockAssistant); |
| 147 | await mcpService.cleanup(); |
| 148 | |
| 149 | const state = mcpService.getState(); |
| 150 | expect( |
| 151 | state.mcpService |
| 152 | ?.getState() |
| 153 | .connections.filter((c) => c.status === "connected"), |
| 154 | ).toHaveLength(0); |
| 155 | }); |
| 156 | |
| 157 | it("should handle multiple shutdown calls", async () => { |
| 158 | await mcpService.initialize(mockAssistant); |
| 159 | await mcpService.cleanup(); |
| 160 | await expect(mcpService.cleanup()).resolves.not.toThrow(); |
| 161 | }); |
| 162 | }); |
| 163 | |
| 164 | describe("transport types", () => { |
| 165 | it("should support SSE transport", async () => { |
| 166 | const sseAssistant: AssistantConfig = { |
| 167 | name: "sse-assistant", |
| 168 | version: "1.0.0", |
| 169 | mcpServers: [ |
| 170 | { |
| 171 | name: "sse-server", |
| 172 | type: "sse", |
| 173 | url: "https://example.com/sse", |
| 174 | }, |
| 175 | ], |
| 176 | } as AssistantConfig; |
| 177 | |
| 178 | await expect(mcpService.initialize(sseAssistant)).resolves.not.toThrow(); |
| 179 | }); |
| 180 | |
| 181 | it("should support streamable-http transport", async () => { |
| 182 | const httpAssistant: AssistantConfig = { |
| 183 | name: "http-assistant", |
| 184 | version: "1.0.0", |
| 185 | mcpServers: [ |
| 186 | { |
| 187 | name: "http-server", |
| 188 | type: "streamable-http", |
| 189 | url: "https://example.com/http", |
| 190 | }, |
| 191 | ], |
| 192 | } as AssistantConfig; |
| 193 | |
| 194 | await expect(mcpService.initialize(httpAssistant)).resolves.not.toThrow(); |
| 195 | }); |
| 196 | |
| 197 | it("should default to stdio transport when type is not specified", async () => { |
| 198 | const defaultAssistant: AssistantConfig = { |
| 199 | name: "default-assistant", |
| 200 | version: "1.0.0", |
| 201 | mcpServers: [ |
| 202 | { |
| 203 | name: "default-server", |
| 204 | command: "npx", |
| 205 | args: ["-v"], |
| 206 | }, |
| 207 | ], |
| 208 | } as AssistantConfig; |
| 209 | |
| 210 | await expect( |
| 211 | mcpService.initialize(defaultAssistant), |
| 212 | ).resolves.not.toThrow(); |
| 213 | }); |
| 214 | }); |
| 215 | }); |