|
| 1 | +// @vitest-environment node |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | +import { parseMcpServers } from './handler.js'; |
| 4 | + |
| 5 | +describe('parseMcpServers', () => { |
| 6 | + it('returns undefined for non-array input', () => { |
| 7 | + expect(parseMcpServers(undefined)).toBeUndefined(); |
| 8 | + expect(parseMcpServers(null)).toBeUndefined(); |
| 9 | + expect(parseMcpServers('string')).toBeUndefined(); |
| 10 | + expect(parseMcpServers(42)).toBeUndefined(); |
| 11 | + }); |
| 12 | + |
| 13 | + it('returns undefined for an empty array', () => { |
| 14 | + expect(parseMcpServers([])).toBeUndefined(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('parses valid HTTP and SSE servers', () => { |
| 18 | + const result = parseMcpServers([ |
| 19 | + { name: 'api', url: 'https://api.example.com/mcp', type: 'http', headers: { 'X-Key': 'k1' }, tools: ['search'] }, |
| 20 | + { name: 'stream', url: 'https://stream.example.com/events', type: 'sse', headers: {}, tools: [] }, |
| 21 | + ]); |
| 22 | + |
| 23 | + expect(result).toEqual([ |
| 24 | + { name: 'api', url: 'https://api.example.com/mcp', type: 'http', headers: { 'X-Key': 'k1' }, tools: ['search'] }, |
| 25 | + { name: 'stream', url: 'https://stream.example.com/events', type: 'sse', headers: {}, tools: [] }, |
| 26 | + ]); |
| 27 | + }); |
| 28 | + |
| 29 | + it('filters out servers with enabled === false', () => { |
| 30 | + const result = parseMcpServers([ |
| 31 | + { name: 'active', url: 'https://a.example.com/mcp', type: 'http', headers: {}, tools: [], enabled: true }, |
| 32 | + { name: 'disabled', url: 'https://b.example.com/mcp', type: 'http', headers: {}, tools: [], enabled: false }, |
| 33 | + ]); |
| 34 | + |
| 35 | + expect(result).toEqual([ |
| 36 | + { name: 'active', url: 'https://a.example.com/mcp', type: 'http', headers: {}, tools: [] }, |
| 37 | + ]); |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns undefined when all servers are disabled', () => { |
| 41 | + const result = parseMcpServers([ |
| 42 | + { name: 's1', url: 'https://a.example.com/mcp', type: 'http', headers: {}, tools: [], enabled: false }, |
| 43 | + { name: 's2', url: 'https://b.example.com/mcp', type: 'sse', headers: {}, tools: [], enabled: false }, |
| 44 | + ]); |
| 45 | + |
| 46 | + expect(result).toBeUndefined(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('includes servers without an enabled field (defaults to enabled)', () => { |
| 50 | + const result = parseMcpServers([ |
| 51 | + { name: 'no-flag', url: 'https://a.example.com/mcp', type: 'http', headers: {}, tools: [] }, |
| 52 | + ]); |
| 53 | + |
| 54 | + expect(result).toHaveLength(1); |
| 55 | + expect(result![0].name).toBe('no-flag'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('rejects entries with missing or invalid fields', () => { |
| 59 | + const result = parseMcpServers([ |
| 60 | + null, |
| 61 | + { name: 'missing-url', type: 'http', headers: {}, tools: [] }, |
| 62 | + { name: 'bad-type', url: 'https://a.example.com', type: 'grpc', headers: {}, tools: [] }, |
| 63 | + { name: 'no-headers', url: 'https://a.example.com', type: 'http', tools: [] }, |
| 64 | + { name: 'no-tools', url: 'https://a.example.com', type: 'http', headers: {} }, |
| 65 | + 'not-an-object', |
| 66 | + ]); |
| 67 | + |
| 68 | + expect(result).toBeUndefined(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('limits to 10 servers', () => { |
| 72 | + const servers = Array.from({ length: 15 }, (_, i) => ({ |
| 73 | + name: `server-${i}`, |
| 74 | + url: `https://s${i}.example.com/mcp`, |
| 75 | + type: 'http' as const, |
| 76 | + headers: {}, |
| 77 | + tools: [], |
| 78 | + })); |
| 79 | + |
| 80 | + const result = parseMcpServers(servers); |
| 81 | + expect(result).toHaveLength(10); |
| 82 | + expect(result![9].name).toBe('server-9'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('filters non-string values from tools arrays', () => { |
| 86 | + const result = parseMcpServers([ |
| 87 | + { name: 'mixed-tools', url: 'https://a.example.com/mcp', type: 'http', headers: {}, tools: ['valid', 42, null, 'also-valid'] }, |
| 88 | + ]); |
| 89 | + |
| 90 | + expect(result![0].tools).toEqual(['valid', 'also-valid']); |
| 91 | + }); |
| 92 | +}); |
0 commit comments