ui: initialize only policy-enabled MCP servers for a flow

ensureInitialized accepts an optional server id set; the agentic
flow passes the servers its tool policy leaves usable, so servers
disabled for the conversation no longer get connected. Callers
without arguments keep the global behavior.

Assisted-by: pi
This commit is contained in:
Aleksander Grygier
2026-08-27 11:03:12 +02:00
committed by GitHub
parent b520daf9a7
commit cbace8af70
2 changed files with 26 additions and 13 deletions
+12 -10
View File
@@ -329,18 +329,20 @@ class AgenticStore {
const disabledToolCategories = new Set(
toolPolicy?.disabledToolCategories ?? toolsStore.disabledToolCategories
);
// skip MCP init when the policy leaves no usable server: either the whole
// MCP category is off, or every globally-enabled server has its group key
// disabled for this flow
const hasMcpServers =
mcpStore.hasEnabledServers() &&
!disabledToolCategories.has(ToolSource.MCP) &&
mcpStore
.getServers()
.some((s) => s.enabled && !disabledTools.has(toolsStore.getMcpServerToolsKey(s.id)));
// servers usable under this flow's policy: globally enabled, category on,
// and their server-scoped group key not disabled
const policyEnabledServerIds = new Set(
disabledToolCategories.has(ToolSource.MCP)
? []
: mcpStore
.getServers()
.filter((s) => s.enabled && !disabledTools.has(toolsStore.getMcpServerToolsKey(s.id)))
.map((s) => s.id)
);
const hasMcpServers = policyEnabledServerIds.size > 0;
if (hasMcpServers) {
const initialized = await mcpStore.ensureInitialized();
const initialized = await mcpStore.ensureInitialized(policyEnabledServerIds);
if (!initialized) {
console.log('[AgenticStore] MCP not initialized');
+14 -3
View File
@@ -306,12 +306,18 @@ class MCPStore implements McpHealthHost {
return extras;
}
async ensureInitialized(): Promise<boolean> {
/**
* Initialize connections. Callers can restrict which servers connect by
* passing their ids (e.g. a conversation's tool policy); a different set
* changes the config signature and re-initializes, same as a settings
* change.
*/
async ensureInitialized(serverIds?: ReadonlySet<string>): Promise<boolean> {
if (!browser) {
return false;
}
const mcpConfig = this.buildMcpClientConfig(settingsStore.config);
const mcpConfig = this.buildMcpClientConfig(settingsStore.config, serverIds);
const signature = mcpConfig ? JSON.stringify(mcpConfig) : null;
if (!signature) {
@@ -1157,7 +1163,10 @@ class MCPStore implements McpHealthHost {
/**
* Builds MCP client configuration from settings.
*/
private buildMcpClientConfig(cfg: SettingsConfigType): MCPClientConfig | undefined {
private buildMcpClientConfig(
cfg: SettingsConfigType,
serverIds?: ReadonlySet<string>
): MCPClientConfig | undefined {
const rawServers = parseMcpServerSettings(cfg.mcpServers);
if (!rawServers.length) {
@@ -1169,6 +1178,8 @@ class MCPStore implements McpHealthHost {
for (const [index, entry] of rawServers.entries()) {
if (!entry.enabled) continue;
if (serverIds && !serverIds.has(entry.id)) continue;
const normalized = this.buildServerConfig(entry);
if (normalized) servers[this.generateServerId(entry.id, index)] = normalized;