|
| 1 | +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'; |
| 12 | +import type { Container } from '../../../../container'; |
| 13 | +import type { StorageChangeEvent } from '../../../../system/-webview/storage'; |
| 14 | +import { getHostAppName } from '../../../../system/-webview/vscode'; |
| 15 | +import { debounce } from '../../../../system/function/debounce'; |
| 16 | +import { satisfies } from '../../../../system/version'; |
| 17 | +import { getPlatform } from '../../platform'; |
| 18 | +import { toMcpInstallProvider } from './utils'; |
| 19 | + |
| 20 | +export class McpProvider implements Disposable { |
| 21 | + static #instance: McpProvider | undefined; |
| 22 | + |
| 23 | + static create(container: Container): McpProvider | undefined { |
| 24 | + if (!satisfies(codeVersion, '>= 1.101.0') || !lm.registerMcpServerDefinitionProvider) return undefined; |
| 25 | + |
| 26 | + if (this.#instance == null) { |
| 27 | + this.#instance = new McpProvider(container); |
| 28 | + } |
| 29 | + |
| 30 | + return this.#instance; |
| 31 | + } |
| 32 | + |
| 33 | + private readonly _disposable: Disposable; |
| 34 | + private readonly _onDidChangeMcpServerDefinitions = new EventEmitter<void>(); |
| 35 | + get onDidChangeMcpServerDefinitions(): Event<void> { |
| 36 | + return this._onDidChangeMcpServerDefinitions.event; |
| 37 | + } |
| 38 | + |
| 39 | + private constructor(private readonly container: Container) { |
| 40 | + this._disposable = Disposable.from( |
| 41 | + this.container.storage.onDidChange(e => this.checkStorage(e)), |
| 42 | + lm.registerMcpServerDefinitionProvider('gitlens.mcpProvider', { |
| 43 | + onDidChangeMcpServerDefinitions: this._onDidChangeMcpServerDefinitions.event, |
| 44 | + provideMcpServerDefinitions: () => this.provideMcpServerDefinitions(), |
| 45 | + }), |
| 46 | + ); |
| 47 | + |
| 48 | + this.checkStorage(); |
| 49 | + } |
| 50 | + |
| 51 | + private checkStorage(e?: StorageChangeEvent): void { |
| 52 | + if (e != null && !(e.keys as string[]).includes('gk:cli:install')) return; |
| 53 | + this._onDidChangeMcpServerDefinitions.fire(); |
| 54 | + } |
| 55 | + |
| 56 | + private async provideMcpServerDefinitions(): Promise<McpServerDefinition[]> { |
| 57 | + const config = await this.getMcpConfiguration(); |
| 58 | + if (config == null) { |
| 59 | + return []; |
| 60 | + } |
| 61 | + |
| 62 | + const serverDefinition = new McpStdioServerDefinition( |
| 63 | + config.name, |
| 64 | + config.command, |
| 65 | + config.args, |
| 66 | + {}, |
| 67 | + config.version, |
| 68 | + ); |
| 69 | + |
| 70 | + this.notifyServerProvided(); |
| 71 | + |
| 72 | + return [serverDefinition]; |
| 73 | + } |
| 74 | + |
| 75 | + private async getMcpConfiguration(): Promise< |
| 76 | + { name: string; type: string; command: string; args: string[]; version?: string } | undefined |
| 77 | + > { |
| 78 | + const cliInstall = this.container.storage.get('gk:cli:install'); |
| 79 | + const cliPath = this.container.storage.get('gk:cli:path'); |
| 80 | + |
| 81 | + if (cliInstall?.status !== 'completed' || !cliPath) { |
| 82 | + return undefined; |
| 83 | + } |
| 84 | + |
| 85 | + const platform = getPlatform(); |
| 86 | + const executable = platform === 'windows' ? 'gk.exe' : 'gk'; |
| 87 | + const command = Uri.joinPath(Uri.file(cliPath), executable); |
| 88 | + |
| 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 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + dispose(): void { |
| 101 | + this._disposable.dispose(); |
| 102 | + this._onDidChangeMcpServerDefinitions.dispose(); |
| 103 | + } |
| 104 | + |
| 105 | + private _notifyServerProvided = false; |
| 106 | + private notifyServerProvided = debounce(() => { |
| 107 | + if (this._notifyServerProvided) return; |
| 108 | + |
| 109 | + void window.showInformationMessage('GitLens can now automatically configure the GitKraken MCP server for you'); |
| 110 | + this._notifyServerProvided = true; |
| 111 | + }, 250); |
| 112 | +} |
0 commit comments