leankg 0.16.7

Lightweight Knowledge Graph for AI-Assisted Development
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
use serde_json::json;
use serde_json::Value;

pub struct ToolRegistry;

impl ToolRegistry {
    pub fn list_tools() -> Vec<ToolDefinition> {
        vec![
            ToolDefinition {
                name: "mcp_init".to_string(),
                description: "Initialize LeanKG project (creates .leankg/ and leankg.yaml)"
                    .to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "path": {"type": "string", "description": "Path for LeanKG project (default: .leankg)"}
                    },
                    "required": []
                }),
            },
            ToolDefinition {
                name: "mcp_index".to_string(),
                description: "Index codebase (mirrors CLI: leankg index)".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "path": {"type": "string", "description": "Path to index (default: current directory)"},
                        "incremental": {"type": "boolean", "description": "Only index changed files (git-based)"},
                        "lang": {"type": "string", "description": "Filter by language (e.g., go,ts,py,rs,kt)"},
                        "exclude": {"type": "string", "description": "Exclude patterns (comma-separated)"}
                    },
                    "required": []
                }),
            },
            ToolDefinition {
                name: "mcp_index_docs".to_string(),
                description: "Index documentation directory to create code-doc traceability edges. \
                              Run after mcp_index to populate documented_by and references relationships."
                    .to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "path": {"type": "string", "description": "Path to docs directory (default: ./docs)"}
                    },
                    "required": []
                }),
            },
            ToolDefinition {
                name: "mcp_install".to_string(),
                description: "Create .mcp.json for MCP client configuration".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "mcp_config_path": {"type": "string", "description": "Path for .mcp.json (default: .mcp.json)"}
                    },
                    "required": []
                }),
            },
            ToolDefinition {
                name: "mcp_status".to_string(),
                description: "Show LeanKG index status".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {},
                    "required": []
                }),
            },
            ToolDefinition {
                name: "mcp_impact".to_string(),
                description: "Calculate impact radius (blast radius) for a file".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "file": {"type": "string", "description": "File to analyze"},
                        "depth": {"type": "integer", "description": "Depth of analysis (default: 3)"}
                    },
                    "required": ["file"]
                }),
            },
            ToolDefinition {
                name: "query_file".to_string(),
                description: "Find file by name or pattern".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "pattern": {"type": "string", "description": "File name or pattern to search"},
                        "element_type": {"type": "string", "enum": ["file", "function", "struct", "class", "module"], "description": "Optional filter by element type"},
                        "project": {"type": "string", "description": "Optional: project path to search in (resolves to nearest .leankg directory)"}
                    },
                    "required": []
                }),
            },
            ToolDefinition {
                name: "get_dependencies".to_string(),
                description: "Get file dependencies (direct imports)".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "file": {"type": "string", "description": "File to get dependencies for"},
                        "project": {"type": "string", "description": "Optional: project path to search in (resolves to nearest .leankg directory)"}
                    },
                    "required": ["file"]
                }),
            },
            ToolDefinition {
                name: "get_dependents".to_string(),
                description: "Get files depending on target".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "file": {"type": "string", "description": "File to get dependents for"},
                        "project": {"type": "string", "description": "Optional: project path to search in (resolves to nearest .leankg directory)"}
                    },
                    "required": ["file"]
                }),
            },
            ToolDefinition {
                name: "get_impact_radius".to_string(),
                description: "Get all files affected by change within N hops. Keep depth<=2 for LLM context budgets. Depth 3 may return hundreds of nodes. Results include confidence scores (0.0-1.0) and severity classification (WILL BREAK, LIKELY AFFECTED, MAY BE AFFECTED). Set compress_response=true for token-optimized output.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "file": {"type": "string", "description": "File to analyze"},
                        "depth": {"type": "integer", "default": 3, "description": "Hop depth (default: 3). Keep <=2 for context budgets."},
                        "min_confidence": {"type": "number", "default": 0.0, "description": "Minimum confidence threshold (0.0-1.0). Only return results with confidence >= this value."},
                        "compress_response": {"type": "boolean", "default": false, "description": "Enable RTK-style compression for token savings"},
                        "project": {"type": "string", "description": "Optional: project path to search in (resolves to nearest .leankg directory)"}
                    },
                    "required": ["file"]
                }),
            },
            ToolDefinition {
                name: "detect_changes".to_string(),
                description: "Pre-commit risk analysis: computes diff between working tree and last indexed commit. Returns changed files, affected symbols, and risk level (critical/high/medium/low). Risk classification: critical>=10 dependents at depth 1, high>=5 dependents or public API changed, medium=2-4 dependents or cross-module dep, low=<=1 dependent within single cluster.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "scope": {"type": "string", "enum": ["staged", "unstaged", "all"], "default": "all", "description": "Scope of changes to analyze: 'staged' (git staged), 'unstaged', or 'all' (default)"},
                        "min_confidence": {"type": "number", "default": 0.0, "description": "Minimum confidence threshold for affected symbols."}
                    },
                    "required": []
                }),
            },
            ToolDefinition {
                name: "get_review_context".to_string(),
                description: "Generate focused subgraph + structured review prompt".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "files": {"type": "array", "items": {"type": "string"}, "description": "Files to include in review context"},
                        "project": {"type": "string", "description": "Optional: project path to search in (resolves to nearest .leankg directory)"}
                    },
                    "required": []
                }),
            },
            ToolDefinition {
                name: "get_context".to_string(),
                description: "Get AI context for file (minimal, token-optimized)".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "file": {"type": "string", "description": "File to get context for"},
                        "signature_only": {"type": "boolean", "default": true, "description": "Return only signatures (default). Set false for full body metadata."},
                        "max_tokens": {"type": "integer", "default": 4000, "description": "Token budget cap"},
                        "project": {"type": "string", "description": "Optional: project path to search in (resolves to nearest .leankg directory)"}
                    },
                    "required": ["file"]
                }),
            },
            ToolDefinition {
                name: "orchestrate".to_string(),
                description: "Smart context orchestration with caching. Provide natural language intent like 'show me impact of changing function X' or 'get context for file Y'. Internally: checks cache -> queries graph -> compresses -> caches result. Use this instead of multiple individual tools when you want LeanKG to optimize the flow.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "intent": {"type": "string", "description": "Natural language intent (e.g., 'show me impact of changing main.rs', 'get context for handler.rs', 'find function named parse')"},
                        "file": {"type": "string", "description": "Optional: specific file to query"},
                        "mode": {"type": "string", "enum": ["adaptive", "full", "map", "signatures"], "default": "adaptive", "description": "Compression mode for file content"},
                        "fresh": {"type": "boolean", "default": false, "description": "Force fresh query, bypass cache"},
                        "project": {"type": "string", "description": "Optional: project path to search in (resolves to nearest .leankg directory)"}
                    },
                    "required": ["intent"]
                }),
            },
            ToolDefinition {
                name: "ctx_read".to_string(),
                description: "Read file with compression modes for efficient LLM context".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "file": {"type": "string", "description": "File path to read"},
                        "mode": {"type": "string", "enum": ["adaptive", "full", "map", "signatures", "diff", "aggressive", "entropy", "lines"], "default": "adaptive", "description": "Compression mode"},
                        "lines": {"type": "string", "description": "Lines specification for 'lines' mode (e.g., '1-10,20,30-40')"},
                        "project": {"type": "string", "description": "Optional: project path to search in (resolves to nearest .leankg directory)"}
                    },
                    "required": ["file"]
                }),
            },
            ToolDefinition {
                name: "find_function".to_string(),
                description: "Locate function definition by name. Optionally scope to a file.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "name": {"type": "string", "description": "Function name to search for"},
                        "file": {"type": "string", "description": "Optional file to scope the search to"},
                        "project": {"type": "string", "description": "Optional: project path to search in (resolves to nearest .leankg directory)"}
                    },
                    "required": ["name"]
                }),
            },
            ToolDefinition {
                name: "get_callers".to_string(),
                description: "Find all functions/methods that call a given function. \
                              Returns the caller name, file path, and line number.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "function": {"type": "string", "description": "Function name to find callers for"},
                        "file": {"type": "string", "description": "Optional file to scope the search"},
                        "project": {"type": "string", "description": "Optional: project path to search in (resolves to nearest .leankg directory)"}
                    },
                    "required": ["function"]
                }),
            },
            ToolDefinition {
                name: "get_call_graph".to_string(),
                description: "Get bounded function call chain. Use depth=1 for direct callees, depth=2 for two hops. Avoid depth>3 to prevent neighbor explosion.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "function": {"type": "string", "description": "Function to get call graph for"},
                        "depth": {"type": "integer", "default": 2, "description": "Maximum call graph depth (default: 2, max: 3)"},
                        "max_results": {"type": "integer", "default": 30, "description": "Maximum number of results (default: 30)"},
                        "project": {"type": "string", "description": "Optional: project path to search in (resolves to nearest .leankg directory)"}
                    },
                    "required": ["function"]
                }),
            },
            ToolDefinition {
                name: "search_code".to_string(),
                description: "Search code elements by name/type".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "query": {"type": "string", "description": "Search query string"},
                        "element_type": {"type": "string", "enum": ["file", "function", "struct", "class", "module", "import"], "description": "Filter by element type"},
                        "limit": {"type": "integer", "default": 20, "description": "Maximum number of results (default: 20, max: 50)"},
                        "project": {"type": "string", "description": "Optional: project path to search in (resolves to nearest .leankg directory)"}
                    },
                    "required": ["query"]
                }),
            },
            ToolDefinition {
                name: "search_annotations".to_string(),
                description: "Search for code elements by annotation. Returns classes, functions, or properties with matching annotations.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "annotation_name": {"type": "string", "description": "Annotation name to search for (e.g., 'Entity', 'HiltViewModel')"},
                        "target_type": {"type": "string", "enum": ["class", "function", "property", "parameter", "all"], "description": "Filter by target type"},
                        "file_pattern": {"type": "string", "description": "Optional file pattern to limit search"},
                        "limit": {"type": "integer", "default": 20, "description": "Maximum number of results (default: 20)"},
                        "project": {"type": "string", "description": "Optional: project path to search in"}
                    },
                    "required": ["annotation_name"]
                }),
            },
            ToolDefinition {
                name: "generate_doc".to_string(),
                description: "Generate documentation for file".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "file": {"type": "string", "description": "File to generate documentation for"},
                        "project": {"type": "string", "description": "Optional: project path to search in (resolves to nearest .leankg directory)"}
                    },
                    "required": ["file"]
                }),
            },
            ToolDefinition {
                name: "find_large_functions".to_string(),
                description: "Find oversized functions by line count".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "min_lines": {"type": "integer", "default": 50, "description": "Minimum line count threshold (default: 50)"},
                        "limit": {"type": "integer", "default": 20, "description": "Maximum number of results (default: 20, max: 100)"},
                        "offset": {"type": "integer", "default": 0, "description": "Number of results to skip (pagination offset)"},
                        "project": {"type": "string", "description": "Optional: project path to search in (resolves to nearest .leankg directory)"}
                    },
                    "required": []
                }),
            },
            ToolDefinition {
                name: "get_tested_by".to_string(),
                description: "Get test coverage for a function/file".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "file": {"type": "string", "description": "File to get test coverage for"}
                    },
                    "required": ["file"]
                }),
            },
            ToolDefinition {
                name: "get_doc_for_file".to_string(),
                description: "Get documentation files that reference a code element".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "file": {"type": "string", "description": "File to get documentation for"}
                    },
                    "required": ["file"]
                }),
            },
            ToolDefinition {
                name: "get_files_for_doc".to_string(),
                description: "Get code elements referenced in a documentation file".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "doc": {"type": "string", "description": "Documentation file path"}
                    },
                    "required": ["doc"]
                }),
            },
            ToolDefinition {
                name: "get_doc_structure".to_string(),
                description: "Get documentation directory structure".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {},
                    "required": []
                }),
            },
            ToolDefinition {
                name: "get_traceability".to_string(),
                description: "Get full traceability chain for a code element".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "element": {"type": "string", "description": "Code element to trace"}
                    },
                    "required": ["element"]
                }),
            },
            ToolDefinition {
                name: "search_by_requirement".to_string(),
                description: "Find code elements related to a specific requirement".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "requirement_id": {"type": "string", "description": "Requirement ID to search for"}
                    },
                    "required": ["requirement_id"]
                }),
            },
            ToolDefinition {
                name: "get_doc_tree".to_string(),
                description: "Get documentation tree structure with hierarchy".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "limit": {"type": "integer", "default": 50, "description": "Maximum number of categories (default: 50, max: 200)"},
                        "offset": {"type": "integer", "default": 0, "description": "Number of categories to skip (pagination offset)"}
                    },
                    "required": []
                }),
            },
            ToolDefinition {
                name: "get_code_tree".to_string(),
                description: "Get codebase structure".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "limit": {"type": "integer", "default": 50, "description": "Maximum number of files (default: 50, max: 200)"},
                        "offset": {"type": "integer", "default": 0, "description": "Number of files to skip (pagination offset)"}
                    },
                    "required": []
                }),
            },
            ToolDefinition {
                name: "find_related_docs".to_string(),
                description: "Find documentation related to a code change".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "file": {"type": "string", "description": "File that was changed"}
                    },
                    "required": ["file"]
                }),
            },
            ToolDefinition {
                name: "mcp_hello".to_string(),
                description: "Returns 'Hello, World!'".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {},
                    "required": []
                }),
            },
            ToolDefinition {
                name: "get_clusters".to_string(),
                description: "Get all clusters (functional communities) in the codebase. Returns cluster ID, label, member count, and representative files.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "limit": {"type": "integer", "default": 50, "description": "Maximum number of clusters (default: 50, max: 100)"},
                        "offset": {"type": "integer", "default": 0, "description": "Number of clusters to skip (pagination offset)"}
                    },
                    "required": []
                }),
            },
            ToolDefinition {
                name: "get_cluster_context".to_string(),
                description: "Get all symbols in a cluster with entry points and inter-cluster dependencies.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "cluster_id": {"type": "string", "description": "Cluster ID to get context for"},
                        "cluster_label": {"type": "string", "description": "Alternative: cluster label to search for"}
                    },
                    "required": []
                }),
            },
            ToolDefinition {
                name: "run_raw_query".to_string(),
                description: "Execute a raw Datalog/Cypher query against the LeanKG CozoDB engine".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "query": {"type": "string", "description": "The CozoDB Datalog query to execute"},
                        "params": {
                            "type": "object",
                            "description": "Optional parameters for the parameterized query",
                            "additionalProperties": true
                        }
                    },
                    "required": ["query"]
                }),
            },
            ToolDefinition {
                name: "get_nav_graph".to_string(),
                description: "Get the navigation graph structure for a screen or nav file. Returns destinations, actions, arguments, and deep links.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "file": {"type": "string", "description": "Nav XML file path or Kotlin DSL file path"},
                        "graph_id": {"type": "string", "description": "Nav graph ID (alternative to file)"}
                    },
                    "required": []
                }),
            },
            ToolDefinition {
                name: "find_route".to_string(),
                description: "Find which destination a route string or action ID resolves to.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "route": {"type": "string", "description": "Route string (e.g. 'profile/{userId}') or action ID (e.g. 'action_home_to_detail')"}
                    },
                    "required": ["route"]
                }),
            },
            ToolDefinition {
                name: "get_screen_args".to_string(),
                description: "List all arguments a screen/destination requires, with types and default values.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "destination": {"type": "string", "description": "Destination name, route, or file path"},
                        "limit": {"type": "integer", "default": 20, "description": "Maximum results"}
                    },
                    "required": ["destination"]
                }),
            },
            ToolDefinition {
                name: "get_nav_callers".to_string(),
                description: "Find all call sites that navigate to a given destination. Use for impact radius when changing screen args.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "destination": {"type": "string", "description": "Destination name, route, fragment class, or activity class"}
                    },
                    "required": ["destination"]
                }),
            },
            ToolDefinition {
                name: "get_service_graph".to_string(),
                description: "Get microservice call graph with service repos as nodes. Returns aggregated service-to-service topology from service_calls relationships. The current service repo is the biggest node.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "service": {"type": "string", "description": "Current service name (defaults to project directory name)"}
                    },
                    "required": []
                }),
            },
        ]
    }
}

#[derive(Debug, Clone)]
pub struct ToolDefinition {
    pub name: String,
    pub description: String,
    pub input_schema: Value,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_list_tools_returns_tools() {
        let tools = ToolRegistry::list_tools();
        assert!(!tools.is_empty());
    }

    #[test]
    fn test_list_tools_contains_expected() {
        let tools = ToolRegistry::list_tools();
        let names: Vec<_> = tools.iter().map(|t| t.name.as_str()).collect();
        assert!(names.contains(&"query_file"));
        assert!(names.contains(&"get_dependencies"));
        assert!(names.contains(&"get_impact_radius"));
    }

    #[test]
    fn test_tool_definitions_have_schemas() {
        let tools = ToolRegistry::list_tools();
        for tool in &tools {
            assert!(!tool.description.is_empty());
            assert!(tool.input_schema.is_object());
        }
    }
}