|
| 1 | +// ***************************************************************************** |
| 2 | +// Copyright (C) 2025 EclipseSource |
| 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 | + |
| 17 | +import { interfaces } from '@theia/core/shared/inversify'; |
| 18 | +import { RPCProtocol } from '../../common/rpc-protocol'; |
| 19 | +import { |
| 20 | + McpServerDefinitionRegistryMain, |
| 21 | + McpServerDefinitionRegistryExt, |
| 22 | + McpServerDefinitionDto, |
| 23 | + isMcpHttpServerDefinitionDto, |
| 24 | +} from '../../common/lm-protocol'; |
| 25 | +import { MAIN_RPC_CONTEXT } from '../../common/plugin-api-rpc'; |
| 26 | +import { MCPServerManager, MCPServerDescription } from '@theia/ai-mcp/lib/common'; |
| 27 | + |
| 28 | +export class McpServerDefinitionRegistryMainImpl implements McpServerDefinitionRegistryMain { |
| 29 | + private readonly proxy: McpServerDefinitionRegistryExt; |
| 30 | + private readonly providers = new Map<number, string>(); |
| 31 | + private readonly mcpServerManager: MCPServerManager | undefined; |
| 32 | + |
| 33 | + constructor( |
| 34 | + rpc: RPCProtocol, |
| 35 | + container: interfaces.Container |
| 36 | + ) { |
| 37 | + this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.MCP_SERVER_DEFINITION_REGISTRY_EXT); |
| 38 | + try { |
| 39 | + this.mcpServerManager = container.get(MCPServerManager); |
| 40 | + } catch { |
| 41 | + // MCP Server Manager is optional |
| 42 | + this.mcpServerManager = undefined; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + $registerMcpServerDefinitionProvider(handle: number, name: string): void { |
| 47 | + this.providers.set(handle, name); |
| 48 | + this.loadServerDefinitions(handle); |
| 49 | + } |
| 50 | + |
| 51 | + $unregisterMcpServerDefinitionProvider(handle: number): void { |
| 52 | + if (!this.mcpServerManager) { |
| 53 | + console.warn('MCP Server Manager not available - MCP server definitions will not be loaded'); |
| 54 | + return; |
| 55 | + } |
| 56 | + const provider = this.providers.get(handle); |
| 57 | + if (!provider) { |
| 58 | + console.warn(`No MCP Server provider found for handle '${handle}' - MCP server definitions will not be loaded`); |
| 59 | + return; |
| 60 | + } |
| 61 | + this.mcpServerManager.removeServer(provider); |
| 62 | + this.providers.delete(handle); |
| 63 | + } |
| 64 | + |
| 65 | + $onDidChangeMcpServerDefinitions(handle: number): void { |
| 66 | + // Reload server definitions when provider reports changes |
| 67 | + this.loadServerDefinitions(handle); |
| 68 | + } |
| 69 | + |
| 70 | + async $getServerDefinitions(handle: number): Promise<McpServerDefinitionDto[]> { |
| 71 | + try { |
| 72 | + return await this.proxy.$provideServerDefinitions(handle); |
| 73 | + } catch (error) { |
| 74 | + console.error('Error getting MCP server definitions:', error); |
| 75 | + return []; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + async $resolveServerDefinition(handle: number, server: McpServerDefinitionDto): Promise<McpServerDefinitionDto | undefined> { |
| 80 | + try { |
| 81 | + return await this.proxy.$resolveServerDefinition(handle, server); |
| 82 | + } catch (error) { |
| 83 | + console.error('Error resolving MCP server definition:', error); |
| 84 | + return server; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private async loadServerDefinitions(handle: number): Promise<void> { |
| 89 | + if (!this.mcpServerManager) { |
| 90 | + console.warn('MCP Server Manager not available - MCP server definitions will not be loaded'); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + try { |
| 95 | + const definitions = await this.$getServerDefinitions(handle); |
| 96 | + |
| 97 | + for (const definition of definitions) { |
| 98 | + const resolved = await this.$resolveServerDefinition(handle, definition); |
| 99 | + if (resolved) { |
| 100 | + const mcpServerDescription = this.convertToMcpServerDescription(resolved); |
| 101 | + this.mcpServerManager.addOrUpdateServer(mcpServerDescription); |
| 102 | + } |
| 103 | + } |
| 104 | + } catch (error) { |
| 105 | + console.error('Error loading MCP server definitions:', error); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private convertToMcpServerDescription(definition: McpServerDefinitionDto): MCPServerDescription { |
| 110 | + if (isMcpHttpServerDefinitionDto(definition)) { |
| 111 | + // For HTTP servers, we would need to create a bridge or adapter |
| 112 | + // For now, we'll create a placeholder stdio server that could proxy to HTTP |
| 113 | + console.warn(`HTTP transport not yet supported for MCP server '${definition.label}'. Skipping.`); |
| 114 | + throw new Error(`HTTP transport not yet supported for MCP server '${definition.label}'`); |
| 115 | + } |
| 116 | + |
| 117 | + // Convert env values to strings, filtering out null values |
| 118 | + let convertedEnv: Record<string, string> | undefined; |
| 119 | + if (definition.env) { |
| 120 | + convertedEnv = {}; |
| 121 | + for (const [key, value] of Object.entries(definition.env)) { |
| 122 | + if (value !== null) { |
| 123 | + convertedEnv[key] = String(value); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return { |
| 129 | + name: definition.label, |
| 130 | + command: definition.command!, |
| 131 | + args: definition.args, |
| 132 | + env: convertedEnv, |
| 133 | + autostart: false, // Extensions should manage their own server lifecycle |
| 134 | + }; |
| 135 | + } |
| 136 | +} |
0 commit comments