Path: blob/main/extensions/copilot/src/extension/mcp/vscode-node/mcpToolCallingLoop.tsx
13400 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 type { CancellationToken, LanguageModelToolInformation, Progress } from 'vscode';6import { IAuthenticationChatUpgradeService } from '../../../platform/authentication/common/authenticationUpgrade';7import { IChatHookService } from '../../../platform/chat/common/chatHookService';8import { ChatLocation, ChatResponse } from '../../../platform/chat/common/commonTypes';9import { ISessionTranscriptService } from '../../../platform/chat/common/sessionTranscriptService';10import { IConfigurationService } from '../../../platform/configuration/common/configurationService';11import { IEndpointProvider } from '../../../platform/endpoint/common/endpointProvider';12import { IFileSystemService } from '../../../platform/filesystem/common/fileSystemService';13import { IGitService } from '../../../platform/git/common/gitService';14import { ILogService } from '../../../platform/log/common/logService';15import { IOTelService } from '../../../platform/otel/common/otelService';16import { IRequestLogger } from '../../../platform/requestLogger/common/requestLogger';17import { IExperimentationService } from '../../../platform/telemetry/common/nullExperimentationService';18import { ITelemetryService } from '../../../platform/telemetry/common/telemetry';19import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';20import { ChatResponseProgressPart, ChatResponseReferencePart } from '../../../vscodeTypes';21import { IToolCallingLoopOptions, ToolCallingLoop, ToolCallingLoopFetchOptions } from '../../intents/node/toolCallingLoop';22import { IBuildPromptContext } from '../../prompt/common/intents';23import { IBuildPromptResult } from '../../prompt/node/intents';24import { PromptRenderer } from '../../prompts/node/base/promptRenderer';25import { IMcpToolCallingLoopPromptContext, McpToolCallingLoopPrompt } from './mcpToolCallingLoopPrompt';26import { QuickInputTool, QuickPickTool } from './mcpToolCallingTools';2728export interface IMcpToolCallingLoopOptions extends IToolCallingLoopOptions {29props: IMcpToolCallingLoopPromptContext;30}3132export class McpToolCallingLoop extends ToolCallingLoop<IMcpToolCallingLoopOptions> {33public static readonly ID = 'mcpToolSetupLoop';3435constructor(36options: IMcpToolCallingLoopOptions,37@IInstantiationService private readonly instantiationService: IInstantiationService,38@ILogService logService: ILogService,39@IRequestLogger requestLogger: IRequestLogger,40@IEndpointProvider private readonly endpointProvider: IEndpointProvider,41@IAuthenticationChatUpgradeService authenticationChatUpgradeService: IAuthenticationChatUpgradeService,42@ITelemetryService telemetryService: ITelemetryService,43@IConfigurationService configurationService: IConfigurationService,44@IExperimentationService experimentationService: IExperimentationService,45@IChatHookService chatHookService: IChatHookService,46@ISessionTranscriptService sessionTranscriptService: ISessionTranscriptService,47@IFileSystemService fileSystemService: IFileSystemService,48@IOTelService otelService: IOTelService,49@IGitService gitService: IGitService,50) {51super(options, instantiationService, endpointProvider, logService, requestLogger, authenticationChatUpgradeService, telemetryService, configurationService, experimentationService, chatHookService, sessionTranscriptService, fileSystemService, otelService, gitService);52}5354private async getEndpoint() {55return await this.endpointProvider.getChatEndpoint('copilot-fast');56}5758protected async buildPrompt(buildPromptContext: IBuildPromptContext, progress: Progress<ChatResponseReferencePart | ChatResponseProgressPart>, token: CancellationToken): Promise<IBuildPromptResult> {59const endpoint = await this.getEndpoint();60const renderer = PromptRenderer.create(61this.instantiationService,62endpoint,63McpToolCallingLoopPrompt,64{65promptContext: buildPromptContext,66...this.options.props67}68);69return await renderer.render(progress, token);70}7172protected async getAvailableTools(): Promise<LanguageModelToolInformation[]> {73if (this.options.conversation.turns.length > 5) {74return []; // force a response75}7677return [{78description: QuickInputTool.description,79name: QuickInputTool.ID,80inputSchema: QuickInputTool.schema,81source: undefined,82tags: [],83}, {84description: QuickPickTool.description,85name: QuickPickTool.ID,86inputSchema: QuickPickTool.schema,87source: undefined,88tags: [],89}];90}9192protected async fetch(opts: ToolCallingLoopFetchOptions, token: CancellationToken): Promise<ChatResponse> {93const endpoint = await this.getEndpoint();94return endpoint.makeChatRequest2({95...opts,96debugName: McpToolCallingLoop.ID,97location: ChatLocation.Agent,98requestOptions: {99...opts.requestOptions,100temperature: 0101},102}, token);103}104}105106107