修复了一些挂载bug,将面板技术从纯html变更成了Vue3,界面更有好些,后期扩展更方便。增加了工具配置功能,可以自行选择需要使用的工具。
This commit is contained in:
176
source/main.ts
176
source/main.ts
@@ -1,8 +1,10 @@
|
||||
import { MCPServer } from './mcp-server';
|
||||
import { readSettings, saveSettings } from './settings';
|
||||
import { MCPServerSettings } from './types';
|
||||
import { ToolManager } from './tools/tool-manager';
|
||||
|
||||
let mcpServer: MCPServer | null = null;
|
||||
let toolManager: ToolManager;
|
||||
|
||||
/**
|
||||
* @en Registration method for the main process of Extension
|
||||
@@ -17,12 +19,17 @@ export const methods: { [key: string]: (...any: any) => any } = {
|
||||
Editor.Panel.open('cocos-mcp-server');
|
||||
},
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @en Start the MCP server
|
||||
* @zh 启动 MCP 服务器
|
||||
*/
|
||||
async startServer() {
|
||||
if (mcpServer) {
|
||||
// 确保使用最新的工具配置
|
||||
const enabledTools = toolManager.getEnabledTools();
|
||||
mcpServer.updateEnabledTools(enabledTools);
|
||||
await mcpServer.start();
|
||||
} else {
|
||||
console.warn('[MCP插件] mcpServer 未初始化');
|
||||
@@ -46,7 +53,12 @@ export const methods: { [key: string]: (...any: any) => any } = {
|
||||
* @zh 获取服务器状态
|
||||
*/
|
||||
getServerStatus() {
|
||||
return mcpServer ? mcpServer.getStatus() : { running: false, port: 0, clients: 0 };
|
||||
const status = mcpServer ? mcpServer.getStatus() : { running: false, port: 0, clients: 0 };
|
||||
const settings = mcpServer ? mcpServer.getSettings() : readSettings();
|
||||
return {
|
||||
...status,
|
||||
settings: settings
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -72,12 +84,136 @@ export const methods: { [key: string]: (...any: any) => any } = {
|
||||
getToolsList() {
|
||||
return mcpServer ? mcpServer.getAvailableTools() : [];
|
||||
},
|
||||
|
||||
getFilteredToolsList() {
|
||||
if (!mcpServer) return [];
|
||||
|
||||
// 获取当前启用的工具
|
||||
const enabledTools = toolManager.getEnabledTools();
|
||||
|
||||
// 更新MCP服务器的启用工具列表
|
||||
mcpServer.updateEnabledTools(enabledTools);
|
||||
|
||||
return mcpServer.getFilteredTools(enabledTools);
|
||||
},
|
||||
/**
|
||||
* @en Get server settings
|
||||
* @zh 获取服务器设置
|
||||
*/
|
||||
getServerSettings() {
|
||||
async getServerSettings() {
|
||||
return mcpServer ? mcpServer.getSettings() : readSettings();
|
||||
},
|
||||
|
||||
/**
|
||||
* @en Get server settings (alternative method)
|
||||
* @zh 获取服务器设置(替代方法)
|
||||
*/
|
||||
async getSettings() {
|
||||
return mcpServer ? mcpServer.getSettings() : readSettings();
|
||||
},
|
||||
|
||||
// 工具管理器相关方法
|
||||
async getToolManagerState() {
|
||||
return toolManager.getToolManagerState();
|
||||
},
|
||||
|
||||
async createToolConfiguration(name: string, description?: string) {
|
||||
try {
|
||||
const config = toolManager.createConfiguration(name, description);
|
||||
return { success: true, id: config.id, config };
|
||||
} catch (error: any) {
|
||||
throw new Error(`创建配置失败: ${error.message}`);
|
||||
}
|
||||
},
|
||||
|
||||
async updateToolConfiguration(configId: string, updates: any) {
|
||||
try {
|
||||
return toolManager.updateConfiguration(configId, updates);
|
||||
} catch (error: any) {
|
||||
throw new Error(`更新配置失败: ${error.message}`);
|
||||
}
|
||||
},
|
||||
|
||||
async deleteToolConfiguration(configId: string) {
|
||||
try {
|
||||
toolManager.deleteConfiguration(configId);
|
||||
return { success: true };
|
||||
} catch (error: any) {
|
||||
throw new Error(`删除配置失败: ${error.message}`);
|
||||
}
|
||||
},
|
||||
|
||||
async setCurrentToolConfiguration(configId: string) {
|
||||
try {
|
||||
toolManager.setCurrentConfiguration(configId);
|
||||
return { success: true };
|
||||
} catch (error: any) {
|
||||
throw new Error(`设置当前配置失败: ${error.message}`);
|
||||
}
|
||||
},
|
||||
|
||||
async updateToolStatus(category: string, toolName: string, enabled: boolean) {
|
||||
try {
|
||||
const currentConfig = toolManager.getCurrentConfiguration();
|
||||
if (!currentConfig) {
|
||||
throw new Error('没有当前配置');
|
||||
}
|
||||
|
||||
toolManager.updateToolStatus(currentConfig.id, category, toolName, enabled);
|
||||
|
||||
// 更新MCP服务器的工具列表
|
||||
if (mcpServer) {
|
||||
const enabledTools = toolManager.getEnabledTools();
|
||||
mcpServer.updateEnabledTools(enabledTools);
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
} catch (error: any) {
|
||||
throw new Error(`更新工具状态失败: ${error.message}`);
|
||||
}
|
||||
},
|
||||
|
||||
async updateToolStatusBatch(updates: any[]) {
|
||||
try {
|
||||
console.log(`[Main] updateToolStatusBatch called with updates count:`, updates ? updates.length : 0);
|
||||
|
||||
const currentConfig = toolManager.getCurrentConfiguration();
|
||||
if (!currentConfig) {
|
||||
throw new Error('没有当前配置');
|
||||
}
|
||||
|
||||
toolManager.updateToolStatusBatch(currentConfig.id, updates);
|
||||
|
||||
// 更新MCP服务器的工具列表
|
||||
if (mcpServer) {
|
||||
const enabledTools = toolManager.getEnabledTools();
|
||||
mcpServer.updateEnabledTools(enabledTools);
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
} catch (error: any) {
|
||||
throw new Error(`批量更新工具状态失败: ${error.message}`);
|
||||
}
|
||||
},
|
||||
|
||||
async exportToolConfiguration(configId: string) {
|
||||
try {
|
||||
return { configJson: toolManager.exportConfiguration(configId) };
|
||||
} catch (error: any) {
|
||||
throw new Error(`导出配置失败: ${error.message}`);
|
||||
}
|
||||
},
|
||||
|
||||
async importToolConfiguration(configJson: string) {
|
||||
try {
|
||||
return toolManager.importConfiguration(configJson);
|
||||
} catch (error: any) {
|
||||
throw new Error(`导入配置失败: ${error.message}`);
|
||||
}
|
||||
},
|
||||
|
||||
async getEnabledTools() {
|
||||
return toolManager.getEnabledTools();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -86,24 +222,24 @@ export const methods: { [key: string]: (...any: any) => any } = {
|
||||
* @zh 扩展启动时触发的方法
|
||||
*/
|
||||
export function load() {
|
||||
console.log('[MCP Plugin] Loading MCP server plugin...');
|
||||
try {
|
||||
const settings = readSettings();
|
||||
console.log('[MCP Plugin] Settings loaded:', settings);
|
||||
mcpServer = new MCPServer(settings);
|
||||
|
||||
// 如果设置了自动启动,则启动服务器
|
||||
if (settings.autoStart) {
|
||||
console.log('[MCP Plugin] Auto-starting MCP server...');
|
||||
mcpServer.start().catch(error => {
|
||||
console.error('[MCP Plugin] Failed to auto-start server:', error);
|
||||
});
|
||||
} else {
|
||||
console.log('[MCP Plugin] MCP server created but not started (autoStart=false)');
|
||||
console.log('[MCP Plugin] Use the MCP panel or call startServer() to start the server');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[MCP Plugin] Failed to load MCP server:', error);
|
||||
console.log('Cocos MCP Server extension loaded');
|
||||
|
||||
// 初始化工具管理器
|
||||
toolManager = new ToolManager();
|
||||
|
||||
// 读取设置
|
||||
const settings = readSettings();
|
||||
mcpServer = new MCPServer(settings);
|
||||
|
||||
// 初始化MCP服务器的工具列表
|
||||
const enabledTools = toolManager.getEnabledTools();
|
||||
mcpServer.updateEnabledTools(enabledTools);
|
||||
|
||||
// 如果设置了自动启动,则启动服务器
|
||||
if (settings.autoStart) {
|
||||
mcpServer.start().catch(err => {
|
||||
console.error('Failed to auto-start MCP server:', err);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user