1- import { createRoot , getOwner , onCleanup , runWithOwner , type Owner } from "solid-js"
1+ import { createRoot , createSignal , getOwner , onCleanup , runWithOwner , type Owner } from "solid-js"
22import { createStore , type SetStoreFunction , type Store } from "solid-js/store"
33import { Persist , persisted } from "@/utils/persist"
44import type { VcsInfo } from "@opencode-ai/sdk/v2/client"
@@ -24,6 +24,7 @@ export function createChildStoreManager(input: {
2424 isBooting : ( directory : string ) => boolean
2525 isLoadingSessions : ( directory : string ) => boolean
2626 onBootstrap : ( directory : string ) => void
27+ onMcp : ( directory : string , setStore : SetStoreFunction < State > ) => void
2728 onDispose : ( directory : string ) => void
2829 translate : ( key : string , vars ?: Record < string , string | number > ) => string
2930 queryOptions : QueryOptionsApi
@@ -39,6 +40,8 @@ export function createChildStoreManager(input: {
3940 const pins = new Map < string , number > ( )
4041 const ownerPins = new WeakMap < object , Set < string > > ( )
4142 const disposers = new Map < string , ( ) => void > ( )
43+ const mcpDirectories = new Set < string > ( )
44+ const mcpToggles = new Map < string , ( enabled : boolean ) => void > ( )
4245
4346 const markKey = ( key : DirectoryKey ) => {
4447 if ( ! key ) return
@@ -99,6 +102,8 @@ export function createChildStoreManager(input: {
99102 metaCache . delete ( key )
100103 iconCache . delete ( key )
101104 lifecycle . delete ( key )
105+ mcpDirectories . delete ( key )
106+ mcpToggles . delete ( key )
102107 disposers . delete ( key )
103108 delete children [ key ]
104109 input . onDispose ( key )
@@ -121,7 +126,6 @@ export function createChildStoreManager(input: {
121126 ) {
122127 return false
123128 }
124-
125129 return disposeChild ( key )
126130 }
127131
@@ -184,11 +188,12 @@ export function createChildStoreManager(input: {
184188 createRoot ( ( dispose ) => {
185189 const initialMeta = meta [ 0 ] . value
186190 const initialIcon = icon [ 0 ] . value
191+ const [ mcpEnabled , setMcpEnabled ] = createSignal ( false )
187192
188193 const [ pathQuery , mcpQuery , lspQuery , providerQuery ] = useQueries ( ( ) => ( {
189194 queries : [
190195 input . queryOptions . path ( key ) ,
191- input . queryOptions . mcp ( key ) ,
196+ { ... input . queryOptions . mcp ( key ) , enabled : mcpEnabled ( ) } ,
192197 input . queryOptions . lsp ( key ) ,
193198 input . queryOptions . providers ( key ) ,
194199 ] ,
@@ -247,6 +252,7 @@ export function createChildStoreManager(input: {
247252 } )
248253 children [ key ] = child
249254 disposers . set ( key , dispose )
255+ mcpToggles . set ( key , setMcpEnabled )
250256
251257 const onPersistedInit = ( init : Promise < string > | string | null , run : ( ) => void ) => {
252258 if ( ! ( init instanceof Promise ) ) return
@@ -285,6 +291,7 @@ export function createChildStoreManager(input: {
285291 const key = directoryKey ( directory )
286292 const childStore = ensureChild ( directory )
287293 pinForOwner ( key )
294+ if ( options . mcp ) enableMcp ( directory , key , childStore )
288295 const shouldBootstrap = options . bootstrap ?? true
289296 if ( shouldBootstrap && childStore [ 0 ] . status === "loading" ) {
290297 input . onBootstrap ( directory )
@@ -295,13 +302,27 @@ export function createChildStoreManager(input: {
295302 function peek ( directory : string , options : ChildOptions = { } ) {
296303 const key = directoryKey ( directory )
297304 const childStore = ensureChild ( directory )
305+ if ( options . mcp ) enableMcp ( directory , key , childStore )
298306 const shouldBootstrap = options . bootstrap ?? true
299307 if ( shouldBootstrap && childStore [ 0 ] . status === "loading" ) {
300308 input . onBootstrap ( directory )
301309 }
302310 return childStore
303311 }
304312
313+ function enableMcp ( directory : string , key : DirectoryKey , childStore : [ Store < State > , SetStoreFunction < State > ] ) {
314+ if ( mcpDirectories . has ( key ) ) return
315+ mcpDirectories . add ( key )
316+ mcpToggles . get ( key ) ?.( true )
317+ if ( childStore [ 0 ] . status !== "loading" ) input . onMcp ( directory , childStore [ 1 ] )
318+ }
319+
320+ function disableMcp ( directory : string ) {
321+ const key = directoryKey ( directory )
322+ if ( ! mcpDirectories . delete ( key ) ) return
323+ mcpToggles . get ( key ) ?.( false )
324+ }
325+
305326 function projectMeta ( directory : string , patch : ProjectMeta ) {
306327 const key = directoryKey ( directory )
307328 const [ store , setStore ] = ensureChild ( directory )
@@ -341,6 +362,8 @@ export function createChildStoreManager(input: {
341362 pin,
342363 unpin,
343364 pinned,
365+ mcp : ( directory : string ) => mcpDirectories . has ( directoryKey ( directory ) ) ,
366+ disableMcp,
344367 disposeDirectory,
345368 disposeAll,
346369 runEviction,
0 commit comments