test-client.js•2.53 kB
import fetch from 'node-fetch';
const BASE_URL = 'http://localhost:3000';
// Test client for MCP server
class MCPTestClient {
async testEndpoint(method, endpoint, data = null) {
try {
const options = {
method,
headers: {
'Content-Type': 'application/json',
},
};
if (data) {
options.body = JSON.stringify(data);
}
const response = await fetch(`${BASE_URL}${endpoint}`, options);
const result = await response.json();
console.log(`\n${method} ${endpoint}:`);
console.log(`Status: ${response.status}`);
console.log('Response:', JSON.stringify(result, null, 2));
return result;
} catch (error) {
console.error(`Error testing ${method} ${endpoint}:`, error.message);
}
}
async runTests() {
console.log('🧪 Starting MCP Server Tests...\n');
// Test root endpoint
await this.testEndpoint('GET', '/');
// Test health check
await this.testEndpoint('GET', '/health');
// Test status
await this.testEndpoint('GET', '/status');
// Test users
await this.testEndpoint('GET', '/users');
await this.testEndpoint('GET', '/users/1');
await this.testEndpoint('POST', '/users', {
name: 'Alice Johnson',
email: 'alice@example.com',
role: 'moderator'
});
// Test tasks
await this.testEndpoint('GET', '/tasks');
await this.testEndpoint('POST', '/tasks', {
title: 'Test new task creation',
status: 'pending',
assignedTo: 1
});
// Test search
await this.testEndpoint('GET', '/search?q=john');
// Test metrics
await this.testEndpoint('GET', '/metrics');
// Test generic data endpoint
await this.testEndpoint('POST', '/data', {
data: { message: 'Hello MCP Server!', timestamp: new Date() },
type: 'test-message'
});
console.log('\n✅ All tests completed!');
}
}
// Check if server is running before starting tests
async function checkServerHealth() {
try {
const response = await fetch(`${BASE_URL}/health`);
if (response.ok) {
console.log('✅ Server is running, starting tests...');
return true;
}
} catch (error) {
console.log('❌ Server is not running. Please start the server first with: npm start');
return false;
}
}
// Run tests if server is available
if (await checkServerHealth()) {
const client = new MCPTestClient();
await client.runTests();
}