test-tools.js•2.82 kB
#!/usr/bin/env node
// Simple JSON-RPC stdio test driver for mysql-mcp-server
// Usage: node scripts/test-tools.js [--env-file path]
import { spawn } from 'child_process';
import path from 'path';
import fs from 'fs';
const args = process.argv.slice(2);
let envFile = null;
for (let i=0;i<args.length;i++) {
if ((args[i] === '--env-file' || args[i] === '-e') && args[i+1]) { envFile = args[i+1]; i++; }
}
if (!envFile) envFile = path.resolve('.env');
if (!fs.existsSync(envFile)) {
console.error('Env file not found:', envFile);
process.exit(1);
}
const serverPath = path.resolve('mcp-mysql-lite.js');
const child = spawn(process.execPath, [serverPath, '--env-file', envFile], {
stdio: ['pipe','pipe','pipe'],
env: process.env,
});
child.stderr.on('data', d => process.stderr.write(d));
let responses = [];
child.stdout.on('data', chunk => {
const text = chunk.toString();
for (const line of text.split(/\r?\n/)) {
if (!line.trim()) continue;
try {
const obj = JSON.parse(line);
responses.push(obj);
console.log('RECV', JSON.stringify(obj));
checkDone();
} catch {/* ignore non-json lines */}
}
});
// Build request list dynamically so we can optionally add describe_table if a test table exists
const requests = [
{ id: 1, method: 'initialize', params: { protocolVersion: '2024-11-05', capabilities: {}, clientInfo: { name: 'test-client', version: '1.0.0' } } },
{ id: 2, method: 'tools/list' },
// Logging level change (should return empty result {})
{ id: 3, method: 'logging/setLevel', params: { level: 'debug' } },
{ id: 4, method: 'tools/call', params: { name: 'list_databases', arguments: {} } },
{ id: 5, method: 'tools/call', params: { name: 'query_database', arguments: { sql: 'SELECT 1 AS one', database: 'mcp_bridge' } } },
];
// Optionally add a describe_table call if we can guess a table name from env (simple heuristic)
try {
const envTxt = fs.readFileSync(envFile, 'utf8');
// If a line like MCP_BRIDGE_DEFAULT_TABLE=some_table appears we'll test it
const m = envTxt.match(/^MCP_BRIDGE_DEFAULT_TABLE=(.+)$/m);
if (m) {
requests.push({ id: 6, method: 'tools/call', params: { name: 'describe_table', arguments: { table_name: m[1].trim(), database: 'mcp_bridge' } } });
}
} catch { /* ignore */ }
function sendAll() {
for (const r of requests) {
const line = JSON.stringify({ jsonrpc: '2.0', ...r }) + '\n';
child.stdin.write(line);
}
}
function checkDone() {
if (responses.length >= requests.length) {
console.log('\nAll expected responses received.');
child.kill();
}
}
// Send after small delay to ensure process is ready
setTimeout(sendAll, 150);
setTimeout(() => {
console.error('Timeout waiting for responses. Collected:', responses.length);
child.kill();
process.exit(1);
}, 8000);