coding_agent_tools 0.4.0

Coding agent tools (CLI + MCP). First tool: ls.
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
//! Mapping utilities for agent configuration.

use std::collections::HashMap;

use agentic_config::types::SubagentsConfig;
use claudecode::config::MCPConfig;
use claudecode::config::MCPServer;
use claudecode::types::Model;

use super::prompts::compose_prompt_impl;
use crate::types::AgentLocation;
use crate::types::AgentType;

/// Select model for agent type based on config.
///
/// Maps config strings to claudecode Model enum variants.
/// TODO(2): claudecode SDK could be enhanced with Custom(String) variant
/// for more flexibility, but enum mapping works for now.
pub fn model_for(agent_type: AgentType, cfg: &SubagentsConfig) -> Model {
    let raw = match agent_type {
        AgentType::Locator => cfg.locator_model.as_str(),
        AgentType::Analyzer => cfg.analyzer_model.as_str(),
    };

    // Map known model strings to enum variants
    match raw.trim().to_lowercase().as_str() {
        "haiku" | "claude-haiku-4-5" => Model::Haiku,
        "sonnet" | "claude-sonnet-4-6" => Model::Sonnet,
        "opus" | "claude-opus-4-6" => Model::Opus,
        _ => {
            // Fallback based on agent type
            match agent_type {
                AgentType::Locator => Model::Haiku,
                AgentType::Analyzer => Model::Sonnet,
            }
        }
    }
}

// TODO(2): Intentional explicit match for clarity and compile-time exhaustiveness.
// We keep the hardcoded mapping to avoid accidental tool exposure and ensure deterministic tests.
/// Get the enabled tools for a given type × location combination.
/// This list includes both built-in tools and MCP tools (prefixed with "mcp__").
pub fn enabled_tools_for(agent_type: AgentType, location: AgentLocation) -> Vec<String> {
    use AgentLocation::Codebase;
    use AgentLocation::References;
    use AgentLocation::Thoughts;
    use AgentLocation::Web;
    use AgentType::Analyzer;
    use AgentType::Locator;

    match (agent_type, location) {
        (Locator, Codebase) => vec![
            "mcp__agentic-mcp__cli_ls".into(),
            "mcp__agentic-mcp__cli_grep".into(),
            "mcp__agentic-mcp__cli_glob".into(),
        ],
        (Locator, Thoughts) => vec![
            "mcp__agentic-mcp__cli_ls".into(),
            "mcp__agentic-mcp__thoughts_list_documents".into(),
            "mcp__agentic-mcp__cli_grep".into(),
            "mcp__agentic-mcp__cli_glob".into(),
        ],
        (Locator, References) => vec![
            "mcp__agentic-mcp__cli_ls".into(),
            "mcp__agentic-mcp__thoughts_list_references".into(),
            "mcp__agentic-mcp__cli_grep".into(),
            "mcp__agentic-mcp__cli_glob".into(),
        ],
        (Locator, Web) => vec![
            "mcp__agentic-mcp__web_search".into(),
            "mcp__agentic-mcp__web_fetch".into(),
        ],
        (Analyzer, Codebase) => vec![
            "Read".into(),
            "mcp__agentic-mcp__cli_ls".into(),
            "mcp__agentic-mcp__cli_grep".into(),
            "mcp__agentic-mcp__cli_glob".into(),
            "TodoWrite".into(),
        ],
        (Analyzer, Thoughts) => vec![
            "Read".into(),
            "mcp__agentic-mcp__cli_ls".into(),
            "mcp__agentic-mcp__thoughts_list_documents".into(),
            "mcp__agentic-mcp__cli_grep".into(),
            "mcp__agentic-mcp__cli_glob".into(),
        ],
        (Analyzer, References) => vec![
            "Read".into(),
            "mcp__agentic-mcp__cli_ls".into(),
            "mcp__agentic-mcp__thoughts_list_references".into(),
            "mcp__agentic-mcp__cli_grep".into(),
            "mcp__agentic-mcp__cli_glob".into(),
            "TodoWrite".into(),
        ],
        (Analyzer, Web) => vec![
            "mcp__agentic-mcp__web_search".into(),
            "mcp__agentic-mcp__web_fetch".into(),
            "TodoWrite".into(),
            "Read".into(),
            "mcp__agentic-mcp__cli_grep".into(),
            "mcp__agentic-mcp__cli_glob".into(),
            "mcp__agentic-mcp__cli_ls".into(),
        ],
    }
}

/// Compose the system prompt for a given type × location combination.
pub fn compose_prompt(agent_type: AgentType, location: AgentLocation) -> String {
    compose_prompt_impl(agent_type, location)
}

/// Extract base tool names for our agentic-mcp server from enabled tool IDs.
/// Example: "mcp__agentic-mcp__cli_ls" -> "`cli_ls`".
/// Uses `BTreeSet` for deterministic ordering.
fn agentic_mcp_allowlist_from(enabled: &[String]) -> Vec<String> {
    use std::collections::BTreeSet;
    const PREFIX: &str = "mcp__agentic-mcp__";

    let mut set = BTreeSet::new();
    for e in enabled {
        if let Some(rest) = e.strip_prefix(PREFIX) {
            let name = rest.trim();
            if !name.is_empty() {
                set.insert(name.to_string());
            }
        }
    }
    set.into_iter().collect()
}

// NOTE: Binary existence checks (bin_in_path, require_binaries_for_location) have been removed.
// MCP server validation now happens via claudecode::mcp::validate in ask_agent, which provides
// better error messages with stderr capture and actual handshake verification.

/// Build MCP server configuration for a given location, with tool allowlist.
/// Uses single agentic-mcp server with --allow flag for all locations.
/// Returns empty config if no MCP tools are enabled.
pub fn build_mcp_config(_location: AgentLocation, enabled_tools: &[String]) -> MCPConfig {
    let mut servers: HashMap<String, MCPServer> = HashMap::new();

    // Build allowlist from enabled MCP tools
    let allowlist = agentic_mcp_allowlist_from(enabled_tools);

    // If no MCP tools are enabled, do not expose the server at all
    if allowlist.is_empty() {
        return MCPConfig {
            mcp_servers: servers,
        };
    }

    // Use --allow "tool1,tool2" (no "mcp" subcommand, no individual flags)
    let args = vec![
        "--allow".to_string(),
        allowlist.join(","),
        "--suppress-search-reminder".to_string(),
    ];

    servers.insert(
        "agentic-mcp".to_string(),
        MCPServer::stdio("agentic-mcp", args),
    );

    MCPConfig {
        mcp_servers: servers,
    }
}

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

    #[test]
    fn test_model_for_locator_default() {
        let cfg = SubagentsConfig::default();
        assert_eq!(model_for(AgentType::Locator, &cfg), Model::Haiku);
    }

    #[test]
    fn test_model_for_analyzer_default() {
        let cfg = SubagentsConfig::default();
        assert_eq!(model_for(AgentType::Analyzer, &cfg), Model::Sonnet);
    }

    #[test]
    fn test_model_for_with_custom_config() {
        let cfg = SubagentsConfig {
            locator_model: "sonnet".into(),
            analyzer_model: "opus".into(),
        };
        assert_eq!(model_for(AgentType::Locator, &cfg), Model::Sonnet);
        assert_eq!(model_for(AgentType::Analyzer, &cfg), Model::Opus);
    }

    #[test]
    fn test_model_for_fallback_on_unknown() {
        let cfg = SubagentsConfig {
            locator_model: "unknown-model".into(),
            analyzer_model: "another-unknown".into(),
        };
        // Falls back based on agent type
        assert_eq!(model_for(AgentType::Locator, &cfg), Model::Haiku);
        assert_eq!(model_for(AgentType::Analyzer, &cfg), Model::Sonnet);
    }

    #[test]
    fn locator_default_model_string_is_explicitly_recognized() {
        let mut cfg = SubagentsConfig::default();

        // Use the default locator model string for the Analyzer slot.
        // If this string stops being explicitly recognized by `model_for()`,
        // the Analyzer fallback would return Sonnet (wrong for this assertion).
        let locator_default = cfg.locator_model.clone();
        cfg.analyzer_model = locator_default;

        assert_eq!(model_for(AgentType::Analyzer, &cfg), Model::Haiku);
    }

    #[test]
    fn analyzer_default_model_string_is_explicitly_recognized() {
        let mut cfg = SubagentsConfig::default();

        // Use the default analyzer model string for the Locator slot.
        // If this string stops being explicitly recognized by `model_for()`,
        // the Locator fallback would return Haiku (wrong for this assertion).
        let analyzer_default = cfg.analyzer_model.clone();
        cfg.locator_model = analyzer_default;

        assert_eq!(model_for(AgentType::Locator, &cfg), Model::Sonnet);
    }

    #[test]
    fn test_enabled_tools_locator_codebase() {
        let tools = enabled_tools_for(AgentType::Locator, AgentLocation::Codebase);
        assert!(tools.contains(&"mcp__agentic-mcp__cli_ls".to_string()));
        assert!(tools.contains(&"mcp__agentic-mcp__cli_grep".to_string()));
        assert!(tools.contains(&"mcp__agentic-mcp__cli_glob".to_string()));
        assert!(!tools.contains(&"Read".to_string())); // Locator doesn't read deeply
    }

    #[test]
    fn test_enabled_tools_analyzer_codebase() {
        let tools = enabled_tools_for(AgentType::Analyzer, AgentLocation::Codebase);
        assert!(tools.contains(&"Read".to_string())); // Analyzer can read
        assert!(tools.contains(&"mcp__agentic-mcp__cli_ls".to_string()));
        assert!(tools.contains(&"mcp__agentic-mcp__cli_grep".to_string()));
        assert!(tools.contains(&"mcp__agentic-mcp__cli_glob".to_string()));
    }

    #[test]
    fn test_enabled_tools_locator_thoughts() {
        let tools = enabled_tools_for(AgentType::Locator, AgentLocation::Thoughts);
        assert!(tools.contains(&"mcp__agentic-mcp__thoughts_list_documents".to_string()));
        assert!(tools.contains(&"mcp__agentic-mcp__cli_ls".to_string()));
    }

    #[test]
    fn test_enabled_tools_locator_references() {
        let tools = enabled_tools_for(AgentType::Locator, AgentLocation::References);
        assert!(tools.contains(&"mcp__agentic-mcp__thoughts_list_references".to_string()));
    }

    #[test]
    fn test_enabled_tools_analyzer_thoughts_has_ls() {
        let tools = enabled_tools_for(AgentType::Analyzer, AgentLocation::Thoughts);
        assert!(tools.contains(&"mcp__agentic-mcp__cli_ls".to_string()));
        assert!(tools.contains(&"mcp__agentic-mcp__thoughts_list_documents".to_string()));
        assert!(tools.contains(&"Read".to_string()));
        assert!(tools.contains(&"mcp__agentic-mcp__cli_grep".to_string()));
        assert!(tools.contains(&"mcp__agentic-mcp__cli_glob".to_string()));
    }

    #[test]
    fn test_enabled_tools_analyzer_references_has_ls() {
        let tools = enabled_tools_for(AgentType::Analyzer, AgentLocation::References);
        assert!(tools.contains(&"mcp__agentic-mcp__cli_ls".to_string()));
        assert!(tools.contains(&"mcp__agentic-mcp__thoughts_list_references".to_string()));
        assert!(tools.contains(&"Read".to_string()));
        assert!(tools.contains(&"mcp__agentic-mcp__cli_grep".to_string()));
        assert!(tools.contains(&"mcp__agentic-mcp__cli_glob".to_string()));
        assert!(tools.contains(&"TodoWrite".to_string()));
    }

    #[test]
    fn test_enabled_tools_locator_web() {
        let tools = enabled_tools_for(AgentType::Locator, AgentLocation::Web);
        assert_eq!(
            tools,
            vec![
                "mcp__agentic-mcp__web_search".to_string(),
                "mcp__agentic-mcp__web_fetch".to_string()
            ]
        );
    }

    #[test]
    fn test_enabled_tools_analyzer_web() {
        let tools = enabled_tools_for(AgentType::Analyzer, AgentLocation::Web);
        assert!(tools.contains(&"mcp__agentic-mcp__web_search".to_string()));
        assert!(tools.contains(&"mcp__agentic-mcp__web_fetch".to_string()));
    }

    #[test]
    fn test_enabled_tools_analyzer_web_full_set() {
        let tools = enabled_tools_for(AgentType::Analyzer, AgentLocation::Web);
        let expected = [
            "mcp__agentic-mcp__web_search",
            "mcp__agentic-mcp__web_fetch",
            "TodoWrite",
            "Read",
            "mcp__agentic-mcp__cli_grep",
            "mcp__agentic-mcp__cli_glob",
            "mcp__agentic-mcp__cli_ls",
        ];
        for t in expected {
            assert!(tools.contains(&t.to_string()), "missing tool: {t}");
        }
        assert_eq!(tools.len(), 7);
    }

    #[test]
    fn test_compose_prompt_locator_codebase() {
        let prompt = compose_prompt(AgentType::Locator, AgentLocation::Codebase);
        assert!(prompt.contains("finding WHERE"));
        assert!(prompt.contains("Local codebase"));
    }

    #[test]
    fn test_compose_prompt_analyzer_web() {
        let prompt = compose_prompt(AgentType::Analyzer, AgentLocation::Web);
        assert!(prompt.contains("understanding HOW"));
        assert!(prompt.contains("web_fetch"));
    }

    #[test]
    fn test_build_mcp_config_codebase() {
        let enabled = enabled_tools_for(AgentType::Locator, AgentLocation::Codebase);
        let config = build_mcp_config(AgentLocation::Codebase, &enabled);
        assert!(config.mcp_servers.contains_key("agentic-mcp"));
        assert_eq!(config.mcp_servers.len(), 1); // Single server for all locations
    }

    #[test]
    fn test_build_mcp_config_thoughts() {
        let enabled = enabled_tools_for(AgentType::Locator, AgentLocation::Thoughts);
        let config = build_mcp_config(AgentLocation::Thoughts, &enabled);
        assert!(config.mcp_servers.contains_key("agentic-mcp"));
        assert_eq!(config.mcp_servers.len(), 1); // Single server for all locations
    }

    #[test]
    fn test_build_mcp_config_references() {
        let enabled = enabled_tools_for(AgentType::Locator, AgentLocation::References);
        let config = build_mcp_config(AgentLocation::References, &enabled);
        assert!(config.mcp_servers.contains_key("agentic-mcp"));
        assert_eq!(config.mcp_servers.len(), 1); // Single server for all locations
    }

    #[test]
    fn test_build_mcp_config_web() {
        let enabled = enabled_tools_for(AgentType::Analyzer, AgentLocation::Web);
        let config = build_mcp_config(AgentLocation::Web, &enabled);
        assert!(config.mcp_servers.contains_key("agentic-mcp"));
        assert_eq!(config.mcp_servers.len(), 1); // Single server for all locations
    }

    // Test all 8 type×location combinations have valid tools
    #[test]
    fn test_all_combinations_have_tools() {
        for agent_type in [AgentType::Locator, AgentType::Analyzer] {
            for location in [
                AgentLocation::Codebase,
                AgentLocation::Thoughts,
                AgentLocation::References,
                AgentLocation::Web,
            ] {
                let tools = enabled_tools_for(agent_type, location);
                assert!(
                    !tools.is_empty(),
                    "No tools for {agent_type:?} + {location:?}"
                );
            }
        }
    }

    // Test all 8 type×location combinations have valid prompts
    #[test]
    fn test_all_combinations_have_prompts() {
        for agent_type in [AgentType::Locator, AgentType::Analyzer] {
            for location in [
                AgentLocation::Codebase,
                AgentLocation::Thoughts,
                AgentLocation::References,
                AgentLocation::Web,
            ] {
                let prompt = compose_prompt(agent_type, location);
                assert!(
                    !prompt.is_empty(),
                    "Empty prompt for {agent_type:?} + {location:?}"
                );
                assert!(
                    prompt.len() > 100,
                    "Prompt too short for {agent_type:?} + {location:?}"
                );
            }
        }
    }

    #[test]
    fn test_agentic_mcp_allowlist_locator_codebase() {
        let enabled = enabled_tools_for(AgentType::Locator, AgentLocation::Codebase);
        let list = agentic_mcp_allowlist_from(&enabled);
        assert_eq!(list, vec!["cli_glob", "cli_grep", "cli_ls"]);
    }

    #[test]
    fn test_agentic_mcp_allowlist_locator_web_and_server_present() {
        let enabled = enabled_tools_for(AgentType::Locator, AgentLocation::Web);
        let list = agentic_mcp_allowlist_from(&enabled);
        assert_eq!(list, vec!["web_fetch", "web_search"]);

        let cfg = build_mcp_config(AgentLocation::Web, &enabled);
        assert!(cfg.mcp_servers.contains_key("agentic-mcp"));
        assert_eq!(cfg.mcp_servers.len(), 1);
    }

    #[test]
    fn test_agentic_mcp_allowlist_locator_thoughts() {
        let enabled = enabled_tools_for(AgentType::Locator, AgentLocation::Thoughts);
        let list = agentic_mcp_allowlist_from(&enabled);
        assert_eq!(
            list,
            vec!["cli_glob", "cli_grep", "cli_ls", "thoughts_list_documents"]
        );
    }

    #[test]
    fn test_agentic_mcp_allowlist_locator_references() {
        let enabled = enabled_tools_for(AgentType::Locator, AgentLocation::References);
        let list = agentic_mcp_allowlist_from(&enabled);
        assert_eq!(
            list,
            vec!["cli_glob", "cli_grep", "cli_ls", "thoughts_list_references"]
        );
    }

    #[test]
    fn test_agentic_mcp_allowlist_analyzer_codebase() {
        let enabled = enabled_tools_for(AgentType::Analyzer, AgentLocation::Codebase);
        let list = agentic_mcp_allowlist_from(&enabled);
        assert_eq!(list, vec!["cli_glob", "cli_grep", "cli_ls"]);
    }

    #[test]
    fn test_agentic_mcp_allowlist_analyzer_thoughts() {
        let enabled = enabled_tools_for(AgentType::Analyzer, AgentLocation::Thoughts);
        let list = agentic_mcp_allowlist_from(&enabled);
        assert_eq!(
            list,
            vec!["cli_glob", "cli_grep", "cli_ls", "thoughts_list_documents"]
        );
    }

    #[test]
    fn test_agentic_mcp_allowlist_analyzer_references() {
        let enabled = enabled_tools_for(AgentType::Analyzer, AgentLocation::References);
        let list = agentic_mcp_allowlist_from(&enabled);
        assert_eq!(
            list,
            vec!["cli_glob", "cli_grep", "cli_ls", "thoughts_list_references"]
        );
    }

    #[test]
    fn test_agentic_mcp_allowlist_analyzer_web() {
        let enabled = enabled_tools_for(AgentType::Analyzer, AgentLocation::Web);
        let list = agentic_mcp_allowlist_from(&enabled);
        assert_eq!(
            list,
            vec!["cli_glob", "cli_grep", "cli_ls", "web_fetch", "web_search"]
        );
    }

    #[test]
    fn test_build_mcp_config_includes_suppress_search_reminder_flag() {
        let enabled = enabled_tools_for(AgentType::Locator, AgentLocation::Codebase);
        let config = build_mcp_config(AgentLocation::Codebase, &enabled);
        let Some(server) = config.mcp_servers.get("agentic-mcp") else {
            panic!("expected agentic-mcp server to be configured");
        };

        match server {
            MCPServer::Stdio { command, args, .. } => {
                assert_eq!(command, "agentic-mcp");
                assert!(args.contains(&"--suppress-search-reminder".to_string()));
            }
            MCPServer::Http { .. } => panic!("expected stdio MCP server"),
        }
    }
}