agentis-ctx 0.3.3

Fast CLI tool that generates AI-ready context from your codebase, with built-in code intelligence
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
//! Search-related MCP tools.

use rmcp::model::{CallToolResult, Content, ErrorCode, Tool};
use serde_json::Value;

use super::{parse_params, schema_for, DefinitionParams, ReferencesParams, SearchParams};
use crate::mcp::server::CtxServer;

/// Helper to create an internal error.
fn internal_error(msg: impl Into<String>) -> rmcp::ErrorData {
    rmcp::ErrorData::new(ErrorCode::INTERNAL_ERROR, msg.into(), None)
}

/// Create the search_symbols tool definition.
pub fn search_symbols_tool() -> Tool {
    Tool::new(
        "search_symbols",
        "Search for symbols (functions, structs, enums, etc.) by name pattern. \
         Supports partial matching and can filter by kind and file path.",
        schema_for::<SearchParams>(),
    )
}

/// Create the get_definition tool definition.
pub fn get_definition_tool() -> Tool {
    Tool::new(
        "get_definition",
        "Get the full source code definition of a symbol. \
         Returns the complete implementation including signature, body, and documentation.",
        schema_for::<DefinitionParams>(),
    )
}

/// Create the find_references tool definition.
pub fn find_references_tool() -> Tool {
    Tool::new(
        "find_references",
        "Find all places where a symbol is referenced or called. \
         Useful for understanding how a function or type is used throughout the codebase.",
        schema_for::<ReferencesParams>(),
    )
}

/// Execute the search_symbols tool.
pub async fn search_symbols(
    server: &CtxServer,
    args: Option<&serde_json::Map<String, Value>>,
) -> Result<CallToolResult, rmcp::ErrorData> {
    let params: SearchParams = parse_params(args)?;

    let limit = params.limit.unwrap_or(20);

    let symbols = server
        .with_db(|db| {
            db.find_symbols_filtered(
                &params.query,
                limit,
                params.file.as_deref(),
                params.kind.as_deref(),
            )
        })
        .map_err(|e| internal_error(e.to_string()))?;

    if symbols.is_empty() {
        return Ok(CallToolResult::success(vec![Content::text(format!(
            "No symbols found matching '{}'",
            params.query
        ))]));
    }

    // Format results
    let mut output = format!(
        "Found {} symbols matching '{}':\n\n",
        symbols.len(),
        params.query
    );
    for symbol in &symbols {
        output.push_str(&format!(
            "- {} ({}) in {}:{}\n",
            symbol.name,
            symbol.kind.as_str(),
            symbol.file_path,
            symbol.line_start
        ));
        if let Some(ref sig) = symbol.signature {
            output.push_str(&format!("  Signature: {}\n", sig));
        }
        if let Some(ref brief) = symbol.brief {
            output.push_str(&format!("  Description: {}\n", brief));
        }
        output.push('\n');
    }

    Ok(CallToolResult::success(vec![Content::text(output)]))
}

/// Execute the get_definition tool.
pub async fn get_definition(
    server: &CtxServer,
    args: Option<&serde_json::Map<String, Value>>,
) -> Result<CallToolResult, rmcp::ErrorData> {
    let params: DefinitionParams = parse_params(args)?;

    // First, try to find the symbol
    let symbols = server
        .with_db(|db| {
            db.find_symbols_filtered(
                &params.symbol,
                100,
                params.file.as_deref(),
                params.kind.as_deref(),
            )
        })
        .map_err(|e| internal_error(e.to_string()))?;

    if symbols.is_empty() {
        return Ok(CallToolResult::success(vec![Content::text(format!(
            "Symbol '{}' not found",
            params.symbol
        ))]));
    }

    // If multiple matches and no filters, show disambiguation
    if symbols.len() > 1 && params.file.is_none() && params.kind.is_none() {
        let mut output = format!(
            "Found {} symbols named '{}'. Please narrow your search using 'file' or 'kind' parameters:\n\n",
            symbols.len(),
            params.symbol
        );
        for s in symbols.iter().take(10) {
            output.push_str(&format!(
                "- {} ({}) in {}:{}\n",
                s.name,
                s.kind.as_str(),
                s.file_path,
                s.line_start
            ));
        }
        if symbols.len() > 10 {
            output.push_str(&format!("... and {} more\n", symbols.len() - 10));
        }
        return Ok(CallToolResult::success(vec![Content::text(output)]));
    }

    // Get the source for the first matching symbol
    let sym = &symbols[0];
    let sym_id = sym.id.clone();
    let source = server
        .with_db(|db| db.get_source(&sym_id))
        .map_err(|e| internal_error(e.to_string()))?;

    match source {
        Some(src) => {
            let mut output = format!(
                "// {} ({}) - {}:{}\n",
                sym.name,
                sym.kind.as_str(),
                sym.file_path,
                sym.line_start
            );
            if let Some(ref brief) = sym.brief {
                output.push_str(&format!("// {}\n", brief));
            }
            output.push('\n');
            output.push_str(&src);
            Ok(CallToolResult::success(vec![Content::text(output)]))
        }
        None => Ok(CallToolResult::success(vec![Content::text(format!(
            "Source code not available for '{}'",
            sym.name
        ))])),
    }
}

/// Execute the find_references tool.
pub async fn find_references(
    server: &CtxServer,
    args: Option<&serde_json::Map<String, Value>>,
) -> Result<CallToolResult, rmcp::ErrorData> {
    let params: ReferencesParams = parse_params(args)?;

    // Find the symbol first
    let symbols = server
        .with_db(|db| db.find_symbols_filtered(&params.symbol, 100, params.file.as_deref(), None))
        .map_err(|e| internal_error(e.to_string()))?;

    if symbols.is_empty() {
        return Ok(CallToolResult::success(vec![Content::text(format!(
            "Symbol '{}' not found",
            params.symbol
        ))]));
    }

    // Get incoming edges (places that reference this symbol)
    let sym = &symbols[0];
    let sym_name = sym.name.clone();
    let edges = server
        .with_db(|db| db.get_incoming_edges(&sym_name))
        .map_err(|e| internal_error(e.to_string()))?;

    if edges.is_empty() {
        return Ok(CallToolResult::success(vec![Content::text(format!(
            "No references found for '{}'",
            sym.name
        ))]));
    }

    let mut output = format!("Found {} references to '{}':\n\n", edges.len(), sym.name);

    for edge in &edges {
        let source_id = edge.source_id.clone();
        if let Ok(Some(source_sym)) = server.with_db(|db| db.get_symbol(&source_id)) {
            output.push_str(&format!(
                "- {} ({}:{})\n",
                source_sym.name,
                source_sym.file_path,
                edge.line.unwrap_or(source_sym.line_start)
            ));
            if let Some(ref ctx) = edge.context {
                output.push_str(&format!("  Context: {}\n", ctx));
            }
        }
    }

    Ok(CallToolResult::success(vec![Content::text(output)]))
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use std::fs;
    use tempfile::TempDir;

    use crate::index::Indexer;

    /// Create a test database with some symbols for testing.
    fn setup_test_project() -> (TempDir, CtxServer) {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path().to_path_buf();

        // Create a simple Rust file with testable symbols
        let src_dir = root.join("src");
        fs::create_dir_all(&src_dir).unwrap();
        fs::write(
            src_dir.join("lib.rs"),
            r#"
/// A greeting function.
pub fn hello_world() -> String {
    "Hello, World!".to_string()
}

/// Another function that calls hello_world.
pub fn greet() -> String {
    hello_world()
}

/// A simple struct.
pub struct Greeter {
    name: String,
}

impl Greeter {
    pub fn new(name: String) -> Self {
        Self { name }
    }

    pub fn greet(&self) -> String {
        format!("Hello, {}!", self.name)
    }
}
"#,
        )
        .unwrap();

        // Index the project using Indexer
        let mut indexer =
            Indexer::with_config(&root, false, crate::walker::WalkerConfig::default()).unwrap();
        indexer.index().unwrap();

        let server = CtxServer::new(root).unwrap();
        (temp_dir, server)
    }

    /// Helper to extract text from Content
    fn get_text_content(result: &CallToolResult) -> &str {
        let content = &result.content[0];
        match &content.raw {
            rmcp::model::RawContent::Text(text) => &text.text,
            _ => panic!("Expected text content"),
        }
    }

    #[test]
    fn test_search_symbols_tool_definition() {
        let tool = search_symbols_tool();
        assert_eq!(tool.name.as_ref(), "search_symbols");
        assert!(tool.description.is_some());
    }

    #[test]
    fn test_get_definition_tool_definition() {
        let tool = get_definition_tool();
        assert_eq!(tool.name.as_ref(), "get_definition");
        assert!(tool.description.is_some());
    }

    #[test]
    fn test_find_references_tool_definition() {
        let tool = find_references_tool();
        assert_eq!(tool.name.as_ref(), "find_references");
        assert!(tool.description.is_some());
    }

    #[tokio::test]
    async fn test_mcp_search_symbols_tool() {
        let (_temp_dir, server) = setup_test_project();

        // Test searching for "hello"
        let args = json!({
            "query": "hello"
        });
        let args_map = args.as_object().unwrap();

        let result = search_symbols(&server, Some(args_map)).await;
        assert!(result.is_ok(), "search_symbols should succeed");

        let result = result.unwrap();
        let text = get_text_content(&result);
        assert!(
            text.contains("hello_world"),
            "Result should contain hello_world function: {}",
            text
        );
    }

    #[tokio::test]
    async fn test_search_symbols_with_kind_filter() {
        let (_temp_dir, server) = setup_test_project();

        // Test searching for structs only
        let args = json!({
            "query": "Greeter",
            "kind": "struct"
        });
        let args_map = args.as_object().unwrap();

        let result = search_symbols(&server, Some(args_map)).await;
        assert!(result.is_ok());

        let result = result.unwrap();
        let text = get_text_content(&result);
        assert!(
            text.contains("Greeter") && text.contains("struct"),
            "Result should contain Greeter struct: {}",
            text
        );
    }

    #[tokio::test]
    async fn test_search_symbols_no_results() {
        let (_temp_dir, server) = setup_test_project();

        let args = json!({
            "query": "nonexistent_symbol_xyz"
        });
        let args_map = args.as_object().unwrap();

        let result = search_symbols(&server, Some(args_map)).await;
        assert!(result.is_ok());

        let result = result.unwrap();
        let text = get_text_content(&result);
        assert!(
            text.contains("No symbols found"),
            "Should indicate no symbols found: {}",
            text
        );
    }

    #[tokio::test]
    async fn test_get_definition_tool() {
        let (_temp_dir, server) = setup_test_project();

        // First search to verify symbol exists and get its exact name
        let search_args = json!({
            "query": "hello_world"
        });
        let search_result = search_symbols(&server, Some(search_args.as_object().unwrap())).await;
        assert!(search_result.is_ok(), "Search should succeed");
        let search_unwrapped = search_result.unwrap();
        let search_text = get_text_content(&search_unwrapped);
        assert!(
            search_text.contains("hello_world"),
            "Should find hello_world: {}",
            search_text
        );

        // Now get definition
        let args = json!({
            "symbol": "hello_world",
            "kind": "function"
        });
        let args_map = args.as_object().unwrap();

        let result = get_definition(&server, Some(args_map)).await;
        assert!(result.is_ok(), "get_definition should succeed");

        let result = result.unwrap();
        let text = get_text_content(&result);
        // Should contain the function signature and body, or at least the symbol info
        assert!(
            text.contains("hello_world"),
            "Should contain hello_world: {}",
            text
        );
    }

    #[tokio::test]
    async fn test_missing_params_error() {
        let (_temp_dir, server) = setup_test_project();

        // Call with no args should fail
        let result = search_symbols(&server, None).await;
        assert!(result.is_err(), "Should fail with missing params");

        let err = result.unwrap_err();
        assert_eq!(err.code, ErrorCode::INVALID_PARAMS);
    }
}