Path: blob/main/src/vs/workbench/contrib/mcp/electron-browser/mcpGatewayService.ts
5263 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { URI } from '../../../../base/common/uri.js';6import { ProxyChannel } from '../../../../base/parts/ipc/common/ipc.js';7import { IMainProcessService } from '../../../../platform/ipc/common/mainProcessService.js';8import { IMcpGatewayService, McpGatewayChannelName } from '../../../../platform/mcp/common/mcpGateway.js';9import { IRemoteAgentService } from '../../../services/remote/common/remoteAgentService.js';10import { IMcpGatewayResult, IWorkbenchMcpGatewayService } from '../common/mcpGatewayService.js';1112/**13* Electron workbench implementation of the MCP Gateway Service.14*15* This implementation can create gateways either in the main process (local)16* or on a remote server (if connected).17*/18export class WorkbenchMcpGatewayService implements IWorkbenchMcpGatewayService {19declare readonly _serviceBrand: undefined;2021private readonly _localPlatformService: IMcpGatewayService;2223constructor(24@IMainProcessService mainProcessService: IMainProcessService,25@IRemoteAgentService private readonly _remoteAgentService: IRemoteAgentService,26) {27this._localPlatformService = ProxyChannel.toService<IMcpGatewayService>(28mainProcessService.getChannel(McpGatewayChannelName)29);30}3132async createGateway(inRemote: boolean): Promise<IMcpGatewayResult | undefined> {33if (inRemote) {34return this._createRemoteGateway();35} else {36return this._createLocalGateway();37}38}3940private async _createLocalGateway(): Promise<IMcpGatewayResult> {41const info = await this._localPlatformService.createGateway(undefined);4243return {44address: URI.revive(info.address),45dispose: () => {46this._localPlatformService.disposeGateway(info.gatewayId);47}48};49}5051private async _createRemoteGateway(): Promise<IMcpGatewayResult | undefined> {52const connection = this._remoteAgentService.getConnection();53if (!connection) {54// No remote connection - cannot create remote gateway55return undefined;56}5758return connection.withChannel(McpGatewayChannelName, async channel => {59const service = ProxyChannel.toService<IMcpGatewayService>(channel);60const info = await service.createGateway(undefined);6162return {63address: URI.revive(info.address),64dispose: () => {65service.disposeGateway(info.gatewayId);66}67};68});69}70}717273