-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathlm-main.ts
More file actions
136 lines (122 loc) · 5.46 KB
/
lm-main.ts
File metadata and controls
136 lines (122 loc) · 5.46 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
// *****************************************************************************
// Copyright (C) 2025 EclipseSource
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { interfaces } from '@theia/core/shared/inversify';
import { RPCProtocol } from '../../common/rpc-protocol';
import {
McpServerDefinitionRegistryMain,
McpServerDefinitionRegistryExt,
McpServerDefinitionDto,
isMcpHttpServerDefinitionDto,
} from '../../common/lm-protocol';
import { MAIN_RPC_CONTEXT } from '../../common/plugin-api-rpc';
import { MCPServerManager, MCPServerDescription } from '@theia/ai-mcp/lib/common';
export class McpServerDefinitionRegistryMainImpl implements McpServerDefinitionRegistryMain {
private readonly proxy: McpServerDefinitionRegistryExt;
private readonly providers = new Map<number, string>();
private readonly mcpServerManager: MCPServerManager | undefined;
constructor(
rpc: RPCProtocol,
container: interfaces.Container
) {
this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.MCP_SERVER_DEFINITION_REGISTRY_EXT);
try {
this.mcpServerManager = container.get(MCPServerManager);
} catch {
// MCP Server Manager is optional
this.mcpServerManager = undefined;
}
}
$registerMcpServerDefinitionProvider(handle: number, name: string): void {
this.providers.set(handle, name);
this.loadServerDefinitions(handle);
}
$unregisterMcpServerDefinitionProvider(handle: number): void {
if (!this.mcpServerManager) {
console.warn('MCP Server Manager not available - MCP server definitions will not be loaded');
return;
}
const provider = this.providers.get(handle);
if (!provider) {
console.warn(`No MCP Server provider found for handle '${handle}' - MCP server definitions will not be loaded`);
return;
}
this.mcpServerManager.removeServer(provider);
this.providers.delete(handle);
}
$onDidChangeMcpServerDefinitions(handle: number): void {
// Reload server definitions when provider reports changes
this.loadServerDefinitions(handle);
}
async $getServerDefinitions(handle: number): Promise<McpServerDefinitionDto[]> {
try {
return await this.proxy.$provideServerDefinitions(handle);
} catch (error) {
console.error('Error getting MCP server definitions:', error);
return [];
}
}
async $resolveServerDefinition(handle: number, server: McpServerDefinitionDto): Promise<McpServerDefinitionDto | undefined> {
try {
return await this.proxy.$resolveServerDefinition(handle, server);
} catch (error) {
console.error('Error resolving MCP server definition:', error);
return server;
}
}
private async loadServerDefinitions(handle: number): Promise<void> {
if (!this.mcpServerManager) {
console.warn('MCP Server Manager not available - MCP server definitions will not be loaded');
return;
}
try {
const definitions = await this.$getServerDefinitions(handle);
for (const definition of definitions) {
const resolved = await this.$resolveServerDefinition(handle, definition);
if (resolved) {
const mcpServerDescription = this.convertToMcpServerDescription(resolved);
this.mcpServerManager.addOrUpdateServer(mcpServerDescription);
}
}
} catch (error) {
console.error('Error loading MCP server definitions:', error);
}
}
private convertToMcpServerDescription(definition: McpServerDefinitionDto): MCPServerDescription {
if (isMcpHttpServerDefinitionDto(definition)) {
// For HTTP servers, we would need to create a bridge or adapter
// For now, we'll create a placeholder stdio server that could proxy to HTTP
console.warn(`HTTP transport not yet supported for MCP server '${definition.label}'. Skipping.`);
throw new Error(`HTTP transport not yet supported for MCP server '${definition.label}'`);
}
// Convert env values to strings, filtering out null values
let convertedEnv: Record<string, string> | undefined;
if (definition.env) {
convertedEnv = {};
for (const [key, value] of Object.entries(definition.env)) {
if (value !== null) {
convertedEnv[key] = String(value);
}
}
}
return {
name: definition.label,
command: definition.command!,
args: definition.args,
env: convertedEnv,
autostart: false, // Extensions should manage their own server lifecycle
};
}
}