|
| 1 | +// ***************************************************************************** |
| 2 | +// Copyright (C) 2024 EclipseSource GmbH. |
| 3 | +// |
| 4 | +// This program and the accompanying materials are made available under the |
| 5 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | +// http://www.eclipse.org/legal/epl-2.0. |
| 7 | +// |
| 8 | +// This Source Code may also be made available under the following Secondary |
| 9 | +// Licenses when the conditions for such availability set forth in the Eclipse |
| 10 | +// Public License v. 2.0 are satisfied: GNU General Public License, version 2 |
| 11 | +// with the GNU Classpath Exception which is available at |
| 12 | +// https://www.gnu.org/software/classpath/license.html. |
| 13 | +// |
| 14 | +// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 |
| 15 | +// ***************************************************************************** |
| 16 | +import { AICommandHandlerFactory } from '@theia/ai-core/lib/browser/ai-command-handler-factory'; |
| 17 | +import { CommandContribution, CommandRegistry, MessageService } from '@theia/core'; |
| 18 | +import { QuickInputService } from '@theia/core/lib/browser'; |
| 19 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 20 | +import { MCPServerManager } from '../common/mcp-server-manager'; |
| 21 | +import { ToolInvocationRegistry, ToolRequest } from '@theia/ai-core'; |
| 22 | + |
| 23 | +export const StartMCPServer = { |
| 24 | + id: 'mcp.startserver', |
| 25 | + label: 'MCP: Start MCP Server', |
| 26 | +}; |
| 27 | +export const StopMCPServer = { |
| 28 | + id: 'mcp.stopserver', |
| 29 | + label: 'MCP: Stop MCP Server', |
| 30 | +}; |
| 31 | + |
| 32 | +@injectable() |
| 33 | +export class MCPCommandContribution implements CommandContribution { |
| 34 | + @inject(AICommandHandlerFactory) |
| 35 | + protected readonly commandHandlerFactory: AICommandHandlerFactory; |
| 36 | + |
| 37 | + @inject(QuickInputService) |
| 38 | + protected readonly quickInputService: QuickInputService; |
| 39 | + |
| 40 | + @inject(MessageService) |
| 41 | + protected messageService: MessageService; |
| 42 | + |
| 43 | + @inject(MCPServerManager) |
| 44 | + protected readonly mcpServerManager: MCPServerManager; |
| 45 | + |
| 46 | + @inject(ToolInvocationRegistry) |
| 47 | + protected readonly toolInvocationRegistry: ToolInvocationRegistry; |
| 48 | + |
| 49 | + |
| 50 | + private async getMCPServerSelection(serverNames: String[]): Promise<string | undefined> { |
| 51 | + if (!serverNames || serverNames.length === 0) { |
| 52 | + this.messageService.error('No MCP Servers configured.'); |
| 53 | + return undefined; |
| 54 | + } |
| 55 | + const options = serverNames.map(mcpServerName => ({ label: mcpServerName as string })); |
| 56 | + const result = await this.quickInputService.showQuickPick(options); |
| 57 | + if (!result) { |
| 58 | + return undefined; |
| 59 | + } |
| 60 | + return result.label; |
| 61 | + } |
| 62 | + |
| 63 | + registerCommands(commandRegistry: CommandRegistry): void { |
| 64 | + commandRegistry.registerCommand(StopMCPServer, this.commandHandlerFactory({ |
| 65 | + execute: async () => { |
| 66 | + try { |
| 67 | + const selection = await this.getMCPServerSelection(await this.mcpServerManager.getStartedServers()); |
| 68 | + if (!selection) { |
| 69 | + return; |
| 70 | + } |
| 71 | + this.toolInvocationRegistry.unregisterAllTools(`mcp_${selection}`); |
| 72 | + this.mcpServerManager.stopServer(selection); |
| 73 | + } catch (error) { |
| 74 | + console.error('Error while stopping MCP server:', error); |
| 75 | + } |
| 76 | + } |
| 77 | + })); |
| 78 | + |
| 79 | + commandRegistry.registerCommand(StartMCPServer, this.commandHandlerFactory({ |
| 80 | + execute: async () => { |
| 81 | + try { |
| 82 | + const selection = await this.getMCPServerSelection(await this.mcpServerManager.getServerNames()); |
| 83 | + if (!selection) { |
| 84 | + return; |
| 85 | + } |
| 86 | + this.mcpServerManager.startServer(selection); |
| 87 | + const { tools } = await this.mcpServerManager.getTools(selection); |
| 88 | + const toolRequests: ToolRequest[] = tools.map((tool: any) => this.convertToToolRequest(tool, selection)); |
| 89 | + |
| 90 | + for (const toolRequest of toolRequests) { |
| 91 | + this.toolInvocationRegistry.registerTool(toolRequest); |
| 92 | + } |
| 93 | + } catch (error) { |
| 94 | + console.error('Error while starting MCP server:', error); |
| 95 | + } |
| 96 | + } |
| 97 | + })); |
| 98 | + } |
| 99 | + |
| 100 | + convertToToolRequest(tool: any, serverName: string): ToolRequest { |
| 101 | + const id = `mcp_${serverName}_${tool.name}`; |
| 102 | + return { |
| 103 | + id: id, |
| 104 | + name: id, |
| 105 | + providerName: `mcp_${serverName}`, |
| 106 | + parameters: tool.inputSchema ? { |
| 107 | + type: tool.inputSchema.type, |
| 108 | + properties: tool.inputSchema.properties, |
| 109 | + } : undefined, |
| 110 | + description: tool.description, |
| 111 | + handler: async (arg_string: string) => { |
| 112 | + try { |
| 113 | + return await this.mcpServerManager.callTool(serverName, tool.name, arg_string); |
| 114 | + } catch (error) { |
| 115 | + console.error(`Error in tool handler for ${tool.name} on server ${serverName}:`, error); |
| 116 | + throw error; |
| 117 | + } |
| 118 | + }, |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments