|
| 1 | +import { DRAFT_PROTOCOL_VERSION_2026, InMemoryTransport, isJSONRPCRequest, LATEST_PROTOCOL_VERSION } from '@modelcontextprotocol/core'; |
| 2 | + |
| 3 | +import { Client } from '../../src/client/client.js'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Links the client to a hand-rolled in-memory "server" that records the protocol version of each |
| 7 | + * initialize request crossing the wire and replies by echoing the requested version. Lets tests |
| 8 | + * observe exactly what the client put on the wire. |
| 9 | + */ |
| 10 | +function fakeInitializeServer(respondWithVersion?: string): { clientTransport: InMemoryTransport; requestedVersions: string[] } { |
| 11 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); |
| 12 | + const requestedVersions: string[] = []; |
| 13 | + serverTransport.onmessage = message => { |
| 14 | + if (isJSONRPCRequest(message) && message.method === 'initialize') { |
| 15 | + const params = message.params as { protocolVersion: string }; |
| 16 | + requestedVersions.push(params.protocolVersion); |
| 17 | + void serverTransport.send({ |
| 18 | + jsonrpc: '2.0', |
| 19 | + id: message.id, |
| 20 | + result: { |
| 21 | + protocolVersion: respondWithVersion ?? params.protocolVersion, |
| 22 | + capabilities: {}, |
| 23 | + serverInfo: { name: 'fake-server', version: '0.0.0' } |
| 24 | + } |
| 25 | + }); |
| 26 | + } |
| 27 | + // Notifications (e.g. notifications/initialized) need no reply. |
| 28 | + }; |
| 29 | + return { clientTransport, requestedVersions }; |
| 30 | +} |
| 31 | + |
| 32 | +describe('Client', () => { |
| 33 | + describe('initialize never negotiates draft protocol versions', () => { |
| 34 | + it('connect() rejects when supportedProtocolVersions contains only draft versions', async () => { |
| 35 | + const client = new Client( |
| 36 | + { name: 'test-client', version: '1.0.0' }, |
| 37 | + { supportedProtocolVersions: [DRAFT_PROTOCOL_VERSION_2026] } |
| 38 | + ); |
| 39 | + const { clientTransport, requestedVersions } = fakeInitializeServer(); |
| 40 | + |
| 41 | + await expect(client.connect(clientTransport)).rejects.toThrow('initialize cannot negotiate draft protocol versions'); |
| 42 | + // The handshake never started: nothing was put on the wire. |
| 43 | + expect(requestedVersions).toEqual([]); |
| 44 | + expect(client.getNegotiatedProtocolVersion()).toBeUndefined(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('rejects an initialize result carrying a draft version, even when the draft is listed as supported', async () => { |
| 48 | + const client = new Client( |
| 49 | + { name: 'test-client', version: '1.0.0' }, |
| 50 | + { supportedProtocolVersions: [DRAFT_PROTOCOL_VERSION_2026, LATEST_PROTOCOL_VERSION] } |
| 51 | + ); |
| 52 | + // Nonconforming server: answers the released-version request with the draft version. |
| 53 | + const { clientTransport } = fakeInitializeServer(DRAFT_PROTOCOL_VERSION_2026); |
| 54 | + |
| 55 | + await expect(client.connect(clientTransport)).rejects.toThrow( |
| 56 | + `Server's protocol version is not supported: ${DRAFT_PROTOCOL_VERSION_2026}` |
| 57 | + ); |
| 58 | + // Nothing was recorded: the draft version is not negotiated in either direction. |
| 59 | + expect(client.getNegotiatedProtocolVersion()).toBeUndefined(); |
| 60 | + }); |
| 61 | + }); |
| 62 | +}); |
0 commit comments