|
1 | 1 | /**
|
2 |
| - * @fileoverview Basic tests for the MCP server entry point |
| 2 | + * @fileoverview Comprehensive tests for the MCP server entry point |
3 | 3 | * @module index.test
|
4 | 4 | */
|
5 | 5 |
|
6 |
| -import { describe, expect, it } from 'vitest'; |
| 6 | +import { Server } from '@modelcontextprotocol/sdk/server/index.js'; |
| 7 | +import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; |
| 8 | +import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js'; |
| 9 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 10 | +import * as cliModule from './cli.js'; |
| 11 | +import { isCliMode, main, startMcpServer } from './index.js'; |
| 12 | +import * as exampleModule from './tools/example.js'; |
| 13 | + |
| 14 | +// Mock external dependencies |
| 15 | +vi.mock('@modelcontextprotocol/sdk/server/index.js'); |
| 16 | +vi.mock('@modelcontextprotocol/sdk/server/stdio.js'); |
| 17 | +vi.mock('./cli.js'); |
| 18 | +vi.mock('./tools/example.js'); |
7 | 19 |
|
8 | 20 | /**
|
9 | 21 | * Test suite for the MCP server module
|
10 | 22 | */
|
11 |
| -describe('MCP Server', () => { |
| 23 | +describe('MCP Server Entry Point', () => { |
| 24 | + let mockServer: { |
| 25 | + setRequestHandler: ReturnType<typeof vi.fn>; |
| 26 | + connect: ReturnType<typeof vi.fn>; |
| 27 | + close: ReturnType<typeof vi.fn>; |
| 28 | + }; |
| 29 | + let mockTransport: object; |
| 30 | + let mockProgram: { |
| 31 | + parseAsync: ReturnType<typeof vi.fn>; |
| 32 | + }; |
| 33 | + let originalArgv: string[]; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + vi.clearAllMocks(); |
| 37 | + |
| 38 | + // Store original argv |
| 39 | + originalArgv = [...process.argv]; |
| 40 | + |
| 41 | + // Mock Server |
| 42 | + mockServer = { |
| 43 | + setRequestHandler: vi.fn(), |
| 44 | + connect: vi.fn(), |
| 45 | + close: vi.fn(), |
| 46 | + }; |
| 47 | + vi.mocked(Server).mockImplementation(() => mockServer); |
| 48 | + |
| 49 | + // Mock Transport |
| 50 | + mockTransport = {}; |
| 51 | + vi.mocked(StdioServerTransport).mockImplementation(() => mockTransport); |
| 52 | + |
| 53 | + // Mock CLI |
| 54 | + mockProgram = { |
| 55 | + parseAsync: vi.fn(), |
| 56 | + }; |
| 57 | + vi.mocked(cliModule.createCLI).mockReturnValue(mockProgram); |
| 58 | + |
| 59 | + // Mock example tool |
| 60 | + vi.mocked(exampleModule.exampleTool).mockResolvedValue({ |
| 61 | + content: [{ type: 'text', text: 'Test result' }], |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + afterEach(() => { |
| 66 | + // Restore original argv |
| 67 | + process.argv = originalArgv; |
| 68 | + }); |
| 69 | + |
12 | 70 | /**
|
13 | 71 | * Test that the server module can be imported as an ES module
|
14 | 72 | */
|
15 | 73 | it('should export as ES module', async () => {
|
16 | 74 | const module = await import('./index.js');
|
17 | 75 | expect(module).toBeDefined();
|
18 | 76 | });
|
| 77 | + |
| 78 | + /** |
| 79 | + * Test CLI mode detection |
| 80 | + */ |
| 81 | + it('should detect CLI mode when arguments are provided', () => { |
| 82 | + // Set CLI mode (more than 2 arguments) |
| 83 | + process.argv = ['node', 'index.js', 'example', 'test']; |
| 84 | + expect(isCliMode()).toBe(true); |
| 85 | + }); |
| 86 | + |
| 87 | + /** |
| 88 | + * Test MCP mode detection |
| 89 | + */ |
| 90 | + it('should detect MCP mode when no CLI arguments provided', () => { |
| 91 | + // Set MCP mode (only 2 arguments: node and script name) |
| 92 | + process.argv = ['node', 'index.js']; |
| 93 | + expect(isCliMode()).toBe(false); |
| 94 | + }); |
| 95 | + |
| 96 | + /** |
| 97 | + * Test main function in CLI mode |
| 98 | + */ |
| 99 | + it('should run CLI when in CLI mode', async () => { |
| 100 | + process.argv = ['node', 'index.js', 'example', 'test']; |
| 101 | + |
| 102 | + await main(); |
| 103 | + |
| 104 | + expect(cliModule.createCLI).toHaveBeenCalled(); |
| 105 | + expect(mockProgram.parseAsync).toHaveBeenCalledWith(process.argv); |
| 106 | + }); |
| 107 | + |
| 108 | + /** |
| 109 | + * Test main function in MCP mode |
| 110 | + */ |
| 111 | + it('should start MCP server when in MCP mode', async () => { |
| 112 | + process.argv = ['node', 'index.js']; |
| 113 | + |
| 114 | + await main(); |
| 115 | + |
| 116 | + expect(Server).toHaveBeenCalledWith( |
| 117 | + { name: 'mcp-template', version: '0.1.0' }, |
| 118 | + { capabilities: { tools: {} } }, |
| 119 | + ); |
| 120 | + expect(mockServer.connect).toHaveBeenCalledWith(mockTransport); |
| 121 | + }); |
| 122 | + |
| 123 | + /** |
| 124 | + * Test server creation and setup |
| 125 | + */ |
| 126 | + it('should create MCP server with correct configuration', async () => { |
| 127 | + await startMcpServer(); |
| 128 | + |
| 129 | + expect(Server).toHaveBeenCalledWith( |
| 130 | + { name: 'mcp-template', version: '0.1.0' }, |
| 131 | + { capabilities: { tools: {} } }, |
| 132 | + ); |
| 133 | + expect(mockServer.connect).toHaveBeenCalledWith(mockTransport); |
| 134 | + }); |
| 135 | + |
| 136 | + /** |
| 137 | + * Test server request handlers registration |
| 138 | + */ |
| 139 | + it('should register ListTools and CallTool handlers', async () => { |
| 140 | + await startMcpServer(); |
| 141 | + |
| 142 | + // Should register 2 handlers: ListTools and CallTool |
| 143 | + expect(mockServer.setRequestHandler).toHaveBeenCalledTimes(2); |
| 144 | + |
| 145 | + // Check that the correct schemas are used |
| 146 | + const calls = mockServer.setRequestHandler.mock.calls; |
| 147 | + expect(calls[0][0]).toBe(ListToolsRequestSchema); |
| 148 | + expect(calls[1][0]).toBe(CallToolRequestSchema); |
| 149 | + }); |
| 150 | + |
| 151 | + /** |
| 152 | + * Test ListTools handler |
| 153 | + */ |
| 154 | + it('should handle ListTools requests correctly', async () => { |
| 155 | + await startMcpServer(); |
| 156 | + |
| 157 | + // Get the ListTools handler |
| 158 | + const listToolsHandler = mockServer.setRequestHandler.mock.calls[0][1]; |
| 159 | + |
| 160 | + const result = await listToolsHandler(); |
| 161 | + expect(result).toEqual({ |
| 162 | + tools: [ |
| 163 | + { |
| 164 | + name: 'example_tool', |
| 165 | + description: 'An example tool that echoes back the input', |
| 166 | + inputSchema: expect.any(Object), |
| 167 | + }, |
| 168 | + ], |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + /** |
| 173 | + * Test CallTool handler for example tool |
| 174 | + */ |
| 175 | + it('should handle CallTool requests for example_tool', async () => { |
| 176 | + await startMcpServer(); |
| 177 | + |
| 178 | + // Get the CallTool handler |
| 179 | + const callToolHandler = mockServer.setRequestHandler.mock.calls[1][1]; |
| 180 | + |
| 181 | + const request = { |
| 182 | + params: { |
| 183 | + name: 'example_tool', |
| 184 | + arguments: { message: 'test', uppercase: false }, |
| 185 | + }, |
| 186 | + }; |
| 187 | + |
| 188 | + const result = await callToolHandler(request); |
| 189 | + |
| 190 | + expect(exampleModule.exampleTool).toHaveBeenCalledWith({ |
| 191 | + message: 'test', |
| 192 | + uppercase: false, |
| 193 | + }); |
| 194 | + expect(result).toEqual({ |
| 195 | + content: [{ type: 'text', text: 'Test result' }], |
| 196 | + }); |
| 197 | + }); |
| 198 | + |
| 199 | + /** |
| 200 | + * Test CallTool handler error for unknown tool |
| 201 | + */ |
| 202 | + it('should throw error for unknown tool in CallTool request', async () => { |
| 203 | + await startMcpServer(); |
| 204 | + |
| 205 | + // Get the CallTool handler |
| 206 | + const callToolHandler = mockServer.setRequestHandler.mock.calls[1][1]; |
| 207 | + |
| 208 | + const request = { |
| 209 | + params: { |
| 210 | + name: 'unknown_tool', |
| 211 | + arguments: {}, |
| 212 | + }, |
| 213 | + }; |
| 214 | + |
| 215 | + await expect(callToolHandler(request)).rejects.toThrow('Unknown tool: unknown_tool'); |
| 216 | + }); |
| 217 | + |
| 218 | + /** |
| 219 | + * Test SIGINT handler registration |
| 220 | + */ |
| 221 | + it('should register SIGINT handler for graceful shutdown', async () => { |
| 222 | + const mockProcessOn = vi.spyOn(process, 'on').mockImplementation(() => process); |
| 223 | + const mockProcessExit = vi.spyOn(process, 'exit').mockImplementation(() => { |
| 224 | + throw new Error('Process exit called'); |
| 225 | + }); |
| 226 | + |
| 227 | + await startMcpServer(); |
| 228 | + |
| 229 | + // Find the SIGINT handler |
| 230 | + const sigintCall = mockProcessOn.mock.calls.find((call) => call[0] === 'SIGINT'); |
| 231 | + expect(sigintCall).toBeDefined(); |
| 232 | + |
| 233 | + if (sigintCall) { |
| 234 | + const sigintHandler = sigintCall[1] as () => Promise<void>; |
| 235 | + |
| 236 | + // Test the SIGINT handler |
| 237 | + try { |
| 238 | + await sigintHandler(); |
| 239 | + } catch (error) { |
| 240 | + // Expected to throw due to process.exit mock |
| 241 | + } |
| 242 | + |
| 243 | + expect(mockServer.close).toHaveBeenCalled(); |
| 244 | + expect(mockProcessExit).toHaveBeenCalledWith(0); |
| 245 | + } |
| 246 | + |
| 247 | + mockProcessOn.mockRestore(); |
| 248 | + mockProcessExit.mockRestore(); |
| 249 | + }); |
19 | 250 | });
|
0 commit comments