File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -35,13 +35,25 @@ def mcp(
3535 - sse: Server-Sent Events (for compatibility with existing clients)
3636 """
3737
38+ from basic_memory .config import get_project_config
39+ from basic_memory .mcp .project_session import session
3840 from basic_memory .services .initialization import initialize_file_sync
3941
4042 # Use unified thread-based sync approach for both transports
4143 import threading
4244
45+ if session .current_project :
46+ project_config = get_project_config (session .current_project )
47+ else :
48+ project_config = get_project_config ()
49+
4350 app_config = ConfigManager ().config
4451
52+ # Ensure the session is properly initialized with the project before starting MCP
53+ # This will be picked up by the MCP server lifespan
54+ session .initialize (app_config .default_project )
55+ session .set_current_project (project_config .name )
56+
4557 def run_file_sync ():
4658 """Run file sync in a separate thread with its own event loop."""
4759 loop = asyncio .new_event_loop ()
Original file line number Diff line number Diff line change @@ -68,16 +68,22 @@ def reset_to_default(self) -> None: # pragma: no cover
6868 def refresh_from_config (self ) -> None :
6969 """Refresh session state from current configuration.
7070
71- This method reloads the default project from config and reinitializes
72- the session. This should be called when the default project is changed
73- via CLI or API to ensure MCP session stays in sync .
71+ This method reloads the default project from config and updates
72+ the default_project. It preserves the current_project if it was
73+ explicitly set (e.g. via CLI --project flag) .
7474 """
7575 # Reload config to get latest default project
7676 current_config = ConfigManager ().config
7777 new_default = current_config .default_project
7878
79- # Reinitialize with new default
80- self .initialize (new_default )
79+ # Update default project without touching current project
80+ old_default = self .default_project
81+ self .default_project = new_default
82+
83+ # Only update current project if it was the same as the old default
84+ if self .current_project == old_default :
85+ self .current_project = new_default
86+
8187 logger .info (f"Refreshed project session from config, new default: { new_default } " )
8288
8389
Original file line number Diff line number Diff line change @@ -29,8 +29,10 @@ async def app_lifespan(server: FastMCP) -> AsyncIterator[AppContext]: # pragma:
2929 # Initialize on startup (now returns migration_manager)
3030 migration_manager = await initialize_app (app_config )
3131
32- # Initialize project session with default project
33- session .initialize (app_config .default_project )
32+ # Initialize project session - preserve existing session if already set
33+ if not session .current_project :
34+ # Session not initialized by CLI, use default
35+ session .initialize (app_config .default_project )
3436
3537 try :
3638 yield AppContext (watch_task = None , migration_manager = migration_manager )
You can’t perform that action at this time.
0 commit comments