@theia/ai-mcp 1.57.0-next.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +91 -0
  2. package/lib/browser/mcp-command-contribution.d.ts +26 -0
  3. package/lib/browser/mcp-command-contribution.d.ts.map +1 -0
  4. package/lib/browser/mcp-command-contribution.js +146 -0
  5. package/lib/browser/mcp-command-contribution.js.map +1 -0
  6. package/lib/browser/mcp-frontend-application-contribution.d.ts +26 -0
  7. package/lib/browser/mcp-frontend-application-contribution.d.ts.map +1 -0
  8. package/lib/browser/mcp-frontend-application-contribution.js +129 -0
  9. package/lib/browser/mcp-frontend-application-contribution.js.map +1 -0
  10. package/lib/browser/mcp-frontend-module.d.ts +4 -0
  11. package/lib/browser/mcp-frontend-module.d.ts.map +1 -0
  12. package/lib/browser/mcp-frontend-module.js +34 -0
  13. package/lib/browser/mcp-frontend-module.js.map +1 -0
  14. package/lib/browser/mcp-preferences.d.ts +4 -0
  15. package/lib/browser/mcp-preferences.d.ts.map +1 -0
  16. package/lib/browser/mcp-preferences.js +83 -0
  17. package/lib/browser/mcp-preferences.js.map +1 -0
  18. package/lib/common/index.d.ts +2 -0
  19. package/lib/common/index.d.ts.map +1 -0
  20. package/lib/common/index.js +20 -0
  21. package/lib/common/index.js.map +1 -0
  22. package/lib/common/mcp-server-manager.d.ts +38 -0
  23. package/lib/common/mcp-server-manager.d.ts.map +1 -0
  24. package/lib/common/mcp-server-manager.js +21 -0
  25. package/lib/common/mcp-server-manager.js.map +1 -0
  26. package/lib/node/mcp-backend-module.d.ts +4 -0
  27. package/lib/node/mcp-backend-module.d.ts.map +1 -0
  28. package/lib/node/mcp-backend-module.js +29 -0
  29. package/lib/node/mcp-backend-module.js.map +1 -0
  30. package/lib/node/mcp-server-manager-impl.d.ts +14 -0
  31. package/lib/node/mcp-server-manager-impl.d.ts.map +1 -0
  32. package/lib/node/mcp-server-manager-impl.js +93 -0
  33. package/lib/node/mcp-server-manager-impl.js.map +1 -0
  34. package/lib/node/mcp-server.d.ts +19 -0
  35. package/lib/node/mcp-server.d.ts.map +1 -0
  36. package/lib/node/mcp-server.js +97 -0
  37. package/lib/node/mcp-server.js.map +1 -0
  38. package/lib/package.spec.d.ts +1 -0
  39. package/lib/package.spec.d.ts.map +1 -0
  40. package/lib/package.spec.js +26 -0
  41. package/lib/package.spec.js.map +1 -0
  42. package/package.json +50 -0
  43. package/src/browser/mcp-command-contribution.ts +142 -0
  44. package/src/browser/mcp-frontend-application-contribution.ts +141 -0
  45. package/src/browser/mcp-frontend-module.ts +33 -0
  46. package/src/browser/mcp-preferences.ts +83 -0
  47. package/src/common/index.ts +16 -0
  48. package/src/common/mcp-server-manager.ts +58 -0
  49. package/src/node/mcp-backend-module.ts +31 -0
  50. package/src/node/mcp-server-manager-impl.ts +93 -0
  51. package/src/node/mcp-server.ts +111 -0
  52. package/src/package.spec.ts +28 -0
@@ -0,0 +1,141 @@
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 { FrontendApplicationContribution, PreferenceProvider, PreferenceService } from '@theia/core/lib/browser';
17
+ import { inject, injectable } from '@theia/core/shared/inversify';
18
+ import { MCPServerDescription, MCPServerManager } from '../common';
19
+ import { MCP_SERVERS_PREF } from './mcp-preferences';
20
+ import { JSONObject } from '@theia/core/shared/@phosphor/coreutils';
21
+
22
+ interface MCPServersPreferenceValue {
23
+ command: string;
24
+ args?: string[];
25
+ env?: { [key: string]: string };
26
+ };
27
+
28
+ interface MCPServersPreference {
29
+ [name: string]: MCPServersPreferenceValue
30
+ };
31
+
32
+ namespace MCPServersPreference {
33
+ export function isValue(obj: unknown): obj is MCPServersPreferenceValue {
34
+ return !!obj && typeof obj === 'object' &&
35
+ 'command' in obj && typeof obj.command === 'string' &&
36
+ (!('args' in obj) || Array.isArray(obj.args) && obj.args.every(arg => typeof arg === 'string')) &&
37
+ (!('env' in obj) || !!obj.env && typeof obj.env === 'object' && Object.values(obj.env).every(value => typeof value === 'string'));
38
+ }
39
+ }
40
+
41
+ function filterValidValues(servers: unknown): MCPServersPreference {
42
+ const result: MCPServersPreference = {};
43
+ if (!servers || typeof servers !== 'object') {
44
+ return result;
45
+ }
46
+ for (const [name, value] of Object.entries(servers)) {
47
+ if (typeof name === 'string' && MCPServersPreference.isValue(value)) {
48
+ result[name] = value;
49
+ }
50
+ }
51
+ return result;
52
+ }
53
+
54
+ @injectable()
55
+ export class McpFrontendApplicationContribution implements FrontendApplicationContribution {
56
+
57
+ @inject(PreferenceService)
58
+ protected preferenceService: PreferenceService;
59
+
60
+ @inject(MCPServerManager)
61
+ protected manager: MCPServerManager;
62
+
63
+ protected prevServers: Map<string, MCPServerDescription> = new Map();
64
+
65
+ onStart(): void {
66
+ this.preferenceService.ready.then(() => {
67
+ const servers = filterValidValues(this.preferenceService.get(
68
+ MCP_SERVERS_PREF,
69
+ {}
70
+ ));
71
+ this.syncServers(servers);
72
+ this.prevServers = this.convertToMap(servers);
73
+
74
+ this.preferenceService.onPreferenceChanged(event => {
75
+ if (event.preferenceName === MCP_SERVERS_PREF) {
76
+ this.handleServerChanges(filterValidValues(event.newValue));
77
+ }
78
+ });
79
+ });
80
+ }
81
+
82
+ protected handleServerChanges(newServers: MCPServersPreference): void {
83
+ const oldServers = this.prevServers;
84
+ const updatedServers = this.convertToMap(newServers);
85
+
86
+ for (const [name] of oldServers) {
87
+ if (!updatedServers.has(name)) {
88
+ this.manager.removeServer(name);
89
+ }
90
+ }
91
+
92
+ for (const [name, description] of updatedServers) {
93
+ const oldDescription = oldServers.get(name);
94
+ let diff = false;
95
+ try {
96
+ // We know that that the descriptions are actual JSONObjects as we construct them ourselves
97
+ if (!oldDescription || !PreferenceProvider.deepEqual(oldDescription as unknown as JSONObject, description as unknown as JSONObject)) {
98
+ diff = true;
99
+ }
100
+ } catch (e) {
101
+ // In some cases the deepEqual function throws an error, so we fall back to assuming that there is a difference
102
+ // This seems to happen in cases where the objects are structured differently, e.g. whole sub-objects are missing
103
+ console.debug('Failed to compare MCP server descriptions, assuming a difference', e);
104
+ diff = true;
105
+ }
106
+ if (diff) {
107
+ this.manager.addOrUpdateServer(description);
108
+ }
109
+ }
110
+
111
+ this.prevServers = updatedServers;
112
+ }
113
+
114
+ protected syncServers(servers: MCPServersPreference): void {
115
+ const updatedServers = this.convertToMap(servers);
116
+
117
+ for (const [, description] of updatedServers) {
118
+ this.manager.addOrUpdateServer(description);
119
+ }
120
+
121
+ for (const [name] of this.prevServers) {
122
+ if (!updatedServers.has(name)) {
123
+ this.manager.removeServer(name);
124
+ }
125
+ }
126
+
127
+ this.prevServers = updatedServers;
128
+ }
129
+
130
+ protected convertToMap(servers: MCPServersPreference): Map<string, MCPServerDescription> {
131
+ const map = new Map<string, MCPServerDescription>();
132
+ Object.entries(servers).forEach(([name, description]) => {
133
+ map.set(name, {
134
+ name,
135
+ ...description,
136
+ env: description.env || undefined
137
+ });
138
+ });
139
+ return map;
140
+ }
141
+ }
@@ -0,0 +1,33 @@
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
+
17
+ import { CommandContribution } from '@theia/core';
18
+ import { ContainerModule } from '@theia/core/shared/inversify';
19
+ import { MCPCommandContribution } from './mcp-command-contribution';
20
+ import { FrontendApplicationContribution, PreferenceContribution, RemoteConnectionProvider, ServiceConnectionProvider } from '@theia/core/lib/browser';
21
+ import { MCPServerManager, MCPServerManagerPath } from '../common/mcp-server-manager';
22
+ import { McpServersPreferenceSchema } from './mcp-preferences';
23
+ import { McpFrontendApplicationContribution } from './mcp-frontend-application-contribution';
24
+
25
+ export default new ContainerModule(bind => {
26
+ bind(PreferenceContribution).toConstantValue({ schema: McpServersPreferenceSchema });
27
+ bind(FrontendApplicationContribution).to(McpFrontendApplicationContribution).inSingletonScope();
28
+ bind(CommandContribution).to(MCPCommandContribution);
29
+ bind(MCPServerManager).toDynamicValue(ctx => {
30
+ const connection = ctx.container.get<ServiceConnectionProvider>(RemoteConnectionProvider);
31
+ return connection.createProxy<MCPServerManager>(MCPServerManagerPath);
32
+ }).inSingletonScope();
33
+ });
@@ -0,0 +1,83 @@
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
+
17
+ import { PreferenceSchema } from '@theia/core/lib/browser/preferences/preference-contribution';
18
+
19
+ export const MCP_SERVERS_PREF = 'ai-features.mcp.mcpServers';
20
+
21
+ export const McpServersPreferenceSchema: PreferenceSchema = {
22
+ type: 'object',
23
+ properties: {
24
+ [MCP_SERVERS_PREF]: {
25
+ type: 'object',
26
+ title: 'MCP Servers Configuration',
27
+ markdownDescription: 'Configure MCP servers with command, arguments and optionally environment variables. Each server is identified by a unique key, such as\
28
+ "brave-search" or "filesystem".\
29
+ To start a server, use the "MCP: Start MCP Server" command, which enables you to select the desired server.\
30
+ To stop a server, use the "MCP: Stop MCP Server" command.\
31
+ \n\
32
+ Example configuration:\n\
33
+ ```\
34
+ {\n\
35
+ "brave-search": {\n\
36
+ "command": "npx",\n\
37
+ "args": [\n\
38
+ "-y",\n\
39
+ "@modelcontextprotocol/server-brave-search"\n\
40
+ ],\n\
41
+ "env": {\n\
42
+ "BRAVE_API_KEY": "YOUR_API_KEY"\n\
43
+ }\n\
44
+ },\n\
45
+ "filesystem": {\n\
46
+ "command": "npx",\n\
47
+ "args": ["-y", "@modelcontextprotocol/server-filesystem", "/Users/YOUR_USERNAME/Desktop"],\n\
48
+ "env": {\n\
49
+ "CUSTOM_ENV_VAR": "custom-value"\n\
50
+ }\n\
51
+ }\n\
52
+ }\
53
+ ```',
54
+ additionalProperties: {
55
+ type: 'object',
56
+ properties: {
57
+ command: {
58
+ type: 'string',
59
+ title: 'Command to execute the MCP server',
60
+ markdownDescription: 'The command used to start the MCP server, e.g., "uvx" or "npx".'
61
+ },
62
+ args: {
63
+ type: 'array',
64
+ title: 'Arguments for the command',
65
+ markdownDescription: 'An array of arguments to pass to the command.',
66
+ items: {
67
+ type: 'string'
68
+ }
69
+ },
70
+ env: {
71
+ type: 'object',
72
+ title: 'Environment variables',
73
+ markdownDescription: 'Optional environment variables to set for the server, such as an API key.',
74
+ additionalProperties: {
75
+ type: 'string'
76
+ }
77
+ }
78
+ },
79
+ required: ['command', 'args']
80
+ }
81
+ }
82
+ }
83
+ };
@@ -0,0 +1,16 @@
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
+ export * from './mcp-server-manager';
@@ -0,0 +1,58 @@
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
+
17
+ import type { Client } from '@modelcontextprotocol/sdk/client/index';
18
+
19
+ export interface MCPServer {
20
+ callTool(toolName: string, arg_string: string): ReturnType<Client['callTool']>;
21
+ getTools(): ReturnType<Client['listTools']>;
22
+ }
23
+
24
+ export interface MCPServerManager {
25
+ callTool(serverName: string, toolName: string, arg_string: string): ReturnType<MCPServer['callTool']>;
26
+ removeServer(name: string): void;
27
+ addOrUpdateServer(description: MCPServerDescription): void;
28
+ getTools(serverName: string): ReturnType<MCPServer['getTools']>
29
+ getServerNames(): Promise<string[]>;
30
+ startServer(serverName: string): Promise<void>;
31
+ stopServer(serverName: string): Promise<void>;
32
+ getStartedServers(): Promise<string[]>;
33
+ }
34
+
35
+ export interface MCPServerDescription {
36
+ /**
37
+ * The unique name of the MCP server.
38
+ */
39
+ name: string;
40
+
41
+ /**
42
+ * The command to execute the MCP server.
43
+ */
44
+ command: string;
45
+
46
+ /**
47
+ * An array of arguments to pass to the command.
48
+ */
49
+ args?: string[];
50
+
51
+ /**
52
+ * Optional environment variables to set when starting the server.
53
+ */
54
+ env?: { [key: string]: string };
55
+ }
56
+
57
+ export const MCPServerManager = Symbol('MCPServerManager');
58
+ export const MCPServerManagerPath = '/services/mcpservermanager';
@@ -0,0 +1,31 @@
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
+
17
+ import { ContainerModule } from '@theia/core/shared/inversify';
18
+ import { ConnectionHandler, RpcConnectionHandler } from '@theia/core';
19
+ import { MCPServerManagerImpl } from './mcp-server-manager-impl';
20
+ import { MCPServerManager, MCPServerManagerPath } from '../common/mcp-server-manager';
21
+
22
+ export default new ContainerModule(bind => {
23
+ bind(MCPServerManager).to(MCPServerManagerImpl).inSingletonScope();
24
+ bind(ConnectionHandler).toDynamicValue(ctx => new RpcConnectionHandler(
25
+ MCPServerManagerPath,
26
+ () => {
27
+ const service = ctx.container.get<MCPServerManager>(MCPServerManager);
28
+ return service;
29
+ }
30
+ )).inSingletonScope();
31
+ });
@@ -0,0 +1,93 @@
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 { injectable } from '@theia/core/shared/inversify';
17
+ import { MCPServerDescription, MCPServerManager } from '../common/mcp-server-manager';
18
+ import { MCPServer } from './mcp-server';
19
+
20
+ @injectable()
21
+ export class MCPServerManagerImpl implements MCPServerManager {
22
+
23
+ protected servers: Map<string, MCPServer> = new Map();
24
+
25
+ async stopServer(serverName: string): Promise<void> {
26
+ const server = this.servers.get(serverName);
27
+ if (!server) {
28
+ throw new Error(`MCP server "${serverName}" not found.`);
29
+ }
30
+ server.stop();
31
+ console.log(`MCP server "${serverName}" stopped.`);
32
+ }
33
+
34
+ async getStartedServers(): Promise<string[]> {
35
+ const startedServers: string[] = [];
36
+ for (const [name, server] of this.servers.entries()) {
37
+ if (server.isStarted()) {
38
+ startedServers.push(name);
39
+ }
40
+ }
41
+ return startedServers;
42
+ }
43
+
44
+ callTool(serverName: string, toolName: string, arg_string: string): ReturnType<MCPServer['callTool']> {
45
+ const server = this.servers.get(serverName);
46
+ if (!server) {
47
+ throw new Error(`MCP server "${toolName}" not found.`);
48
+ }
49
+ return server.callTool(toolName, arg_string);
50
+ }
51
+
52
+ async startServer(serverName: string): Promise<void> {
53
+ const server = this.servers.get(serverName);
54
+ if (!server) {
55
+ throw new Error(`MCP server "${serverName}" not found.`);
56
+ }
57
+ await server.start();
58
+ }
59
+ async getServerNames(): Promise<string[]> {
60
+ return Array.from(this.servers.keys());
61
+ }
62
+
63
+ public async getTools(serverName: string): ReturnType<MCPServer['getTools']> {
64
+ const server = this.servers.get(serverName);
65
+ if (!server) {
66
+ throw new Error(`MCP server "${serverName}" not found.`);
67
+ }
68
+ return server.getTools();
69
+
70
+ }
71
+
72
+ addOrUpdateServer(description: MCPServerDescription): void {
73
+ const { name, command, args, env } = description;
74
+ const existingServer = this.servers.get(name);
75
+
76
+ if (existingServer) {
77
+ existingServer.update(command, args, env);
78
+ } else {
79
+ const newServer = new MCPServer(name, command, args, env);
80
+ this.servers.set(name, newServer);
81
+ }
82
+ }
83
+
84
+ removeServer(name: string): void {
85
+ const server = this.servers.get(name);
86
+ if (server) {
87
+ server.stop();
88
+ this.servers.delete(name);
89
+ } else {
90
+ console.warn(`MCP server "${name}" not found.`);
91
+ }
92
+ }
93
+ }
@@ -0,0 +1,111 @@
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 { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio';
17
+ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
18
+
19
+ export class MCPServer {
20
+ private name: string;
21
+ private command: string;
22
+ private args?: string[];
23
+ private client: Client;
24
+ private env?: { [key: string]: string };
25
+ private started: boolean = false;
26
+
27
+ constructor(name: string, command: string, args?: string[], env?: Record<string, string>) {
28
+ this.name = name;
29
+ this.command = command;
30
+ this.args = args;
31
+ this.env = env;
32
+ }
33
+
34
+ isStarted(): boolean {
35
+ return this.started;
36
+ }
37
+
38
+ async start(): Promise<void> {
39
+ if (this.started) {
40
+ return;
41
+ }
42
+ console.log(`Starting server "${this.name}" with command: ${this.command} and args: ${this.args?.join(' ')} and env: ${JSON.stringify(this.env)}`);
43
+ // Filter process.env to exclude undefined values
44
+ const sanitizedEnv: Record<string, string> = Object.fromEntries(
45
+ Object.entries(process.env).filter((entry): entry is [string, string] => entry[1] !== undefined)
46
+ );
47
+
48
+ const mergedEnv: Record<string, string> = {
49
+ ...sanitizedEnv,
50
+ ...(this.env || {})
51
+ };
52
+ const transport = new StdioClientTransport({
53
+ command: this.command,
54
+ args: this.args,
55
+ env: mergedEnv,
56
+ });
57
+ transport.onerror = error => {
58
+ console.error('Error: ' + error);
59
+ };
60
+
61
+ this.client = new Client({
62
+ name: 'theia-client',
63
+ version: '1.0.0',
64
+ }, {
65
+ capabilities: {}
66
+ });
67
+ this.client.onerror = error => {
68
+ console.error('Error in MCP client: ' + error);
69
+ };
70
+
71
+ await this.client.connect(transport);
72
+ this.started = true;
73
+ }
74
+
75
+ async callTool(toolName: string, arg_string: string): ReturnType<Client['callTool']> {
76
+ let args;
77
+ try {
78
+ args = JSON.parse(arg_string);
79
+ } catch (error) {
80
+ console.error(
81
+ `Failed to parse arguments for calling tool "${toolName}" in MCP server "${this.name}" with command "${this.command}".
82
+ Invalid JSON: ${arg_string}`,
83
+ error
84
+ );
85
+ }
86
+ const params = {
87
+ name: toolName,
88
+ arguments: args,
89
+ };
90
+ return this.client.callTool(params);
91
+ }
92
+
93
+ async getTools(): ReturnType<Client['listTools']> {
94
+ return this.client.listTools();
95
+ }
96
+
97
+ update(command: string, args?: string[], env?: { [key: string]: string }): void {
98
+ this.command = command;
99
+ this.args = args;
100
+ this.env = env;
101
+ }
102
+
103
+ stop(): void {
104
+ if (!this.started || !this.client) {
105
+ return;
106
+ }
107
+ console.log(`Stopping MCP server "${this.name}"`);
108
+ this.client.close();
109
+ this.started = false;
110
+ }
111
+ }
@@ -0,0 +1,28 @@
1
+ // *****************************************************************************
2
+ // Copyright (C) 2024 EclipseSource GmbH and others.
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
+ /* note: this bogus test file is required so that
18
+ we are able to run mocha unit tests on this
19
+ package, without having any actual unit tests in it.
20
+ This way a coverage report will be generated,
21
+ showing 0% coverage, instead of no report.
22
+ This file can be removed once we have real unit
23
+ tests in place. */
24
+
25
+ describe('ai-mcp package', () => {
26
+
27
+ it('support code coverage statistics', () => true);
28
+ });