|
| 1 | +"""Project session management for Basic Memory MCP server. |
| 2 | +
|
| 3 | +Provides simple in-memory project context for MCP tools, allowing users to switch |
| 4 | +between projects during a conversation without restarting the server. |
| 5 | +
|
| 6 | +Session Persistence Flow: |
| 7 | + 1. Pre-tool: Middleware loads self.sessions[session_id] → sets context state |
| 8 | + 2. Tool execution: Uses context.get_state("active_project") |
| 9 | + 3. Project switching: Tool calls set_active_project() → updates context state |
| 10 | + 4. Post-tool: Middleware detects context change → saves to self.sessions[session_id] |
| 11 | +""" |
| 12 | + |
| 13 | +from typing import Optional |
| 14 | +from httpx import AsyncClient |
| 15 | +from loguru import logger |
| 16 | + |
| 17 | +from fastmcp import Context |
| 18 | + |
| 19 | +from basic_memory.mcp.tools.utils import call_get |
| 20 | +from basic_memory.schemas.project_info import ProjectItem |
| 21 | +from basic_memory.utils import generate_permalink |
| 22 | + |
| 23 | + |
| 24 | +async def set_active_project( |
| 25 | + client: AsyncClient, *, context: Context | None, project: ProjectItem |
| 26 | +) -> None: |
| 27 | + """Set the active project context. |
| 28 | +
|
| 29 | + Args: |
| 30 | + client (AsyncClient): The client to use for making requests to fastapi. |
| 31 | + project_name: The project to switch to |
| 32 | + context: Optional FastMCP context containing project session state |
| 33 | + """ |
| 34 | + active_project = await get_active_project(client, context=context) |
| 35 | + if active_project.name != project.name: |
| 36 | + previous_project = active_project |
| 37 | + # set project in the context |
| 38 | + if context: |
| 39 | + context.set_state("active_project", project) |
| 40 | + await context.info( |
| 41 | + f"Context {context.session_id} Switched active project: {previous_project} -> {project.name}" |
| 42 | + ) |
| 43 | + logger.info(f"Switched active project: {previous_project} -> {project.name}") |
| 44 | + else: |
| 45 | + logger.debug(f"No change for active project: {active_project}") |
| 46 | + |
| 47 | + |
| 48 | +async def get_active_project( |
| 49 | + client: AsyncClient, *, context: Context | None, project_override: Optional[str] = None |
| 50 | +) -> ProjectItem: |
| 51 | + """ |
| 52 | + Get the active project for a tool call. |
| 53 | + If no context is provided, the active project is returned from the default project |
| 54 | +
|
| 55 | + Args: |
| 56 | + client (AsyncClient): The client to use for making requests to fastapi. |
| 57 | + project_override: Optional explicit project name from tool parameter |
| 58 | + context: Optional FastMCP context containing project session state |
| 59 | +
|
| 60 | + Returns: |
| 61 | + The project to use for the tool call |
| 62 | + """ |
| 63 | + |
| 64 | + # Try to get active project from context first |
| 65 | + if context: |
| 66 | + active_project = context.get_state("active_project") |
| 67 | + logger.debug(f"Context {context.session_id} found active project: {active_project}") |
| 68 | + else: |
| 69 | + response = await call_get( |
| 70 | + client, |
| 71 | + "/projects/default", |
| 72 | + ) |
| 73 | + active_project = ProjectItem.model_validate(response.json()) |
| 74 | + logger.debug(f"No context provided. Using default project: {active_project}") |
| 75 | + |
| 76 | + # Handle project override |
| 77 | + if project_override: |
| 78 | + logger.debug(f"overriding active project: {project_override}") |
| 79 | + permalink = generate_permalink(project_override) |
| 80 | + response = await call_get( |
| 81 | + client, |
| 82 | + f"/{permalink}/project/item", |
| 83 | + ) |
| 84 | + active_project = ProjectItem.model_validate(response.json()) |
| 85 | + |
| 86 | + return active_project |
| 87 | + |
| 88 | + |
| 89 | +def add_project_metadata(result: str, project_name: str) -> str: |
| 90 | + """Add project context as metadata footer for LLM awareness. |
| 91 | +
|
| 92 | + Args: |
| 93 | + result: The tool result string |
| 94 | + project_name: The project name that was used |
| 95 | +
|
| 96 | + Returns: |
| 97 | + Result with project metadata footer |
| 98 | + """ |
| 99 | + return f"{result}\n\n<!-- Project: {project_name} -->" # pragma: no cover |
0 commit comments