Skip to content

Commit f28ba46

Browse files
committed
Update provideMcpServerDefinitions to get MCP config from CLI
1 parent 5db3062 commit f28ba46

File tree

1 file changed

+76
-23
lines changed

1 file changed

+76
-23
lines changed

src/env/node/gk/mcp/integration.ts

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import type { Event, McpServerDefinition } from 'vscode';
2-
import {
3-
version as codeVersion,
4-
Disposable,
5-
env,
6-
EventEmitter,
7-
lm,
8-
McpStdioServerDefinition,
9-
Uri,
10-
window,
11-
} from 'vscode';
2+
import { version as codeVersion, Disposable, env, EventEmitter, lm, McpStdioServerDefinition, window } from 'vscode';
123
import type { Container } from '../../../../container';
134
import type { StorageChangeEvent } from '../../../../system/-webview/storage';
145
import { getHostAppName } from '../../../../system/-webview/vscode';
156
import { debounce } from '../../../../system/function/debounce';
7+
import { Logger } from '../../../../system/logger';
168
import { satisfies } from '../../../../system/version';
9+
import { run } from '../../git/shell';
1710
import { getPlatform } from '../../platform';
1811
import { toMcpInstallProvider } from './utils';
1912

13+
const CLIProxyMCPConfigOutputs = {
14+
checkingForUpdates: /checking for updates.../i,
15+
};
16+
2017
export class McpProvider implements Disposable {
2118
static #instance: McpProvider | undefined;
2219

@@ -54,7 +51,7 @@ export class McpProvider implements Disposable {
5451
}
5552

5653
private async provideMcpServerDefinitions(): Promise<McpServerDefinition[]> {
57-
const config = await this.getMcpConfiguration();
54+
const config = await this.getMcpConfigurationFromCLI();
5855
if (config == null) {
5956
return [];
6057
}
@@ -72,7 +69,32 @@ export class McpProvider implements Disposable {
7269
return [serverDefinition];
7370
}
7471

75-
private async getMcpConfiguration(): Promise<
72+
// private async getMcpConfiguration(): Promise<
73+
// { name: string; type: string; command: string; args: string[]; version?: string } | undefined
74+
// > {
75+
// const cliInstall = this.container.storage.get('gk:cli:install');
76+
// const cliPath = this.container.storage.get('gk:cli:path');
77+
78+
// if (cliInstall?.status !== 'completed' || !cliPath) {
79+
// return undefined;
80+
// }
81+
82+
// const platform = getPlatform();
83+
// const executable = platform === 'windows' ? 'gk.exe' : 'gk';
84+
// const command = Uri.joinPath(Uri.file(cliPath), executable);
85+
86+
// const appName = toMcpInstallProvider(await getHostAppName());
87+
// const args = ['mcp', `--host=${appName}`, '--source=gitlens', `--scheme=${env.uriScheme}`];
88+
// return {
89+
// name: 'GitKraken MCP Server',
90+
// type: 'stdio',
91+
// command: command.fsPath,
92+
// args: args,
93+
// version: cliInstall.version,
94+
// };
95+
// }
96+
97+
private async getMcpConfigurationFromCLI(): Promise<
7698
{ name: string; type: string; command: string; args: string[]; version?: string } | undefined
7799
> {
78100
const cliInstall = this.container.storage.get('gk:cli:install');
@@ -82,19 +104,50 @@ export class McpProvider implements Disposable {
82104
return undefined;
83105
}
84106

107+
const appName = toMcpInstallProvider(await getHostAppName());
108+
if (appName == null) {
109+
return undefined;
110+
}
111+
112+
let output = await this.runCLICommand(
113+
['mcp', 'config', appName, '--source=gitlens', `--scheme=${env.uriScheme}`],
114+
{
115+
cwd: cliPath,
116+
},
117+
);
118+
output = output.replace(CLIProxyMCPConfigOutputs.checkingForUpdates, '').trim();
119+
console.log(output);
120+
121+
try {
122+
const configuration = JSON.parse(output) as { name: string; type: string; command: string; args: string[] };
123+
124+
return {
125+
name: configuration.name,
126+
type: configuration.type,
127+
command: configuration.command,
128+
args: configuration.args,
129+
version: cliInstall.version,
130+
};
131+
} catch (ex) {
132+
Logger.error(`Error getting MCP configuration: ${ex}`);
133+
}
134+
135+
return undefined;
136+
}
137+
138+
private async runCLICommand(
139+
args: string[],
140+
options?: {
141+
cwd?: string;
142+
},
143+
): Promise<string> {
85144
const platform = getPlatform();
86-
const executable = platform === 'windows' ? 'gk.exe' : 'gk';
87-
const command = Uri.joinPath(Uri.file(cliPath), executable);
145+
const cwd = options?.cwd ?? this.container.storage.get('gk:cli:path');
146+
if (cwd == null) {
147+
throw new Error('CLI is not installed');
148+
}
88149

89-
const appName = toMcpInstallProvider(await getHostAppName());
90-
const args = ['mcp', `--host=${appName}`, '--source=gitlens', `--scheme=${env.uriScheme}`];
91-
return {
92-
name: 'GitKraken MCP Server',
93-
type: 'stdio',
94-
command: command.fsPath,
95-
args: args,
96-
version: cliInstall.version,
97-
};
150+
return run(platform === 'windows' ? 'gk.exe' : './gk', args, 'utf8', { cwd: cwd });
98151
}
99152

100153
dispose(): void {

0 commit comments

Comments
 (0)