/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { equals } from '../../../../base/common/arrays.js'; import { assertNever, softAssertNever } from '../../../../base/common/assert.js'; import { DeferredPromise, disposableTimeout, IntervalTimer, isThenable } from '../../../../base/common/async.js'; import { CancellationToken, CancellationTokenSource } from '../../../../base/common/cancellation.js'; import { CancellationError } from '../../../../base/common/errors.js'; import { Emitter } from '../../../../base/common/event.js'; import { Iterable } from '../../../../base/common/iterator.js'; import { JsonRpcError, JsonRpcProtocol } from '../../../../base/common/jsonRpcProtocol.js'; import { Disposable, DisposableStore, toDisposable } from '../../../../base/common/lifecycle.js'; import { autorun, ISettableObservable, ObservablePromise, observableValue, transaction } from '../../../../base/common/observable.js'; import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js'; import { canLog, ILogger, log, LogLevel } from '../../../../platform/log/common/log.js'; import { IProductService } from '../../../../platform/product/common/productService.js'; import { IMcpMessageTransport } from './mcpRegistryTypes.js'; import { IMcpTaskInternal, McpTaskManager } from './mcpTaskManager.js'; import { IMcpClientMethods, McpConnectionState, McpError, MpcResponseError } from './mcpTypes.js'; import { isTaskResult, translateMcpLogMessage } from './mcpTypesUtils.js'; import { MCP } from './modelContextProtocol.js'; export interface McpRoot { uri: string; name?: string; } export interface IMcpServerRequestHandlerOptions extends IMcpClientMethods { /** MCP message transport */ launch: IMcpMessageTransport; /** Logger instance. */ logger: ILogger; /** Log level MCP messages is logged at */ requestLogLevel?: LogLevel; /** Task manager for server-side MCP tasks (shared across reconnections) */ taskManager: McpTaskManager; } /** * Request handler for communicating with an MCP server. * * Handles sending requests and receiving responses, with automatic * handling of ping requests and typed client request methods. */ export class McpServerRequestHandler extends Disposable { private readonly _rpc: JsonRpcProtocol; private _hasAnnouncedRoots = false; private _roots: MCP.Root[] = []; public set roots(roots: MCP.Root[]) { if (!equals(this._roots, roots)) { this._roots = roots; if (this._hasAnnouncedRoots) { this.sendNotification({ method: 'notifications/roots/list_changed' }); this._hasAnnouncedRoots = false; } } } private _serverInit!: MCP.InitializeResult; public get capabilities(): MCP.ServerCapabilities { return this._serverInit.capabilities; } public get serverInfo(): MCP.Implementation { return this._serverInit.serverInfo; } public get serverInstructions(): string | undefined { return this._serverInit.instructions; } // Event emitters for server notifications private readonly _onDidReceiveCancelledNotification = this._register(new Emitter()); readonly onDidReceiveCancelledNotification = this._onDidReceiveCancelledNotification.event; private readonly _onDidReceiveProgressNotification = this._register(new Emitter()); readonly onDidReceiveProgressNotification = this._onDidReceiveProgressNotification.event; private readonly _onDidReceiveElicitationCompleteNotification = this._register(new Emitter()); readonly onDidReceiveElicitationCompleteNotification = this._onDidReceiveElicitationCompleteNotification.event; private readonly _onDidChangeResourceList = this._register(new Emitter()); readonly onDidChangeResourceList = this._onDidChangeResourceList.event; private readonly _onDidUpdateResource = this._register(new Emitter()); readonly onDidUpdateResource = this._onDidUpdateResource.event; private readonly _onDidChangeToolList = this._register(new Emitter()); readonly onDidChangeToolList = this._onDidChangeToolList.event; private readonly _onDidChangePromptList = this._register(new Emitter()); readonly onDidChangePromptList = this._onDidChangePromptList.event; /** * Connects to the MCP server and does the initialization handshake. * @throws MpcResponseError if the server fails to initialize. */ public static async create(instaService: IInstantiationService, opts: IMcpServerRequestHandlerOptions, token?: CancellationToken) { const mcp = new McpServerRequestHandler(opts); const store = new DisposableStore(); try { const timer = store.add(new IntervalTimer()); timer.cancelAndSet(() => { opts.logger.info('Waiting for server to respond to `initialize` request...'); }, 5000); await instaService.invokeFunction(async accessor => { const productService = accessor.get(IProductService); const initialized = await mcp.sendRequest({ method: 'initialize', params: { protocolVersion: MCP.LATEST_PROTOCOL_VERSION, capabilities: { roots: { listChanged: true }, sampling: opts.createMessageRequestHandler ? {} : undefined, elicitation: opts.elicitationRequestHandler ? { form: {}, url: {} } : undefined, tasks: { list: {}, cancel: {}, requests: { sampling: opts.createMessageRequestHandler ? { createMessage: {} } : undefined, elicitation: opts.elicitationRequestHandler ? { create: {} } : undefined, }, }, extensions: { 'io.modelcontextprotocol/ui': { mimeTypes: ['text/html;profile=mcp-app'] } } }, clientInfo: { name: productService.nameLong, version: productService.version, } } }, token); mcp._serverInit = initialized; mcp._sendLogLevelToServer(opts.logger.getLevel()); mcp.sendNotification({ method: 'notifications/initialized' }); }); return mcp; } catch (e) { mcp.dispose(); throw e; } finally { store.dispose(); } } public readonly logger: ILogger; private readonly _launch: IMcpMessageTransport; private readonly _requestLogLevel: LogLevel; private readonly _createMessageRequestHandler: IMcpServerRequestHandlerOptions['createMessageRequestHandler']; private readonly _elicitationRequestHandler: IMcpServerRequestHandlerOptions['elicitationRequestHandler']; private readonly _taskManager: McpTaskManager; protected constructor({ launch, logger, createMessageRequestHandler, elicitationRequestHandler, requestLogLevel = LogLevel.Debug, taskManager, }: IMcpServerRequestHandlerOptions) { super(); this._launch = launch; this.logger = logger; this._requestLogLevel = requestLogLevel; this._createMessageRequestHandler = createMessageRequestHandler; this._elicitationRequestHandler = elicitationRequestHandler; this._taskManager = taskManager; this._rpc = this._register(new JsonRpcProtocol( message => this.send(message as MCP.JSONRPCMessage), { handleRequest: (request, token) => this.handleServerRequest(request as MCP.JSONRPCRequest & MCP.ServerRequest, token), handleNotification: notification => this.handleServerNotification(notification as MCP.JSONRPCNotification & MCP.ServerNotification), } )); // Attach this handler to the task manager this._taskManager.setHandler(this); this._register(this._taskManager.onDidUpdateTask(task => { this.send({ jsonrpc: MCP.JSONRPC_VERSION, method: 'notifications/tasks/status', params: task } satisfies MCP.TaskStatusNotification); })); this._register(toDisposable(() => this._taskManager.setHandler(undefined))); this._register(launch.onDidReceiveMessage(message => { if (canLog(this.logger.getLevel(), this._requestLogLevel)) { log(this.logger, this._requestLogLevel, `[server -> editor] ${JSON.stringify(message)}`); } void this._rpc.handleMessage(message); })); this._register(autorun(reader => { const state = launch.state.read(reader).state; // the handler will get disposed when the launch stops, but if we're still // create()'ing we need to make sure to cancel the initialize request. if (state === McpConnectionState.Kind.Error || state === McpConnectionState.Kind.Stopped) { this.cancelAllRequests(); } })); // Listen for log level changes and forward them to the MCP server this._register(logger.onDidChangeLogLevel((logLevel) => { this._sendLogLevelToServer(logLevel); })); } /** * Send a client request to the server and return the response. * * @param request The request to send * @param token Cancellation token * @param timeoutMs Optional timeout in milliseconds * @returns A promise that resolves with the response */ private async sendRequest( request: Pick, token: CancellationToken = CancellationToken.None ): Promise { if (this._store.isDisposed) { return Promise.reject(new CancellationError()); } return this._rpc.sendRequest( request, token, id => this.sendNotification({ method: 'notifications/cancelled', params: { requestId: id } }) ).catch(error => { if (error instanceof JsonRpcError) { throw new MpcResponseError(error.message, error.code, error.data); } throw error; }); } private send(mcp: MCP.JSONRPCMessage) { if (canLog(this.logger.getLevel(), this._requestLogLevel)) { // avoid building the string if we don't need to log(this.logger, this._requestLogLevel, `[editor -> server] ${JSON.stringify(mcp)}`); } this._launch.send(mcp); } /** * Handles paginated requests by making multiple requests until all items are retrieved. * * @param method The method name to call * @param getItems Function to extract the array of items from a result * @param initialParams Initial parameters * @param token Cancellation token * @returns Promise with all items combined */ private async *sendRequestPaginated(method: T['method'], getItems: (result: R) => I[], initialParams?: Omit, token: CancellationToken = CancellationToken.None): AsyncIterable { let nextCursor: MCP.Cursor | undefined = undefined; do { const params: T['params'] = { ...initialParams, cursor: nextCursor }; const result: R = await this.sendRequest({ method, params }, token); yield getItems(result); nextCursor = result.nextCursor; } while (nextCursor !== undefined && !token.isCancellationRequested); } private sendNotification(notification: Omit): void { this.send({ ...notification, jsonrpc: MCP.JSONRPC_VERSION }); } /** * Handle incoming server requests */ private handleServerRequest(request: MCP.JSONRPCRequest & MCP.ServerRequest, token: CancellationToken): MCP.Result | Promise { const mapError = (error: unknown): JsonRpcError => { if (error instanceof McpError) { return new JsonRpcError(error.code, error.message, error.data); } this.logger.error(`Error handling request ${request.method}:`, error); const mcpError = McpError.unknown(error instanceof Error ? error : new Error(String(error))); return new JsonRpcError(mcpError.code, mcpError.message, mcpError.data); }; try { let result: MCP.Result | Promise; if (request.method === 'ping') { result = this.handlePing(request); } else if (request.method === 'roots/list') { result = this.handleRootsList(request); } else if (request.method === 'sampling/createMessage' && this._createMessageRequestHandler) { // Check if this is a task-augmented request if (request.params.task) { const taskResult = this._taskManager.createTask( request.params.task.ttl ?? null, (token) => this._createMessageRequestHandler!(request.params, token) ); taskResult._meta ??= {}; taskResult._meta['io.modelcontextprotocol/related-task'] = { taskId: taskResult.task.taskId }; result = taskResult; } else { result = this._createMessageRequestHandler(request.params, token); } } else if (request.method === 'elicitation/create' && this._elicitationRequestHandler) { if (typeof request.params?.message !== 'string') { throw new McpError(MCP.INVALID_PARAMS, 'Elicitation message must be a string'); } // Check if this is a task-augmented request if (request.params.task) { const taskResult = this._taskManager.createTask( request.params.task.ttl ?? null, (token) => this._elicitationRequestHandler!(request.params, token) ); taskResult._meta ??= {}; taskResult._meta['io.modelcontextprotocol/related-task'] = { taskId: taskResult.task.taskId }; result = taskResult; } else { result = this._elicitationRequestHandler(request.params, token); } } else if (request.method === 'tasks/get') { result = this._taskManager.getTask(request.params.taskId); } else if (request.method === 'tasks/result') { result = this._taskManager.getTaskResult(request.params.taskId); } else if (request.method === 'tasks/cancel') { result = this._taskManager.cancelTask(request.params.taskId); } else if (request.method === 'tasks/list') { result = this._taskManager.listTasks(); } else { throw McpError.methodNotFound(request.method); } if (isThenable(result)) { return result.then(undefined, (error: unknown) => { throw mapError(error); }); } return result; } catch (e) { throw mapError(e); } } /** * Handle incoming server notifications */ private handleServerNotification(request: MCP.JSONRPCNotification & MCP.ServerNotification): void { try { switch (request.method) { case 'notifications/message': return this.handleLoggingNotification(request); case 'notifications/cancelled': this._onDidReceiveCancelledNotification.fire(request); return this.handleCancelledNotification(request); case 'notifications/progress': this._onDidReceiveProgressNotification.fire(request); return; case 'notifications/resources/list_changed': this._onDidChangeResourceList.fire(); return; case 'notifications/resources/updated': this._onDidUpdateResource.fire(request); return; case 'notifications/tools/list_changed': this._onDidChangeToolList.fire(); return; case 'notifications/prompts/list_changed': this._onDidChangePromptList.fire(); return; case 'notifications/elicitation/complete': this._onDidReceiveElicitationCompleteNotification.fire(request); return; case 'notifications/tasks/status': this._taskManager.getClientTask(request.params.taskId)?.onDidUpdateState(request.params); return; default: softAssertNever(request); } } catch (error) { this.logger.error(`Error handling notification ${request.method}:`, error); } } private handleCancelledNotification(request: MCP.CancelledNotification): void { if (request.params.requestId) { this._rpc.cancelPendingRequest(request.params.requestId); } } private handleLoggingNotification(request: MCP.LoggingMessageNotification): void { translateMcpLogMessage(this.logger, request.params); } /** * Send a response to a ping request */ private handlePing(_request: MCP.PingRequest): {} { return {}; } /** * Send a response to a roots/list request */ private handleRootsList(_request: MCP.ListRootsRequest): MCP.ListRootsResult { this._hasAnnouncedRoots = true; return { roots: this._roots }; } private cancelAllRequests() { this._rpc.cancelAllRequests(); } public override dispose(): void { this.cancelAllRequests(); super.dispose(); } /** * Forwards log level changes to the MCP server if it supports logging */ private async _sendLogLevelToServer(logLevel: LogLevel): Promise { try { // Only send if the server supports logging capabilities if (!this.capabilities.logging) { return; } await this.setLevel({ level: mapLogLevelToMcp(logLevel) }); } catch (error) { this.logger.error(`Failed to set MCP server log level: ${error}`); } } /** * Send an initialize request */ initialize(params: MCP.InitializeRequest['params'], token?: CancellationToken): Promise { return this.sendRequest({ method: 'initialize', params }, token); } /** * List available resources */ listResources(params?: MCP.ListResourcesRequest['params'], token?: CancellationToken): Promise { return Iterable.asyncToArrayFlat(this.listResourcesIterable(params, token)); } /** * List available resources (iterable) */ listResourcesIterable(params?: MCP.ListResourcesRequest['params'], token?: CancellationToken): AsyncIterable { return this.sendRequestPaginated('resources/list', result => result.resources, params, token); } /** * Read a specific resource */ readResource(params: MCP.ReadResourceRequest['params'], token?: CancellationToken): Promise { return this.sendRequest({ method: 'resources/read', params }, token); } /** * List available resource templates */ listResourceTemplates(params?: MCP.ListResourceTemplatesRequest['params'], token?: CancellationToken): Promise { return Iterable.asyncToArrayFlat(this.sendRequestPaginated('resources/templates/list', result => result.resourceTemplates, params, token)); } /** * Subscribe to resource updates */ subscribe(params: MCP.SubscribeRequest['params'], token?: CancellationToken): Promise { return this.sendRequest({ method: 'resources/subscribe', params }, token); } /** * Unsubscribe from resource updates */ unsubscribe(params: MCP.UnsubscribeRequest['params'], token?: CancellationToken): Promise { return this.sendRequest({ method: 'resources/unsubscribe', params }, token); } /** * List available prompts */ listPrompts(params?: MCP.ListPromptsRequest['params'], token?: CancellationToken): Promise { return Iterable.asyncToArrayFlat(this.sendRequestPaginated('prompts/list', result => result.prompts, params, token)); } /** * Get a specific prompt */ getPrompt(params: MCP.GetPromptRequest['params'], token?: CancellationToken): Promise { return this.sendRequest({ method: 'prompts/get', params }, token); } /** * List available tools */ listTools(params?: MCP.ListToolsRequest['params'], token?: CancellationToken): Promise { return Iterable.asyncToArrayFlat(this.sendRequestPaginated('tools/list', result => result.tools, params, token)); } /** * Call a specific tool. Supports tasks automatically if `task` is set on the request. */ async callTool(params: MCP.CallToolRequest['params'] & MCP.Request['params'], token?: CancellationToken, onStatusMessage?: (message: string) => void): Promise { const response = await this.sendRequest({ method: 'tools/call', params }, token); if (isTaskResult(response)) { const task = new McpTask(response.task, token, onStatusMessage); this._taskManager.adoptClientTask(task); task.setHandler(this); return task.result.finally(() => { this._taskManager.abandonClientTask(task.id); }); } return response; } /** * Set the logging level */ setLevel(params: MCP.SetLevelRequest['params'], token?: CancellationToken): Promise { return this.sendRequest({ method: 'logging/setLevel', params }, token); } /** * Find completions for an argument */ complete(params: MCP.CompleteRequest['params'], token?: CancellationToken): Promise { return this.sendRequest({ method: 'completion/complete', params }, token); } /** * Get task status */ getTask(params: { taskId: string }, token?: CancellationToken): Promise { return this.sendRequest({ method: 'tasks/get', params }, token); } /** * Get task result */ getTaskResult(params: { taskId: string }, token?: CancellationToken): Promise { return this.sendRequest({ method: 'tasks/result', params }, token); } /** * Cancel a task */ cancelTask(params: { taskId: string }, token?: CancellationToken): Promise { return this.sendRequest({ method: 'tasks/cancel', params }, token); } /** * List all tasks */ listTasks(params?: MCP.ListTasksRequest['params'], token?: CancellationToken): Promise { return Iterable.asyncToArrayFlat( this.sendRequestPaginated( 'tasks/list', result => result.tasks, params, token ) ); } } function isTaskInTerminalState(task: MCP.Task): boolean { return task.status === 'completed' || task.status === 'failed' || task.status === 'cancelled'; } /** * Implementation of a task that handles polling, status notifications, and handler reconnections. It implements the task polling loop internally and can also be * updated externally via `onDidUpdateState`, when notifications are received * for example. * @internal */ export class McpTask extends Disposable implements IMcpTaskInternal { private readonly promise = new DeferredPromise(); public get result(): Promise { return this.promise.p; } public get id() { return this._task.taskId; } private _lastTaskState: ISettableObservable; private _handler = observableValue('mcpTaskHandler', undefined); constructor( private readonly _task: MCP.Task, _token: CancellationToken = CancellationToken.None, private readonly _onStatusMessage?: (message: string) => void, ) { super(); const expiresAt = _task.ttl ? (Date.now() + _task.ttl) : undefined; this._lastTaskState = observableValue('lastTaskState', this._task); const store = this._register(new DisposableStore()); // Handle external cancellation token if (_token.isCancellationRequested) { this._lastTaskState.set({ ...this._task, status: 'cancelled' }, undefined); } else { store.add(_token.onCancellationRequested(() => { const current = this._lastTaskState.get(); if (!isTaskInTerminalState(current)) { this._lastTaskState.set({ ...current, status: 'cancelled' }, undefined); } })); } // Handle TTL expiration with an explicit timeout if (expiresAt) { const ttlTimeout = expiresAt - Date.now(); if (ttlTimeout <= 0) { this._lastTaskState.set({ ...this._task, status: 'cancelled', statusMessage: 'Task timed out.' }, undefined); } else { store.add(disposableTimeout(() => { const current = this._lastTaskState.get(); if (!isTaskInTerminalState(current)) { this._lastTaskState.set({ ...current, status: 'cancelled', statusMessage: 'Task timed out.' }, undefined); } }, ttlTimeout)); } } // A `tasks/result` call triggered by an input_required state. const inputRequiredLookup = observableValue | undefined>('activeResultLookup', undefined); // 1. Poll for task updates when the task isn't in a terminal state store.add(autorun(reader => { const current = this._lastTaskState.read(reader); if (isTaskInTerminalState(current)) { return; } // When a task goes into the input_required state, by spec we should call // `tasks/result` which can return an SSE stream of task updates. No need // to poll while such a lookup is going on, but once it resolves we should // clear and update our state. const lookup = inputRequiredLookup.read(reader); if (lookup) { const result = lookup.promiseResult.read(reader); return transaction(tx => { if (!result) { // still ongoing } else if (result.data) { inputRequiredLookup.set(undefined, tx); this._lastTaskState.set(result.data, tx); } else { inputRequiredLookup.set(undefined, tx); if (result.error instanceof McpError && result.error.code === MCP.INVALID_PARAMS) { this._lastTaskState.set({ ...current, status: 'cancelled' }, undefined); } else { // Maybe a connection error -- start polling again this._lastTaskState.set({ ...current, status: 'working' }, undefined); } } }); } const handler = this._handler.read(reader); if (!handler) { return; } const pollInterval = _task.pollInterval ?? 2000; const cts = new CancellationTokenSource(_token); reader.store.add(toDisposable(() => cts.dispose(true))); reader.store.add(disposableTimeout(() => { handler.getTask({ taskId: current.taskId }, cts.token) .catch((e): MCP.Task | undefined => { if (e instanceof McpError && e.code === MCP.INVALID_PARAMS) { return { ...current, status: 'cancelled' }; } else { return { ...current }; // errors are already logged, keep in current state } }) .then(r => { if (r && !cts.token.isCancellationRequested) { this._lastTaskState.set(r, undefined); } }); }, pollInterval)); })); // 2. Get the result once it's available (or propagate errors). Trigger // input_required handling as needed. Only react when the status itself changes. const lastStatus = this._lastTaskState.map(task => task.status); store.add(autorun(reader => { const status = lastStatus.read(reader); if (status === 'failed') { const current = this._lastTaskState.read(undefined); this.promise.error(new Error(`Task ${current.taskId} failed: ${current.statusMessage ?? 'unknown error'}`)); store.dispose(); } else if (status === 'cancelled') { this.promise.cancel(); store.dispose(); } else if (status === 'input_required') { const handler = this._handler.read(reader); if (handler) { const current = this._lastTaskState.read(undefined); const cts = new CancellationTokenSource(_token); reader.store.add(toDisposable(() => cts.dispose(true))); inputRequiredLookup.set(new ObservablePromise(handler.getTask({ taskId: current.taskId }, cts.token)), undefined); } } else if (status === 'completed') { const handler = this._handler.read(reader); if (handler) { this.promise.settleWith(handler.getTaskResult({ taskId: _task.taskId }, _token) as Promise); store.dispose(); } } else if (status === 'working') { // no-op } else { softAssertNever(status); } })); } onDidUpdateState(task: MCP.Task) { this._lastTaskState.set(task, undefined); if (task.statusMessage && this._onStatusMessage) { this._onStatusMessage(task.statusMessage); } } setHandler(handler: McpServerRequestHandler | undefined): void { this._handler.set(handler, undefined); } } /** * Maps VSCode LogLevel to MCP LoggingLevel */ function mapLogLevelToMcp(logLevel: LogLevel): MCP.LoggingLevel { switch (logLevel) { case LogLevel.Trace: return 'debug'; // MCP doesn't have trace, use debug case LogLevel.Debug: return 'debug'; case LogLevel.Info: return 'info'; case LogLevel.Warning: return 'warning'; case LogLevel.Error: return 'error'; case LogLevel.Off: return 'emergency'; // MCP doesn't have off, use emergency default: return assertNever(logLevel); // Off and other levels are not supported } }