@@ -17,10 +17,7 @@ import { AICommandHandlerFactory } from '@theia/ai-core/lib/browser/ai-command-h
17
17
import { CommandContribution , CommandRegistry , MessageService } from '@theia/core' ;
18
18
import { QuickInputService } from '@theia/core/lib/browser' ;
19
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
- type MCPTool = Awaited < ReturnType < MCPServerManager [ 'getTools' ] > > [ 'tools' ] [ number ] ;
20
+ import { MCPFrontendService } from './mcp-frontend-service' ;
24
21
25
22
export const StartMCPServer = {
26
23
id : 'mcp.startserver' ,
@@ -42,11 +39,8 @@ export class MCPCommandContribution implements CommandContribution {
42
39
@inject ( MessageService )
43
40
protected messageService : MessageService ;
44
41
45
- @inject ( MCPServerManager )
46
- protected readonly mcpServerManager : MCPServerManager ;
47
-
48
- @inject ( ToolInvocationRegistry )
49
- protected readonly toolInvocationRegistry : ToolInvocationRegistry ;
42
+ @inject ( MCPFrontendService )
43
+ protected readonly mcpFrontendService : MCPFrontendService ;
50
44
51
45
private async getMCPServerSelection ( serverNames : string [ ] ) : Promise < string | undefined > {
52
46
if ( ! serverNames || serverNames . length === 0 ) {
@@ -61,7 +55,7 @@ export class MCPCommandContribution implements CommandContribution {
61
55
commandRegistry . registerCommand ( StopMCPServer , this . commandHandlerFactory ( {
62
56
execute : async ( ) => {
63
57
try {
64
- const startedServers = await this . mcpServerManager . getStartedServers ( ) ;
58
+ const startedServers = await this . mcpFrontendService . getStartedServers ( ) ;
65
59
if ( ! startedServers || startedServers . length === 0 ) {
66
60
this . messageService . error ( 'No MCP servers running.' ) ;
67
61
return ;
@@ -70,8 +64,7 @@ export class MCPCommandContribution implements CommandContribution {
70
64
if ( ! selection ) {
71
65
return ;
72
66
}
73
- this . toolInvocationRegistry . unregisterAllTools ( `mcp_${ selection } ` ) ;
74
- this . mcpServerManager . stopServer ( selection ) ;
67
+ await this . mcpFrontendService . stopServer ( selection ) ;
75
68
} catch ( error ) {
76
69
console . error ( 'Error while stopping MCP server:' , error ) ;
77
70
}
@@ -81,8 +74,8 @@ export class MCPCommandContribution implements CommandContribution {
81
74
commandRegistry . registerCommand ( StartMCPServer , this . commandHandlerFactory ( {
82
75
execute : async ( ) => {
83
76
try {
84
- const servers = await this . mcpServerManager . getServerNames ( ) ;
85
- const startedServers = await this . mcpServerManager . getStartedServers ( ) ;
77
+ const servers = await this . mcpFrontendService . getServerNames ( ) ;
78
+ const startedServers = await this . mcpFrontendService . getStartedServers ( ) ;
86
79
const startableServers = servers . filter ( server => ! startedServers . includes ( server ) ) ;
87
80
if ( ! startableServers || startableServers . length === 0 ) {
88
81
if ( startedServers && startedServers . length > 0 ) {
@@ -97,13 +90,8 @@ export class MCPCommandContribution implements CommandContribution {
97
90
if ( ! selection ) {
98
91
return ;
99
92
}
100
- this . mcpServerManager . startServer ( selection ) ;
101
- const { tools } = await this . mcpServerManager . getTools ( selection ) ;
102
- const toolRequests : ToolRequest [ ] = tools . map ( tool => this . convertToToolRequest ( tool , selection ) ) ;
103
-
104
- for ( const toolRequest of toolRequests ) {
105
- this . toolInvocationRegistry . registerTool ( toolRequest ) ;
106
- }
93
+ await this . mcpFrontendService . startServer ( selection ) ;
94
+ const { tools } = await this . mcpFrontendService . getTools ( selection ) ;
107
95
const toolNames = tools . map ( tool => tool . name || 'Unnamed Tool' ) . join ( ', ' ) ;
108
96
this . messageService . info (
109
97
`MCP server "${ selection } " successfully started. Registered tools: ${ toolNames || 'No tools available.' } `
@@ -115,29 +103,4 @@ export class MCPCommandContribution implements CommandContribution {
115
103
}
116
104
} ) ) ;
117
105
}
118
-
119
- convertToToolRequest ( tool : MCPTool , serverName : string ) : ToolRequest {
120
- const id = `mcp_${ serverName } _${ tool . name } ` ;
121
-
122
- return {
123
- id : id ,
124
- name : id ,
125
- providerName : `mcp_${ serverName } ` ,
126
- parameters : ToolRequest . isToolRequestParameters ( tool . inputSchema ) ? {
127
- type : tool . inputSchema . type ,
128
- properties : tool . inputSchema . properties ,
129
- required : tool . inputSchema . required
130
- } : undefined ,
131
- description : tool . description ,
132
- handler : async ( arg_string : string ) => {
133
- try {
134
- return await this . mcpServerManager . callTool ( serverName , tool . name , arg_string ) ;
135
- } catch ( error ) {
136
- console . error ( `Error in tool handler for ${ tool . name } on MCP server ${ serverName } :` , error ) ;
137
- throw error ;
138
- }
139
- } ,
140
- } ;
141
- }
142
-
143
106
}
0 commit comments