Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/mcp/common/mcpContextKeys.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
7
import { Disposable } from '../../../../base/common/lifecycle.js';
8
import { autorun } from '../../../../base/common/observable.js';
9
import { localize } from '../../../../nls.js';
10
import { IContextKeyService, RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';
11
import { bindContextKey } from '../../../../platform/observable/common/platformObservableUtils.js';
12
import { IWorkbenchContribution } from '../../../common/contributions.js';
13
import { IMcpService, LazyCollectionState, McpConnectionState, McpServerCacheState } from './mcpTypes.js';
14
15
16
export namespace McpContextKeys {
17
18
export 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") });
19
export const hasUnknownTools = new RawContextKey<boolean>('mcp.hasUnknownTools', undefined, { type: 'boolean', description: localize('mcp.hasUnknownTools.description', "Indicates whether there are MCP servers with unknown tools.") });
20
/**
21
* A context key that indicates whether there are any servers with errors.
22
*
23
* @type {boolean}
24
* @default undefined
25
* @description This key is used to track the presence of servers with errors in the MCP context.
26
*/
27
export const hasServersWithErrors = new RawContextKey<boolean>('mcp.hasServersWithErrors', undefined, { type: 'boolean', description: localize('mcp.hasServersWithErrors.description', "Indicates whether there are any MCP servers with errors.") });
28
export 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") });
29
}
30
31
32
export class McpContextKeysController extends Disposable implements IWorkbenchContribution {
33
34
static readonly ID = 'workbench.contrib.mcp.contextKey';
35
36
constructor(
37
@IMcpService mcpService: IMcpService,
38
@IContextKeyService contextKeyService: IContextKeyService,
39
) {
40
super();
41
42
const ctxServerCount = McpContextKeys.serverCount.bindTo(contextKeyService);
43
const ctxToolsCount = McpContextKeys.toolsCount.bindTo(contextKeyService);
44
const ctxHasUnknownTools = McpContextKeys.hasUnknownTools.bindTo(contextKeyService);
45
46
this._store.add(bindContextKey(McpContextKeys.hasServersWithErrors, contextKeyService, r => mcpService.servers.read(r).some(c => c.connectionState.read(r).state === McpConnectionState.Kind.Error)));
47
48
this._store.add(autorun(r => {
49
const servers = mcpService.servers.read(r);
50
const serverTools = servers.map(s => s.tools.read(r));
51
ctxServerCount.set(servers.length);
52
ctxToolsCount.set(serverTools.reduce((count, tools) => count + tools.length, 0));
53
ctxHasUnknownTools.set(mcpService.lazyCollectionState.read(r).state !== LazyCollectionState.AllKnown || servers.some(s => {
54
const toolState = s.cacheState.read(r);
55
return toolState === McpServerCacheState.Unknown || toolState === McpServerCacheState.Outdated || toolState === McpServerCacheState.RefreshingFromUnknown;
56
}));
57
}));
58
}
59
}
60
61