code-graph-builder 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- code_graph_builder/__init__.py +82 -0
- code_graph_builder/builder.py +366 -0
- code_graph_builder/cgb_cli.py +32 -0
- code_graph_builder/cli.py +564 -0
- code_graph_builder/commands_cli.py +1288 -0
- code_graph_builder/config.py +340 -0
- code_graph_builder/constants.py +708 -0
- code_graph_builder/embeddings/__init__.py +40 -0
- code_graph_builder/embeddings/qwen3_embedder.py +573 -0
- code_graph_builder/embeddings/vector_store.py +584 -0
- code_graph_builder/examples/__init__.py +0 -0
- code_graph_builder/examples/example_configuration.py +276 -0
- code_graph_builder/examples/example_kuzu_usage.py +109 -0
- code_graph_builder/examples/example_semantic_search_full.py +347 -0
- code_graph_builder/examples/generate_wiki.py +915 -0
- code_graph_builder/examples/graph_export_example.py +100 -0
- code_graph_builder/examples/rag_example.py +206 -0
- code_graph_builder/examples/test_cli_demo.py +129 -0
- code_graph_builder/examples/test_embedding_api.py +153 -0
- code_graph_builder/examples/test_kuzu_local.py +190 -0
- code_graph_builder/examples/test_rag_redis.py +390 -0
- code_graph_builder/graph_updater.py +605 -0
- code_graph_builder/guidance/__init__.py +1 -0
- code_graph_builder/guidance/agent.py +123 -0
- code_graph_builder/guidance/prompts.py +74 -0
- code_graph_builder/guidance/toolset.py +264 -0
- code_graph_builder/language_spec.py +536 -0
- code_graph_builder/mcp/__init__.py +21 -0
- code_graph_builder/mcp/api_doc_generator.py +764 -0
- code_graph_builder/mcp/file_editor.py +207 -0
- code_graph_builder/mcp/pipeline.py +777 -0
- code_graph_builder/mcp/server.py +161 -0
- code_graph_builder/mcp/tools.py +1800 -0
- code_graph_builder/models.py +115 -0
- code_graph_builder/parser_loader.py +344 -0
- code_graph_builder/parsers/__init__.py +7 -0
- code_graph_builder/parsers/call_processor.py +306 -0
- code_graph_builder/parsers/call_resolver.py +139 -0
- code_graph_builder/parsers/definition_processor.py +796 -0
- code_graph_builder/parsers/factory.py +119 -0
- code_graph_builder/parsers/import_processor.py +293 -0
- code_graph_builder/parsers/structure_processor.py +145 -0
- code_graph_builder/parsers/type_inference.py +143 -0
- code_graph_builder/parsers/utils.py +134 -0
- code_graph_builder/rag/__init__.py +68 -0
- code_graph_builder/rag/camel_agent.py +429 -0
- code_graph_builder/rag/client.py +298 -0
- code_graph_builder/rag/config.py +239 -0
- code_graph_builder/rag/cypher_generator.py +67 -0
- code_graph_builder/rag/llm_backend.py +210 -0
- code_graph_builder/rag/markdown_generator.py +352 -0
- code_graph_builder/rag/prompt_templates.py +440 -0
- code_graph_builder/rag/rag_engine.py +640 -0
- code_graph_builder/rag/review_report.md +172 -0
- code_graph_builder/rag/tests/__init__.py +3 -0
- code_graph_builder/rag/tests/test_camel_agent.py +313 -0
- code_graph_builder/rag/tests/test_client.py +221 -0
- code_graph_builder/rag/tests/test_config.py +177 -0
- code_graph_builder/rag/tests/test_markdown_generator.py +240 -0
- code_graph_builder/rag/tests/test_prompt_templates.py +160 -0
- code_graph_builder/services/__init__.py +39 -0
- code_graph_builder/services/graph_service.py +465 -0
- code_graph_builder/services/kuzu_service.py +665 -0
- code_graph_builder/services/memory_service.py +171 -0
- code_graph_builder/settings.py +75 -0
- code_graph_builder/tests/ACCEPTANCE_CRITERIA_PHASE2.md +401 -0
- code_graph_builder/tests/__init__.py +1 -0
- code_graph_builder/tests/run_acceptance_check.py +378 -0
- code_graph_builder/tests/test_api_find.py +231 -0
- code_graph_builder/tests/test_api_find_integration.py +226 -0
- code_graph_builder/tests/test_basic.py +78 -0
- code_graph_builder/tests/test_c_api_extraction.py +388 -0
- code_graph_builder/tests/test_call_resolution_scenarios.py +504 -0
- code_graph_builder/tests/test_embedder.py +411 -0
- code_graph_builder/tests/test_integration_semantic.py +434 -0
- code_graph_builder/tests/test_mcp_protocol.py +298 -0
- code_graph_builder/tests/test_mcp_user_flow.py +190 -0
- code_graph_builder/tests/test_rag.py +404 -0
- code_graph_builder/tests/test_settings.py +135 -0
- code_graph_builder/tests/test_step1_graph_build.py +264 -0
- code_graph_builder/tests/test_step2_api_docs.py +323 -0
- code_graph_builder/tests/test_step3_embedding.py +278 -0
- code_graph_builder/tests/test_vector_store.py +552 -0
- code_graph_builder/tools/__init__.py +40 -0
- code_graph_builder/tools/graph_query.py +495 -0
- code_graph_builder/tools/semantic_search.py +387 -0
- code_graph_builder/types.py +333 -0
- code_graph_builder/utils/__init__.py +0 -0
- code_graph_builder/utils/path_utils.py +30 -0
- code_graph_builder-0.2.0.dist-info/METADATA +321 -0
- code_graph_builder-0.2.0.dist-info/RECORD +93 -0
- code_graph_builder-0.2.0.dist-info/WHEEL +4 -0
- code_graph_builder-0.2.0.dist-info/entry_points.txt +3 -0
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
"""Code Graph Builder - Language Specifications."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
from . import constants as cs
|
|
9
|
+
from .models import FQNSpec, LanguageSpec
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from tree_sitter import Node
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _python_get_name(node: Node) -> str | None:
|
|
16
|
+
"""Extract name from Python AST node."""
|
|
17
|
+
name_node = node.child_by_field_name("name")
|
|
18
|
+
return (
|
|
19
|
+
name_node.text.decode(cs.ENCODING_UTF8)
|
|
20
|
+
if name_node and name_node.text
|
|
21
|
+
else None
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _python_file_to_module(file_path: Path, repo_root: Path) -> list[str]:
|
|
26
|
+
"""Convert Python file path to module parts."""
|
|
27
|
+
try:
|
|
28
|
+
rel = file_path.relative_to(repo_root)
|
|
29
|
+
parts = list(rel.with_suffix("").parts)
|
|
30
|
+
if parts and parts[-1] == cs.INDEX_INIT:
|
|
31
|
+
parts = parts[:-1]
|
|
32
|
+
return parts
|
|
33
|
+
except ValueError:
|
|
34
|
+
return []
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _js_get_name(node: Node) -> str | None:
|
|
38
|
+
"""Extract name from JavaScript/TypeScript AST node."""
|
|
39
|
+
if node.type in cs.JS_NAME_NODE_TYPES:
|
|
40
|
+
name_node = node.child_by_field_name(cs.FIELD_NAME)
|
|
41
|
+
return (
|
|
42
|
+
name_node.text.decode(cs.ENCODING_UTF8)
|
|
43
|
+
if name_node and name_node.text
|
|
44
|
+
else None
|
|
45
|
+
)
|
|
46
|
+
return None
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _js_file_to_module(file_path: Path, repo_root: Path) -> list[str]:
|
|
50
|
+
"""Convert JS/TS file path to module parts."""
|
|
51
|
+
try:
|
|
52
|
+
rel = file_path.relative_to(repo_root)
|
|
53
|
+
parts = list(rel.with_suffix("").parts)
|
|
54
|
+
if parts and parts[-1] == cs.INDEX_INDEX:
|
|
55
|
+
parts = parts[:-1]
|
|
56
|
+
return parts
|
|
57
|
+
except ValueError:
|
|
58
|
+
return []
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _generic_get_name(node: Node) -> str | None:
|
|
62
|
+
"""Extract name from generic AST node."""
|
|
63
|
+
name_node = node.child_by_field_name("name")
|
|
64
|
+
if name_node and name_node.text:
|
|
65
|
+
return name_node.text.decode(cs.ENCODING_UTF8)
|
|
66
|
+
|
|
67
|
+
for field_name in cs.NAME_FIELDS:
|
|
68
|
+
name_node = node.child_by_field_name(field_name)
|
|
69
|
+
if name_node and name_node.text:
|
|
70
|
+
return name_node.text.decode(cs.ENCODING_UTF8)
|
|
71
|
+
|
|
72
|
+
return None
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _generic_file_to_module(file_path: Path, repo_root: Path) -> list[str]:
|
|
76
|
+
"""Convert generic file path to module parts."""
|
|
77
|
+
try:
|
|
78
|
+
rel = file_path.relative_to(repo_root)
|
|
79
|
+
return list(rel.with_suffix("").parts)
|
|
80
|
+
except ValueError:
|
|
81
|
+
return []
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _rust_get_name(node: Node) -> str | None:
|
|
85
|
+
"""Extract name from Rust AST node."""
|
|
86
|
+
if node.type in cs.RS_TYPE_NODE_TYPES:
|
|
87
|
+
name_node = node.child_by_field_name(cs.FIELD_NAME)
|
|
88
|
+
if name_node and name_node.type == cs.TS_TYPE_IDENTIFIER and name_node.text:
|
|
89
|
+
return name_node.text.decode(cs.ENCODING_UTF8)
|
|
90
|
+
elif node.type in cs.RS_IDENT_NODE_TYPES:
|
|
91
|
+
name_node = node.child_by_field_name(cs.FIELD_NAME)
|
|
92
|
+
if name_node and name_node.type == cs.TS_IDENTIFIER and name_node.text:
|
|
93
|
+
return name_node.text.decode(cs.ENCODING_UTF8)
|
|
94
|
+
|
|
95
|
+
return _generic_get_name(node)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _rust_file_to_module(file_path: Path, repo_root: Path) -> list[str]:
|
|
99
|
+
"""Convert Rust file path to module parts."""
|
|
100
|
+
try:
|
|
101
|
+
rel = file_path.relative_to(repo_root)
|
|
102
|
+
parts = list(rel.with_suffix("").parts)
|
|
103
|
+
if parts and parts[-1] == cs.INDEX_MOD:
|
|
104
|
+
parts = parts[:-1]
|
|
105
|
+
return parts
|
|
106
|
+
except ValueError:
|
|
107
|
+
return []
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _cpp_get_name(node: Node) -> str | None:
|
|
111
|
+
"""Extract name from C++ AST node."""
|
|
112
|
+
if node.type in cs.CPP_NAME_NODE_TYPES:
|
|
113
|
+
name_node = node.child_by_field_name(cs.FIELD_NAME)
|
|
114
|
+
if name_node and name_node.text:
|
|
115
|
+
return name_node.text.decode(cs.ENCODING_UTF8)
|
|
116
|
+
elif node.type == cs.TS_CPP_FUNCTION_DEFINITION:
|
|
117
|
+
declarator = node.child_by_field_name(cs.FIELD_DECLARATOR)
|
|
118
|
+
if declarator and declarator.type == cs.TS_CPP_FUNCTION_DECLARATOR:
|
|
119
|
+
name_node = declarator.child_by_field_name(cs.FIELD_DECLARATOR)
|
|
120
|
+
if name_node and name_node.type == cs.TS_IDENTIFIER and name_node.text:
|
|
121
|
+
return name_node.text.decode(cs.ENCODING_UTF8)
|
|
122
|
+
|
|
123
|
+
return _generic_get_name(node)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
# FQN Specifications for each language
|
|
127
|
+
PYTHON_FQN_SPEC = FQNSpec(
|
|
128
|
+
scope_node_types=frozenset(cs.FQN_PY_SCOPE_TYPES),
|
|
129
|
+
function_node_types=frozenset(cs.FQN_PY_FUNCTION_TYPES),
|
|
130
|
+
get_name=_python_get_name,
|
|
131
|
+
file_to_module_parts=_python_file_to_module,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
JS_FQN_SPEC = FQNSpec(
|
|
135
|
+
scope_node_types=frozenset(cs.FQN_JS_SCOPE_TYPES),
|
|
136
|
+
function_node_types=frozenset(cs.FQN_JS_FUNCTION_TYPES),
|
|
137
|
+
get_name=_js_get_name,
|
|
138
|
+
file_to_module_parts=_js_file_to_module,
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
TS_FQN_SPEC = FQNSpec(
|
|
142
|
+
scope_node_types=frozenset(cs.FQN_TS_SCOPE_TYPES),
|
|
143
|
+
function_node_types=frozenset(cs.FQN_TS_FUNCTION_TYPES),
|
|
144
|
+
get_name=_js_get_name,
|
|
145
|
+
file_to_module_parts=_js_file_to_module,
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
RUST_FQN_SPEC = FQNSpec(
|
|
149
|
+
scope_node_types=frozenset(cs.FQN_RS_SCOPE_TYPES),
|
|
150
|
+
function_node_types=frozenset(cs.FQN_RS_FUNCTION_TYPES),
|
|
151
|
+
get_name=_rust_get_name,
|
|
152
|
+
file_to_module_parts=_rust_file_to_module,
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
JAVA_FQN_SPEC = FQNSpec(
|
|
156
|
+
scope_node_types=frozenset(cs.FQN_JAVA_SCOPE_TYPES),
|
|
157
|
+
function_node_types=frozenset(cs.FQN_JAVA_FUNCTION_TYPES),
|
|
158
|
+
get_name=_generic_get_name,
|
|
159
|
+
file_to_module_parts=_generic_file_to_module,
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
CPP_FQN_SPEC = FQNSpec(
|
|
163
|
+
scope_node_types=frozenset(cs.FQN_CPP_SCOPE_TYPES),
|
|
164
|
+
function_node_types=frozenset(cs.FQN_CPP_FUNCTION_TYPES),
|
|
165
|
+
get_name=_cpp_get_name,
|
|
166
|
+
file_to_module_parts=_generic_file_to_module,
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
LUA_FQN_SPEC = FQNSpec(
|
|
170
|
+
scope_node_types=frozenset(cs.FQN_LUA_SCOPE_TYPES),
|
|
171
|
+
function_node_types=frozenset(cs.FQN_LUA_FUNCTION_TYPES),
|
|
172
|
+
get_name=_generic_get_name,
|
|
173
|
+
file_to_module_parts=_generic_file_to_module,
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
GO_FQN_SPEC = FQNSpec(
|
|
177
|
+
scope_node_types=frozenset(cs.FQN_GO_SCOPE_TYPES),
|
|
178
|
+
function_node_types=frozenset(cs.FQN_GO_FUNCTION_TYPES),
|
|
179
|
+
get_name=_generic_get_name,
|
|
180
|
+
file_to_module_parts=_generic_file_to_module,
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
SCALA_FQN_SPEC = FQNSpec(
|
|
184
|
+
scope_node_types=frozenset(cs.FQN_SCALA_SCOPE_TYPES),
|
|
185
|
+
function_node_types=frozenset(cs.FQN_SCALA_FUNCTION_TYPES),
|
|
186
|
+
get_name=_generic_get_name,
|
|
187
|
+
file_to_module_parts=_generic_file_to_module,
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
CSHARP_FQN_SPEC = FQNSpec(
|
|
191
|
+
scope_node_types=frozenset(cs.FQN_CS_SCOPE_TYPES),
|
|
192
|
+
function_node_types=frozenset(cs.FQN_CS_FUNCTION_TYPES),
|
|
193
|
+
get_name=_generic_get_name,
|
|
194
|
+
file_to_module_parts=_generic_file_to_module,
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
PHP_FQN_SPEC = FQNSpec(
|
|
198
|
+
scope_node_types=frozenset(cs.FQN_PHP_SCOPE_TYPES),
|
|
199
|
+
function_node_types=frozenset(cs.FQN_PHP_FUNCTION_TYPES),
|
|
200
|
+
get_name=_generic_get_name,
|
|
201
|
+
file_to_module_parts=_generic_file_to_module,
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
LANGUAGE_FQN_SPECS: dict[cs.SupportedLanguage, FQNSpec] = {
|
|
205
|
+
cs.SupportedLanguage.PYTHON: PYTHON_FQN_SPEC,
|
|
206
|
+
cs.SupportedLanguage.JS: JS_FQN_SPEC,
|
|
207
|
+
cs.SupportedLanguage.TS: TS_FQN_SPEC,
|
|
208
|
+
cs.SupportedLanguage.RUST: RUST_FQN_SPEC,
|
|
209
|
+
cs.SupportedLanguage.JAVA: JAVA_FQN_SPEC,
|
|
210
|
+
cs.SupportedLanguage.CPP: CPP_FQN_SPEC,
|
|
211
|
+
cs.SupportedLanguage.LUA: LUA_FQN_SPEC,
|
|
212
|
+
cs.SupportedLanguage.GO: GO_FQN_SPEC,
|
|
213
|
+
cs.SupportedLanguage.SCALA: SCALA_FQN_SPEC,
|
|
214
|
+
cs.SupportedLanguage.CSHARP: CSHARP_FQN_SPEC,
|
|
215
|
+
cs.SupportedLanguage.PHP: PHP_FQN_SPEC,
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
LANGUAGE_SPECS: dict[cs.SupportedLanguage, LanguageSpec] = {
|
|
219
|
+
cs.SupportedLanguage.PYTHON: LanguageSpec(
|
|
220
|
+
language=cs.SupportedLanguage.PYTHON,
|
|
221
|
+
file_extensions=cs.PY_EXTENSIONS,
|
|
222
|
+
function_node_types=cs.SPEC_PY_FUNCTION_TYPES,
|
|
223
|
+
class_node_types=cs.SPEC_PY_CLASS_TYPES,
|
|
224
|
+
module_node_types=cs.SPEC_PY_MODULE_TYPES,
|
|
225
|
+
call_node_types=cs.SPEC_PY_CALL_TYPES,
|
|
226
|
+
import_node_types=cs.SPEC_PY_IMPORT_TYPES,
|
|
227
|
+
import_from_node_types=cs.SPEC_PY_IMPORT_FROM_TYPES,
|
|
228
|
+
package_indicators=cs.SPEC_PY_PACKAGE_INDICATORS,
|
|
229
|
+
function_query="""
|
|
230
|
+
(function_definition
|
|
231
|
+
name: (identifier) @name) @function
|
|
232
|
+
""",
|
|
233
|
+
class_query="""
|
|
234
|
+
(class_definition
|
|
235
|
+
name: (identifier) @name) @class
|
|
236
|
+
""",
|
|
237
|
+
call_query="""
|
|
238
|
+
(call
|
|
239
|
+
function: (identifier) @name) @call
|
|
240
|
+
(call
|
|
241
|
+
function: (attribute
|
|
242
|
+
attribute: (identifier) @name)) @call
|
|
243
|
+
""",
|
|
244
|
+
),
|
|
245
|
+
cs.SupportedLanguage.JS: LanguageSpec(
|
|
246
|
+
language=cs.SupportedLanguage.JS,
|
|
247
|
+
file_extensions=cs.JS_EXTENSIONS,
|
|
248
|
+
function_node_types=cs.JS_TS_FUNCTION_NODES,
|
|
249
|
+
class_node_types=cs.JS_TS_CLASS_NODES,
|
|
250
|
+
module_node_types=cs.SPEC_JS_MODULE_TYPES,
|
|
251
|
+
call_node_types=cs.SPEC_JS_CALL_TYPES,
|
|
252
|
+
import_node_types=cs.JS_TS_IMPORT_NODES,
|
|
253
|
+
import_from_node_types=cs.JS_TS_IMPORT_NODES,
|
|
254
|
+
function_query="""
|
|
255
|
+
(function_declaration
|
|
256
|
+
name: (identifier) @name) @function
|
|
257
|
+
(function_expression
|
|
258
|
+
name: (identifier)? @name) @function
|
|
259
|
+
(arrow_function) @function
|
|
260
|
+
(method_definition
|
|
261
|
+
name: (property_identifier) @name) @function
|
|
262
|
+
""",
|
|
263
|
+
class_query="""
|
|
264
|
+
(class_declaration
|
|
265
|
+
name: (identifier) @name) @class
|
|
266
|
+
(class
|
|
267
|
+
name: (identifier) @name) @class
|
|
268
|
+
""",
|
|
269
|
+
call_query="""
|
|
270
|
+
(call_expression
|
|
271
|
+
function: (identifier) @name) @call
|
|
272
|
+
(call_expression
|
|
273
|
+
function: (member_expression
|
|
274
|
+
property: (property_identifier) @name)) @call
|
|
275
|
+
""",
|
|
276
|
+
),
|
|
277
|
+
cs.SupportedLanguage.TS: LanguageSpec(
|
|
278
|
+
language=cs.SupportedLanguage.TS,
|
|
279
|
+
file_extensions=cs.TS_EXTENSIONS,
|
|
280
|
+
function_node_types=cs.JS_TS_FUNCTION_NODES + (cs.TS_FUNCTION_SIGNATURE,),
|
|
281
|
+
class_node_types=cs.JS_TS_CLASS_NODES
|
|
282
|
+
+ (
|
|
283
|
+
cs.TS_ABSTRACT_CLASS_DECLARATION,
|
|
284
|
+
cs.TS_ENUM_DECLARATION,
|
|
285
|
+
cs.TS_INTERFACE_DECLARATION,
|
|
286
|
+
cs.TS_TYPE_ALIAS_DECLARATION,
|
|
287
|
+
cs.TS_INTERNAL_MODULE,
|
|
288
|
+
),
|
|
289
|
+
module_node_types=cs.SPEC_JS_MODULE_TYPES,
|
|
290
|
+
call_node_types=cs.SPEC_JS_CALL_TYPES,
|
|
291
|
+
import_node_types=cs.JS_TS_IMPORT_NODES,
|
|
292
|
+
import_from_node_types=cs.JS_TS_IMPORT_NODES,
|
|
293
|
+
function_query="""
|
|
294
|
+
(function_declaration
|
|
295
|
+
name: (identifier) @name) @function
|
|
296
|
+
(function_expression
|
|
297
|
+
name: (identifier)? @name) @function
|
|
298
|
+
(arrow_function) @function
|
|
299
|
+
(method_definition
|
|
300
|
+
name: (property_identifier) @name) @function
|
|
301
|
+
""",
|
|
302
|
+
class_query="""
|
|
303
|
+
(class_declaration
|
|
304
|
+
name: (identifier) @name) @class
|
|
305
|
+
(class
|
|
306
|
+
name: (identifier) @name) @class
|
|
307
|
+
(interface_declaration
|
|
308
|
+
name: (type_identifier) @name) @class
|
|
309
|
+
(enum_declaration
|
|
310
|
+
name: (identifier) @name) @class
|
|
311
|
+
(type_alias_declaration
|
|
312
|
+
name: (type_identifier) @name) @class
|
|
313
|
+
""",
|
|
314
|
+
call_query="""
|
|
315
|
+
(call_expression
|
|
316
|
+
function: (identifier) @name) @call
|
|
317
|
+
(call_expression
|
|
318
|
+
function: (member_expression
|
|
319
|
+
property: (property_identifier) @name)) @call
|
|
320
|
+
""",
|
|
321
|
+
),
|
|
322
|
+
cs.SupportedLanguage.RUST: LanguageSpec(
|
|
323
|
+
language=cs.SupportedLanguage.RUST,
|
|
324
|
+
file_extensions=cs.RS_EXTENSIONS,
|
|
325
|
+
function_node_types=cs.SPEC_RS_FUNCTION_TYPES,
|
|
326
|
+
class_node_types=cs.SPEC_RS_CLASS_TYPES,
|
|
327
|
+
module_node_types=cs.SPEC_RS_MODULE_TYPES,
|
|
328
|
+
call_node_types=cs.SPEC_RS_CALL_TYPES,
|
|
329
|
+
import_node_types=cs.SPEC_RS_IMPORT_TYPES,
|
|
330
|
+
import_from_node_types=cs.SPEC_RS_IMPORT_FROM_TYPES,
|
|
331
|
+
package_indicators=cs.SPEC_RS_PACKAGE_INDICATORS,
|
|
332
|
+
function_query="""
|
|
333
|
+
(function_item
|
|
334
|
+
name: (identifier) @name) @function
|
|
335
|
+
(function_signature_item
|
|
336
|
+
name: (identifier) @name) @function
|
|
337
|
+
(closure_expression) @function
|
|
338
|
+
""",
|
|
339
|
+
class_query="""
|
|
340
|
+
(struct_item
|
|
341
|
+
name: (type_identifier) @name) @class
|
|
342
|
+
(enum_item
|
|
343
|
+
name: (type_identifier) @name) @class
|
|
344
|
+
(union_item
|
|
345
|
+
name: (type_identifier) @name) @class
|
|
346
|
+
(trait_item
|
|
347
|
+
name: (type_identifier) @name) @class
|
|
348
|
+
(type_item
|
|
349
|
+
name: (type_identifier) @name) @class
|
|
350
|
+
(impl_item) @class
|
|
351
|
+
(mod_item
|
|
352
|
+
name: (identifier) @name) @module
|
|
353
|
+
""",
|
|
354
|
+
call_query="""
|
|
355
|
+
(call_expression
|
|
356
|
+
function: (identifier) @name) @call
|
|
357
|
+
(call_expression
|
|
358
|
+
function: (field_expression
|
|
359
|
+
field: (field_identifier) @name)) @call
|
|
360
|
+
(call_expression
|
|
361
|
+
function: (scoped_identifier
|
|
362
|
+
"::"
|
|
363
|
+
name: (identifier) @name)) @call
|
|
364
|
+
(macro_invocation
|
|
365
|
+
macro: (identifier) @name) @call
|
|
366
|
+
""",
|
|
367
|
+
),
|
|
368
|
+
cs.SupportedLanguage.GO: LanguageSpec(
|
|
369
|
+
language=cs.SupportedLanguage.GO,
|
|
370
|
+
file_extensions=cs.GO_EXTENSIONS,
|
|
371
|
+
function_node_types=cs.SPEC_GO_FUNCTION_TYPES,
|
|
372
|
+
class_node_types=cs.SPEC_GO_CLASS_TYPES,
|
|
373
|
+
module_node_types=cs.SPEC_GO_MODULE_TYPES,
|
|
374
|
+
call_node_types=cs.SPEC_GO_CALL_TYPES,
|
|
375
|
+
import_node_types=cs.SPEC_GO_IMPORT_TYPES,
|
|
376
|
+
import_from_node_types=cs.SPEC_GO_IMPORT_TYPES,
|
|
377
|
+
),
|
|
378
|
+
cs.SupportedLanguage.SCALA: LanguageSpec(
|
|
379
|
+
language=cs.SupportedLanguage.SCALA,
|
|
380
|
+
file_extensions=cs.SCALA_EXTENSIONS,
|
|
381
|
+
function_node_types=cs.SPEC_SCALA_FUNCTION_TYPES,
|
|
382
|
+
class_node_types=cs.SPEC_SCALA_CLASS_TYPES,
|
|
383
|
+
module_node_types=cs.SPEC_SCALA_MODULE_TYPES,
|
|
384
|
+
call_node_types=cs.SPEC_SCALA_CALL_TYPES,
|
|
385
|
+
import_node_types=cs.SPEC_SCALA_IMPORT_TYPES,
|
|
386
|
+
import_from_node_types=cs.SPEC_SCALA_IMPORT_TYPES,
|
|
387
|
+
),
|
|
388
|
+
cs.SupportedLanguage.JAVA: LanguageSpec(
|
|
389
|
+
language=cs.SupportedLanguage.JAVA,
|
|
390
|
+
file_extensions=cs.JAVA_EXTENSIONS,
|
|
391
|
+
function_node_types=cs.SPEC_JAVA_FUNCTION_TYPES,
|
|
392
|
+
class_node_types=cs.SPEC_JAVA_CLASS_TYPES,
|
|
393
|
+
module_node_types=cs.SPEC_JAVA_MODULE_TYPES,
|
|
394
|
+
call_node_types=cs.SPEC_JAVA_CALL_TYPES,
|
|
395
|
+
import_node_types=cs.SPEC_JAVA_IMPORT_TYPES,
|
|
396
|
+
import_from_node_types=cs.SPEC_JAVA_IMPORT_TYPES,
|
|
397
|
+
function_query="""
|
|
398
|
+
(method_declaration
|
|
399
|
+
name: (identifier) @name) @function
|
|
400
|
+
(constructor_declaration
|
|
401
|
+
name: (identifier) @name) @function
|
|
402
|
+
""",
|
|
403
|
+
class_query="""
|
|
404
|
+
(class_declaration
|
|
405
|
+
name: (identifier) @name) @class
|
|
406
|
+
(interface_declaration
|
|
407
|
+
name: (identifier) @name) @class
|
|
408
|
+
(enum_declaration
|
|
409
|
+
name: (identifier) @name) @class
|
|
410
|
+
(annotation_type_declaration
|
|
411
|
+
name: (identifier) @name) @class
|
|
412
|
+
(record_declaration
|
|
413
|
+
name: (identifier) @name) @class
|
|
414
|
+
""",
|
|
415
|
+
call_query="""
|
|
416
|
+
(method_invocation
|
|
417
|
+
name: (identifier) @name) @call
|
|
418
|
+
(object_creation_expression
|
|
419
|
+
type: (type_identifier) @name) @call
|
|
420
|
+
""",
|
|
421
|
+
),
|
|
422
|
+
cs.SupportedLanguage.C: LanguageSpec(
|
|
423
|
+
language=cs.SupportedLanguage.C,
|
|
424
|
+
file_extensions=cs.C_EXTENSIONS,
|
|
425
|
+
function_node_types=cs.SPEC_C_FUNCTION_TYPES,
|
|
426
|
+
class_node_types=cs.SPEC_C_CLASS_TYPES,
|
|
427
|
+
module_node_types=cs.SPEC_C_MODULE_TYPES,
|
|
428
|
+
call_node_types=cs.SPEC_C_CALL_TYPES,
|
|
429
|
+
import_node_types=cs.SPEC_C_IMPORT_TYPES,
|
|
430
|
+
import_from_node_types=cs.SPEC_C_IMPORT_TYPES,
|
|
431
|
+
package_indicators=cs.SPEC_C_PACKAGE_INDICATORS,
|
|
432
|
+
function_query="""
|
|
433
|
+
(function_definition) @function
|
|
434
|
+
(declaration
|
|
435
|
+
declarator: (function_declarator)) @function
|
|
436
|
+
""",
|
|
437
|
+
class_query="""
|
|
438
|
+
(struct_specifier) @class
|
|
439
|
+
(union_specifier) @class
|
|
440
|
+
(enum_specifier) @class
|
|
441
|
+
""",
|
|
442
|
+
call_query="""
|
|
443
|
+
(call_expression) @call
|
|
444
|
+
""",
|
|
445
|
+
typedef_query="""
|
|
446
|
+
(type_definition) @typedef
|
|
447
|
+
""",
|
|
448
|
+
macro_query="""
|
|
449
|
+
(preproc_def) @macro
|
|
450
|
+
(preproc_function_def) @macro
|
|
451
|
+
""",
|
|
452
|
+
),
|
|
453
|
+
cs.SupportedLanguage.CPP: LanguageSpec(
|
|
454
|
+
language=cs.SupportedLanguage.CPP,
|
|
455
|
+
file_extensions=cs.CPP_EXTENSIONS,
|
|
456
|
+
function_node_types=cs.SPEC_CPP_FUNCTION_TYPES,
|
|
457
|
+
class_node_types=cs.SPEC_CPP_CLASS_TYPES,
|
|
458
|
+
module_node_types=cs.SPEC_CPP_MODULE_TYPES,
|
|
459
|
+
call_node_types=cs.SPEC_CPP_CALL_TYPES,
|
|
460
|
+
import_node_types=cs.CPP_IMPORT_NODES,
|
|
461
|
+
import_from_node_types=cs.CPP_IMPORT_NODES,
|
|
462
|
+
package_indicators=cs.SPEC_CPP_PACKAGE_INDICATORS,
|
|
463
|
+
function_query="""
|
|
464
|
+
(field_declaration) @function
|
|
465
|
+
(declaration) @function
|
|
466
|
+
(function_definition) @function
|
|
467
|
+
(template_declaration (function_definition)) @function
|
|
468
|
+
(lambda_expression) @function
|
|
469
|
+
""",
|
|
470
|
+
class_query="""
|
|
471
|
+
(class_specifier) @class
|
|
472
|
+
(struct_specifier) @class
|
|
473
|
+
(union_specifier) @class
|
|
474
|
+
(enum_specifier) @class
|
|
475
|
+
(template_declaration (class_specifier)) @class
|
|
476
|
+
(template_declaration (struct_specifier)) @class
|
|
477
|
+
(template_declaration (union_specifier)) @class
|
|
478
|
+
(template_declaration (enum_specifier)) @class
|
|
479
|
+
""",
|
|
480
|
+
call_query="""
|
|
481
|
+
(call_expression) @call
|
|
482
|
+
(binary_expression) @call
|
|
483
|
+
(unary_expression) @call
|
|
484
|
+
(update_expression) @call
|
|
485
|
+
(field_expression) @call
|
|
486
|
+
(subscript_expression) @call
|
|
487
|
+
(new_expression) @call
|
|
488
|
+
(delete_expression) @call
|
|
489
|
+
""",
|
|
490
|
+
),
|
|
491
|
+
cs.SupportedLanguage.CSHARP: LanguageSpec(
|
|
492
|
+
language=cs.SupportedLanguage.CSHARP,
|
|
493
|
+
file_extensions=cs.CS_EXTENSIONS,
|
|
494
|
+
function_node_types=cs.SPEC_CS_FUNCTION_TYPES,
|
|
495
|
+
class_node_types=cs.SPEC_CS_CLASS_TYPES,
|
|
496
|
+
module_node_types=cs.SPEC_CS_MODULE_TYPES,
|
|
497
|
+
call_node_types=cs.SPEC_CS_CALL_TYPES,
|
|
498
|
+
import_node_types=cs.IMPORT_NODES_USING,
|
|
499
|
+
import_from_node_types=cs.IMPORT_NODES_USING,
|
|
500
|
+
),
|
|
501
|
+
cs.SupportedLanguage.PHP: LanguageSpec(
|
|
502
|
+
language=cs.SupportedLanguage.PHP,
|
|
503
|
+
file_extensions=cs.PHP_EXTENSIONS,
|
|
504
|
+
function_node_types=cs.SPEC_PHP_FUNCTION_TYPES,
|
|
505
|
+
class_node_types=cs.SPEC_PHP_CLASS_TYPES,
|
|
506
|
+
module_node_types=cs.SPEC_PHP_MODULE_TYPES,
|
|
507
|
+
call_node_types=cs.SPEC_PHP_CALL_TYPES,
|
|
508
|
+
),
|
|
509
|
+
cs.SupportedLanguage.LUA: LanguageSpec(
|
|
510
|
+
language=cs.SupportedLanguage.LUA,
|
|
511
|
+
file_extensions=cs.LUA_EXTENSIONS,
|
|
512
|
+
function_node_types=cs.SPEC_LUA_FUNCTION_TYPES,
|
|
513
|
+
class_node_types=cs.SPEC_LUA_CLASS_TYPES,
|
|
514
|
+
module_node_types=cs.SPEC_LUA_MODULE_TYPES,
|
|
515
|
+
call_node_types=cs.SPEC_LUA_CALL_TYPES,
|
|
516
|
+
import_node_types=cs.SPEC_LUA_IMPORT_TYPES,
|
|
517
|
+
),
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
_EXTENSION_TO_SPEC: dict[str, LanguageSpec] = {}
|
|
521
|
+
for _config in LANGUAGE_SPECS.values():
|
|
522
|
+
for _ext in _config.file_extensions:
|
|
523
|
+
_EXTENSION_TO_SPEC[_ext] = _config
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
def get_language_spec(file_extension: str) -> LanguageSpec | None:
|
|
527
|
+
"""Get language specification by file extension."""
|
|
528
|
+
return _EXTENSION_TO_SPEC.get(file_extension)
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
def get_language_for_extension(file_extension: str) -> cs.SupportedLanguage | None:
|
|
532
|
+
"""Get language enum by file extension."""
|
|
533
|
+
spec = _EXTENSION_TO_SPEC.get(file_extension)
|
|
534
|
+
if spec and isinstance(spec.language, cs.SupportedLanguage):
|
|
535
|
+
return spec.language
|
|
536
|
+
return None
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""MCP server for Code Graph Builder.
|
|
2
|
+
|
|
3
|
+
Exposes graph query, semantic search, and code retrieval tools
|
|
4
|
+
via the Model Context Protocol (MCP) stdio transport.
|
|
5
|
+
|
|
6
|
+
Usage:
|
|
7
|
+
CGB_WORKSPACE=~/.code-graph-builder python3 -m code_graph_builder.mcp.server
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def main() -> None:
|
|
14
|
+
import asyncio
|
|
15
|
+
|
|
16
|
+
from .server import main as _main
|
|
17
|
+
|
|
18
|
+
asyncio.run(_main())
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
__all__ = ["main"]
|