@@ -329,6 +329,52 @@ function MCPHub:start()
329329 end )
330330end
331331
332+ --- Handle directory changes - reconnect to appropriate workspace hub
333+ function MCPHub :handle_directory_change ()
334+ if not State .config .workspace .enabled then
335+ return
336+ end
337+
338+ log .debug (" Directory changed, checking if workspace hub should change" )
339+
340+ -- Resolve new context for current directory
341+ local new_context = self :resolve_context ()
342+ if not new_context then
343+ log .warn (" Failed to resolve context after directory change" )
344+ return
345+ end
346+
347+ -- Compare with current context
348+ local current_context = State .current_hub
349+ if not current_context then
350+ log .debug (" No current hub context, starting new hub" )
351+ self :start ()
352+ return
353+ end
354+
355+ vim .notify (" Dir changed" )
356+
357+ -- Check if we need to switch hubs
358+ local needs_switch = false
359+ local reason = " "
360+
361+ if new_context .port ~= current_context .port then
362+ needs_switch = true
363+ reason = string.format (" port change (%d -> %d)" , current_context .port , new_context .port )
364+ end
365+
366+ if needs_switch then
367+ log .debug (" Switching hub due to: " .. reason )
368+ -- self:restart(nil, reason)
369+ self :stop_sse ()
370+ vim .schedule (function ()
371+ self :start ()
372+ end )
373+ else
374+ log .debug (" No hub switch needed - context unchanged" )
375+ end
376+ end
377+
332378function MCPHub :handle_hub_ready ()
333379 self .ready = true
334380 self .is_restarting = false
@@ -1150,8 +1196,6 @@ function MCPHub:hard_refresh(callback)
11501196 if not self :ensure_ready () then
11511197 return
11521198 end
1153- -- Make sure to update the cache as well
1154- config_manager .refresh_config ()
11551199 self :api_request (" GET" , " refresh" , {
11561200 callback = function (response , err )
11571201 if err then
@@ -1189,7 +1233,10 @@ function MCPHub:handle_hub_stopping()
11891233 end
11901234end
11911235
1192- function MCPHub :restart (callback )
1236+ --- Restart the MCP Hub server
1237+ --- @param callback function | nil Optional callback to execute after restart
1238+ --- @param reason string | nil Optional reason for the restart
1239+ function MCPHub :restart (callback , reason )
11931240 if not self :ensure_ready () then
11941241 return self :start ()
11951242 end
0 commit comments