Skip to content

feat(mcp): GraphRAG ask tool — init + prompt seam + tool (T9/T10/T11)#681

Draft
DvirDukhan wants to merge 1 commit into
dvirdukhan/mcp-t6-impact-analysisfrom
dvirdukhan/mcp-t9-t10-t11-ask
Draft

feat(mcp): GraphRAG ask tool — init + prompt seam + tool (T9/T10/T11)#681
DvirDukhan wants to merge 1 commit into
dvirdukhan/mcp-t6-impact-analysisfrom
dvirdukhan/mcp-t9-t10-t11-ask

Conversation

@DvirDukhan
Copy link
Copy Markdown

Bundles T9 (#657) + T10 (#658) + T11 (#659) — the strategic differentiator vs structural-only MCP servers. Three tightly coupled tickets land together because T11 needs T9 + T10 to do anything.

Stacked on:

What ships

Layer File Purpose
Init api/mcp/graphrag_init.py get_or_create_kg(project, branch) — process-wide cache; reuses the hand-coded ontology from api/llm.py; graph name follows T17 convention code:{project}:{branch}.
Prompts api/mcp/code_prompts.py Re-export seam. v1 mirrors api/prompts.py; the value is having a place for MCP-flavoured divergence without touching the FastAPI chat prompts.
Tool api/mcp/tools/ask.py ask(question, project, branch=None){answer, cypher_query, context_nodes}. cypher_query is the design-doc transparency requirement so agents can verify the executed Cypher.

T9 also renames _define_ontologydefine_ontology in api/llm.py so the public-and-importable form is the only one. No other call sites in the repo.

Tests

14 new tests, all pass with mocked LiteModel (no network in CI):

  • tests/mcp/test_code_prompts.py (3) — re-exports match originals, __all__ shape, snapshot hash stability.
  • tests/mcp/test_graphrag_init.py (5) — per-branch graph name, cache identity, distinct keys → distinct instances, ontology reuse, define_ontology is public.
  • tests/mcp/test_ask.py (6) — tool registered, normalised payload, alternate response shapes, plain-string responses, errors as payload, JSON serialisable.

Full MCP suite: 48 passed in 27.5s.

Out of scope (per tickets)

  • Real-LLM E2E (Phase 1.5 nightly with secrets).
  • Streaming responses.
  • Multi-turn memory.
  • Prompt iteration.

Closes #657, #658, #659.

…10/T11)

Bundles three tightly-coupled tickets: T9 builds the per-(project,branch)
KnowledgeGraph cache, T10 adds the prompt-override seam, T11 wires both
together into the `ask` MCP tool that gives agents natural-language access
to the graph.

T9 (#657) — api/mcp/graphrag_init.py
- get_or_create_kg(project, branch) — process-wide cache keyed by
  (project, branch). Identity-stable: same key returns the same KG.
- reset_cache() for tests.
- Reuses the hand-coded ontology from api/llm.define_ontology (200+
  lines of File/Class/Function descriptions the LLM relies on for
  Cypher quality). Do NOT replace with auto-extraction.
- Graph name uses the T17 convention `code:{project}:{branch}` so it
  matches what index_repo writes.

T9 — api/llm.py rename
- _define_ontology → define_ontology (drop underscore so it's
  importable). Internal callers updated. No other call sites in the
  repo.

T10 (#658) — api/mcp/code_prompts.py
- Thin re-export of api.prompts (CYPHER_GEN_SYSTEM/PROMPT,
  GRAPH_QA_SYSTEM/PROMPT). The value is the seam: when the MCP ask
  tool needs agent-flavoured prompts (vs human-chat framing), the
  divergence happens here without touching api/prompts.py.

T11 (#659) — api/mcp/tools/ask.py
- ask(question, project, branch=None) MCP tool.
- Uses get_or_create_kg + chat_session().send_message() in an
  executor so the MCP event loop stays responsive.
- Returns the design-doc-mandated {answer, cypher_query,
  context_nodes} shape. cypher_query is the transparency requirement
  so agents can verify the executed query and learn the schema.
- _normalize_response tolerates the graphrag-sdk response shape
  variance ({response/answer, cypher/query, context/results}).
- Errors are surfaced as a structured {error: ...} payload, never as
  a transport exception — the agent always sees a valid tool result.

Tests (14 new, all pass with mocked LiteModel — no network in CI):
- tests/mcp/test_code_prompts.py (3): re-exports match originals,
  __all__ shape, snapshot hash stability.
- tests/mcp/test_graphrag_init.py (5): per-branch graph name, cache
  identity, distinct keys yield distinct instances, ontology reuse,
  define_ontology is public.
- tests/mcp/test_ask.py (6): tool registered, normalised payload,
  alternate response keys, plain-string response, errors surfaced as
  payload, JSON serialisable.

Full MCP suite still green (48 passed in 27.5s).

Out of scope per tickets: real-LLM E2E (Phase 1.5 with API-key
secrets), streaming, multi-turn memory, prompt iteration.

Closes #657, #658, #659.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 27, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: deffc7ff-db5c-4910-9a88-8ed36337389c

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch dvirdukhan/mcp-t9-t10-t11-ask

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.

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