import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Path to MCP server
const serverPath = join(__dirname, '../dist/index.js');
// MCP Client simulation
class MCPClient {
constructor(serverProcess) {
this.process = serverProcess;
this.messageId = 1;
}
sendMessage(message) {
const fullMessage = {
jsonrpc: "2.0",
id: this.messageId++,
...message
};
console.log('→ Sending:', JSON.stringify(fullMessage, null, 2));
this.process.stdin.write(JSON.stringify(fullMessage) + '\n');
}
// Initialize the connection
initialize() {
this.sendMessage({
method: "initialize",
params: {
protocolVersion: "2024-11-05",
capabilities: {
roots: {
listChanged: true
}
},
clientInfo: {
name: "test-client",
version: "1.0.0"
}
}
});
}
// List available tools
listTools() {
this.sendMessage({
method: "tools/list",
params: {}
});
}
// Call tools
callTool(name, args) {
this.sendMessage({
method: "tools/call",
params: {
name,
arguments: args
}
});
}
}
// Start the server
console.log('Starting MCP Server...');
const serverProcess = spawn('node', [serverPath], {
stdio: ['pipe', 'pipe', 'pipe']
});
const client = new MCPClient(serverProcess);
// Handle server output
serverProcess.stdout.on('data', (data) => {
const lines = data.toString().split('\n').filter(line => line.trim());
lines.forEach(line => {
try {
const response = JSON.parse(line);
console.log('← Received:', JSON.stringify(response, null, 2));
} catch (e) {
console.log('← Raw output:', line);
}
});
});
serverProcess.stderr.on('data', (data) => {
console.error('Server stderr:', data.toString());
});
serverProcess.on('close', (code) => {
console.log(`Server process exited with code ${code}`);
});
// Test sequence
setTimeout(() => {
console.log('\n=== Initializing ===');
client.initialize();
}, 100);
setTimeout(() => {
console.log('\n=== Listing All Tools ===');
client.listTools();
}, 500);
setTimeout(() => {
console.log('\n=== Testing memo_list (active channel) ===');
client.callTool('memo_list', { take: 5 });
}, 1000);
setTimeout(() => {
console.log('\n=== Testing memo_search (active channel) ===');
client.callTool('memo_search', { query: 'Claude', limit: 3 });
}, 1500);
setTimeout(() => {
console.log('\n=== Testing memo_get (first memo from list) ===');
// Use an existing memo ID - you may need to update this
client.callTool('memo_get', { memoId: 'b02b39db-99b1-43b8-9280-81eb121a25e2' });
}, 2000);
setTimeout(() => {
console.log('\n=== Testing memo_create (active channel) ===');
client.callTool('memo_create', {
content: 'This is a test memo created via MCP!',
metadata: { testType: 'all-tools-test' }
});
}, 2500);
// Close after tests
setTimeout(() => {
console.log('\n=== Closing ===');
serverProcess.kill();
}, 4000);