#!/usr/bin/env node
const SERVER_URL = process.argv[2] || 'http://localhost:3000';
const MCP_ENDPOINT = `${SERVER_URL}/mcp`;
console.log(`π οΈ Comprehensive tool testing at ${MCP_ENDPOINT}\n`);
async function callTool(name, args = {}) {
const response = await fetch(MCP_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
id: Math.floor(Math.random() * 1000),
method: 'tools/call',
params: {
name,
arguments: args
}
})
});
const result = await response.json();
return result;
}
async function listTools() {
const response = await fetch(MCP_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'tools/list'
})
});
const result = await response.json();
return result.result?.tools || [];
}
async function runTests() {
try {
// Get available tools
console.log('π Listing available tools...');
const tools = await listTools();
console.log(`Found ${tools.length} tools:`, tools.map(t => t.name).join(', '));
console.log();
// Test each tool
for (const tool of tools) {
console.log(`π§ Testing "${tool.name}": ${tool.description}`);
try {
let result;
switch (tool.name) {
case 'echo':
result = await callTool('echo', { message: 'Test message from tool tester!' });
break;
case 'get-current-time':
result = await callTool('get-current-time');
break;
case 'calculate':
// Test multiple calculations
const expressions = ['2 + 2', '10 * 5', '100 / 4', '(15 + 5) * 2'];
for (const expr of expressions) {
result = await callTool('calculate', { expression: expr });
console.log(` π ${expr} = ${result.result?.content?.[0]?.text || 'Error'}`);
}
continue; // Skip the general result display for calculator
default:
result = await callTool(tool.name);
}
if (result.error) {
console.log(` β Error: ${result.error.message}`);
} else {
console.log(` β ${result.result?.content?.[0]?.text || 'Success'}`);
}
} catch (error) {
console.log(` β Failed: ${error.message}`);
}
console.log();
}
// Test error handling
console.log('π¨ Testing error handling...');
try {
const errorResult = await callTool('nonexistent-tool');
console.log(' β Error handling:', errorResult.error?.message || 'No error returned');
} catch (error) {
console.log(' β Error handling: Request failed as expected');
}
console.log('\nπ All tool tests completed!');
} catch (error) {
console.error('β Test suite failed:', error.message);
process.exit(1);
}
}
runTests();