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
@@ -110,6 +113,8 @@ export function createChildStoreManager(input: {
110113 metaCache . delete ( key )
111114 iconCache . delete ( key )
112115 lifecycle . delete ( key )
116+ mcpDirectories . delete ( key )
117+ mcpToggles . delete ( key )
113118 const dispose = disposers . get ( key )
114119 if ( dispose ) {
115120 dispose ( )
@@ -173,11 +178,12 @@ export function createChildStoreManager(input: {
173178 createRoot ( ( dispose ) => {
174179 const initialMeta = meta [ 0 ] . value
175180 const initialIcon = icon [ 0 ] . value
181+ const [ mcpEnabled , setMcpEnabled ] = createSignal ( false )
176182
177183 const [ pathQuery , mcpQuery , lspQuery , providerQuery ] = useQueries ( ( ) => ( {
178184 queries : [
179185 input . queryOptions . path ( key ) ,
180- input . queryOptions . mcp ( key ) ,
186+ { ... input . queryOptions . mcp ( key ) , enabled : mcpEnabled ( ) } ,
181187 input . queryOptions . lsp ( key ) ,
182188 input . queryOptions . providers ( key ) ,
183189 ] ,
@@ -236,6 +242,7 @@ export function createChildStoreManager(input: {
236242 } )
237243 children [ key ] = child
238244 disposers . set ( key , dispose )
245+ mcpToggles . set ( key , setMcpEnabled )
239246
240247 const onPersistedInit = ( init : Promise < string > | string | null , run : ( ) => void ) => {
241248 if ( ! ( init instanceof Promise ) ) return
@@ -274,6 +281,7 @@ export function createChildStoreManager(input: {
274281 const key = directoryKey ( directory )
275282 const childStore = ensureChild ( directory )
276283 pinForOwner ( key )
284+ if ( options . mcp ) enableMcp ( directory , key , childStore )
277285 const shouldBootstrap = options . bootstrap ?? true
278286 if ( shouldBootstrap && childStore [ 0 ] . status === "loading" ) {
279287 input . onBootstrap ( directory )
@@ -284,13 +292,27 @@ export function createChildStoreManager(input: {
284292 function peek ( directory : string , options : ChildOptions = { } ) {
285293 const key = directoryKey ( directory )
286294 const childStore = ensureChild ( directory )
295+ if ( options . mcp ) enableMcp ( directory , key , childStore )
287296 const shouldBootstrap = options . bootstrap ?? true
288297 if ( shouldBootstrap && childStore [ 0 ] . status === "loading" ) {
289298 input . onBootstrap ( directory )
290299 }
291300 return childStore
292301 }
293302
303+ function enableMcp ( directory : string , key : DirectoryKey , childStore : [ Store < State > , SetStoreFunction < State > ] ) {
304+ if ( mcpDirectories . has ( key ) ) return
305+ mcpDirectories . add ( key )
306+ mcpToggles . get ( key ) ?.( true )
307+ if ( childStore [ 0 ] . status !== "loading" ) input . onMcp ( directory , childStore [ 1 ] )
308+ }
309+
310+ function disableMcp ( directory : string ) {
311+ const key = directoryKey ( directory )
312+ if ( ! mcpDirectories . delete ( key ) ) return
313+ mcpToggles . get ( key ) ?.( false )
314+ }
315+
294316 function projectMeta ( directory : string , patch : ProjectMeta ) {
295317 const key = directoryKey ( directory )
296318 const [ store , setStore ] = ensureChild ( directory )
@@ -330,6 +352,8 @@ export function createChildStoreManager(input: {
330352 pin,
331353 unpin,
332354 pinned,
355+ mcp : ( directory : string ) => mcpDirectories . has ( directoryKey ( directory ) ) ,
356+ disableMcp,
333357 disposeDirectory,
334358 runEviction,
335359 vcsCache,
0 commit comments