import assert from 'node:assert/strict'; import { after, before, test } from 'node:test'; import { writeFile } from 'node:fs/promises'; import { Client } from '@modelcontextprotocol/sdk/client/index.js'; import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'; const client = new Client({ name: 'opx-fixture-client', version: '0.1.0' }); const transport = new StdioClientTransport({ command: process.execPath, args: ['dist/server.js'], stderr: 'pipe' }); const transcript = []; const wire = []; const originalSend = transport.send.bind(transport); transport.send = async (message, options) => { wire.push({ direction: 'client-to-server', message }); return originalSend(message, options); }; const originalStart = transport.start.bind(transport); transport.start = async () => { const receive = transport.onmessage; transport.onmessage = (message, extra) => { wire.push({ direction: 'server-to-client', message }); receive?.(message, extra); }; return originalStart(); }; let serverErrors = ''; before(async () => { transport.stderr?.on('data', (data) => { serverErrors += data.toString(); }); await client.connect(transport); transcript.push({ step: 'initialized', server: client.getServerVersion(), capabilities: client.getServerCapabilities() }); }); after(async () => { await client.close(); await writeFile('client-transcript.json', JSON.stringify({ transport: 'stdio', wire, calls: transcript, serverStderr: serverErrors }, null, 2) + '\n'); }); test('discovers one read-only tool with typed input and output schemas', async () => { const result = await client.listTools(); transcript.push({ method: 'tools/list', result }); assert.equal(result.tools.length, 1); const tool = result.tools[0]; assert.equal(tool.name, 'inventory_lookup'); assert.equal(tool.inputSchema.properties.sku.type, 'string'); assert.deepEqual(tool.inputSchema.required, ['sku']); assert.equal(tool.inputSchema.additionalProperties, false); assert.equal(tool.annotations.readOnlyHint, true); assert.equal(tool.annotations.destructiveHint, false); assert.ok(tool.outputSchema); }); for (const [sku, expected] of [ ['A001', { sku: 'A001', status: 'found', units: 7 }], ['B002', { sku: 'B002', status: 'found', units: 0 }], ['C003', { sku: 'C003', status: 'not_found', units: null }], ]) { test(`calls ${sku} over MCP stdio`, async () => { const result = await client.callTool({ name: 'inventory_lookup', arguments: { sku } }); transcript.push({ method: 'tools/call', arguments: { sku }, result }); assert.notEqual(result.isError, true); assert.deepEqual(result.structuredContent, expected); assert.deepEqual(JSON.parse(result.content[0].text), expected); }); } for (const [name, args] of [['numeric SKU', { sku: 7 }], ['malformed SKU', { sku: 'a001' }], ['extra property', { sku: 'A001', update: 99 }]]) { test(`rejects ${name} through the protocol`, async () => { const result = await client.callTool({ name: 'inventory_lookup', arguments: args }); transcript.push({ method: 'tools/call', arguments: args, result }); assert.equal(result.isError, true); assert.equal(result.structuredContent, undefined); }); } test('repeated reads preserve the fixture stock', async () => { const result = await client.callTool({ name: 'inventory_lookup', arguments: { sku: 'A001' } }); transcript.push({ method: 'tools/call', arguments: { sku: 'A001' }, result }); assert.deepEqual(result.structuredContent, { sku: 'A001', status: 'found', units: 7 }); });