Skip to content

fix(mcp): ask the user before initializing a project that has no index - #351

Merged
sunerpy merged 1 commit into
mainfrom
fix/mcp-ask-before-init
Nov 27, 2026
Merged

sunerpy merged 1 commit into
mainfrom
fix/mcp-ask-before-init

Conversation

@sunerpy

@sunerpy sunerpy commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Summary

When a CodeGraph MCP tool finds no indexed project, its text no longer tells the agent to run codegraph init itself. It now says that if the project has no CodeGraph index yet, the agent should tell the user so, offer to run codegraph init . in the project directory, and not run it without the user's consent. The Limitations bullet of the server instructions returned by initialize gives the same rule.

  • crates/codegraph-mcp/src/roots.rs: not_indexed_message appends the consent sentence to the explicit-projectPath error and to the no-default text. The projectPath and --path remedies, the sub-project candidate list and the truncated-scan note are unchanged. The candidate-list text still does not mention init.
  • crates/codegraph-mcp/src/instructions.rs: the Limitations bullet. The module doc notes that this bullet is this repository's own wording.
  • reference/golden/mcp/initialize.json: the instructions string changes in that one bullet.
  • docs/mcp.md and skills/codegraph/SKILL.md describe the rule. In the skill, "follow the recovery command a failed command prints" no longer covers a project that was never indexed.
  • docs/equivalence.md: a new v0.56.1 regeneration log with the initialize.json row.

Why

#269, option A (owner decision OD-C8): CodeGraph does not create an index on its own, and when a project has none the agent tells the user and offers codegraph init. Before this change the tool text said "run codegraph init there" or "run codegraph init in the project", and the instructions only said "offer to run codegraph init .". Neither said the user decides. Opt-in automatic init (option B) is planned separately and is not in this PR.

Verification

Red, at 8166929 with only the test and golden expectations changed:

  • cargo test -p codegraph-mcp --locked --lib: roots::tests::not_indexed_messages_offer_init_only_with_the_users_consent, instructions::tests::instructions_offer_init_only_with_the_users_consent, server::tests::tools_call_without_a_default_returns_success_text, server::tests::tools_call_bogus_project_path_returns_actionable_error_result and rmcp_handler::handler_tests::call_tool_bad_project_path_returns_error_result failed on the old text, for example got: No indexed project resolved. Pass a `projectPath` argument, run `codegraph init` in the project, or start the server with `--path <project>`.
  • --test golden_mcp (initialize_matches_golden) and --test rmcp_parity failed with "instructions must be byte-identical".
  • --test rmcp_roots (rmcp_does_not_adopt_unindexed_client_root, rmcp_http_no_roots_never_adopts) failed on the old text.
  • cargo test -p codegraph-rs --locked --test http_serve -- serve_http_global_without_projectpath_returns_actionable_error failed: the real serve --http server returned the old text.

Green, on the tree committed as 58f66f4:

  • cargo test -p codegraph-mcp --locked --no-fail-fast: 548 passed, 0 failed.
  • cargo test -p codegraph-rs --locked --test http_serve --test rmcp_serve_direct --test zed_bare_serve --test batch_m_failed_open_recovery --test skill_install: all pass. cargo test -p codegraph-rs --locked --bin codegraph -- installer::skill: 19 passed.
  • cargo clippy -p codegraph-mcp -p codegraph-rs --all-targets --locked -- -D warnings, cargo fmt --all --check, python3 scripts/docs-check.py and the oxfmt check are clean.
  • scripts/regen-goldens.sh --check with this tree's release codegraph and bench: all 32 corpora identical.
  • A stdio session of the release binary in an unindexed directory returns the new instructions bullet, the new no-default text and the new explicit-path error.

Notes

  • No extraction golden, schema or extraction-version change. The only golden change is one bullet of the instructions string in initialize.json.
  • These messages are unchanged because they are not the MCP agent instruction for a project without an index: the daemon's "refusing live services for unindexed project …" (an agent only sees it if the index disappears between path resolution and live-service start), the CLI's "CodeGraph not initialized in …" messages, invalid_config_message for an unsafe CODEGRAPH_DIR, and the viewer hint.
  • Upstream's bullet (src/mcp/server-instructions.ts, unchanged since f9fcc2c through 461a5d87) tells the agent to stop calling codegraph tools for an unindexed project and never run codegraph init itself. Under OD-C8, this repository instead offers the command and runs it only after the user agrees.
  • After v0.56.1 is released, ## Feature request: simple bootstrap script for new projects #269 gets a status reply and stays open until option B ships.

The pre-push make pre-ci passed on 58f66f4.

An agent that met a project without an index was told to build one
itself. The no-project tool text said "run `codegraph init` there" or
"run `codegraph init` in the project", and the server instructions only
said to offer `codegraph init .` without saying that the user decides
(#269).

The no-default text and the explicit-projectPath error now say: if the
project has no CodeGraph index yet, tell the user so, offer to run
`codegraph init .` in the project directory, and do not run it without
the user's consent. The rest of each message (projectPath, --path, the
truncated-scan note) is unchanged, and the text that lists indexed
sub-projects still does not mention init. The Limitations bullet of the
server instructions gives the same rule, so the instructions string of
reference/golden/mcp/initialize.json changes in that one bullet and is
recorded in the new v0.56.1 regeneration log of docs/equivalence.md.
docs/mcp.md describes the rule. The skill told agents to follow the
recovery command a CLI command prints, which for a project without an
index is `codegraph init`; it now says that case is the user's decision.

Verified: the new tests in roots.rs and instructions.rs, the extended
server.rs, rmcp_handler.rs, rmcp_roots.rs and codegraph-rs http_serve
tests, golden_mcp and rmcp_parity all failed on the old text and pass
now. cargo test -p codegraph-mcp --locked passes (548 tests). The
codegraph-rs http_serve, rmcp_serve_direct, zed_bare_serve,
batch_m_failed_open_recovery and skill_install tests and the embedded
skill unit tests pass. Clippy with -D warnings passes on codegraph-mcp
and codegraph-rs. scripts/regen-goldens.sh --check with the release
codegraph and bench binaries of this tree reports all 32 corpora
identical. A stdio session of the release binary in an unindexed
directory returns the new text for both cases.
@codecov

codecov Bot commented Nov 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #351   +/-   ##
=======================================
  Coverage   96.40%   96.40%           
=======================================
  Files         368      368           
  Lines      254382   254436   +54     
=======================================
+ Hits       245229   245283   +54     
  Misses       9153     9153           
Files with missing lines Coverage Δ
crates/codegraph-mcp/src/instructions.rs 100.00% <100.00%> (ø)
crates/codegraph-mcp/src/rmcp_handler.rs 90.61% <100.00%> (+0.02%) ⬆️
crates/codegraph-mcp/src/roots.rs 98.83% <100.00%> (+0.02%) ⬆️
crates/codegraph-mcp/src/server.rs 95.22% <100.00%> (+0.02%) ⬆️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sunerpy
sunerpy merged commit 47f3e82 into main Nov 27, 2026
12 checks passed
@sunerpy
sunerpy deleted the fix/mcp-ask-before-init branch November 27, 2026 08:56
@github-actions github-actions Bot mentioned this pull request Nov 27, 2026
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.

1 participant