|
| 1 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 2 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
| 3 | +import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js'; |
| 4 | +import { LATEST_PROTOCOL_VERSION, type JSONRPCMessage } from '@modelcontextprotocol/sdk/types.js'; |
| 5 | +import { SERVER_NAME, SERVER_VERSION } from '../constants.js'; |
| 6 | +import { setupCoreServer, teardownServer, type ServerHandle } from '../core/setup.js'; |
| 7 | +import { setupAllianceServer } from '../alliance/setup.js'; |
| 8 | +import { resolveAllianceConfig } from '../alliance/config.js'; |
| 9 | +import { createIsolatedContext } from '../core/server/server-context.js'; |
| 10 | +import { |
| 11 | + createTestServerContext, |
| 12 | + isolateFromDefaultContext, |
| 13 | + makeMockPineconeClient, |
| 14 | +} from '../core/server/tools/test-helpers.js'; |
| 15 | + |
| 16 | +/** |
| 17 | + * End-to-end MCP verification harness (#202). |
| 18 | + * |
| 19 | + * Drives the real `McpServer` over an in-memory transport with the SDK's own |
| 20 | + * `Client`, so a future SDK bump (the 2026-07-28 RC protocol revision, once it |
| 21 | + * ships) is verified in one place: the initialize handshake and protocol |
| 22 | + * negotiation, the full registered tool surface, and a round-trip tool call. |
| 23 | + * The protocol assertions key off the SDK's `LATEST_PROTOCOL_VERSION`, so when |
| 24 | + * the RC SDK is pinned they re-check the server against the new revision with no |
| 25 | + * edits here. If the RC is not published in time, this still guards the current |
| 26 | + * pinned SDK. |
| 27 | + */ |
| 28 | + |
| 29 | +const CORE_TOOLS = [ |
| 30 | + 'list_namespaces', |
| 31 | + 'namespace_router', |
| 32 | + 'count', |
| 33 | + 'query', |
| 34 | + 'keyword_search', |
| 35 | + 'query_documents', |
| 36 | + 'generate_urls', |
| 37 | + 'guided_query', |
| 38 | +].sort(); |
| 39 | + |
| 40 | +/** A fresh, isolated core server (own context + mock client) so several can coexist. */ |
| 41 | +async function freshCoreServer(namespaces: string[] = ['ns']): Promise<ServerHandle> { |
| 42 | + const ctx = createTestServerContext({ client: makeMockPineconeClient(namespaces) as never }); |
| 43 | + return setupCoreServer({ context: ctx }); |
| 44 | +} |
| 45 | + |
| 46 | +/** Link the SDK Client to a live server over paired in-memory transports. */ |
| 47 | +async function connectClient(server: ServerHandle): Promise<Client> { |
| 48 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); |
| 49 | + await server.connect(serverTransport); |
| 50 | + const client = new Client({ name: 'rc-readiness-harness', version: '0.0.0' }); |
| 51 | + await client.connect(clientTransport); |
| 52 | + return client; |
| 53 | +} |
| 54 | + |
| 55 | +/** Send a raw JSON-RPC initialize and return the negotiated result. */ |
| 56 | +async function rawInitialize( |
| 57 | + server: ServerHandle, |
| 58 | + requestedProtocolVersion: string |
| 59 | +): Promise<{ protocolVersion: string; serverInfo: { name: string; version: string } }> { |
| 60 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); |
| 61 | + await server.connect(serverTransport); |
| 62 | + try { |
| 63 | + return await new Promise<{ |
| 64 | + protocolVersion: string; |
| 65 | + serverInfo: { name: string; version: string }; |
| 66 | + }>((resolve, reject) => { |
| 67 | + clientTransport.onmessage = (message: JSONRPCMessage) => { |
| 68 | + if (!('id' in message) || message.id !== 1) return; |
| 69 | + if ('error' in message) { |
| 70 | + reject(new Error(message.error.message)); |
| 71 | + } else if ('result' in message) { |
| 72 | + resolve( |
| 73 | + message.result as { |
| 74 | + protocolVersion: string; |
| 75 | + serverInfo: { name: string; version: string }; |
| 76 | + } |
| 77 | + ); |
| 78 | + } |
| 79 | + }; |
| 80 | + void clientTransport |
| 81 | + .start() |
| 82 | + .then(() => |
| 83 | + clientTransport.send({ |
| 84 | + jsonrpc: '2.0', |
| 85 | + id: 1, |
| 86 | + method: 'initialize', |
| 87 | + params: { |
| 88 | + protocolVersion: requestedProtocolVersion, |
| 89 | + capabilities: {}, |
| 90 | + clientInfo: { name: 'raw-harness', version: '0.0.0' }, |
| 91 | + }, |
| 92 | + }) |
| 93 | + ) |
| 94 | + .catch(reject); |
| 95 | + }); |
| 96 | + } finally { |
| 97 | + await clientTransport.close(); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +describe('MCP RC-readiness harness (#202)', () => { |
| 102 | + afterEach(() => { |
| 103 | + teardownServer(); |
| 104 | + isolateFromDefaultContext(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('initializes over the transport and round-trips server metadata', async () => { |
| 108 | + const server = await freshCoreServer(); |
| 109 | + const client = await connectClient(server); |
| 110 | + |
| 111 | + // A resolved connect proves the initialize handshake and protocol |
| 112 | + // negotiation succeeded: the SDK Client throws if the server answers with a |
| 113 | + // protocolVersion outside SUPPORTED_PROTOCOL_VERSIONS. |
| 114 | + expect(client.getServerVersion()).toEqual({ name: SERVER_NAME, version: SERVER_VERSION }); |
| 115 | + expect(client.getServerCapabilities()?.tools).toBeDefined(); |
| 116 | + expect(client.getInstructions()).toBeTruthy(); |
| 117 | + |
| 118 | + await client.close(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('exposes the full core tool surface through tools/list', async () => { |
| 122 | + const server = await freshCoreServer(); |
| 123 | + const client = await connectClient(server); |
| 124 | + |
| 125 | + const names = (await client.listTools()).tools.map((t) => t.name).sort(); |
| 126 | + expect(names).toEqual(CORE_TOOLS); |
| 127 | + |
| 128 | + await client.close(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('registers the Alliance suggest_query_params tool on top of the core surface', async () => { |
| 132 | + const ctx = createIsolatedContext( |
| 133 | + resolveAllianceConfig({ apiKey: 'sk-test', indexName: 'test-index' }), |
| 134 | + { client: makeMockPineconeClient(['ns']) as never } |
| 135 | + ); |
| 136 | + const server = await setupAllianceServer({ context: ctx }); |
| 137 | + const client = await connectClient(server); |
| 138 | + |
| 139 | + const names = (await client.listTools()).tools.map((t) => t.name); |
| 140 | + for (const core of CORE_TOOLS) expect(names).toContain(core); |
| 141 | + expect(names).toContain('suggest_query_params'); |
| 142 | + |
| 143 | + await client.close(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('round-trips a tool call over the transport', async () => { |
| 147 | + const server = await freshCoreServer(['alpha', 'beta']); |
| 148 | + const client = await connectClient(server); |
| 149 | + |
| 150 | + const res = await client.callTool({ name: 'list_namespaces', arguments: {} }); |
| 151 | + expect(res.isError ?? false).toBe(false); |
| 152 | + // The seeded namespaces must round-trip through the handler and transport, |
| 153 | + // not just any array, so a regression returning empty/garbage content fails. |
| 154 | + const text = (res.content as Array<{ type: string; text: string }>)[0]?.text ?? ''; |
| 155 | + expect(text).toContain('alpha'); |
| 156 | + expect(text).toContain('beta'); |
| 157 | + |
| 158 | + await client.close(); |
| 159 | + }); |
| 160 | + |
| 161 | + it('negotiates the SDK latest protocol version and falls back for an unknown request', async () => { |
| 162 | + // A server connects to one transport for its lifetime, so use a fresh one per probe. |
| 163 | + const negotiated = await rawInitialize(await freshCoreServer(), LATEST_PROTOCOL_VERSION); |
| 164 | + expect(negotiated.protocolVersion).toBe(LATEST_PROTOCOL_VERSION); |
| 165 | + expect(negotiated.serverInfo).toMatchObject({ name: SERVER_NAME, version: SERVER_VERSION }); |
| 166 | + |
| 167 | + // An unsupported request must not error; the server falls back to its latest. |
| 168 | + const fallback = await rawInitialize(await freshCoreServer(), 'not-a-real-protocol-version'); |
| 169 | + expect(fallback.protocolVersion).toBe(LATEST_PROTOCOL_VERSION); |
| 170 | + }); |
| 171 | +}); |
0 commit comments