feat(mcp): auto-init — ensure FalkorDB + opt-in auto-index (T12)#682
Conversation
|
Warning Review limit reached
More reviews will be available in 19 minutes and 43 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
5e376e6 to
ead5047
Compare
65ad16b to
9b0fa42
Compare
ead5047 to
de11c6a
Compare
9b0fa42 to
c906311
Compare
de11c6a to
fec4bac
Compare
c906311 to
f3eac12
Compare
fec4bac to
e14f7f9
Compare
There was a problem hiding this comment.
Pull request overview
Adds “auto-init” behavior to the MCP server so new users can start cgraph-mcp without manually ensuring FalkorDB is up, and (optionally) auto-index the current working directory into a per-branch graph.
Changes:
- Introduces
api/mcp/auto_init.pywithensure_falkordb()andmaybe_auto_index()plus supporting helpers/cache. - Calls auto-init helpers from the MCP stdio entrypoint (
api/mcp/server.py) beforeapp.run(). - Adds mocked unit tests covering the main ensure/auto-index scenarios (
tests/mcp/test_auto_init.py).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
api/mcp/auto_init.py |
New auto-init helpers for FalkorDB reachability/bootstrap and optional auto-indexing with per-(project, branch) cache. |
api/mcp/server.py |
Entry point now invokes auto-init prior to starting the stdio transport. |
tests/mcp/test_auto_init.py |
New mocked tests for ensure-db behavior and auto-index gating/idempotence. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Zero-config startup so a fresh user doesn't need to run `cgraph ensure-db` and `index_repo` manually. api/mcp/auto_init.py - ensure_falkordb(): on server boot, ping FalkorDB; if unreachable on a localhost host, shell out to `cgraph ensure-db` (reuses the existing CLI's Docker bootstrap rather than duplicating it). Subprocess (not in-process call) so the CLI's stdout JSON doesn't pollute the MCP server's stdio transport. Never raises — server start continues even on bootstrap failure so individual tools can surface their own errors. - maybe_auto_index(cwd=None, project=None, branch=None): opt-in via CODE_GRAPH_AUTO_INDEX env var (off by default — indexing a large repo can take minutes and surprising the user on first call is bad UX). Detects current branch via `git rev-parse`, falls back to `_default`. Per-(project, branch) idempotency via a module-level set; second call for the same key is a no-op. - _truthy helper accepts 1/true/yes/on (case insensitive). api/mcp/server.py - main() now runs ensure_falkordb() and maybe_auto_index() before app.run(). Module-level import behaviour unchanged (tests that `import api.mcp.server` don't trigger any I/O). tests/mcp/test_auto_init.py (9 tests) - ensure_falkordb: no-op when reachable, runs cgraph when not, skips Docker for remote hosts, handles missing CLI binary. - maybe_auto_index: skipped when env unset, indexes when opt-in, idempotent across calls for same key, distinct branches each get one auto-index, _truthy semantics. All mocks — no Docker, no real FalkorDB writes — so the tests run in <2s without external dependencies. Out of scope per ticket: watch mode / re-indexing on FS change, auto-pulling Docker image (cgraph ensure-db handles that), cross- session state. Closes #660. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
e14f7f9 to
d6bfbde
Compare
api/mcp/auto_init.py:
- ensure_falkordb: reachability now uses a Redis PING (with FALKORDB_USERNAME/
PASSWORD) instead of a bare TCP connect, so a listener that isn't actually a
ready FalkorDB no longer reports reachable; validate FALKORDB_PORT is in
1..65535 (matching api/cli.py).
- _detect_branch: a detached HEAD ("HEAD") now maps to the default branch
instead of creating a code:<project>:HEAD graph (matches project.detect_branch).
- maybe_auto_index: honor ALLOWED_ANALYSIS_DIR (skip paths outside the sandbox,
like /api/analyze_folder and index_repo), and only index when the target
graph is empty — skip when it already holds data to avoid duplicate writes
and multi-minute latency.
- Fix module docstring (ensure_falkordb shells out to `cgraph ensure-db`).
api/mcp/server.py:
- Run opt-in auto-index off the startup path in a daemon thread so indexing a
large repo never blocks the stdio handshake (the analyzer logs to stderr, so
it can't corrupt the JSON-RPC stream); failures are caught + logged.
tests/mcp/test_auto_init.py: cover port-range rejection, detached-HEAD →
default, ALLOWED_ANALYSIS_DIR allow/deny, and skip-when-populated /
index-when-empty.
87/87 mcp tests pass; ruff clean.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
| # code:<project>:HEAD. | ||
| if branch and branch != "HEAD": | ||
| return branch | ||
| except FileNotFoundError: |
Prerequisites (merge order)
Merge in order — this PR is stacked on:
index_repo(T4)impact_analysis(T6)ask(T9/T10/T11)Base: #681.
Implements T12 (#660) — zero-config startup. Fresh users don't need to run
cgraph ensure-dbandindex_repomanually before the agent can use the server.Stacked on:
What ships
api/mcp/auto_init.py:ensure_falkordb()— pings FalkorDB on$FALKORDB_HOST:$FALKORDB_PORT; if unreachable on a localhost host, shells out tocgraph ensure-db(reuses the existing CLI Docker bootstrap; subprocess rather than in-process so the CLI's stdout JSON doesn't corrupt the MCP stdio transport). Never raises — server start continues even if bootstrap fails.maybe_auto_index(...)— gated byCODE_GRAPH_AUTO_INDEX=true|1|yes|on(off by default). Detects branch viagit rev-parse, falls back to_default. Idempotent per-(project, branch).api/mcp/server.py:main()runs both beforeapp.run(). Module-level import is still I/O-free.Tests
tests/mcp/test_auto_init.py— 9 tests, all mocked (no Docker, no real FalkorDB writes), <2s:ensure_falkordb: no-op when reachable / runscgraphwhen not / refuses non-localhost / handles missing CLI binary.maybe_auto_index: skipped when env unset / indexes when opt-in / idempotent / per-branch /_truthysemantics.Full MCP suite: 57 passed in 24.6s.
Out of scope (per ticket)
cgraph ensure-db).Closes #660.