Phase 1 ticket T12. Depends on #648 (T1 scaffold) and #652 (T4 index_repo).
Context
Zero-config startup is the difference between "this MCP server is annoying to install" and "this just works." Two automation behaviors make this real:
-
Ensure FalkorDB is running: when the MCP server boots, ping FalkorDB; if unreachable, start the existing Docker container automatically. The CLI already has cgraph ensure-db machinery — reuse it, don't duplicate.
-
Auto-index CWD on first tool call: when CODE_GRAPH_AUTO_INDEX=true is set (opt-in for safety), the first tool call indexes the current working directory automatically so the agent doesn't have to call index_repo first. Lazy and idempotent — second call is a no-op.
Both behaviors are gated and conservative: ensure-db is on-by-default but only takes action if FalkorDB is missing; auto-index requires explicit opt-in.
Scope
In:
- New
api/mcp/auto_init.py containing:
ensure_falkordb() — pings FalkorDB; if unreachable, calls the existing cgraph ensure-db Docker bootstrap from api/cli.py. Reuses the function — does not duplicate the Docker logic.
maybe_auto_index(project, branch) — checks CODE_GRAPH_AUTO_INDEX env var; if set and the (project, branch) graph is empty, indexes CWD via Project.from_local_directory() + analyze_sources(). Tracks "already auto-indexed this session" in a module-level flag so subsequent tool calls skip.
- Modified
api/mcp/server.py:
- On startup: call
ensure_falkordb() before any tool is registered.
- First tool-call middleware: call
maybe_auto_index() once per session per (project, branch).
- Tests in
tests/mcp/test_auto_init.py:
- ensure_falkordb test: mock
subprocess.run to simulate FalkorDB unreachable → assert the Docker bootstrap is invoked.
- ensure_falkordb test: simulate FalkorDB reachable → assert no Docker call is made.
- auto-index test: env var set, empty graph → first tool call triggers
index_repo; second tool call does not.
- auto-index test: env var unset → first tool call does not trigger indexing.
Out:
- Auto-detection of
MODEL_NAME or other config (env-only).
- Auto-cleanup / shutdown teardown.
- Re-indexing on file change (Phase 2 or Phase 3 watch mode).
- Container management beyond what
cgraph ensure-db already does.
Files to create / modify
- new
api/mcp/auto_init.py
api/mcp/server.py — startup hook + first-tool-call middleware
- new
tests/mcp/test_auto_init.py
Acceptance criteria
Dependencies
Out of scope (do NOT do in this PR)
- Watch-mode / re-indexing on filesystem change.
- Auto-pulling the FalkorDB Docker image if missing (the existing
cgraph ensure-db already handles that).
- Cross-session state (tracking which projects were auto-indexed across server restarts).
- Telemetry.
Notes for the implementer
- Critical: the
cgraph ensure-db logic already exists in api/cli.py. Find it and call it directly — do not reimplement Docker/subprocess code in api/mcp/.
- The "first tool call" middleware can be a FastMCP tool-call hook (check the SDK's hook API) or a small wrapper around each registered tool. Prefer the SDK hook if it exists.
- The auto-index flag should be a module-level
set[tuple[str, str]] of (project, branch) tuples already auto-indexed this session — that way different projects in the same session each get one auto-index.
- Default
CODE_GRAPH_AUTO_INDEX to off because indexing can take minutes on large repos and surprising the user with that on first call is bad UX. Document the env var clearly in T13's agent guidance.
- For unit tests, mock
redis.Redis.ping to simulate reachable/unreachable.
Phase 1 ticket T12. Depends on #648 (T1 scaffold) and #652 (T4 index_repo).
Context
Zero-config startup is the difference between "this MCP server is annoying to install" and "this just works." Two automation behaviors make this real:
Ensure FalkorDB is running: when the MCP server boots, ping FalkorDB; if unreachable, start the existing Docker container automatically. The CLI already has
cgraph ensure-dbmachinery — reuse it, don't duplicate.Auto-index CWD on first tool call: when
CODE_GRAPH_AUTO_INDEX=trueis set (opt-in for safety), the first tool call indexes the current working directory automatically so the agent doesn't have to callindex_repofirst. Lazy and idempotent — second call is a no-op.Both behaviors are gated and conservative: ensure-db is on-by-default but only takes action if FalkorDB is missing; auto-index requires explicit opt-in.
Scope
In:
api/mcp/auto_init.pycontaining:ensure_falkordb()— pings FalkorDB; if unreachable, calls the existingcgraph ensure-dbDocker bootstrap fromapi/cli.py. Reuses the function — does not duplicate the Docker logic.maybe_auto_index(project, branch)— checksCODE_GRAPH_AUTO_INDEXenv var; if set and the(project, branch)graph is empty, indexes CWD viaProject.from_local_directory()+analyze_sources(). Tracks "already auto-indexed this session" in a module-level flag so subsequent tool calls skip.api/mcp/server.py:ensure_falkordb()before any tool is registered.maybe_auto_index()once per session per(project, branch).tests/mcp/test_auto_init.py:subprocess.runto simulate FalkorDB unreachable → assert the Docker bootstrap is invoked.index_repo; second tool call does not.Out:
MODEL_NAMEor other config (env-only).cgraph ensure-dbalready does.Files to create / modify
api/mcp/auto_init.pyapi/mcp/server.py— startup hook + first-tool-call middlewaretests/mcp/test_auto_init.pyAcceptance criteria
cgraph-mcpagainst an unreachable FalkorDB triggers the existingcgraph ensure-dbDocker bootstrap and continues to start the server.cgraph-mcpagainst a reachable FalkorDB does not invoke any Docker subprocess.CODE_GRAPH_AUTO_INDEX=trueand an empty graph, the first tool call (e.g.search_code) triggersindex_repo(".")automatically before the tool's own logic runs.CODE_GRAPH_AUTO_INDEX=true, no auto-indexing occurs (default off).Dependencies
index_repoto call).Out of scope (do NOT do in this PR)
cgraph ensure-dbalready handles that).Notes for the implementer
cgraph ensure-dblogic already exists inapi/cli.py. Find it and call it directly — do not reimplement Docker/subprocess code inapi/mcp/.set[tuple[str, str]]of(project, branch)tuples already auto-indexed this session — that way different projects in the same session each get one auto-index.CODE_GRAPH_AUTO_INDEXto off because indexing can take minutes on large repos and surprising the user with that on first call is bad UX. Document the env var clearly in T13's agent guidance.redis.Redis.pingto simulate reachable/unreachable.