codescout 0.12.1

High-performance coding agent toolkit MCP server
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
use anyhow::Result;
use serde_json::{json, Value};

use super::{Tool, ToolContext};

pub struct ListLibraries;

#[async_trait::async_trait]
impl Tool for ListLibraries {
    fn name(&self) -> &str {
        "list_libraries"
    }

    fn description(&self) -> &str {
        "List registered libraries and their index status. \
         Use scope='lib:<name>' in semantic_search, symbols, or index(action='build') to target a library. \
         Version staleness detection currently supports Cargo.lock (Rust) and package-lock.json (npm/Node); \
         Go, Python, and Yarn lockfiles are not yet tracked."
    }

    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {}
        })
    }

    async fn call(&self, _input: Value, ctx: &ToolContext) -> Result<Value> {
        let inner = ctx.agent.inner.read().await;
        let project = inner.active_project().ok_or_else(|| {
            super::RecoverableError::with_hint(
                "No active project. Use workspace(action='activate') first.",
                "Call workspace(action='activate', path=\"/path/to/project\") to set the active project.",
            )
        })?;

        let libs: Vec<Value> = project
            .library_registry
            .all()
            .iter()
            .map(|entry| {
                let stale = entry.indexed
                    && entry.version.is_some()
                    && entry.version_indexed.is_some()
                    && entry.version != entry.version_indexed;
                json!({
                    "name": entry.name,
                    "version": entry.version,
                    "version_indexed": entry.version_indexed,
                    "stale": stale,
                    "path": entry.path.display().to_string(),
                    "language": entry.language,
                    "discovered_via": entry.discovered_via,
                    "indexed": entry.indexed,
                    "source_available": entry.source_available,
                })
            })
            .collect();

        Ok(json!({ "libraries": libs }))
    }

    fn format_compact(&self, result: &Value) -> Option<String> {
        Some(format_list_libraries(result))
    }

    fn availability(&self, _caps: &crate::tools::ToolCapabilities) -> crate::tools::Availability {
        crate::tools::Availability::RequiresLibraries
    }
}

pub struct RegisterLibrary;

#[async_trait::async_trait]
impl Tool for RegisterLibrary {
    fn name(&self) -> &str {
        "register_library"
    }

    fn is_write(&self, _input: &Value) -> bool {
        true
    }

    fn description(&self) -> &str {
        "Register an external library for searching with scope='lib:<name>'. \
         Auto-detects name and language from manifest files (Cargo.toml, package.json, etc.). \
         Use name/language params to override auto-detection."
    }

    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "required": ["path"],
            "properties": {
                "path": {
                    "type": "string",
                    "description": "Absolute path to the library root directory"
                },
                "name": {
                    "type": "string",
                    "description": "Library name (auto-detected from manifest if omitted)"
                },
                "language": {
                    "type": "string",
                    "description": "Primary language (auto-detected if omitted)"
                }
            }
        })
    }

    async fn call(&self, input: Value, ctx: &ToolContext) -> Result<Value> {
        let raw_path = super::require_str_param(&input, "path")?;
        let lib_path = std::path::PathBuf::from(raw_path);

        if !lib_path.exists() {
            return Err(super::RecoverableError::with_hint(
                format!("Path does not exist: {}", lib_path.display()),
                "Provide an absolute path to an existing directory.",
            )
            .into());
        }
        if !lib_path.is_dir() {
            return Err(super::RecoverableError::with_hint(
                format!("Path is not a directory: {}", lib_path.display()),
                "Provide a path to a directory, not a file.",
            )
            .into());
        }

        // Scope guard (phase-5 S2): reject registering the home directory,
        // its parent, or a system path like `/etc` / `/usr`. Without this,
        // a prompt-injected `library(action="register", path="/etc")` would let a later
        // `index_project(scope="lib:…")` walk and embed the entire directory,
        // leaking secrets back to the LLM via `semantic_search`.
        //
        // Canonicalize first so relative traversals (`../..`) and symlinks
        // cannot bypass the classifier.
        let canon_lib_path = std::fs::canonicalize(&lib_path).unwrap_or_else(|_| lib_path.clone());
        if let Some(reason) = crate::embed::preflight::classify_path(&canon_lib_path) {
            return Err(super::RecoverableError::with_hint(
                format!(
                    "refusing to register library at '{}': {:?}",
                    canon_lib_path.display(),
                    reason,
                ),
                "Register a library root under a specific package directory, \
                 not your home directory or a system path.",
            )
            .into());
        }

        // Auto-detect from manifest, with user overrides.
        // IMPORTANT: discover_library_root expects a *file* path and calls .parent()
        // to start searching. Passing a directory would skip the directory itself.
        // We pass a synthetic file path inside the directory to work around this.
        let discovered = crate::library::discovery::discover_library_root(&lib_path.join("_probe"));
        let name = input["name"]
            .as_str()
            .map(String::from)
            .or_else(|| discovered.as_ref().map(|d| d.name.clone()))
            .unwrap_or_else(|| {
                lib_path
                    .file_name()
                    .unwrap_or_default()
                    .to_string_lossy()
                    .to_string()
            });
        let language = input["language"]
            .as_str()
            .map(String::from)
            .or_else(|| discovered.as_ref().map(|d| d.language.clone()))
            .unwrap_or_else(|| "unknown".to_string());

        // Register and save
        {
            let mut inner = ctx.agent.inner.write().await;
            let project = inner.active_project_mut().ok_or_else(|| {
                super::RecoverableError::with_hint(
                    "No active project.",
                    "Call workspace(action='activate') first.",
                )
            })?;
            project.library_registry.register(
                name.clone(),
                lib_path.clone(),
                language.clone(),
                crate::library::registry::DiscoveryMethod::Manual,
                true,
            );
            let registry_path = project.root.join(".codescout").join("libraries.json");
            project.library_registry.save(&registry_path)?;
        }

        Ok(json!({
            "status": "ok",
            "name": name,
            "language": language,
            "hint": format!(
                "Use scope='lib:{}' in symbols/semantic_search. \
                 Run index(action='build', scope='lib:{}') to enable semantic search.",
                name, name
            ),
        }))
    }

    fn format_compact(&self, result: &Value) -> Option<String> {
        Some(format!(
            "Registered library '{}' ({})",
            result["name"].as_str().unwrap_or("?"),
            result["language"].as_str().unwrap_or("?"),
        ))
    }

    fn availability(&self, _caps: &crate::tools::ToolCapabilities) -> crate::tools::Availability {
        crate::tools::Availability::RequiresLibraries
    }
}

fn format_list_libraries(result: &Value) -> String {
    let libs = match result["libraries"].as_array() {
        Some(l) if !l.is_empty() => l,
        _ => return "0 libraries".to_string(),
    };
    let name_width = libs
        .iter()
        .filter_map(|l| l["name"].as_str())
        .map(|n| n.len())
        .max()
        .unwrap_or(0);
    let mut out = format!("{} libraries", libs.len());
    for lib in libs.iter() {
        let name = lib["name"].as_str().unwrap_or("?");
        let status = if lib["indexed"].as_bool().unwrap_or(false) {
            "indexed"
        } else {
            "not indexed"
        };
        let stale_marker = if lib["stale"].as_bool().unwrap_or(false) {
            " [stale]"
        } else {
            ""
        };
        out.push_str(&format!("\n  {name:<name_width$}  {status}{stale_marker}"));
    }
    out
}

pub struct Library;

#[async_trait::async_trait]
impl Tool for Library {
    fn name(&self) -> &str {
        "library"
    }

    fn is_write(&self, input: &Value) -> bool {
        input.get("action").and_then(Value::as_str) == Some("register")
    }

    fn description(&self) -> &str {
        "Library registry. Actions: \
         `list` (show registered libraries with index/version status), \
         `register` (add a library directory for cross-project search; \
         pass `path` and optional `name`/`language`)."
    }

    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "enum": ["list", "register"],
                    "description": "Operation to perform."
                },
                "path": {
                    "type": "string",
                    "description": "For action='register': directory path of the library."
                },
                "name": {
                    "type": "string",
                    "description": "For action='register': override the auto-detected library name."
                },
                "language": {
                    "type": "string",
                    "description": "For action='register': override the auto-detected language."
                }
            },
            "required": ["action"]
        })
    }

    async fn call(&self, input: Value, ctx: &ToolContext) -> Result<Value> {
        let action = input
            .get("action")
            .and_then(|v| v.as_str())
            .ok_or_else(|| {
                super::RecoverableError::with_hint(
                    "library requires 'action' parameter",
                    "Pass action='list' or action='register'.",
                )
            })?;
        match action {
            "list" => ListLibraries.call(input, ctx).await,
            "register" => RegisterLibrary.call(input, ctx).await,
            other => Err(super::RecoverableError::with_hint(
                format!("unknown library action: {}", other),
                "Valid actions: 'list', 'register'.",
            )
            .into()),
        }
    }

    fn format_compact(&self, result: &Value) -> Option<String> {
        if result.get("libraries").is_some() {
            ListLibraries.format_compact(result)
        } else {
            RegisterLibrary.format_compact(result)
        }
    }

    fn availability(&self, caps: &crate::tools::ToolCapabilities) -> crate::tools::Availability {
        ListLibraries.availability(caps)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::agent::Agent;
    use crate::lsp::LspManager;
    use std::path::PathBuf;

    async fn project_ctx() -> ToolContext {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path().to_path_buf();
        std::fs::create_dir_all(root.join(".codescout")).unwrap();
        let agent = Agent::new(Some(root)).await.unwrap();
        // Leak the tempdir so it stays alive
        std::mem::forget(dir);
        ToolContext {
            agent,
            lsp: LspManager::new_arc(),
            output_buffer: std::sync::Arc::new(crate::tools::output_buffer::OutputBuffer::new(20)),
            progress: None,
            peer: None,
            section_coverage: std::sync::Arc::new(std::sync::Mutex::new(
                crate::tools::section_coverage::SectionCoverage::new(),
            )),
        }
    }

    fn project_ctx_with_agent(agent: Agent) -> ToolContext {
        ToolContext {
            agent,
            lsp: LspManager::new_arc(),
            output_buffer: std::sync::Arc::new(crate::tools::output_buffer::OutputBuffer::new(20)),
            progress: None,
            peer: None,
            section_coverage: std::sync::Arc::new(std::sync::Mutex::new(
                crate::tools::section_coverage::SectionCoverage::new(),
            )),
        }
    }

    #[tokio::test]
    async fn list_libraries_empty() {
        let ctx = project_ctx().await;
        let result = Library
            .call(json!({ "action": "list" }), &ctx)
            .await
            .unwrap();
        let libs = result["libraries"].as_array().unwrap();
        assert!(libs.is_empty());
    }

    #[tokio::test]
    async fn list_libraries_shows_registered() {
        let ctx = project_ctx().await;
        {
            let mut inner = ctx.agent.inner.write().await;
            let project = inner.active_project_mut().unwrap();
            project.library_registry.register(
                "serde".into(),
                PathBuf::from("/tmp/serde"),
                "rust".into(),
                crate::library::registry::DiscoveryMethod::Manual,
                true,
            );
        }
        let result = Library
            .call(json!({ "action": "list" }), &ctx)
            .await
            .unwrap();
        let libs = result["libraries"].as_array().unwrap();
        assert_eq!(libs.len(), 1);
        assert_eq!(libs[0]["name"], "serde");
        assert_eq!(libs[0]["indexed"], false);
    }

    #[tokio::test]
    async fn list_libraries_errors_without_project() {
        let agent = Agent::new(None).await.unwrap();
        let ctx = ToolContext {
            agent,
            lsp: LspManager::new_arc(),
            output_buffer: std::sync::Arc::new(crate::tools::output_buffer::OutputBuffer::new(20)),
            progress: None,
            peer: None,
            section_coverage: std::sync::Arc::new(std::sync::Mutex::new(
                crate::tools::section_coverage::SectionCoverage::new(),
            )),
        };
        let result = Library.call(json!({ "action": "list" }), &ctx).await;
        assert!(result.is_err());
    }

    // --- format_list_libraries tests ---

    #[test]
    fn format_list_libraries_shows_names_and_status() {
        let result = serde_json::json!({
            "libraries": [
                {"name": "serde", "indexed": true},
                {"name": "tokio", "indexed": false}
            ]
        });
        let out = format_list_libraries(&result);
        assert!(
            out.contains("serde"),
            "should show library name, got: {out}"
        );
        assert!(
            out.contains("tokio"),
            "should show library name, got: {out}"
        );
        assert!(
            out.contains("indexed"),
            "should show index status, got: {out}"
        );
    }

    #[tokio::test]
    async fn index_project_scope_lib_errors_for_unknown() {
        let ctx = project_ctx().await;
        // Register nothing — querying an unknown lib name should return RecoverableError
        let tool = crate::tools::semantic::IndexProject;
        let result = tool.call(json!({ "scope": "lib:nonexistent" }), &ctx).await;
        assert!(result.is_err(), "expected error for unknown library");
        let msg = result.unwrap_err().to_string();
        assert!(
            msg.contains("nonexistent") || msg.contains("not found"),
            "error should mention the library name: {msg}"
        );
    }

    // --- RegisterLibrary tests ---

    #[tokio::test]
    async fn register_library_manual() {
        let dir = tempfile::tempdir().unwrap();
        let lib_dir = tempfile::tempdir().unwrap();
        std::fs::write(
            lib_dir.path().join("Cargo.toml"),
            "[package]\nname = \"mylib\"\nversion = \"0.1.0\"\n",
        )
        .unwrap();

        let agent = Agent::new(Some(dir.path().to_path_buf())).await.unwrap();
        let ctx = project_ctx_with_agent(agent.clone());
        let result = Library
            .call(
                json!({
                    "action": "register",
                    "path": lib_dir.path().display().to_string(),
                }),
                &ctx,
            )
            .await
            .unwrap();

        assert_eq!(result["status"], "ok");
        assert_eq!(result["name"], "mylib");
        assert_eq!(result["language"], "rust");

        let reg = agent.library_registry().await.unwrap();
        assert_eq!(reg.all().len(), 1);
        assert_eq!(reg.all()[0].name, "mylib");
    }

    #[tokio::test]
    async fn register_library_with_explicit_name_and_language() {
        let dir = tempfile::tempdir().unwrap();
        let lib_dir = tempfile::tempdir().unwrap();

        let agent = Agent::new(Some(dir.path().to_path_buf())).await.unwrap();
        let ctx = project_ctx_with_agent(agent.clone());
        let result = Library
            .call(
                json!({
                    "action": "register",
                    "path": lib_dir.path().display().to_string(),
                    "name": "custom-name",
                    "language": "python",
                }),
                &ctx,
            )
            .await
            .unwrap();

        assert_eq!(result["status"], "ok");
        assert_eq!(result["name"], "custom-name");
        assert_eq!(result["language"], "python");
    }

    #[tokio::test]
    async fn register_library_fails_for_nonexistent_path() {
        let dir = tempfile::tempdir().unwrap();
        let agent = Agent::new(Some(dir.path().to_path_buf())).await.unwrap();
        let ctx = project_ctx_with_agent(agent);
        let result = Library
            .call(
                json!({
                    "action": "register",
                    "path": "/nonexistent/path/to/lib",
                }),
                &ctx,
            )
            .await;

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn library_action_unknown_errors() {
        let ctx = project_ctx().await;
        let err = Library
            .call(json!({ "action": "wat" }), &ctx)
            .await
            .unwrap_err();
        assert!(
            err.to_string().contains("unknown library action"),
            "expected unknown action error, got: {err}"
        );
    }

    #[tokio::test]
    async fn library_action_missing_errors() {
        let ctx = project_ctx().await;
        let err = Library.call(json!({}), &ctx).await.unwrap_err();
        assert!(
            err.to_string().contains("library requires 'action'"),
            "expected missing action error, got: {err}"
        );
    }

    #[test]
    fn library_is_write_depends_on_action() {
        assert!(Library.is_write(&json!({ "action": "register" })));
        assert!(!Library.is_write(&json!({ "action": "list" })));
        assert!(!Library.is_write(&json!({})));
    }
}