feat(query-graph): Cypher-subset graph query engine + MCP tool (closes #9)#149
Merged
Conversation
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
This was referenced Jul 1, 2026
f191875 to
25b1d11
Compare
…#9) Agents currently must chain multiple MCP tools (trace -> impact -> context) to answer structural questions. This adds a single expressive query language —an openCypher subset — that replaces 3-5 tool calls with one. New files: - scripts/query_graph_engine.py — pure-Python Cypher-subset parser + SQL compiler + executor. Zero external deps. Read-only (no CREATE/DELETE). Supported clauses: MATCH, WHERE, RETURN, LIMIT. Supported predicates: =, !=, <, >, <=, >=, CONTAINS, IS NULL, IS NOT NULL, NOT EXISTS { pattern }, AND, OR. Node labels: function, class, file, module, route, type, interface (case-insensitive — PascalCase convention normalized to lowercase). Edge types: CALLS, IMPORTS, DEFINES, INHERITS, IMPLEMENTS, USES_TYPE. Edge syntax: -[:TYPE]->, <-[:TYPE]-, -[:TYPE]- (undirected). Anonymous nodes () supported (matches any node — used in NOT EXISTS). Default edge type is CALLS when none specified. Correlated subqueries for NOT EXISTS (dead-code detection pattern). Defensive: parse/compile/DB errors return structured error dicts, not exceptions. validate_query() checks syntax without touching the DB. - scripts/commands/query_graph.py — CLI command 'codelens query-graph'. --validate flag for syntax-only check. --limit CLI flag overrides query LIMIT. Auto-registered via commands/__init__.py. - tests/test_query_graph.py — 68 tests covering tokenizer, parser, SQL compilation, executor, error handling, CLI registration, MCP tool schema, file headers, and all three example queries from issue #9 spec. Modified: - scripts/mcp_server.py — added 'query-graph' to _TOOL_DEFINITIONS (static schema with query, limit, validate params; description includes examples from issue #9). - README/SKILL/SKILL-QUICK/pyproject.toml/skill.json/graph_model.py — command count synced 69->70, MCP static 54->55 via sync_command_count.py --apply. Test results: 68/68 new tests pass. 442 passed / 40 skipped / 0 failed in safe-to-run test subset. No regressions. Pre-existing failures in test_universal_grammar_loader (tree-sitter-zig env) unchanged.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Closes #9
Summary
Agents currently must chain multiple MCP tools (trace → impact → context) to answer structural questions. This PR adds a single expressive query language — an openCypher subset — that replaces 3-5 tool calls with one.
What changed
New files:
scripts/query_graph_engine.py— Pure-Python Cypher-subset parser + SQL compiler + executor. Zero external deps. Read-only (no CREATE/DELETE/MERGE).scripts/commands/query_graph.py— CLI commandcodelens query-graph.tests/test_query_graph.py— 68 tests.Modified:
scripts/mcp_server.py— addedquery-graphto_TOOL_DEFINITIONS.Supported Cypher subset (MVP per issue #9 spec)
Clauses: MATCH, WHERE, RETURN, LIMIT
Predicates:
=,!=,<,>,<=,>=,CONTAINS,IS NULL,IS NOT NULL,NOT EXISTS { pattern },AND,ORNode labels: function, class, file, module, route, type, interface (case-insensitive — PascalCase normalized to lowercase)
Edge types: CALLS, IMPORTS, DEFINES, INHERITS, IMPLEMENTS, USES_TYPE
Edge syntax:
-[:TYPE]->,<-[:TYPE]-,-[:TYPE]-(undirected)Anonymous nodes:
()matches any node (used in NOT EXISTS)Example queries from issue #9
Design decisions
NOT EXISTS { ()-[:CALLS]->(f) }) compiles to a correlated SQL subquery that references the outer query's node via shared variable names.(). Valid in Cypher — matches any node. Used in NOT EXISTS patterns where the caller side is unbound.-[]->or-->), CALLS is assumed (most common query).{status: error, error: parse_error, message: ...}), not exceptions.validate_query()checks syntax without touching the DB.Test results
tests/test_query_graph.pypass:TestTokenizer(8) — strings, brackets, arrows, comments, errorsTestParser(19) — nodes, edges, WHERE predicates, LIMIT, RETURN *, errorsTestValidateQuery(3) — syntax-only validationTestExecuteSingleNode(10) — MATCH, WHERE, LIMIT, RETURN *, AND/ORTestExecuteWithEdges(4) — CALLS right/left, INHERITS, multi-hopTestNotExists(2) — dead-code detection + positive EXISTSTestErrorHandling(6) — parse errors, missing DB, missing tables, echoTestCliCommandRegistration(4) — registration, help, --validate, --limitTestMcpToolRegistration(4) — schema, required fields, descriptionTestFileHeaders(2) — @WHO/@WHAT/@PART/@entryTestIssueSpecExamples(3) — all three examples from issue [FEATURE] Cypher-like graph query engine for MCP toolquery_graph#9 spectest_command_count.pypasses — counts are in sync.test_command_registry.pypasses —query-graphauto-registers.test_universal_grammar_loader(tree-sitter-zig env) unchanged.Non-breaking
sync_command_count.py --apply.