Skip to content

feat(mcp): auto-init — ensure FalkorDB + opt-in auto-index (T12)#682

Merged
DvirDukhan merged 3 commits into
stagingfrom
dvirdukhan/mcp-t12-auto-init
Jun 9, 2026
Merged

feat(mcp): auto-init — ensure FalkorDB + opt-in auto-index (T12)#682
DvirDukhan merged 3 commits into
stagingfrom
dvirdukhan/mcp-t12-auto-init

Conversation

@DvirDukhan

@DvirDukhan DvirDukhan commented May 27, 2026

Copy link
Copy Markdown
Contributor

Prerequisites (merge order)

Merge in order — this PR is stacked on:

  1. feat(mcp): index_repo tool (T4 #652) #678index_repo (T4)
  2. feat(mcp): query tools — get_callers/callees/deps, find_path, search_code (T5/T7/T8) #679 — query tools (T5/T7/T8)
  3. feat(mcp): impact_analysis tool — variable-depth Cypher (T6) #680impact_analysis (T6)
  4. feat(mcp): GraphRAG ask tool — init + prompt seam + tool (T9/T10/T11) #681 — GraphRAG ask (T9/T10/T11)

Base: #681.


Implements T12 (#660) — zero-config startup. Fresh users don't need to run cgraph ensure-db and index_repo manually 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 to cgraph 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 by CODE_GRAPH_AUTO_INDEX=true|1|yes|on (off by default). Detects branch via git rev-parse, falls back to _default. Idempotent per-(project, branch).

api/mcp/server.py:

  • main() runs both before app.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 / runs cgraph when not / refuses non-localhost / handles missing CLI binary.
  • maybe_auto_index: skipped when env unset / indexes when opt-in / idempotent / per-branch / _truthy semantics.

Full MCP suite: 57 passed in 24.6s.

Out of scope (per ticket)

  • Watch mode / re-indexing on FS change.
  • Auto-pulling Docker image (already handled by cgraph ensure-db).
  • Cross-session state.

Closes #660.

@coderabbitai

coderabbitai Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@DvirDukhan, we couldn't start this review because you've reached your PR review rate limit.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7be0fab3-1c41-47a4-9572-9101e1f86395

📥 Commits

Reviewing files that changed from the base of the PR and between 347dd46 and 12c0b0d.

📒 Files selected for processing (3)
  • api/mcp/auto_init.py
  • api/mcp/server.py
  • tests/mcp/test_auto_init.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch dvirdukhan/mcp-t12-auto-init

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@DvirDukhan DvirDukhan force-pushed the dvirdukhan/mcp-t12-auto-init branch from 5e376e6 to ead5047 Compare June 8, 2026 13:26
@DvirDukhan DvirDukhan changed the base branch from dvirdukhan/mcp-t9-t10-t11-ask to dvirdukhan/mcp-find-symbol June 8, 2026 13:26
@DvirDukhan DvirDukhan force-pushed the dvirdukhan/mcp-find-symbol branch from 65ad16b to 9b0fa42 Compare June 8, 2026 14:05
@DvirDukhan DvirDukhan force-pushed the dvirdukhan/mcp-t12-auto-init branch from ead5047 to de11c6a Compare June 8, 2026 14:06
@DvirDukhan DvirDukhan force-pushed the dvirdukhan/mcp-find-symbol branch from 9b0fa42 to c906311 Compare June 8, 2026 16:44
@DvirDukhan DvirDukhan force-pushed the dvirdukhan/mcp-t12-auto-init branch from de11c6a to fec4bac Compare June 8, 2026 16:44
@DvirDukhan DvirDukhan force-pushed the dvirdukhan/mcp-find-symbol branch from c906311 to f3eac12 Compare June 8, 2026 17:10
@DvirDukhan DvirDukhan force-pushed the dvirdukhan/mcp-t12-auto-init branch from fec4bac to e14f7f9 Compare June 8, 2026 17:10
@DvirDukhan DvirDukhan marked this pull request as ready for review June 8, 2026 19:53
@DvirDukhan DvirDukhan requested a review from Copilot June 8, 2026 19:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py with ensure_falkordb() and maybe_auto_index() plus supporting helpers/cache.
  • Calls auto-init helpers from the MCP stdio entrypoint (api/mcp/server.py) before app.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.

Comment thread api/mcp/auto_init.py Outdated
Comment thread api/mcp/auto_init.py
Comment thread api/mcp/auto_init.py
Comment thread api/mcp/auto_init.py Outdated
Comment thread api/mcp/auto_init.py
Comment thread api/mcp/auto_init.py
Comment thread api/mcp/server.py Outdated
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>
@DvirDukhan DvirDukhan force-pushed the dvirdukhan/mcp-t12-auto-init branch from e14f7f9 to d6bfbde Compare June 8, 2026 20:18
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>
galshubeli
galshubeli previously approved these changes Jun 9, 2026
Base automatically changed from dvirdukhan/mcp-find-symbol to staging June 9, 2026 09:50
@DvirDukhan DvirDukhan dismissed galshubeli’s stale review June 9, 2026 09:50

The base branch was changed.

Comment thread api/mcp/auto_init.py
# code:<project>:HEAD.
if branch and branch != "HEAD":
return branch
except FileNotFoundError:
@DvirDukhan DvirDukhan merged commit 6b23cb6 into staging Jun 9, 2026
11 of 13 checks passed
@DvirDukhan DvirDukhan deleted the dvirdukhan/mcp-t12-auto-init branch June 9, 2026 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MCP T12] Auto-init: ensure FalkorDB + auto-index CWD

3 participants