forked from fhyfhy17/panel-feedback
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-stdio-wrapper.js
More file actions
407 lines (362 loc) · 13 KB
/
mcp-stdio-wrapper.js
File metadata and controls
407 lines (362 loc) · 13 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/usr/bin/env node
/**
* Stdio wrapper for windsurf-feedback-panel MCP
* 使用轮询机制等待用户反馈,支持长时间等待(几天)
*/
const http = require('http');
const readline = require('readline');
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const os = require('os');
const { execSync } = require('child_process');
// 端口注册表配置
const REGISTRY_DIR = path.join(os.homedir(), '.panel-feedback');
const REGISTRY_FILE = path.join(REGISTRY_DIR, 'ports.json');
const DEFAULT_PORT = 19876;
const POLL_INTERVAL = 500; // 500ms 轮询间隔
const MAX_POLL_TIME = 86400000 * 7; // 最长等待 7 天
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
// 读取端口注册表
function readRegistry() {
try {
if (fs.existsSync(REGISTRY_FILE)) {
const content = fs.readFileSync(REGISTRY_FILE, 'utf-8');
return JSON.parse(content);
}
} catch (e) {
process.stderr.write(`Failed to read registry: ${e.message}\n`);
}
return { windows: [] };
}
function readPidRegistry(extensionHostPid) {
try {
if (!extensionHostPid) return null;
const f = path.join(REGISTRY_DIR, `port-${extensionHostPid}.json`);
if (!fs.existsSync(f)) return null;
const content = fs.readFileSync(f, 'utf-8');
return JSON.parse(content);
} catch (e) {
process.stderr.write(`Failed to read pid registry: ${e.message}\n`);
return null;
}
}
function getParentPid(pid) {
try {
const result = execSync(`ps -o ppid= -p ${pid}`, { encoding: 'utf-8' });
const parentPid = result.trim();
return parentPid;
} catch {
return null;
}
}
function findPidRegistryOwnerPid() {
let cur = process.ppid;
for (let i = 0; i < 8 && cur; i++) {
const f = path.join(REGISTRY_DIR, `port-${cur}.json`);
if (fs.existsSync(f)) {
return String(cur);
}
const next = getParentPid(cur);
if (!next) return null;
cur = next;
}
return null;
}
// 根据 Extension Host PID、VSCODE_PID、工作区或最近活动时间选择目标端口
function selectTargetPort(workspace) {
const ownerPid = findPidRegistryOwnerPid();
if (ownerPid) {
const pidReg = readPidRegistry(ownerPid);
if (pidReg && pidReg.port) {
process.stderr.write(`Matched by pid registry: ${ownerPid} -> port ${pidReg.port}\n`);
return pidReg.port;
}
}
const registry = readRegistry();
if (registry.windows.length === 0) {
return DEFAULT_PORT; // 回退到默认端口
}
// 策略 2:使用 VSCODE_PID 匹配(支持多窗口)
const vscodePid = process.env.VSCODE_PID;
if (vscodePid) {
const entry = registry.windows.find(e => e.vscodePid === vscodePid);
if (entry) {
process.stderr.write(`Matched by VSCODE_PID: ${vscodePid} -> port ${entry.port}\n`);
return entry.port;
}
}
// 策略 3:如果指定了工作区,精确匹配
if (workspace) {
const entry = registry.windows.find(e => e.workspace === workspace);
if (entry) {
process.stderr.write(`Matched by workspace: ${workspace} -> port ${entry.port}\n`);
return entry.port;
}
}
// 策略 4:否则选择最近活动的窗口
const sorted = [...registry.windows].sort((a, b) => b.lastActive - a.lastActive);
process.stderr.write(`Fallback to most recent: port ${sorted[0].port}\n`);
return sorted[0].port;
}
// 生成唯一请求 ID
function generateRequestId() {
return crypto.randomUUID();
}
// 休眠函数
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// 发送 HTTP 请求
function sendRequest(urlPath, data, targetPort) {
const port = targetPort || DEFAULT_PORT;
return new Promise((resolve, reject) => {
const postData = JSON.stringify(data);
const options = {
hostname: '127.0.0.1',
port: port,
path: urlPath,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
},
timeout: 5000 // 短超时,快速失败
};
const req = http.request(options, (res) => {
let body = '';
res.on('data', chunk => body += chunk);
res.on('end', () => {
try {
resolve(JSON.parse(body));
} catch (e) {
reject(new Error('Invalid JSON response'));
}
});
});
req.on('error', (e) => {
if (e.code === 'ECONNREFUSED') {
// 服务器未启动,返回特殊标记
resolve({ _connectionRefused: true });
} else {
reject(e);
}
});
req.on('timeout', () => {
req.destroy();
reject(new Error('Request timeout'));
});
req.write(postData);
req.end();
});
}
// 轮询获取结果
async function pollForResult(requestId, targetPort) {
const startTime = Date.now();
let connectionRefusedCount = 0;
while (Date.now() - startTime < MAX_POLL_TIME) {
try {
const result = await sendRequest('/poll', { requestId }, targetPort);
// 检查连接被拒绝
if (result._connectionRefused) {
connectionRefusedCount++;
// 连续多次连接失败才报错
if (connectionRefusedCount > 10) {
throw new Error('扩展未启动。请先在 Windsurf 中打开 AI Feedback 面板。');
}
} else {
connectionRefusedCount = 0; // 重置计数
if (result.status === 'completed') {
return result.data;
} else if (result.status === 'error') {
throw new Error(result.error || 'Unknown error');
}
// status === 'pending',继续轮询
}
} catch (err) {
// 非连接拒绝的错误,记录但继续轮询
if (!err.message.includes('扩展未启动')) {
process.stderr.write(`Poll error: ${err.message}\n`);
} else {
throw err; // 连接拒绝太多次,抛出错误
}
}
await sleep(POLL_INTERVAL);
}
throw new Error('Poll timeout after 7 days');
}
// 写调试日志到文件
function writeDebugLog(content) {
try {
const logDir = path.join(os.homedir(), '.panel-feedback');
const logFile = path.join(logDir, 'debug.log');
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir, { recursive: true });
}
const timestamp = new Date().toISOString();
fs.appendFileSync(logFile, `[${timestamp}] ${content}\n`);
} catch (e) {
// ignore
}
}
// 处理 tools/call 请求(使用轮询)
async function handleToolCall(mcpId, params) {
const requestId = generateRequestId();
// 调试日志:写入文件
writeDebugLog('=== MCP Call Debug ===');
writeDebugLog(`Params: ${JSON.stringify(params, null, 2)}`);
writeDebugLog(`PID: ${process.pid}, PPID: ${process.ppid}`);
writeDebugLog(`Pid registry owner PID: ${findPidRegistryOwnerPid() || 'unknown'}`);
writeDebugLog(`CWD: ${process.cwd()}`);
writeDebugLog(`PWD: ${process.env.PWD || 'not set'}`);
writeDebugLog(`HOME: ${process.env.HOME || 'not set'}`);
// 检查是否有 workspace 相关的环境变量
const workspaceEnvs = Object.entries(process.env).filter(([k]) =>
k.toLowerCase().includes('workspace') ||
k.toLowerCase().includes('folder') ||
k.toLowerCase().includes('project') ||
k.toLowerCase().includes('vscode') ||
k.toLowerCase().includes('windsurf')
);
writeDebugLog(`Workspace-related envs: ${JSON.stringify(Object.fromEntries(workspaceEnvs), null, 2)}`);
writeDebugLog('======================');
// 从参数中提取 workspace(如果有)
const workspace = params?.arguments?.workspace || '';
const targetPort = selectTargetPort(workspace);
writeDebugLog(`Selected target port: ${targetPort}`);
process.stderr.write(`Routing to port ${targetPort}${workspace ? ` (workspace: ${workspace})` : ' (most recent)'}\n`);
// 1. 提交请求(快速返回)
const submitResult = await sendRequest('/submit', {
requestId,
params
}, targetPort);
if (submitResult._connectionRefused) {
return {
jsonrpc: '2.0',
id: mcpId,
error: {
code: -32000,
message: '扩展未启动。请先在 Windsurf 中打开 AI Feedback 面板。'
}
};
}
if (submitResult.error) {
return {
jsonrpc: '2.0',
id: mcpId,
error: { code: -32000, message: submitResult.error }
};
}
// 2. 轮询等待结果
try {
const result = await pollForResult(requestId, targetPort);
return {
jsonrpc: '2.0',
id: mcpId,
result
};
} catch (err) {
return {
jsonrpc: '2.0',
id: mcpId,
error: { code: -32000, message: err.message }
};
}
}
// 处理其他 MCP 请求(直接转发)
async function handleOtherRequest(request, workspace) {
const targetPort = selectTargetPort(workspace);
const result = await sendRequest('/', request, targetPort);
if (result._connectionRefused) {
return {
jsonrpc: '2.0',
id: request.id,
error: {
code: -32000,
message: '扩展未启动。请先在 Windsurf 中打开 AI Feedback 面板。'
}
};
}
return result;
}
function respond(response) {
console.log(JSON.stringify(response));
}
// 处理标准输入
rl.on('line', async (line) => {
try {
const request = JSON.parse(line);
const { id, method, params } = request;
let response;
// 本地处理 initialize(不依赖扩展)
if (method === 'initialize') {
response = {
jsonrpc: '2.0',
id,
result: {
protocolVersion: '2024-11-05',
serverInfo: { name: 'panel-feedback', version: '1.1.0' },
capabilities: { tools: {} }
}
};
}
// 本地处理 tools/list(不依赖扩展)
else if (method === 'tools/list') {
response = {
jsonrpc: '2.0',
id,
result: {
tools: [{
name: 'panel_feedback',
description: '在 IDE 侧边栏显示消息并获取用户反馈,支持预定义选项和图片上传',
inputSchema: {
type: 'object',
properties: {
message: {
type: 'string',
description: '要显示给用户的消息,支持 Markdown 格式'
},
predefined_options: {
type: 'array',
items: { type: 'string' },
description: '预定义的选项按钮列表'
},
workspace: {
type: 'string',
description: '目标工作区路径(可选,用于多窗口时精确路由)'
}
},
required: ['message']
}
}]
}
};
}
// 本地处理 notifications/initialized
else if (method === 'notifications/initialized') {
// 通知类请求不需要响应
return;
}
// tools/call 请求使用轮询机制(需要扩展)
else if (method === 'tools/call' && params?.name === 'panel_feedback') {
response = await handleToolCall(id, params);
}
// 其他请求本地处理
else {
response = { jsonrpc: '2.0', id, result: {} };
}
respond(response);
} catch (err) {
respond({
jsonrpc: '2.0',
id: null,
error: { code: -32700, message: 'Parse error: ' + err.message }
});
}
});
// 启动时输出就绪信息
process.stderr.write('windsurf-feedback-panel MCP wrapper started (polling mode)\n');