-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathdebug-interactive.js
More file actions
128 lines (115 loc) · 3.09 KB
/
Copy pathdebug-interactive.js
File metadata and controls
128 lines (115 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env node
import { spawn } from 'child_process';
import path from 'path';
import { fileURLToPath } from 'url';
import readline from 'readline';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// 创建readline接口
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log('🔄 Starting MCP Server...');
// 启动服务器进程
const serverProcess = spawn('node', ['debug-local.js'], {
cwd: __dirname,
stdio: ['pipe', 'pipe', 'inherit']
});
let serverReady = false;
// 监听服务器输出
serverProcess.stdout.on('data', (data) => {
const output = data.toString();
// 检查服务器是否准备好
if (output.includes('Search index loaded with') && !serverReady) {
serverReady = true;
console.log('\n🚀 Server is ready! You can now send commands.');
showPrompt();
}
});
// 显示命令提示
function showPrompt() {
console.log('\n📝 Available commands:');
console.log('1. List all tools');
console.log('2. Search CSS techniques');
console.log('3. Get random CSS tip');
console.log('4. Custom command');
console.log('5. Exit');
rl.question('\n👉 Enter command number: ', (answer) => {
switch(answer) {
case '1':
sendCommand({
jsonrpc: "2.0",
id: 1,
method: "tools/list",
params: {}
});
break;
case '2':
rl.question('Enter search query: ', (query) => {
sendCommand({
jsonrpc: "2.0",
id: 2,
method: "tools/call",
params: {
name: "search_css_techniques",
arguments: {
query: query,
limit: 3
}
}
});
});
break;
case '3':
sendCommand({
jsonrpc: "2.0",
id: 3,
method: "tools/call",
params: {
name: "get_random_css_tip",
arguments: {}
}
});
break;
case '4':
rl.question('Enter custom JSON-RPC command: ', (cmd) => {
try {
const command = JSON.parse(cmd);
sendCommand(command);
} catch (e) {
console.error('❌ Invalid JSON:', e.message);
showPrompt();
}
});
break;
case '5':
console.log('👋 Shutting down server...');
serverProcess.kill();
rl.close();
process.exit(0);
break;
default:
console.log('❌ Invalid command number');
showPrompt();
}
});
}
// 发送命令到服务器
function sendCommand(command) {
console.log('\n📤 Sending command:', JSON.stringify(command, null, 2));
serverProcess.stdin.write(JSON.stringify(command) + '\n');
setTimeout(showPrompt, 1000);
}
// 处理退出
process.on('SIGINT', () => {
console.log('\n👋 Shutting down server...');
serverProcess.kill();
rl.close();
process.exit(0);
});
serverProcess.on('error', (error) => {
console.error('❌ Server error:', error);
rl.close();
process.exit(1);
});