integrations/beads-mcp/CONTEXT_MANAGEMENT.md
MCP servers don't receive working directory context from AI clients (Claude Code/Amp), causing database routing issues:
bd uses tree-walking to discover databases based on CWDbd discovers wrong database or falls back to ~/.beadsWe've added two new MCP tools to allow explicit context management:
set_contextSets the workspace root directory for all bd operations.
Parameters:
workspace_root (string): Absolute path to workspace/project root directoryReturns: Confirmation message with resolved paths (workspace root and database)
Behavior:
.beads/*.dbBEADS_WORKING_DIR, BEADS_DB, and BEADS_CONTEXT_SET environment variableswhere_am_iShows current workspace context and database path for debugging.
Returns: Current context information including workspace root, database path, and actor
All write operations (create, update, close, reopen, dep, init) are decorated with @require_context.
Enforcement: Only enforced when BEADS_REQUIRE_CONTEXT=1 environment variable is set.
This allows backward compatibility while adding safety for multi-repo setups.
Environment Variable Persistence: FastMCP's architecture doesn't guarantee environment variables persist between tool calls. This means:
set_context sets env vars for that tool callNo changes needed. The MCP server works as before with auto-discovery.
Option 1: Explicit Database Path (Current Workaround)
{
"mcpServers": {
"beads-repo1": {
"command": "uvx",
"args": ["beads-mcp"],
"env": {
"BEADS_DB": "/path/to/repo1/.beads/prefix.db"
}
},
"beads-repo2": {
"command": "uvx",
"args": ["beads-mcp"],
"env": {
"BEADS_DB": "/path/to/repo2/.beads/prefix.db"
}
}
}
}
Option 2: Client-Side Context Management (Future) AI clients would need to:
set_context at session start with workspace rootOption 3: Dolt Server with RPC (Future - Path 1.5 from bd-105)
cwd parameter to Dolt server RPC protocolOption 4: Advanced Routing Server (Future - Path 2 from bd-105) For >50 repos:
The context management tools are tested in:
tests/test_mcp_server_integration.py: MCP tool tests/tmp/test-repo-{1,2} exampleRun tests:
uv run pytest tests/test_mcp_server_integration.py -v
See bd-105 for full architectural analysis and roadmap.
Priority: P0/P1 - Active data corruption risk in multi-repo setups.