codanna 0.9.23

Code Intelligence for Large Language Models
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
//! Symbol-target tools: find_symbol, get_calls, find_callers, analyze_impact.

use rmcp::model::ErrorData as McpError;
use rmcp::model::*;
use rmcp::{handler::server::wrapper::Parameters, tool, tool_router};

use crate::Symbol;
use crate::mcp::requests::{
    AnalyzeImpactRequest, FindCallersRequest, FindSymbolRequest, GetCallsRequest,
};
use crate::mcp::server::{CodeIntelligenceServer, generate_mcp_guidance};
use crate::mcp::service::{
    self, SymbolResolution, parse_receiver_context, qualified_call, render_ambiguity,
};

#[tool_router(router = symbols_router, vis = "pub(crate)")]
impl CodeIntelligenceServer {
    #[tool(description = "Find a symbol by name in the indexed codebase")]
    pub async fn find_symbol(
        &self,
        Parameters(FindSymbolRequest { name, lang }): Parameters<FindSymbolRequest>,
    ) -> Result<CallToolResult, McpError> {
        use crate::symbol::context::ContextIncludes;

        let indexer = self.facade.read().await;

        // Support symbol_id:XXX format for direct lookup (from semantic search results)
        let symbols = if let Some(id_str) = name.strip_prefix("symbol_id:") {
            if let Ok(id) = id_str.parse::<u32>() {
                indexer
                    .get_symbol(crate::SymbolId(id))
                    .map(|s| vec![s])
                    .unwrap_or_default()
            } else {
                return Ok(CallToolResult::error(vec![ContentBlock::text(format!(
                    "Invalid symbol_id format: {id_str}"
                ))]));
            }
        } else {
            let mut found = indexer.find_symbols_by_name(&name, lang.as_deref());
            if found.is_empty() {
                found = crate::mcp::service::find_dotted_members(&name, |n| {
                    indexer.find_symbols_by_name(n, lang.as_deref())
                });
            }
            found
        };

        if symbols.is_empty() {
            let mut output = format!("No symbols found with name: {name}");
            // Add guidance for no results
            if let Some(guidance) = generate_mcp_guidance(indexer.settings(), "find_symbol", 0) {
                output.push_str("\n\n---\nGuidance: ");
                output.push_str(&guidance);
                output.push('\n');
            }
            return Ok(CallToolResult::success(vec![ContentBlock::text(output)]));
        }

        let mut result = format!("Found {} symbol(s) named '{}':\n\n", symbols.len(), name);

        for (idx, symbol) in symbols.iter().enumerate() {
            if idx > 0 {
                result.push_str("\n---\n\n");
            }

            // Try to get full context with all relationship types
            if let Some(ctx) = indexer.get_symbol_context(symbol.id, ContextIncludes::SYMBOL_CARD) {
                // Header from the name-matched doc, not the id-keyed context:
                // on an index with duplicate symbol_ids the context lookup
                // returns another generation's doc and the row reads crossed.
                result.push_str(&crate::symbol::context::SymbolContext::location_with_type(
                    symbol,
                ));
                result.push('\n');

                // Add module path if available
                if let Some(module) = symbol.as_module_path() {
                    result.push_str(&format!("Module: {module}\n"));
                }

                // Add signature if available
                if let Some(sig) = symbol.as_signature() {
                    result.push_str(&format!("Signature: {sig}\n"));
                }

                // Add documentation preview
                if let Some(doc) = symbol.as_doc_comment() {
                    let doc_preview: Vec<&str> = doc.lines().take(3).collect();
                    let preview = if doc.lines().count() > 3 {
                        format!("{}...", doc_preview.join(" "))
                    } else {
                        doc_preview.join(" ")
                    };
                    result.push_str(&format!("Documentation: {preview}\n"));
                }

                // Add relationship summary
                let mut has_relationships = false;

                // What traits this type implements
                if let Some(impls) = &ctx.relationships.implements {
                    if !impls.is_empty() {
                        result.push_str(&format!("Implements: {} trait(s)\n", impls.len()));
                        for trait_sym in impls.iter().take(5) {
                            result.push_str(&format!(
                                "  -> {} at {}\n",
                                trait_sym.name,
                                crate::symbol::context::SymbolContext::symbol_location(trait_sym)
                            ));
                        }
                        if impls.len() > 5 {
                            result.push_str(&format!("  ... and {} more\n", impls.len() - 5));
                        }
                        has_relationships = true;
                    }
                }

                // What types implement this trait
                if let Some(impls) = &ctx.relationships.implemented_by {
                    if !impls.is_empty() {
                        result.push_str(&format!("Implemented by: {} type(s)\n", impls.len()));
                        for impl_sym in impls.iter().take(5) {
                            result.push_str(&format!(
                                "  <- {} at {}\n",
                                impl_sym.name,
                                crate::symbol::context::SymbolContext::symbol_location(impl_sym)
                            ));
                        }
                        if impls.len() > 5 {
                            result.push_str(&format!("  ... and {} more\n", impls.len() - 5));
                        }
                        has_relationships = true;
                    }
                }

                if let Some(defines) = &ctx.relationships.defines {
                    if !defines.is_empty() {
                        let methods = defines
                            .iter()
                            .filter(|s| s.kind == crate::SymbolKind::Method)
                            .count();
                        if methods > 0 {
                            result.push_str(&format!("Defines: {methods} method(s)\n"));
                            has_relationships = true;
                        }
                    }
                }

                if let Some(callers) = &ctx.relationships.called_by {
                    if !callers.is_empty() {
                        result.push_str(&format!("Called by: {} function(s)\n", callers.len()));
                        has_relationships = true;
                    }
                }

                // What base class(es) this extends
                if let Some(extends) = &ctx.relationships.extends {
                    if !extends.is_empty() {
                        result.push_str(&format!("Extends: {} class(es)\n", extends.len()));
                        for base in extends.iter().take(3) {
                            result.push_str(&format!(
                                "  -> {} at {}\n",
                                base.name,
                                crate::symbol::context::SymbolContext::symbol_location(base)
                            ));
                        }
                        if extends.len() > 3 {
                            result.push_str(&format!("  ... and {} more\n", extends.len() - 3));
                        }
                        has_relationships = true;
                    }
                }

                // What classes extend this
                if let Some(extended_by) = &ctx.relationships.extended_by {
                    if !extended_by.is_empty() {
                        result.push_str(&format!("Extended by: {} class(es)\n", extended_by.len()));
                        for derived in extended_by.iter().take(3) {
                            result.push_str(&format!(
                                "  <- {} at {}\n",
                                derived.name,
                                crate::symbol::context::SymbolContext::symbol_location(derived)
                            ));
                        }
                        if extended_by.len() > 3 {
                            result.push_str(&format!("  ... and {} more\n", extended_by.len() - 3));
                        }
                        has_relationships = true;
                    }
                }

                // What types this symbol uses
                if let Some(uses) = &ctx.relationships.uses {
                    if !uses.is_empty() {
                        result.push_str(&format!("Uses: {} type(s)\n", uses.len()));
                        for used in uses.iter().take(3) {
                            result.push_str(&format!(
                                "  -> {} at {}\n",
                                used.name,
                                crate::symbol::context::SymbolContext::symbol_location(used)
                            ));
                        }
                        if uses.len() > 3 {
                            result.push_str(&format!("  ... and {} more\n", uses.len() - 3));
                        }
                        has_relationships = true;
                    }
                }

                // What symbols use this type
                if let Some(used_by) = &ctx.relationships.used_by {
                    if !used_by.is_empty() {
                        result.push_str(&format!("Used by: {} symbol(s)\n", used_by.len()));
                        has_relationships = true;
                    }
                }

                if !has_relationships && symbol.kind == crate::SymbolKind::Function {
                    result.push_str("No direct callers found\n");
                }
            } else {
                // Fallback to basic info
                result.push_str(&format!(
                    "{:?} at {}:{}\n",
                    symbol.kind,
                    symbol.file_path,
                    symbol.range.start_line + 1
                ));

                if let Some(ref doc) = symbol.doc_comment {
                    let doc_preview: Vec<&str> = doc.lines().take(3).collect();
                    let preview = if doc.lines().count() > 3 {
                        format!("{}...", doc_preview.join(" "))
                    } else {
                        doc_preview.join(" ")
                    };
                    result.push_str(&format!("Documentation: {preview}\n"));
                }

                if let Some(ref sig) = symbol.signature {
                    result.push_str(&format!("Signature: {sig}\n"));
                }
            }
        }

        // Add system guidance
        if let Some(guidance) =
            generate_mcp_guidance(indexer.settings(), "find_symbol", symbols.len())
        {
            result.push_str("\n---\nGuidance: ");
            result.push_str(&guidance);
            result.push('\n');
        }

        Ok(CallToolResult::success(vec![ContentBlock::text(result)]))
    }

    #[tool(
        description = "Get functions that a given function CALLS (invokes with parentheses).\n\nShows: function_name() → what it calls\nDoes NOT show: Type usage, component rendering, or who calls this function.\n\nUse analyze_impact for: Type dependencies, component usage (JSX), or reverse lookups."
    )]
    pub async fn get_calls(
        &self,
        Parameters(GetCallsRequest {
            function_name,
            symbol_id,
        }): Parameters<GetCallsRequest>,
    ) -> Result<CallToolResult, McpError> {
        let indexer = self.facade.read().await;

        // Resolution policy is shared with the CLI JSON path via the
        // service layer; text renderings stay byte-identical.
        let (symbol, identifier) =
            match service::resolve_symbol_or_id(&indexer, symbol_id, function_name) {
                SymbolResolution::Resolved { symbol, identifier } => (symbol, identifier),
                SymbolResolution::NotFoundById(id) => {
                    return Ok(CallToolResult::success(vec![ContentBlock::text(format!(
                        "Symbol not found: symbol_id:{id}"
                    ))]));
                }
                SymbolResolution::NotFoundByName(name) => {
                    return Ok(CallToolResult::success(vec![ContentBlock::text(format!(
                        "Function not found: {name}"
                    ))]));
                }
                SymbolResolution::Ambiguous { name, candidates } => {
                    return Ok(CallToolResult::success(vec![ContentBlock::text(
                        render_ambiguity("get_calls", &name, &candidates),
                    )]));
                }
                SymbolResolution::MissingParam => {
                    return Ok(CallToolResult::success(vec![ContentBlock::text(
                        "Error: Either function_name or symbol_id must be provided".to_string(),
                    )]));
                }
            };

        // Get calls for this specific symbol
        let all_called_with_metadata = indexer.get_called_functions_with_metadata(symbol.id);

        if all_called_with_metadata.is_empty() {
            let mut output = format!("{identifier} doesn't call any functions");
            // Add guidance for no results
            if let Some(guidance) = generate_mcp_guidance(indexer.settings(), "get_calls", 0) {
                output.push_str("\n\n---\nGuidance: ");
                output.push_str(&guidance);
                output.push('\n');
            }
            return Ok(CallToolResult::success(vec![ContentBlock::text(output)]));
        }

        let result_count = all_called_with_metadata.len();
        let mut result = format!("{identifier} calls {result_count} function(s):\n");
        for (callee, metadata) in all_called_with_metadata {
            // Parse metadata to extract receiver info and call site location
            let (call_display, call_line) = if let Some(ref meta) = metadata {
                let display = meta
                    .context
                    .as_deref()
                    .and_then(parse_receiver_context)
                    .map(|(receiver, is_static)| qualified_call(receiver, is_static, &callee.name))
                    .unwrap_or_else(|| callee.name.to_string());

                // Use call site line if available, otherwise definition line
                let line = meta
                    .line
                    .map(|l| l + 1)
                    .unwrap_or(callee.range.start_line + 1);
                (display, line)
            } else {
                (callee.name.to_string(), callee.range.start_line + 1)
            };

            result.push_str(&format!(
                "  -> {:?} {} at {}:{}\n",
                callee.kind, call_display, callee.file_path, call_line
            ));
            if let Some(ref sig) = callee.signature {
                result.push_str(&format!("     Signature: {sig}\n"));
            }
        }

        // Add system guidance
        if let Some(guidance) = generate_mcp_guidance(indexer.settings(), "get_calls", result_count)
        {
            result.push_str("\n---\nGuidance: ");
            result.push_str(&guidance);
            result.push('\n');
        }

        Ok(CallToolResult::success(vec![ContentBlock::text(result)]))
    }

    #[tool(
        description = "Find functions that CALL a given function (invoke it with parentheses).\n\nShows: what calls → function_name()\nDoes NOT show: Type references, component rendering, or what this function calls.\n\nUse analyze_impact for: Complete dependency graph including type usage and composition."
    )]
    pub async fn find_callers(
        &self,
        Parameters(FindCallersRequest {
            function_name,
            symbol_id,
        }): Parameters<FindCallersRequest>,
    ) -> Result<CallToolResult, McpError> {
        let indexer = self.facade.read().await;

        // Shared resolution policy; see service.rs.
        let (symbol, identifier) =
            match service::resolve_symbol_or_id(&indexer, symbol_id, function_name) {
                SymbolResolution::Resolved { symbol, identifier } => (symbol, identifier),
                SymbolResolution::NotFoundById(id) => {
                    return Ok(CallToolResult::success(vec![ContentBlock::text(format!(
                        "Symbol not found: symbol_id:{id}"
                    ))]));
                }
                SymbolResolution::NotFoundByName(name) => {
                    return Ok(CallToolResult::success(vec![ContentBlock::text(format!(
                        "Function not found: {name}"
                    ))]));
                }
                SymbolResolution::Ambiguous { name, candidates } => {
                    return Ok(CallToolResult::success(vec![ContentBlock::text(
                        render_ambiguity("find_callers", &name, &candidates),
                    )]));
                }
                SymbolResolution::MissingParam => {
                    return Ok(CallToolResult::success(vec![ContentBlock::text(
                        "Error: Either function_name or symbol_id must be provided".to_string(),
                    )]));
                }
            };

        // Get callers for THIS SPECIFIC symbol only (no aggregation)
        let all_callers_with_metadata = indexer.get_calling_functions_with_metadata(symbol.id);

        if all_callers_with_metadata.is_empty() {
            let mut output = format!("No functions call {identifier}");
            // Add guidance for no results
            if let Some(guidance) = generate_mcp_guidance(indexer.settings(), "find_callers", 0) {
                output.push_str("\n\n---\nGuidance: ");
                output.push_str(&guidance);
                output.push('\n');
            }
            return Ok(CallToolResult::success(vec![ContentBlock::text(output)]));
        }

        // Build structured text response with rich metadata
        let result_count = all_callers_with_metadata.len();
        let mut result = format!("{result_count} function(s) call {identifier}:\n");

        for (caller, metadata) in all_callers_with_metadata {
            // Parse metadata to extract receiver info and call site location
            let (call_info, call_line) = if let Some(ref meta) = metadata {
                let info = meta
                    .context
                    .as_deref()
                    .and_then(parse_receiver_context)
                    .map(|(receiver, is_static)| {
                        format!(
                            " (calls {})",
                            qualified_call(receiver, is_static, &symbol.name)
                        )
                    })
                    .unwrap_or_default();

                // Use call site line if available, otherwise definition line
                let line = meta
                    .line
                    .map(|l| l + 1)
                    .unwrap_or(caller.range.start_line + 1);
                (info, line)
            } else {
                (String::new(), caller.range.start_line + 1)
            };

            result.push_str(&format!(
                "  <- {:?} {} at {}:{}{}\n",
                caller.kind, caller.name, caller.file_path, call_line, call_info
            ));

            if let Some(ref sig) = caller.signature {
                result.push_str(&format!("     Signature: {sig}\n"));
            }
        }

        // Add system guidance
        if let Some(guidance) =
            generate_mcp_guidance(indexer.settings(), "find_callers", result_count)
        {
            result.push_str("\n---\nGuidance: ");
            result.push_str(&guidance);
            result.push('\n');
        }

        Ok(CallToolResult::success(vec![ContentBlock::text(result)]))
    }

    #[tool(
        description = "Analyze complete impact of changing a symbol. Shows ALL relationships: function calls, type usage, composition.\n\nShows:\n- What CALLS this function\n- What USES this as a type (fields, parameters, returns)\n- What RENDERS/COMPOSES this (JSX: <Component>, Rust: struct fields, etc.)\n- Full dependency graph across files\n\nUse this when: You need to see everything that depends on a symbol."
    )]
    pub async fn analyze_impact(
        &self,
        Parameters(AnalyzeImpactRequest {
            symbol_name,
            symbol_id,
            max_depth,
        }): Parameters<AnalyzeImpactRequest>,
    ) -> Result<CallToolResult, McpError> {
        use crate::symbol::context::ContextIncludes;

        let indexer = self.facade.read().await;

        // Shared resolution policy; see service.rs.
        let (symbol, identifier) =
            match service::resolve_symbol_or_id(&indexer, symbol_id, symbol_name) {
                SymbolResolution::Resolved { symbol, identifier } => (symbol, identifier),
                SymbolResolution::NotFoundById(id) => {
                    return Ok(CallToolResult::success(vec![ContentBlock::text(format!(
                        "Symbol not found: symbol_id:{id}"
                    ))]));
                }
                SymbolResolution::NotFoundByName(name) => {
                    return Ok(CallToolResult::success(vec![ContentBlock::text(format!(
                        "Symbol not found: {name}"
                    ))]));
                }
                SymbolResolution::Ambiguous { name, candidates } => {
                    return Ok(CallToolResult::success(vec![ContentBlock::text(
                        render_ambiguity("analyze_impact", &name, &candidates),
                    )]));
                }
                SymbolResolution::MissingParam => {
                    return Ok(CallToolResult::success(vec![ContentBlock::text(
                        "Error: Either symbol_name or symbol_id must be provided".to_string(),
                    )]));
                }
            };

        // Analyze impact for THIS SPECIFIC symbol only (no aggregation)
        let impacted = indexer.get_impact_radius(symbol.id, Some(max_depth as usize));

        if impacted.is_empty() {
            let mut output = format!("No symbols would be impacted by changing {identifier}");
            // Add guidance for no results
            if let Some(guidance) = generate_mcp_guidance(indexer.settings(), "analyze_impact", 0) {
                output.push_str("\n\n---\nGuidance: ");
                output.push_str(&guidance);
                output.push('\n');
            }
            return Ok(CallToolResult::success(vec![ContentBlock::text(output)]));
        }

        let mut result = format!("Analyzing impact of changing: {identifier}\n");

        // Show the specific symbol being analyzed
        if let Some(ctx) = indexer.get_symbol_context(
            symbol.id,
            ContextIncludes::CALLERS | ContextIncludes::EXTENDS | ContextIncludes::USES,
        ) {
            // Name-matched doc, not the id-keyed context (see find_symbol).
            let location = crate::symbol::context::SymbolContext::location(&symbol);
            let direct_callers = ctx
                .relationships
                .called_by
                .as_ref()
                .map(|c| c.len())
                .unwrap_or(0);

            // For classes, also show inheritance info
            let inheritance_info = if matches!(
                symbol.kind,
                crate::SymbolKind::Class | crate::SymbolKind::Struct
            ) {
                let extends_count = ctx
                    .relationships
                    .extends
                    .as_ref()
                    .map(|e| e.len())
                    .unwrap_or(0);
                let extended_by_count = ctx
                    .relationships
                    .extended_by
                    .as_ref()
                    .map(|e| e.len())
                    .unwrap_or(0);

                if extends_count > 0 || extended_by_count > 0 {
                    format!(", extends: {extends_count}, extended by: {extended_by_count}")
                } else {
                    String::new()
                }
            } else {
                String::new()
            };

            // Show uses info for all symbols
            let uses_count = ctx
                .relationships
                .uses
                .as_ref()
                .map(|u| u.len())
                .unwrap_or(0);
            let used_by_count = ctx
                .relationships
                .used_by
                .as_ref()
                .map(|u| u.len())
                .unwrap_or(0);

            let uses_info = if uses_count > 0 || used_by_count > 0 {
                format!(", uses: {uses_count}, used by: {used_by_count}")
            } else {
                String::new()
            };

            result.push_str(&format!(
                "Symbol: {:?} at {} (direct callers: {}{}{})\n\n",
                symbol.kind, location, direct_callers, inheritance_info, uses_info
            ));
        }

        let impact_count = impacted.len();
        result.push_str(&format!(
            "Total impact: {impact_count} symbol(s) would be affected (max depth: {max_depth})\n"
        ));

        // Group by symbol kind
        let mut by_kind: std::collections::HashMap<crate::SymbolKind, Vec<Symbol>> =
            std::collections::HashMap::new();

        for id in impacted {
            if let Some(sym) = indexer.get_symbol(id) {
                by_kind.entry(sym.kind).or_default().push(sym);
            }
        }

        // Display grouped by kind with locations
        for (kind, symbols) in by_kind {
            result.push_str(&format!("\n{kind:?} ({}): \n", symbols.len()));
            for sym in symbols {
                result.push_str(&format!(
                    "  - {} at {}:{}\n",
                    sym.name,
                    sym.file_path,
                    sym.range.start_line + 1
                ));
            }
        }

        // Add system guidance
        if let Some(guidance) =
            generate_mcp_guidance(indexer.settings(), "analyze_impact", impact_count)
        {
            result.push_str("\n---\nGuidance: ");
            result.push_str(&guidance);
            result.push('\n');
        }

        Ok(CallToolResult::success(vec![ContentBlock::text(result)]))
    }
}