test-mcp-server.js•5.1 kB
#!/usr/bin/env node
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
class MCPTester {
constructor() {
this.serverProcess = null;
}
async startServer() {
console.log('🚀 Starting MCP Goose Subagents server...');
const serverPath = join(__dirname, '..', 'src', 'index.js');
this.serverProcess = spawn('node', [serverPath], {
stdio: ['pipe', 'pipe', 'pipe'],
env: { ...process.env, ALPHA_FEATURES: 'true' }
});
this.serverProcess.stderr.on('data', (data) => {
console.log('Server:', data.toString().trim());
});
// Wait for server to start
await new Promise(resolve => setTimeout(resolve, 1000));
}
async sendRequest(request) {
return new Promise((resolve, reject) => {
let response = '';
const timeout = setTimeout(() => {
reject(new Error('Request timeout'));
}, 10000);
this.serverProcess.stdout.on('data', (data) => {
response += data.toString();
try {
const parsed = JSON.parse(response);
clearTimeout(timeout);
resolve(parsed);
} catch (e) {
// Still receiving data
}
});
this.serverProcess.stdin.write(JSON.stringify(request) + '\n');
});
}
async testListTools() {
console.log('\n📋 Testing list_tools...');
const request = {
jsonrpc: '2.0',
id: 1,
method: 'tools/list'
};
try {
const response = await this.sendRequest(request);
console.log('✅ Tools available:', response.result?.tools?.map(t => t.name) || 'No tools');
return response.result?.tools || [];
} catch (error) {
console.log('❌ Error listing tools:', error.message);
return [];
}
}
async testCreateRecipe() {
console.log('\n🧪 Testing create_goose_recipe...');
const request = {
jsonrpc: '2.0',
id: 2,
method: 'tools/call',
params: {
name: 'create_goose_recipe',
arguments: {
recipe_name: 'test-developer',
role: 'test_developer',
instructions: 'You are a test developer specialized in writing comprehensive tests',
extensions: ['developer'],
parameters: {
test_framework: 'jest'
}
}
}
};
try {
const response = await this.sendRequest(request);
console.log('✅ Recipe created successfully');
console.log('Response:', response.result?.content?.[0]?.text || 'No content');
} catch (error) {
console.log('❌ Error creating recipe:', error.message);
}
}
async testDelegateSubagents() {
console.log('\n🤖 Testing delegate_to_subagents...');
const request = {
jsonrpc: '2.0',
id: 3,
method: 'tools/call',
params: {
name: 'delegate_to_subagents',
arguments: {
task: 'Create a simple hello world application',
agents: [
{
role: 'backend_developer',
instructions: 'Create a simple Node.js server that returns "Hello World"'
},
{
role: 'frontend_developer',
instructions: 'Create a simple HTML page that displays "Hello World"'
}
],
execution_mode: 'parallel'
}
}
};
try {
const response = await this.sendRequest(request);
console.log('✅ Subagents delegated successfully');
console.log('Session info:', response.result?.content?.[0]?.text || 'No content');
} catch (error) {
console.log('❌ Error delegating to subagents:', error.message);
}
}
async testListActiveSubagents() {
console.log('\n📊 Testing list_active_subagents...');
const request = {
jsonrpc: '2.0',
id: 4,
method: 'tools/call',
params: {
name: 'list_active_subagents',
arguments: {}
}
};
try {
const response = await this.sendRequest(request);
console.log('✅ Active subagents listed');
console.log('Status:', response.result?.content?.[0]?.text || 'No content');
} catch (error) {
console.log('❌ Error listing subagents:', error.message);
}
}
async cleanup() {
if (this.serverProcess) {
console.log('\n🧹 Cleaning up...');
this.serverProcess.kill();
}
}
async runTests() {
try {
await this.startServer();
const tools = await this.testListTools();
if (tools.length > 0) {
await this.testCreateRecipe();
await this.testDelegateSubagents();
await this.testListActiveSubagents();
}
console.log('\n🎉 Test suite completed!');
} catch (error) {
console.error('❌ Test suite failed:', error.message);
} finally {
await this.cleanup();
}
}
}
// Run tests
const tester = new MCPTester();
tester.runTests().catch(console.error);