|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Simple test script to verify the MCP server works |
| 5 | + */ |
| 6 | + |
| 7 | +import { spawn } from 'child_process'; |
| 8 | +import path from 'path'; |
| 9 | +import { fileURLToPath } from 'url'; |
| 10 | + |
| 11 | +const __filename = fileURLToPath(import.meta.url); |
| 12 | +const __dirname = path.dirname(__filename); |
| 13 | + |
| 14 | +function testMcpServer() { |
| 15 | + return new Promise((resolve, reject) => { |
| 16 | + const serverPath = path.join(__dirname, '../dist/index.js'); |
| 17 | + const server = spawn('node', [serverPath], { |
| 18 | + stdio: ['pipe', 'pipe', 'pipe'], |
| 19 | + }); |
| 20 | + |
| 21 | + let output = ''; |
| 22 | + let errorOutput = ''; |
| 23 | + |
| 24 | + server.stdout.on('data', (data) => { |
| 25 | + output += data.toString(); |
| 26 | + }); |
| 27 | + |
| 28 | + server.stderr.on('data', (data) => { |
| 29 | + errorOutput += data.toString(); |
| 30 | + }); |
| 31 | + |
| 32 | + // Send a JSON-RPC request to list tools |
| 33 | + const listToolsRequest = { |
| 34 | + jsonrpc: '2.0', |
| 35 | + id: 1, |
| 36 | + method: 'tools/list', |
| 37 | + params: {}, |
| 38 | + }; |
| 39 | + |
| 40 | + const echoRequest = { |
| 41 | + jsonrpc: '2.0', |
| 42 | + id: 2, |
| 43 | + method: 'tools/call', |
| 44 | + params: { |
| 45 | + name: 'echo', |
| 46 | + arguments: { |
| 47 | + message: 'Hello, MCP!', |
| 48 | + }, |
| 49 | + }, |
| 50 | + }; |
| 51 | + |
| 52 | + // Send requests |
| 53 | + server.stdin.write(JSON.stringify(listToolsRequest) + '\n'); |
| 54 | + server.stdin.write(JSON.stringify(echoRequest) + '\n'); |
| 55 | + server.stdin.end(); |
| 56 | + |
| 57 | + server.on('close', (code) => { |
| 58 | + if (code === 0) { |
| 59 | + console.log('✓ MCP server started successfully'); |
| 60 | + console.log('Server output:', output); |
| 61 | + if (errorOutput) { |
| 62 | + console.log('Server errors:', errorOutput); |
| 63 | + } |
| 64 | + resolve({ output, errorOutput }); |
| 65 | + } else { |
| 66 | + console.error(`✗ MCP server exited with code ${code}`); |
| 67 | + console.error('Error output:', errorOutput); |
| 68 | + reject(new Error(`Server exited with code ${code}`)); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + server.on('error', (error) => { |
| 73 | + console.error('✗ Failed to start MCP server:', error); |
| 74 | + reject(error); |
| 75 | + }); |
| 76 | + |
| 77 | + // Timeout after 10 seconds |
| 78 | + setTimeout(() => { |
| 79 | + server.kill(); |
| 80 | + reject(new Error('Test timeout')); |
| 81 | + }, 10000); |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +// Run the test |
| 86 | +testMcpServer() |
| 87 | + .then(() => { |
| 88 | + console.log('✓ All tests passed'); |
| 89 | + process.exit(0); |
| 90 | + }) |
| 91 | + .catch((error) => { |
| 92 | + console.error('✗ Test failed:', error); |
| 93 | + process.exit(1); |
| 94 | + }); |
0 commit comments