import { spawn } from 'child_process';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const serverPath = path.join(__dirname, 'build', 'index.js');
console.log(`Starting server from: ${serverPath}`);
const server = spawn('node', [serverPath], {
stdio: ['pipe', 'pipe', 'inherit']
});
const query = process.argv[2] || "mcp protocol";
console.log(`--- Calling search tool with query: "${query}" ---`);
const send = (msg) => {
// console.log('-> Sending:', JSON.stringify(msg)); // Commented out to reduce noise
server.stdin.write(JSON.stringify(msg) + '\n');
};
server.stdout.on('data', (data) => {
const lines = data.toString().split('\n').filter(line => line.trim() !== '');
for (const line of lines) {
try {
const response = JSON.parse(line);
// console.log('<- Received:', JSON.stringify(response, null, 2)); // Commented out to reduce noise
if (response.id === 1 && response.result) {
// Initialized, send notification and then call tool
send({
jsonrpc: "2.0",
method: "notifications/initialized"
});
// Skip listing tools, go straight to search
send({
jsonrpc: "2.0",
id: 3,
method: "tools/call",
params: {
name: "search",
arguments: {
query: query,
limit: 3
}
}
});
} else if (response.id === 3) {
if (response.result && response.result.content) {
const content = JSON.parse(response.result.content[0].text);
console.log('\n=== SEARCH RESULTS ===');
content.forEach((item, i) => {
console.log(`\n[${i+1}] ${item}`);
});
console.log('\n======================\n');
} else {
console.log('No results or error:', response);
}
process.exit(0);
}
} catch (e) {
// console.log('<- Raw:', line);
}
}
});
// Start handshake
send({
jsonrpc: "2.0",
id: 1,
method: "initialize",
params: {
"protocolVersion": "2024-11-05",
"capabilities": {},
"clientInfo": {
"name": "manual-test",
"version": "1.0.0"
}
}
});