feat: 支持多实例与配置隔离,全面本地化测试面板
- 实现多实例支持:自动扫描可用端口 - 实现项目级配置隔离:配置存储于项目 settings 目录 - 更新测试面板:界面完全汉化,端口显示实时同步 - 本地化:main.js 日志与 IPC 测试模块全面中文化
This commit is contained in:
@@ -47,6 +47,8 @@
|
|||||||
|
|
||||||
- **端口**: 可以自定义 HTTP 服务监听的端口,默认为 3456
|
- **端口**: 可以自定义 HTTP 服务监听的端口,默认为 3456
|
||||||
- **自动启动**: 可以设置编辑器启动时自动开启服务
|
- **自动启动**: 可以设置编辑器启动时自动开启服务
|
||||||
|
- **多实例支持**: 如果默认端口 (3456) 被占用,插件会自动尝试端口+1 (如 3457),直到找到可用端口。
|
||||||
|
- **配置隔离**: 插件配置(是否自动启动、上次使用的端口)现已存储在项目目录 (`settings/mcp-bridge.json`) 中,不同项目的配置互不干扰。
|
||||||
|
|
||||||
## 连接 AI 编辑器
|
## 连接 AI 编辑器
|
||||||
|
|
||||||
|
|||||||
246
dist/IpcUi.js
vendored
246
dist/IpcUi.js
vendored
@@ -1,38 +1,135 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
var __awaiter =
|
||||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
(this && this.__awaiter) ||
|
||||||
|
function (thisArg, _arguments, P, generator) {
|
||||||
|
function adopt(value) {
|
||||||
|
return value instanceof P
|
||||||
|
? value
|
||||||
|
: new P(function (resolve) {
|
||||||
|
resolve(value);
|
||||||
|
});
|
||||||
|
}
|
||||||
return new (P || (P = Promise))(function (resolve, reject) {
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
function fulfilled(value) {
|
||||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
try {
|
||||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
step(generator.next(value));
|
||||||
|
} catch (e) {
|
||||||
|
reject(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function rejected(value) {
|
||||||
|
try {
|
||||||
|
step(generator["throw"](value));
|
||||||
|
} catch (e) {
|
||||||
|
reject(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function step(result) {
|
||||||
|
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
||||||
|
}
|
||||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
var __generator =
|
||||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
(this && this.__generator) ||
|
||||||
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
function (thisArg, body) {
|
||||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
var _ = {
|
||||||
|
label: 0,
|
||||||
|
sent: function () {
|
||||||
|
if (t[0] & 1) throw t[1];
|
||||||
|
return t[1];
|
||||||
|
},
|
||||||
|
trys: [],
|
||||||
|
ops: [],
|
||||||
|
},
|
||||||
|
f,
|
||||||
|
y,
|
||||||
|
t,
|
||||||
|
g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
||||||
|
return (
|
||||||
|
(g.next = verb(0)),
|
||||||
|
(g["throw"] = verb(1)),
|
||||||
|
(g["return"] = verb(2)),
|
||||||
|
typeof Symbol === "function" &&
|
||||||
|
(g[Symbol.iterator] = function () {
|
||||||
|
return this;
|
||||||
|
}),
|
||||||
|
g
|
||||||
|
);
|
||||||
|
function verb(n) {
|
||||||
|
return function (v) {
|
||||||
|
return step([n, v]);
|
||||||
|
};
|
||||||
|
}
|
||||||
function step(op) {
|
function step(op) {
|
||||||
if (f) throw new TypeError("Generator is already executing.");
|
if (f) throw new TypeError("Generator is already executing.");
|
||||||
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
while ((g && ((g = 0), op[0] && (_ = 0)), _))
|
||||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
try {
|
||||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
if (
|
||||||
|
((f = 1),
|
||||||
|
y &&
|
||||||
|
(t =
|
||||||
|
op[0] & 2
|
||||||
|
? y["return"]
|
||||||
|
: op[0]
|
||||||
|
? y["throw"] || ((t = y["return"]) && t.call(y), 0)
|
||||||
|
: y.next) &&
|
||||||
|
!(t = t.call(y, op[1])).done)
|
||||||
|
)
|
||||||
|
return t;
|
||||||
|
if (((y = 0), t)) op = [op[0] & 2, t.value];
|
||||||
switch (op[0]) {
|
switch (op[0]) {
|
||||||
case 0: case 1: t = op; break;
|
case 0:
|
||||||
case 4: _.label++; return { value: op[1], done: false };
|
case 1:
|
||||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
t = op;
|
||||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
break;
|
||||||
|
case 4:
|
||||||
|
_.label++;
|
||||||
|
return { value: op[1], done: false };
|
||||||
|
case 5:
|
||||||
|
_.label++;
|
||||||
|
y = op[1];
|
||||||
|
op = [0];
|
||||||
|
continue;
|
||||||
|
case 7:
|
||||||
|
op = _.ops.pop();
|
||||||
|
_.trys.pop();
|
||||||
|
continue;
|
||||||
default:
|
default:
|
||||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
if (
|
||||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
!((t = _.trys), (t = t.length > 0 && t[t.length - 1])) &&
|
||||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
(op[0] === 6 || op[0] === 2)
|
||||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
) {
|
||||||
|
_ = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
|
||||||
|
_.label = op[1];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (op[0] === 6 && _.label < t[1]) {
|
||||||
|
_.label = t[1];
|
||||||
|
t = op;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (t && _.label < t[2]) {
|
||||||
|
_.label = t[2];
|
||||||
|
_.ops.push(op);
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (t[2]) _.ops.pop();
|
if (t[2]) _.ops.pop();
|
||||||
_.trys.pop(); continue;
|
_.trys.pop();
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
op = body.call(thisArg, _);
|
op = body.call(thisArg, _);
|
||||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
} catch (e) {
|
||||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
op = [6, e];
|
||||||
|
y = 0;
|
||||||
|
} finally {
|
||||||
|
f = t = 0;
|
||||||
|
}
|
||||||
|
if (op[0] & 5) throw op[1];
|
||||||
|
return { value: op[0] ? op[1] : void 0, done: true };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
@@ -59,57 +156,66 @@ var IpcUi = /** @class */ (function () {
|
|||||||
this.filterSelect = this.root.querySelector("#ipcFilter");
|
this.filterSelect = this.root.querySelector("#ipcFilter");
|
||||||
this.paramInput = this.root.querySelector("#ipcParams");
|
this.paramInput = this.root.querySelector("#ipcParams");
|
||||||
if (btnScan) {
|
if (btnScan) {
|
||||||
btnScan.addEventListener("confirm", function () { return _this.scanMessages(); });
|
btnScan.addEventListener("confirm", function () {
|
||||||
|
return _this.scanMessages();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (btnTest) {
|
if (btnTest) {
|
||||||
btnTest.addEventListener("confirm", function () { return _this.testSelected(); });
|
btnTest.addEventListener("confirm", function () {
|
||||||
|
return _this.testSelected();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (cbSelectAll) {
|
if (cbSelectAll) {
|
||||||
cbSelectAll.addEventListener("change", function (e) { return _this.toggleAll(e.detail ? e.detail.value : (e.target.value === 'true' || e.target.checked)); });
|
cbSelectAll.addEventListener("change", function (e) {
|
||||||
|
return _this.toggleAll(e.detail ? e.detail.value : e.target.value === "true" || e.target.checked);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (this.filterSelect) {
|
if (this.filterSelect) {
|
||||||
this.filterSelect.addEventListener("change", function () { return _this.filterMessages(); });
|
this.filterSelect.addEventListener("change", function () {
|
||||||
|
return _this.filterMessages();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
IpcUi.prototype.scanMessages = function () {
|
IpcUi.prototype.scanMessages = function () {
|
||||||
var _this = this;
|
var _this = this;
|
||||||
this.log("Scanning IPC messages...");
|
this.log("正在扫描 IPC 消息...");
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
Editor.Ipc.sendToMain("mcp-bridge:scan-ipc-messages", function (err, msgs) {
|
Editor.Ipc.sendToMain("mcp-bridge:scan-ipc-messages", function (err, msgs) {
|
||||||
if (err) {
|
if (err) {
|
||||||
_this.log("Scan Error: ".concat(err));
|
_this.log("扫描错误: ".concat(err));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (msgs) {
|
if (msgs) {
|
||||||
_this.allMessages = msgs;
|
_this.allMessages = msgs;
|
||||||
_this.filterMessages();
|
_this.filterMessages();
|
||||||
_this.log("Found ".concat(msgs.length, " messages."));
|
_this.log("找到 ".concat(msgs.length, " 条消息。"));
|
||||||
}
|
} else {
|
||||||
else {
|
_this.log("未找到任何消息。");
|
||||||
_this.log("No messages found.");
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
IpcUi.prototype.filterMessages = function () {
|
IpcUi.prototype.filterMessages = function () {
|
||||||
if (!this.allMessages)
|
if (!this.allMessages) return;
|
||||||
return;
|
|
||||||
var filter = this.filterSelect ? this.filterSelect.value : "all";
|
var filter = this.filterSelect ? this.filterSelect.value : "all";
|
||||||
var filtered = this.allMessages;
|
var filtered = this.allMessages;
|
||||||
if (filter === "available") {
|
if (filter === "available") {
|
||||||
filtered = this.allMessages.filter(function (m) { return m.status === "可用"; });
|
filtered = this.allMessages.filter(function (m) {
|
||||||
}
|
return m.status === "可用";
|
||||||
else if (filter === "unavailable") {
|
});
|
||||||
filtered = this.allMessages.filter(function (m) { return m.status && m.status.includes("不可用"); });
|
} else if (filter === "unavailable") {
|
||||||
}
|
filtered = this.allMessages.filter(function (m) {
|
||||||
else if (filter === "untested") {
|
return m.status && m.status.includes("不可用");
|
||||||
filtered = this.allMessages.filter(function (m) { return !m.status || m.status === "未测试"; });
|
});
|
||||||
|
} else if (filter === "untested") {
|
||||||
|
filtered = this.allMessages.filter(function (m) {
|
||||||
|
return !m.status || m.status === "未测试";
|
||||||
|
});
|
||||||
}
|
}
|
||||||
this.renderList(filtered);
|
this.renderList(filtered);
|
||||||
};
|
};
|
||||||
IpcUi.prototype.renderList = function (msgs) {
|
IpcUi.prototype.renderList = function (msgs) {
|
||||||
var _this = this;
|
var _this = this;
|
||||||
if (!this.ipcList)
|
if (!this.ipcList) return;
|
||||||
return;
|
|
||||||
this.ipcList.innerHTML = "";
|
this.ipcList.innerHTML = "";
|
||||||
msgs.forEach(function (msg) {
|
msgs.forEach(function (msg) {
|
||||||
var item = document.createElement("div");
|
var item = document.createElement("div");
|
||||||
@@ -131,12 +237,24 @@ var IpcUi = /** @class */ (function () {
|
|||||||
var statusColor = "#888"; // Untested
|
var statusColor = "#888"; // Untested
|
||||||
if (msg.status === "可用")
|
if (msg.status === "可用")
|
||||||
statusColor = "#4CAF50"; // Green
|
statusColor = "#4CAF50"; // Green
|
||||||
else if (msg.status && msg.status.includes("不可用"))
|
else if (msg.status && msg.status.includes("不可用")) statusColor = "#F44336"; // Red
|
||||||
statusColor = "#F44336"; // Red
|
content.innerHTML =
|
||||||
content.innerHTML = "\n <div style=\"display:flex; justify-content:space-between;\">\n <span style=\"color: #4CAF50; font-weight: bold;\">".concat(msg.name, "</span>\n <span style=\"color: ").concat(statusColor, "; font-size: 10px; border: 1px solid ").concat(statusColor, "; padding: 0 4px; border-radius: 4px;\">").concat(msg.status || "未测试", "</span>\n </div>\n <div style=\"color: #888;\">").concat(msg.description || "No desc", "</div>\n <div style=\"color: #666; font-size: 10px;\">Params: ").concat(msg.params || "None", "</div>\n ");
|
'\n <div style="display:flex; justify-content:space-between;">\n <span style="color: #4CAF50; font-weight: bold;">'
|
||||||
|
.concat(msg.name, '</span>\n <span style="color: ')
|
||||||
|
.concat(statusColor, "; font-size: 10px; border: 1px solid ")
|
||||||
|
.concat(statusColor, '; padding: 0 4px; border-radius: 4px;">')
|
||||||
|
.concat(
|
||||||
|
msg.status || "未测试",
|
||||||
|
'</span>\n </div>\n <div style="color: #888;">',
|
||||||
|
)
|
||||||
|
.concat(
|
||||||
|
msg.description || "无描述",
|
||||||
|
'</div>\n <div style="color: #666; font-size: 10px;">参数: ',
|
||||||
|
)
|
||||||
|
.concat(msg.params || "无", "</div>\n ");
|
||||||
// Action Button
|
// Action Button
|
||||||
var btnRun = document.createElement("ui-button");
|
var btnRun = document.createElement("ui-button");
|
||||||
btnRun.innerText = "Run";
|
btnRun.innerText = "执行";
|
||||||
btnRun.className = "tiny";
|
btnRun.className = "tiny";
|
||||||
btnRun.style.height = "20px";
|
btnRun.style.height = "20px";
|
||||||
btnRun.style.lineHeight = "20px";
|
btnRun.style.lineHeight = "20px";
|
||||||
@@ -164,11 +282,11 @@ var IpcUi = /** @class */ (function () {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (toTest.length === 0) {
|
if (toTest.length === 0) {
|
||||||
this.log("No messages selected.");
|
this.log("未选择任何消息。");
|
||||||
return [2 /*return*/];
|
return [2 /*return*/];
|
||||||
}
|
}
|
||||||
this.log("Starting batch test for ".concat(toTest.length, " messages..."));
|
this.log("开始批量测试 ".concat(toTest.length, " 条消息..."));
|
||||||
_i = 0, toTest_1 = toTest;
|
((_i = 0), (toTest_1 = toTest));
|
||||||
_a.label = 1;
|
_a.label = 1;
|
||||||
case 1:
|
case 1:
|
||||||
if (!(_i < toTest_1.length)) return [3 /*break*/, 4];
|
if (!(_i < toTest_1.length)) return [3 /*break*/, 4];
|
||||||
@@ -181,7 +299,7 @@ var IpcUi = /** @class */ (function () {
|
|||||||
_i++;
|
_i++;
|
||||||
return [3 /*break*/, 1];
|
return [3 /*break*/, 1];
|
||||||
case 4:
|
case 4:
|
||||||
this.log("Batch test completed.");
|
this.log("批量测试完成。");
|
||||||
return [2 /*return*/];
|
return [2 /*return*/];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -194,24 +312,26 @@ var IpcUi = /** @class */ (function () {
|
|||||||
if (_this.paramInput && _this.paramInput.value.trim()) {
|
if (_this.paramInput && _this.paramInput.value.trim()) {
|
||||||
try {
|
try {
|
||||||
params = JSON.parse(_this.paramInput.value.trim());
|
params = JSON.parse(_this.paramInput.value.trim());
|
||||||
}
|
} catch (e) {
|
||||||
catch (e) {
|
_this.log("[错误] 无效的 JSON 参数: ".concat(e));
|
||||||
_this.log("[Error] Invalid JSON Params: ".concat(e));
|
|
||||||
resolve();
|
resolve();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_this.log("Testing: ".concat(name, " with params: ").concat(JSON.stringify(params), "..."));
|
_this.log("正在测试: ".concat(name, ",参数: ").concat(JSON.stringify(params), "..."));
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
Editor.Ipc.sendToMain("mcp-bridge:test-ipc-message", { name: name, params: params }, function (err, result) {
|
Editor.Ipc.sendToMain(
|
||||||
|
"mcp-bridge:test-ipc-message",
|
||||||
|
{ name: name, params: params },
|
||||||
|
function (err, result) {
|
||||||
if (err) {
|
if (err) {
|
||||||
_this.log("[".concat(name, "] Failed: ").concat(err));
|
_this.log("[".concat(name, "] 失败: ").concat(err));
|
||||||
}
|
} else {
|
||||||
else {
|
_this.log("[".concat(name, "] 成功: ").concat(JSON.stringify(result)));
|
||||||
_this.log("[".concat(name, "] Success: ").concat(JSON.stringify(result)));
|
|
||||||
}
|
}
|
||||||
resolve();
|
resolve();
|
||||||
});
|
},
|
||||||
|
);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
IpcUi.prototype.toggleAll = function (checked) {
|
IpcUi.prototype.toggleAll = function (checked) {
|
||||||
@@ -229,5 +349,5 @@ var IpcUi = /** @class */ (function () {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
return IpcUi;
|
return IpcUi;
|
||||||
}());
|
})();
|
||||||
exports.IpcUi = IpcUi;
|
exports.IpcUi = IpcUi;
|
||||||
|
|||||||
83
main.js
83
main.js
@@ -704,8 +704,8 @@ module.exports = {
|
|||||||
* @returns {Object} Editor.Profile 实例
|
* @returns {Object} Editor.Profile 实例
|
||||||
*/
|
*/
|
||||||
getProfile() {
|
getProfile() {
|
||||||
// 'local' 表示存储在项目本地(local/mcp-bridge.json)
|
// 'project' 表示存储在项目本地(settings/mcp-bridge.json),实现配置隔离
|
||||||
return Editor.Profile.load("profile://local/mcp-bridge.json", "mcp-bridge");
|
return Editor.Profile.load("profile://project/mcp-bridge.json", "mcp-bridge");
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -721,8 +721,53 @@ module.exports = {
|
|||||||
startServer(port) {
|
startServer(port) {
|
||||||
if (mcpServer) this.stopServer();
|
if (mcpServer) this.stopServer();
|
||||||
|
|
||||||
|
const tryStart = (currentPort, retries) => {
|
||||||
|
if (retries <= 0) {
|
||||||
|
addLog("error", `Failed to find an available port after multiple attempts.`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
mcpServer = http.createServer((req, res) => {
|
mcpServer = http.createServer((req, res) => {
|
||||||
|
this._handleRequest(req, res);
|
||||||
|
});
|
||||||
|
|
||||||
|
mcpServer.on("error", (e) => {
|
||||||
|
if (e.code === "EADDRINUSE") {
|
||||||
|
addLog("warn", `Port ${currentPort} is in use, trying ${currentPort + 1}...`);
|
||||||
|
try {
|
||||||
|
mcpServer.close();
|
||||||
|
} catch (err) {
|
||||||
|
// align
|
||||||
|
}
|
||||||
|
mcpServer = null;
|
||||||
|
// Delay slightly to ensure cleanup
|
||||||
|
setTimeout(() => {
|
||||||
|
tryStart(currentPort + 1, retries - 1);
|
||||||
|
}, 100);
|
||||||
|
} else {
|
||||||
|
addLog("error", `Server Error: ${e.message}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
mcpServer.listen(currentPort, () => {
|
||||||
|
serverConfig.active = true;
|
||||||
|
serverConfig.port = currentPort;
|
||||||
|
addLog("success", `MCP Server running at http://127.0.0.1:${currentPort}`);
|
||||||
|
Editor.Ipc.sendToPanel("mcp-bridge", "mcp-bridge:state-changed", serverConfig);
|
||||||
|
|
||||||
|
// Important: Do NOT save the auto-assigned port to profile to avoid pollution
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
addLog("error", `Failed to start server: ${e.message}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Start trying from the configured port, retry 10 times
|
||||||
|
tryStart(port, 10);
|
||||||
|
},
|
||||||
|
|
||||||
|
_handleRequest(req, res) {
|
||||||
res.setHeader("Content-Type", "application/json");
|
res.setHeader("Content-Type", "application/json");
|
||||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||||
|
|
||||||
@@ -735,7 +780,6 @@ module.exports = {
|
|||||||
if (url === "/list-tools") {
|
if (url === "/list-tools") {
|
||||||
const tools = getToolsList();
|
const tools = getToolsList();
|
||||||
addLog("info", `AI Client requested tool list`);
|
addLog("info", `AI Client requested tool list`);
|
||||||
// 明确返回成功结构
|
|
||||||
res.writeHead(200);
|
res.writeHead(200);
|
||||||
return res.end(JSON.stringify({ tools: tools }));
|
return res.end(JSON.stringify({ tools: tools }));
|
||||||
}
|
}
|
||||||
@@ -757,7 +801,6 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
addLog("success", `读取成功: ${uri}`);
|
addLog("success", `读取成功: ${uri}`);
|
||||||
res.writeHead(200);
|
res.writeHead(200);
|
||||||
// 返回 MCP Resource 格式: { contents: [{ uri, mimeType, text }] }
|
|
||||||
res.end(
|
res.end(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
contents: [
|
contents: [
|
||||||
@@ -781,8 +824,6 @@ module.exports = {
|
|||||||
const { name, arguments: args } = JSON.parse(body || "{}");
|
const { name, arguments: args } = JSON.parse(body || "{}");
|
||||||
addLog("mcp", `REQ -> [${name}] (队列长度: ${commandQueue.length})`);
|
addLog("mcp", `REQ -> [${name}] (队列长度: ${commandQueue.length})`);
|
||||||
|
|
||||||
// 【关键修复】所有 MCP 指令通过队列串行化执行,
|
|
||||||
// 防止 AssetDB.refresh 等异步操作被并发请求打断导致编辑器卡死
|
|
||||||
enqueueCommand((done) => {
|
enqueueCommand((done) => {
|
||||||
this.handleMcpCall(name, args, (err, result) => {
|
this.handleMcpCall(name, args, (err, result) => {
|
||||||
const response = {
|
const response = {
|
||||||
@@ -800,15 +841,13 @@ module.exports = {
|
|||||||
if (err) {
|
if (err) {
|
||||||
addLog("error", `RES <- [${name}] 失败: ${err}`);
|
addLog("error", `RES <- [${name}] 失败: ${err}`);
|
||||||
} else {
|
} else {
|
||||||
// 成功时尝试捕获简单的结果预览(如果是字符串或简短对象)
|
|
||||||
let preview = "";
|
let preview = "";
|
||||||
if (typeof result === "string") {
|
if (typeof result === "string") {
|
||||||
preview = result.length > 100 ? result.substring(0, 100) + "..." : result;
|
preview = result.length > 100 ? result.substring(0, 100) + "..." : result;
|
||||||
} else if (typeof result === "object") {
|
} else if (typeof result === "object") {
|
||||||
try {
|
try {
|
||||||
const jsonStr = JSON.stringify(result);
|
const jsonStr = JSON.stringify(result);
|
||||||
preview =
|
preview = jsonStr.length > 100 ? jsonStr.substring(0, 100) + "..." : jsonStr;
|
||||||
jsonStr.length > 100 ? jsonStr.substring(0, 100) + "..." : jsonStr;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
preview = "Object (Circular/Unserializable)";
|
preview = "Object (Circular/Unserializable)";
|
||||||
}
|
}
|
||||||
@@ -817,7 +856,7 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
res.writeHead(200);
|
res.writeHead(200);
|
||||||
res.end(JSON.stringify(response));
|
res.end(JSON.stringify(response));
|
||||||
done(); // 当前指令完成,释放队列给下一个指令
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -834,26 +873,9 @@ module.exports = {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- 兜底处理 (404) ---
|
|
||||||
res.writeHead(404);
|
res.writeHead(404);
|
||||||
res.end(JSON.stringify({ error: "Not Found", url: url }));
|
res.end(JSON.stringify({ error: "Not Found", url: url }));
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
mcpServer.on("error", (e) => {
|
|
||||||
addLog("error", `Server Error: ${e.message}`);
|
|
||||||
});
|
|
||||||
mcpServer.listen(port, () => {
|
|
||||||
serverConfig.active = true;
|
|
||||||
addLog("success", `MCP Server running at http://127.0.0.1:${port}`);
|
|
||||||
Editor.Ipc.sendToPanel("mcp-bridge", "mcp-bridge:state-changed", serverConfig);
|
|
||||||
});
|
|
||||||
// 启动成功后顺便存一下端口
|
|
||||||
this.getProfile().set("last-port", port);
|
|
||||||
this.getProfile().save();
|
|
||||||
} catch (e) {
|
|
||||||
addLog("error", `Failed to start server: ${e.message}`);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2273,7 +2295,12 @@ CCProgram fs %{
|
|||||||
|
|
||||||
"toggle-server"(event, port) {
|
"toggle-server"(event, port) {
|
||||||
if (serverConfig.active) this.stopServer();
|
if (serverConfig.active) this.stopServer();
|
||||||
else this.startServer(port);
|
else {
|
||||||
|
// 用户手动启动时,保存偏好端口
|
||||||
|
this.getProfile().set("last-port", port);
|
||||||
|
this.getProfile().save();
|
||||||
|
this.startServer(port);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"clear-logs"() {
|
"clear-logs"() {
|
||||||
logBuffer = [];
|
logBuffer = [];
|
||||||
|
|||||||
@@ -1,23 +1,23 @@
|
|||||||
<div class="mcp-container">
|
<div class="mcp-container">
|
||||||
<div class="tabs">
|
<div class="tabs">
|
||||||
<ui-button id="tabMain" class="tab-button active">Main</ui-button>
|
<ui-button id="tabMain" class="tab-button active">主页</ui-button>
|
||||||
<ui-button id="tabTest" class="tab-button">Tool Test</ui-button>
|
<ui-button id="tabTest" class="tab-button">工具测试</ui-button>
|
||||||
<ui-button id="tabIpc" class="tab-button">IPC Test</ui-button>
|
<ui-button id="tabIpc" class="tab-button">IPC 测试</ui-button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="panelMain" class="tab-content active">
|
<div id="panelMain" class="tab-content active">
|
||||||
<div class="toolbar">
|
<div class="toolbar">
|
||||||
<div class="ctrl-group">
|
<div class="ctrl-group">
|
||||||
<span>Port:</span>
|
<span>端口:</span>
|
||||||
<ui-input id="portInput" style="width: 60px;"></ui-input>
|
<ui-input id="portInput" style="width: 60px"></ui-input>
|
||||||
<ui-button id="btnToggle">Start</ui-button>
|
<ui-button id="btnToggle">启动</ui-button>
|
||||||
</div>
|
</div>
|
||||||
<div class="ctrl-group" style="margin-left: 10px">
|
<div class="ctrl-group" style="margin-left: 10px">
|
||||||
<ui-checkbox id="autoStartCheck">Auto Start</ui-checkbox>
|
<ui-checkbox id="autoStartCheck">自动启动</ui-checkbox>
|
||||||
</div>
|
</div>
|
||||||
<div class="spacer"></div>
|
<div class="spacer"></div>
|
||||||
<ui-button id="btnClear" class="transparent">Clear</ui-button>
|
<ui-button id="btnClear" class="transparent">清空日志</ui-button>
|
||||||
<ui-button id="btnCopy" class="transparent">Copy All</ui-button>
|
<ui-button id="btnCopy" class="transparent">复制全部</ui-button>
|
||||||
</div>
|
</div>
|
||||||
<div id="logConsole" class="log-view"></div>
|
<div id="logConsole" class="log-view"></div>
|
||||||
</div>
|
</div>
|
||||||
@@ -44,7 +44,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="flex-v">
|
<div class="flex-v">
|
||||||
<label>工具参数 (JSON):</label>
|
<label>工具参数 (JSON):</label>
|
||||||
<textarea id="toolParams" spellcheck="false" placeholder='{}'></textarea>
|
<textarea id="toolParams" spellcheck="false" placeholder="{}"></textarea>
|
||||||
</div>
|
</div>
|
||||||
<div class="button-group">
|
<div class="button-group">
|
||||||
<ui-button id="testBtn" class="green">测试工具</ui-button>
|
<ui-button id="testBtn" class="green">测试工具</ui-button>
|
||||||
@@ -62,32 +62,55 @@
|
|||||||
|
|
||||||
<div id="panelIpc" class="tab-content">
|
<div id="panelIpc" class="tab-content">
|
||||||
<div class="toolbar">
|
<div class="toolbar">
|
||||||
<ui-button id="btnScanIpc">Scan Messages</ui-button>
|
<ui-button id="btnScanIpc">扫描消息</ui-button>
|
||||||
<select id="ipcFilter"
|
<select
|
||||||
style="margin-left: 5px; background: #333; color: #ccc; border: 1px solid #555; height: 25px;">
|
id="ipcFilter"
|
||||||
<option value="all">Check All</option>
|
style="margin-left: 5px; background: #333; color: #ccc; border: 1px solid #555; height: 25px"
|
||||||
<option value="available">Show Available</option>
|
>
|
||||||
<option value="unavailable">Show Unavailable</option>
|
<option value="all">全部显示</option>
|
||||||
<option value="untested">Show Untested</option>
|
<option value="available">仅显示可用</option>
|
||||||
|
<option value="unavailable">仅显示不可用</option>
|
||||||
|
<option value="untested">仅显示未测试</option>
|
||||||
</select>
|
</select>
|
||||||
<div class="spacer"></div>
|
<div class="spacer"></div>
|
||||||
<ui-button id="btnTestIpc" class="green">Test Selected</ui-button>
|
<ui-button id="btnTestIpc" class="green">测试选中项</ui-button>
|
||||||
</div>
|
</div>
|
||||||
<div class="ipc-container" style="display:flex; flex:1; flex-direction:column; min-height:0;">
|
<div class="ipc-container" style="display: flex; flex: 1; flex-direction: column; min-height: 0">
|
||||||
<div class="ipc-list-header" style="padding: 5px; background: #222; border-bottom: 1px solid #444;">
|
<div class="ipc-list-header" style="padding: 5px; background: #222; border-bottom: 1px solid #444">
|
||||||
<ui-checkbox id="cbSelectAllIpc">Select All/None</ui-checkbox>
|
<ui-checkbox id="cbSelectAllIpc">全选/反选</ui-checkbox>
|
||||||
</div>
|
|
||||||
<div id="ipcList" class="ipc-list"
|
|
||||||
style="flex:1; overflow-y:auto; background: #1e1e1e; border: 1px solid #444;"></div>
|
|
||||||
<div style="padding: 5px; border-top: 1px solid #444; display: flex; flex-direction: column;">
|
|
||||||
<label>Parameters (JSON):</label>
|
|
||||||
<textarea id="ipcParams" placeholder='e.g. {"uuid": "..."}'
|
|
||||||
style="height: 50px; width: 100%; box-sizing: border-box; background: #222; color: #ccc; border: 1px solid #444; margin-bottom: 5px;"></textarea>
|
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div
|
||||||
style="height: 100px; display:flex; flex-direction:column; border-top: 1px solid #444; padding-top: 5px;">
|
id="ipcList"
|
||||||
<label>Test Log:</label>
|
class="ipc-list"
|
||||||
<textarea id="ipcLog" readonly style="flex:1; font-family:monospace; font-size:10px;"></textarea>
|
style="flex: 1; overflow-y: auto; background: #1e1e1e; border: 1px solid #444"
|
||||||
|
></div>
|
||||||
|
<div style="padding: 5px; border-top: 1px solid #444; display: flex; flex-direction: column">
|
||||||
|
<label>参数 (JSON):</label>
|
||||||
|
<textarea
|
||||||
|
id="ipcParams"
|
||||||
|
placeholder='例如: {"uuid": "..."}'
|
||||||
|
style="
|
||||||
|
height: 50px;
|
||||||
|
width: 100%;
|
||||||
|
box-sizing: border-box;
|
||||||
|
background: #222;
|
||||||
|
color: #ccc;
|
||||||
|
border: 1px solid #444;
|
||||||
|
margin-bottom: 5px;
|
||||||
|
"
|
||||||
|
></textarea>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
style="
|
||||||
|
height: 100px;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
border-top: 1px solid #444;
|
||||||
|
padding-top: 5px;
|
||||||
|
"
|
||||||
|
>
|
||||||
|
<label>测试日志:</label>
|
||||||
|
<textarea id="ipcLog" readonly style="flex: 1; font-family: monospace; font-size: 10px"></textarea>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -124,7 +147,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.tab-button.active {
|
.tab-button.active {
|
||||||
background: #4CAF50;
|
background: #4caf50;
|
||||||
color: white;
|
color: white;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,6 +185,7 @@
|
|||||||
font-family: monospace;
|
font-family: monospace;
|
||||||
font-size: 11px;
|
font-size: 11px;
|
||||||
-webkit-user-select: text;
|
-webkit-user-select: text;
|
||||||
|
user-select: text;
|
||||||
min-height: 0;
|
min-height: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,7 +253,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.resizer:hover {
|
.resizer:hover {
|
||||||
background: #4CAF50;
|
background: #4caf50;
|
||||||
}
|
}
|
||||||
|
|
||||||
.right-panel {
|
.right-panel {
|
||||||
|
|||||||
@@ -39,6 +39,11 @@ Editor.Panel.extend({
|
|||||||
*/
|
*/
|
||||||
"mcp-bridge:state-changed"(event, config) {
|
"mcp-bridge:state-changed"(event, config) {
|
||||||
this.updateUI(config.active);
|
this.updateUI(config.active);
|
||||||
|
// 如果服务器已启动,更新面板显示的端口为实际运行端口
|
||||||
|
if (config.active && config.port) {
|
||||||
|
const portInput = this.shadowRoot.querySelector("#portInput");
|
||||||
|
if (portInput) portInput.value = config.port;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user