|
1 | | -import { spawn, ChildProcess } from 'child_process'; |
2 | | -import path from 'path'; |
3 | 1 | import { ServerStatelessScenario } from './stateless'; |
| 2 | +import { describe, test, expect } from 'vitest'; |
| 3 | +import { ConformanceCheck } from '../../types'; |
4 | 4 |
|
5 | | -function startServer( |
6 | | - scriptPath: string, |
7 | | - port: number, |
8 | | - envOverrides?: Record<string, string> |
9 | | -): Promise<ChildProcess> { |
10 | | - return new Promise((resolve, reject) => { |
11 | | - const isWindows = process.platform === 'win32'; |
12 | | - const proc = spawn('npx', ['tsx', scriptPath], { |
13 | | - env: { ...process.env, PORT: port.toString(), ...envOverrides }, |
14 | | - stdio: ['ignore', 'pipe', 'pipe'], |
15 | | - shell: isWindows |
16 | | - }); |
17 | | - let stderr = ''; |
18 | | - proc.stderr?.on('data', (d) => (stderr += d.toString())); |
19 | | - const timeout = setTimeout(() => { |
20 | | - proc.kill('SIGKILL'); |
21 | | - reject( |
22 | | - new Error(`Server ${scriptPath} failed to start within 30s: ${stderr}`) |
23 | | - ); |
24 | | - }, 30000); |
25 | | - proc.stdout?.on('data', (data) => { |
26 | | - if (data.toString().includes('running on')) { |
27 | | - clearTimeout(timeout); |
28 | | - resolve(proc); |
| 5 | +const findCheck = (checks: ConformanceCheck[], id: string) => |
| 6 | + checks.find((c) => c.id === id); |
| 7 | + |
| 8 | +describe('Stateless Server Scenario Negative Tests', () => { |
| 9 | + // Inline network mocking helper |
| 10 | + function mockFetchTarget( |
| 11 | + handler: (reqBody: any, reqHeaders: Record<string, string>) => any |
| 12 | + ) { |
| 13 | + global.fetch = async (_url: any, init: any) => { |
| 14 | + const body = JSON.parse(init.body); |
| 15 | + const headers = init.headers || {}; |
| 16 | + const responseConfig = await handler(body, headers); |
| 17 | + |
| 18 | + return { |
| 19 | + status: responseConfig?.status ?? 404, |
| 20 | + json: async () => |
| 21 | + responseConfig?.body ?? { |
| 22 | + jsonrpc: '2.0', |
| 23 | + id: body.id, |
| 24 | + error: { code: -32601, message: 'Not found' } |
| 25 | + } |
| 26 | + } as Response; |
| 27 | + }; |
| 28 | + return 'http://mock-stateless-mcp-server.local'; |
| 29 | + } |
| 30 | + |
| 31 | + test('Fails validation if missing required fields in _meta are allowed to pass', async () => { |
| 32 | + // This bad server completely ignores missing params/_meta fields and returns a fake success result |
| 33 | + const mockUrl = mockFetchTarget((reqBody) => { |
| 34 | + if (reqBody.method === 'server/discover') { |
| 35 | + return { |
| 36 | + status: 200, |
| 37 | + body: { |
| 38 | + jsonrpc: '2.0', |
| 39 | + id: reqBody.id, |
| 40 | + result: { |
| 41 | + supportedVersions: ['DRAFT-2026-v1'], |
| 42 | + capabilities: {}, |
| 43 | + serverInfo: { name: 'bad-meta-server', version: '1.0.0' } |
| 44 | + } |
| 45 | + } |
| 46 | + }; |
29 | 47 | } |
30 | 48 | }); |
31 | | - proc.on('error', (err) => { |
32 | | - clearTimeout(timeout); |
33 | | - reject(err); |
34 | | - }); |
35 | | - }); |
36 | | -} |
37 | 49 |
|
38 | | -function stopServer(proc: ChildProcess | null): Promise<void> { |
39 | | - return new Promise((resolve) => { |
40 | | - if (!proc || proc.killed) return resolve(); |
41 | | - const t = setTimeout(() => { |
42 | | - proc.kill('SIGKILL'); |
43 | | - resolve(); |
44 | | - }, 5000); |
45 | | - proc.once('exit', () => { |
46 | | - clearTimeout(t); |
47 | | - resolve(); |
48 | | - }); |
49 | | - proc.kill('SIGTERM'); |
50 | | - }); |
51 | | -} |
| 50 | + const scenario = new ServerStatelessScenario(); |
| 51 | + const checks = await scenario.run(mockUrl); |
52 | 52 |
|
53 | | -describe('ServerStatelessScenario tests', () => { |
54 | | - describe('passing server', () => { |
55 | | - let serverProcess: ChildProcess | null = null; |
56 | | - const PORT = 3010; |
| 53 | + // The test scenario should flag this server as a FAILURE for skipping meta validation |
| 54 | + const missingMetaCheck = findCheck( |
| 55 | + checks, |
| 56 | + 'sep-2575-request-meta-invalid-missing-meta' |
| 57 | + ); |
| 58 | + const missingVersionCheck = findCheck( |
| 59 | + checks, |
| 60 | + 'sep-2575-request-meta-invalid-missing-protocol-version' |
| 61 | + ); |
57 | 62 |
|
58 | | - beforeAll(async () => { |
59 | | - serverProcess = await startServer( |
60 | | - path.join( |
61 | | - process.cwd(), |
62 | | - 'examples/servers/typescript/everything-server.ts' |
63 | | - ), |
64 | | - PORT |
65 | | - ); |
66 | | - }, 35000); |
| 63 | + expect(missingMetaCheck?.status).toBe('FAILURE'); |
| 64 | + expect(missingVersionCheck?.status).toBe('FAILURE'); |
| 65 | + }); |
67 | 66 |
|
68 | | - afterAll(async () => { |
69 | | - await stopServer(serverProcess); |
| 67 | + test('Fails validation if removed legacy RPCs do not return HTTP 404 Not Found', async () => { |
| 68 | + // This bad server intercepts the removed 'ping' or 'initialize' methods but incorrectly returns HTTP 200 |
| 69 | + const mockUrl = mockFetchTarget((reqBody) => { |
| 70 | + if ( |
| 71 | + [ |
| 72 | + 'initialize', |
| 73 | + 'ping', |
| 74 | + 'logging/setLevel', |
| 75 | + 'resources/subscribe', |
| 76 | + 'resources/unsubscribe' |
| 77 | + ].includes(reqBody.method) |
| 78 | + ) { |
| 79 | + return { |
| 80 | + status: 200, // Spec Violation: Must be HTTP 404 |
| 81 | + body: { |
| 82 | + jsonrpc: '2.0', |
| 83 | + id: reqBody.id, |
| 84 | + error: { |
| 85 | + code: -32601, |
| 86 | + message: 'Method removed but returning HTTP 200' |
| 87 | + } |
| 88 | + } |
| 89 | + }; |
| 90 | + } |
70 | 91 | }); |
71 | 92 |
|
72 | | - it('emits SUCCESS for all checks against a compliant stateless server', async () => { |
73 | | - const scenario = new ServerStatelessScenario(); |
74 | | - const checks = await scenario.run(`http://localhost:${PORT}/mcp`); |
| 93 | + const scenario = new ServerStatelessScenario(); |
| 94 | + const checks = await scenario.run(mockUrl); |
75 | 95 |
|
76 | | - for (const check of checks) { |
77 | | - if (check.status !== 'SUCCESS') { |
78 | | - console.error('FAILED CHECK:', JSON.stringify(check, null, 2)); |
79 | | - } |
80 | | - expect(check.status).toBe('SUCCESS'); |
81 | | - } |
82 | | - }, 15000); |
83 | | - }); |
| 96 | + const pingRouteCheck = findCheck( |
| 97 | + checks, |
| 98 | + 'sep-2575-http-server-method-not-found-404-ping' |
| 99 | + ); |
| 100 | + const initializeRouteCheck = findCheck( |
| 101 | + checks, |
| 102 | + 'sep-2575-http-server-method-not-found-404-initialize' |
| 103 | + ); |
84 | 104 |
|
85 | | - describe('negative server', () => { |
86 | | - let serverProcess: ChildProcess | null = null; |
87 | | - const PORT = 3012; |
88 | | - |
89 | | - beforeAll(async () => { |
90 | | - serverProcess = await startServer( |
91 | | - path.join( |
92 | | - process.cwd(), |
93 | | - 'examples/servers/typescript/everything-server.ts' |
94 | | - ), |
95 | | - PORT, |
96 | | - { STATELESS_NEGATIVE: 'true' } |
97 | | - ); |
98 | | - }, 35000); |
| 105 | + expect(pingRouteCheck?.status).toBe('FAILURE'); |
| 106 | + expect(initializeRouteCheck?.status).toBe('FAILURE'); |
| 107 | + }); |
99 | 108 |
|
100 | | - afterAll(async () => { |
101 | | - await stopServer(serverProcess); |
| 109 | + test('Fails validation when version negotiation returns mismatched supported versions data', async () => { |
| 110 | + // This bad server returns an unexpected array of supported versions during negotiation |
| 111 | + const mockUrl = mockFetchTarget((reqBody) => { |
| 112 | + const meta = reqBody.params?._meta; |
| 113 | + if (meta?.['io.modelcontextprotocol/protocolVersion'] === 'v999.0.0') { |
| 114 | + return { |
| 115 | + status: 400, |
| 116 | + body: { |
| 117 | + jsonrpc: '2.0', |
| 118 | + id: reqBody.id, |
| 119 | + error: { |
| 120 | + code: -32602, |
| 121 | + message: 'Unsupported version', |
| 122 | + data: { supported: ['UNEXPECTED-VERSION-STRING-DRIFT'] } // Spec Violation: Mismatches actual versions |
| 123 | + } |
| 124 | + } |
| 125 | + }; |
| 126 | + } |
102 | 127 | }); |
103 | 128 |
|
104 | | - it('emits FAILURE for checks against a broken stateless server', async () => { |
105 | | - const scenario = new ServerStatelessScenario(); |
106 | | - const checks = await scenario.run(`http://localhost:${PORT}/mcp`); |
| 129 | + const scenario = new ServerStatelessScenario(); |
| 130 | + const checks = await scenario.run(mockUrl); |
107 | 131 |
|
108 | | - const failures = checks.filter((c) => c.status === 'FAILURE'); |
109 | | - expect(failures.length).toBeGreaterThan(0); |
110 | | - }, 15000); |
| 132 | + const negotiationMatchCheck = findCheck( |
| 133 | + checks, |
| 134 | + 'sep-2575-server-unsupported-version-error' |
| 135 | + ); |
| 136 | + expect(negotiationMatchCheck?.status).toBe('FAILURE'); |
111 | 137 | }); |
112 | 138 | }); |
0 commit comments