11import 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' ;
123import type { Container } from '../../../../container' ;
134import type { StorageChangeEvent } from '../../../../system/-webview/storage' ;
145import { getHostAppName } from '../../../../system/-webview/vscode' ;
156import { debounce } from '../../../../system/function/debounce' ;
7+ import { Logger } from '../../../../system/logger' ;
168import { satisfies } from '../../../../system/version' ;
9+ import { run } from '../../git/shell' ;
1710import { getPlatform } from '../../platform' ;
1811import { toMcpInstallProvider } from './utils' ;
1912
13+ const CLIProxyMCPConfigOutputs = {
14+ checkingForUpdates : / c h e c k i n g f o r u p d a t e s .../ i,
15+ } ;
16+
2017export 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