code-explore-by-sql 0.1.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.
@@ -0,0 +1,264 @@
1
+ """MCP server for source code navigation — five tools.
2
+
3
+ Tool 1: read_symbol(qualified_name)
4
+ - Lookup by qualified name, returns identity + code/signature
5
+
6
+ Tool 2: search_fts_tool(keyword, path_filter)
7
+ - FTS5 search, returns located blocks with code preview
8
+
9
+ Tool 3: read_file_range(file_path, start_line, end_line)
10
+ - Read by position, returns code with symbol metadata
11
+
12
+ Tool 4: get_directory_structure()
13
+ - Module/file counts from index
14
+
15
+ Tool 5: list_databases()
16
+ - List available databases and their stats
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ import os
22
+ import sqlite3
23
+ import threading
24
+ from pathlib import Path
25
+ from typing import Any
26
+
27
+ from mcp.server.fastmcp import FastMCP
28
+
29
+ from .db import (
30
+ connect,
31
+ initialize_schema,
32
+ )
33
+ from .db import (
34
+ get_directory_structure as db_get_directory_structure,
35
+ )
36
+ from .db import (
37
+ read_file_range as db_read_file_range,
38
+ )
39
+ from .db import (
40
+ read_symbol as db_read_symbol,
41
+ )
42
+ from .db import (
43
+ search_fts as db_search_fts,
44
+ )
45
+
46
+ mcp = FastMCP("code-source-sql")
47
+
48
+ # ── 多数据库注册表 ──────────────────────────────────────────────────
49
+
50
+
51
+ def _parse_db_registry() -> dict[str, str]:
52
+ """从环境变量解析数据库注册表。
53
+
54
+ CODE_SOURCE_DBS 格式(冒号分隔的路径列表):
55
+ "/data/unreal.db:/data/mygame.db:/data/lib.db"
56
+ 别名从路径的文件主干名(不含扩展名)自动生成:
57
+ "unreal", "mygame", "lib"
58
+
59
+ CODE_SOURCE_DB 仍然作为主库(默认库),同时加入注册表。
60
+ """
61
+ registry: dict[str, str] = {}
62
+
63
+ # 主库 — CODE_SOURCE_DB
64
+ primary = os.environ.get("CODE_SOURCE_DB", "code_source.db")
65
+ primary_alias = Path(primary).stem # e.g. "unreal" from "unreal.db"
66
+ registry[primary_alias] = primary
67
+ registry[""] = primary # 空字符串 → 主库
68
+
69
+ # 附加库 — CODE_SOURCE_DBS
70
+ extra = os.environ.get("CODE_SOURCE_DBS", "")
71
+ if extra:
72
+ for path_str in extra.split(":"):
73
+ path_str = path_str.strip()
74
+ if not path_str:
75
+ continue
76
+ alias = Path(path_str).stem
77
+ # 别名冲突时后者覆盖(概率极低,用户自行保证不重名)
78
+ registry[alias] = path_str
79
+
80
+ return registry
81
+
82
+
83
+ _DB_REGISTRY = _parse_db_registry()
84
+
85
+ # ── 连接缓存 ────────────────────────────────────────────────────────
86
+
87
+ _conn_cache: dict[str, sqlite3.Connection] = {}
88
+ _conn_lock = threading.Lock()
89
+
90
+
91
+ def _get_conn(db_alias: str = "") -> sqlite3.Connection:
92
+ """按别名获取数据库连接,带缓存。"""
93
+ db_path = _DB_REGISTRY.get(db_alias)
94
+ if db_path is None:
95
+ # 别名不存在 → 回退主库
96
+ db_path = _DB_REGISTRY[""]
97
+
98
+ with _conn_lock:
99
+ conn = _conn_cache.get(db_path)
100
+ if conn is not None:
101
+ try:
102
+ conn.execute("SELECT 1")
103
+ return conn
104
+ except Exception:
105
+ # 连接已失效,移除缓存
106
+ del _conn_cache[db_path]
107
+
108
+ conn = connect(db_path)
109
+ initialize_schema(conn)
110
+ _conn_cache[db_path] = conn
111
+ return conn
112
+
113
+
114
+ # ── 工具实现 ─────────────────────────────────────────────────────────
115
+
116
+
117
+ @mcp.tool()
118
+ def list_databases() -> dict[str, Any]:
119
+ """List available databases and their stats.
120
+
121
+ Returns the registry of databases the server can access.
122
+ Use the 'alias' value as the 'db' parameter in other tools.
123
+ The first entry is the default database (used when db is omitted).
124
+ """
125
+ results = []
126
+ seen_paths: set[str] = set()
127
+ for alias, path in _DB_REGISTRY.items():
128
+ if alias == "" or path in seen_paths:
129
+ continue
130
+ seen_paths.add(path)
131
+
132
+ entry: dict[str, Any] = {"alias": alias, "path": path}
133
+ try:
134
+ conn = _get_conn(alias)
135
+ total = conn.execute(
136
+ "SELECT COUNT(*) AS c FROM file_content"
137
+ ).fetchone()["c"]
138
+ sym_total = conn.execute(
139
+ "SELECT COUNT(*) AS c FROM symbol_index"
140
+ ).fetchone()["c"]
141
+ entry["total_files"] = total
142
+ entry["total_symbols"] = sym_total
143
+ except Exception as e:
144
+ entry["error"] = str(e)
145
+ results.append(entry)
146
+
147
+ return {"default": _DB_REGISTRY.get("", ""), "databases": results}
148
+
149
+
150
+ @mcp.tool()
151
+ def read_symbol(
152
+ qualified_name: str,
153
+ view: str = "full",
154
+ expand_item: list[str] | None = None,
155
+ db: str = "",
156
+ ) -> dict[str, Any]:
157
+ """Read a symbol's source code by qualified name.
158
+
159
+ Accepts qualified names like 'ClassName::MethodName' or short names.
160
+ Supports fuzzy matching: 'MethodName' matches 'ClassName::MethodName'.
161
+
162
+ view: "full" (default) = complete code, "signature" = summary, "meta" = identity only.
163
+ expand_item: optional ranking hints for ambiguous matches.
164
+ db: database alias (from list_databases). Default: primary database.
165
+
166
+ Returns dict with {qn, type, file, range, code?, alt?} or {error, query, fts?}.
167
+ """
168
+ conn = _get_conn(db)
169
+ entries = db_read_symbol(conn, qualified_name, view=view, expand_item=expand_item)
170
+ if not entries:
171
+ fts_results = db_search_fts(conn, qualified_name, limit=5)
172
+ if fts_results:
173
+ return {
174
+ "error": "not_found",
175
+ "query": qualified_name,
176
+ "fts": [
177
+ {"file": r["file"], "line": r["line"], "block": r.get("block")}
178
+ for r in fts_results[:5]
179
+ ],
180
+ }
181
+ return {"error": "not_found", "query": qualified_name}
182
+
183
+ result = entries[0]
184
+ if len(entries) > 1:
185
+ result["alt"] = entries[1:5]
186
+ return result
187
+
188
+
189
+ @mcp.tool()
190
+ def search_fts_tool(
191
+ keyword: str = "",
192
+ path_filter: str = "",
193
+ expand_item: list[str] | None = None,
194
+ raw_query: str = "",
195
+ db: str = "",
196
+ ) -> list[dict[str, Any]]:
197
+ """Locate code blocks by keyword or raw FTS5 query.
198
+
199
+ Two query modes (use one):
200
+ - keyword: auto-escaped AND of tokens. Simple, safe. Example: "AddDynamic"
201
+ - raw_query: full FTS5 MATCH expression. Column filters, OR, NOT.
202
+ Example: '(file_path : "Character.h") AND "BeginPlay"'
203
+
204
+ FTS5 columns: file_path, module_name, content (all trigram, ≥3 chars).
205
+
206
+ raw_query operators:
207
+ AND → '"AddDynamic" AND "UObject"'
208
+ OR → '"AActor" OR "APawn"'
209
+ NOT → '"Update" NOT "Test"'
210
+ Column filter → '(file_path : "Shader.h") AND "FShaderType"'
211
+ Module filter → '(module_name : "Renderer") AND "VirtualTexture"'
212
+
213
+ Each result includes file, line, and optionally block QN + block_type.
214
+ For full code, use read_symbol with the block QN, or read_file_range with file+line.
215
+ db: database alias (from list_databases). Default: primary database.
216
+ """
217
+ conn = _get_conn(db)
218
+ return db_search_fts(conn, keyword, path_filter, expand_item=expand_item, raw_query=raw_query)
219
+
220
+
221
+ @mcp.tool()
222
+ def read_file_range(
223
+ file_path: str,
224
+ start_line: int,
225
+ end_line: int,
226
+ view: str = "full",
227
+ expand_item: list[str] | None = None,
228
+ db: str = "",
229
+ ) -> dict[str, Any]:
230
+ """Read source code by file path and line range.
231
+
232
+ Use when search_fts returns a location but read_symbol cannot resolve it,
233
+ or when reading code outside symbol boundaries.
234
+
235
+ view: "full" = complete code, "signature" = summary, "meta" = symbols only.
236
+
237
+ Returns dict with {file, range, code?, symbols?} or {error, file}.
238
+ db: database alias (from list_databases). Default: primary database.
239
+ """
240
+ conn = _get_conn(db)
241
+ entry = db_read_file_range(conn, file_path, start_line, end_line, view=view, expand_item=expand_item)
242
+ if not entry:
243
+ return {"error": "not_found", "file": file_path}
244
+ return entry
245
+
246
+
247
+ @mcp.tool()
248
+ def get_directory_structure(db: str = "") -> dict[str, Any]:
249
+ """Get the directory structure summary from the indexed database.
250
+
251
+ Returns total files, total modules, and module breakdown.
252
+ Use module names as path_filter in search_fts_tool.
253
+ db: database alias (from list_databases). Default: primary database.
254
+ """
255
+ conn = _get_conn(db)
256
+ return db_get_directory_structure(conn)
257
+
258
+
259
+ def main() -> None:
260
+ mcp.run(transport="stdio")
261
+
262
+
263
+ if __name__ == "__main__":
264
+ main()