Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/mcp/common/mcpManagementCli.ts
3294 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { ILogger } from '../../log/common/log.js';
7
import { IMcpServerConfiguration, IMcpServerVariable } from './mcpPlatformTypes.js';
8
import { IMcpManagementService } from './mcpManagement.js';
9
10
type ValidatedConfig = { name: string; config: IMcpServerConfiguration; inputs?: IMcpServerVariable[] };
11
12
export class McpManagementCli {
13
constructor(
14
private readonly _logger: ILogger,
15
@IMcpManagementService private readonly _mcpManagementService: IMcpManagementService,
16
) { }
17
18
async addMcpDefinitions(
19
definitions: string[],
20
) {
21
const configs = definitions.map((config) => this.validateConfiguration(config));
22
await this.updateMcpInResource(configs);
23
this._logger.info(`Added MCP servers: ${configs.map(c => c.name).join(', ')}`);
24
}
25
26
private async updateMcpInResource(configs: ValidatedConfig[]) {
27
await Promise.all(configs.map(({ name, config, inputs }) => this._mcpManagementService.install({ name, config, inputs })));
28
}
29
30
private validateConfiguration(config: string): ValidatedConfig {
31
let parsed: IMcpServerConfiguration & { name: string; inputs?: IMcpServerVariable[] };
32
try {
33
parsed = JSON.parse(config);
34
} catch (e) {
35
throw new InvalidMcpOperationError(`Invalid JSON '${config}': ${e}`);
36
}
37
38
if (!parsed.name) {
39
throw new InvalidMcpOperationError(`Missing name property in ${config}`);
40
}
41
42
if (!('command' in parsed) && !('url' in parsed)) {
43
throw new InvalidMcpOperationError(`Missing command or URL property in ${config}`);
44
}
45
46
const { name, inputs, ...rest } = parsed;
47
return { name, inputs, config: rest as IMcpServerConfiguration };
48
}
49
}
50
51
class InvalidMcpOperationError extends Error {
52
constructor(message: string) {
53
super(message);
54
this.stack = message;
55
}
56
}
57
58