Path: blob/main/src/vs/workbench/contrib/mcp/common/mcpContextKeys.ts
3296 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*--------------------------------------------------------------------------------------------*/456import { Disposable } from '../../../../base/common/lifecycle.js';7import { autorun } from '../../../../base/common/observable.js';8import { localize } from '../../../../nls.js';9import { IContextKeyService, RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';10import { bindContextKey } from '../../../../platform/observable/common/platformObservableUtils.js';11import { IWorkbenchContribution } from '../../../common/contributions.js';12import { IMcpService, LazyCollectionState, McpConnectionState, McpServerCacheState } from './mcpTypes.js';131415export namespace McpContextKeys {1617export const serverCount = new RawContextKey<number>('mcp.serverCount', undefined, { type: 'number', description: localize('mcp.serverCount.description', "Context key that has the number of registered MCP servers") });18export const hasUnknownTools = new RawContextKey<boolean>('mcp.hasUnknownTools', undefined, { type: 'boolean', description: localize('mcp.hasUnknownTools.description', "Indicates whether there are MCP servers with unknown tools.") });19/**20* A context key that indicates whether there are any servers with errors.21*22* @type {boolean}23* @default undefined24* @description This key is used to track the presence of servers with errors in the MCP context.25*/26export const hasServersWithErrors = new RawContextKey<boolean>('mcp.hasServersWithErrors', undefined, { type: 'boolean', description: localize('mcp.hasServersWithErrors.description', "Indicates whether there are any MCP servers with errors.") });27export const toolsCount = new RawContextKey<number>('mcp.toolsCount', undefined, { type: 'number', description: localize('mcp.toolsCount.description', "Context key that has the number of registered MCP tools") });28}293031export class McpContextKeysController extends Disposable implements IWorkbenchContribution {3233static readonly ID = 'workbench.contrib.mcp.contextKey';3435constructor(36@IMcpService mcpService: IMcpService,37@IContextKeyService contextKeyService: IContextKeyService,38) {39super();4041const ctxServerCount = McpContextKeys.serverCount.bindTo(contextKeyService);42const ctxToolsCount = McpContextKeys.toolsCount.bindTo(contextKeyService);43const ctxHasUnknownTools = McpContextKeys.hasUnknownTools.bindTo(contextKeyService);4445this._store.add(bindContextKey(McpContextKeys.hasServersWithErrors, contextKeyService, r => mcpService.servers.read(r).some(c => c.connectionState.read(r).state === McpConnectionState.Kind.Error)));4647this._store.add(autorun(r => {48const servers = mcpService.servers.read(r);49const serverTools = servers.map(s => s.tools.read(r));50ctxServerCount.set(servers.length);51ctxToolsCount.set(serverTools.reduce((count, tools) => count + tools.length, 0));52ctxHasUnknownTools.set(mcpService.lazyCollectionState.read(r).state !== LazyCollectionState.AllKnown || servers.some(s => {53const toolState = s.cacheState.read(r);54return toolState === McpServerCacheState.Unknown || toolState === McpServerCacheState.Outdated || toolState === McpServerCacheState.RefreshingFromUnknown;55}));56}));57}58}596061