demo-mcp.js•5.37 kB
#!/usr/bin/env node
import { spawn } from 'child_process';
import fs from 'fs-extra';
import path from 'path';
import os from 'os';
class TafaMcpDemo {
constructor() {
this.testDir = path.join(os.tmpdir(), 'tafa-mcp-demo');
this.server = null;
this.requestId = 1;
}
async runDemo() {
console.log('🎯 Tafa MCP Server Demo');
console.log('========================\n');
// Setup test environment
await this.setupTestEnv();
// Start server
await this.startServer();
// Run demonstration
await this.runDemoSequence();
// Cleanup
await this.cleanup();
}
async setupTestEnv() {
console.log('🔧 Setting up demo environment...');
await fs.ensureDir(this.testDir);
// Create some demo files
await fs.writeFile(path.join(this.testDir, 'demo.txt'), 'Welcome to Tafa MCP!\nThis is a demo file.');
await fs.writeFile(path.join(this.testDir, 'config.json'), JSON.stringify({name: 'demo', version: '1.0'}, null, 2));
await fs.ensureDir(path.join(this.testDir, 'docs'));
await fs.writeFile(path.join(this.testDir, 'docs', 'README.md'), '# Demo Project\nThis is a demo project.');
console.log(`✅ Demo environment ready at: ${this.testDir}\n`);
}
async startServer() {
return new Promise((resolve, reject) => {
console.log('🚀 Starting Tafa MCP Server...');
this.server = spawn('node', ['src/index.js', this.testDir], {
stdio: ['pipe', 'pipe', 'pipe'],
cwd: process.cwd()
});
this.server.stdout.on('data', (data) => {
const output = data.toString();
if (output.includes('Tafa MCP Server connected and ready')) {
console.log('✅ Server started successfully!\n');
resolve();
}
});
this.server.stderr.on('data', (data) => {
console.error('❌ Server error:', data.toString());
reject(new Error(data.toString()));
});
setTimeout(() => {
reject(new Error('Server startup timeout'));
}, 10000);
});
}
async runDemoSequence() {
console.log('🎬 Demo Sequence Starting...\n');
// 1. Get server info
await this.sendRequest('get_server_info', {}, '📋 Server Information');
// 2. List directory
await this.sendRequest('list_directory', {
path: this.testDir,
recursive: false,
showHidden: false
}, '📁 Directory Listing');
// 3. Read a file
await this.sendRequest('read_file', {
path: path.join(this.testDir, 'demo.txt')
}, '📄 Reading File');
// 4. Create a new file
await this.sendRequest('write_file', {
path: path.join(this.testDir, 'new-file.txt'),
content: 'This is a new file created by Tafa MCP!',
backup: true
}, '✍️ Writing New File');
// 5. Search for files
await this.sendRequest('search_files', {
directory: this.testDir,
pattern: '*.txt',
recursive: true
}, '🔍 Searching Files');
// 6. Search content
await this.sendRequest('search_content', {
directory: this.testDir,
searchTerm: 'demo',
filePattern: '*',
recursive: true
}, '🔍 Searching Content');
// 7. Get file info
await this.sendRequest('get_file_info', {
path: path.join(this.testDir, 'demo.txt')
}, '📊 File Information');
console.log('🎉 Demo sequence completed successfully!\n');
}
async sendRequest(tool, params, description) {
return new Promise((resolve, reject) => {
console.log(`${description}:`);
console.log(`🔧 Calling: ${tool}`);
const request = {
jsonrpc: '2.0',
id: this.requestId++,
method: 'tools/call',
params: {
name: tool,
arguments: params
}
};
// Set up response handler
const responseHandler = (data) => {
try {
const lines = data.toString().split('\n').filter(line => line.trim());
for (const line of lines) {
if (line.startsWith('{') && line.includes('"result"')) {
const response = JSON.parse(line);
if (response.id === request.id) {
console.log('📤 Response:', response.result.content[0].text);
console.log(''); // Empty line for spacing
this.server.stdout.removeListener('data', responseHandler);
resolve(response);
return;
}
}
}
} catch (error) {
console.error('❌ Error parsing response:', error.message);
this.server.stdout.removeListener('data', responseHandler);
reject(error);
}
};
this.server.stdout.on('data', responseHandler);
this.server.stdin.write(JSON.stringify(request) + '\n');
// Timeout after 5 seconds
setTimeout(() => {
this.server.stdout.removeListener('data', responseHandler);
reject(new Error('Request timeout'));
}, 5000);
});
}
async cleanup() {
console.log('🧹 Cleaning up...');
if (this.server) {
this.server.kill();
}
await fs.remove(this.testDir);
console.log('✅ Demo complete!');
}
}
// Run demo
const demo = new TafaMcpDemo();
demo.runDemo().catch(console.error);