terraphim_agent 1.16.30

Terraphim AI Agent CLI - Command-line interface with interactive REPL and ASCII graph visualization
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
//! MCP Tool Index for discovering and searching available MCP tools.
//!
//! This module provides an index of MCP (Model Context Protocol) tools from configured
//! servers, enabling fast searchable discovery via terraphim_automata's Aho-Corasick
//! pattern matching.
//!
//! # Examples
//!
//! ```
//! use terraphim_agent::mcp_tool_index::McpToolIndex;
//! use terraphim_types::McpToolEntry;
//! use std::path::PathBuf;
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Create or load an index
//! let index_path = PathBuf::from("/tmp/mcp-tools.json");
//! let mut index = McpToolIndex::new(index_path);
//!
//! // Add a tool
//! let tool = McpToolEntry::new(
//!     "search_files",
//!     "Search for files matching a pattern",
//!     "filesystem"
//! );
//! index.add_tool(tool);
//!
//! // Search for tools
//! let results = index.search("file");
//! # Ok(())
//! # }
//! ```

use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use terraphim_automata::find_matches;
use terraphim_types::{McpToolEntry, NormalizedTerm, NormalizedTermValue, Thesaurus};

/// Index of MCP tools for searchable discovery.
///
/// The index stores tools and provides fast search capabilities using
/// terraphim_automata's Aho-Corasick pattern matching against tool names
/// and descriptions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct McpToolIndex {
    tools: Vec<McpToolEntry>,
    index_path: PathBuf,
}

impl McpToolIndex {
    /// Create a new empty tool index.
    ///
    /// # Arguments
    ///
    /// * `index_path` - Path where the index will be saved/loaded from
    ///
    /// # Examples
    ///
    /// ```
    /// use terraphim_agent::mcp_tool_index::McpToolIndex;
    /// use std::path::PathBuf;
    ///
    /// let index = McpToolIndex::new(PathBuf::from("~/.config/terraphim/mcp-tools.json"));
    /// ```
    pub fn new(index_path: PathBuf) -> Self {
        Self {
            tools: Vec::new(),
            index_path,
        }
    }

    /// Add a tool to the index.
    ///
    /// # Arguments
    ///
    /// * `tool` - The MCP tool entry to add
    ///
    /// # Examples
    ///
    /// ```
    /// use terraphim_agent::mcp_tool_index::McpToolIndex;
    /// use terraphim_types::McpToolEntry;
    /// use std::path::PathBuf;
    ///
    /// let mut index = McpToolIndex::new(PathBuf::from("/tmp/mcp-tools.json"));
    /// let tool = McpToolEntry::new("search_files", "Search for files", "filesystem");
    /// index.add_tool(tool);
    /// ```
    pub fn add_tool(&mut self, tool: McpToolEntry) {
        self.tools.push(tool);
    }

    /// Search for tools matching the query.
    ///
    /// Uses terraphim_automata to build a Thesaurus from tool names and descriptions,
    /// then performs pattern matching against the query.
    ///
    /// # Arguments
    ///
    /// * `query` - The search query string
    ///
    /// # Returns
    ///
    /// A vector of references to matching tool entries.
    ///
    /// # Examples
    ///
    /// ```
    /// use terraphim_agent::mcp_tool_index::McpToolIndex;
    /// use terraphim_types::McpToolEntry;
    /// use std::path::PathBuf;
    ///
    /// let mut index = McpToolIndex::new(PathBuf::from("/tmp/mcp-tools.json"));
    /// index.add_tool(McpToolEntry::new("search_files", "Search for files", "filesystem"));
    /// index.add_tool(McpToolEntry::new("read_file", "Read file contents", "filesystem"));
    ///
    /// let results = index.search("search");
    /// assert_eq!(results.len(), 1);
    /// ```
    pub fn search(&self, query: &str) -> Vec<&McpToolEntry> {
        if self.tools.is_empty() || query.trim().is_empty() {
            return Vec::new();
        }

        // Split query into keywords and build a thesaurus from them
        // Each keyword becomes a pattern that we search for in tool descriptions
        let mut thesaurus = Thesaurus::new("query_terms".to_string());
        let keywords: Vec<&str> = query.split_whitespace().collect();

        for (idx, keyword) in keywords.iter().enumerate() {
            if keyword.len() >= 2 {
                let key = NormalizedTermValue::from(*keyword);
                let term = NormalizedTerm::new(idx as u64, key.clone());
                thesaurus.insert(key, term);
            }
        }

        if thesaurus.is_empty() {
            return Vec::new();
        }

        // Search each tool's text for query matches
        let mut results: Vec<&McpToolEntry> = Vec::new();
        let mut seen_ids = std::collections::HashSet::new();

        for (tool_idx, tool) in self.tools.iter().enumerate() {
            let search_text = tool.search_text();

            // Use terraphim_automata to find query keywords in the tool's search text
            match find_matches(&search_text, thesaurus.clone(), false) {
                Ok(matches) => {
                    if !matches.is_empty() && seen_ids.insert(tool_idx) {
                        results.push(&self.tools[tool_idx]);
                    }
                }
                Err(_) => continue,
            }
        }

        results
    }

    /// Save the index to disk.
    ///
    /// # Returns
    ///
    /// `Ok(())` on success, or an IO error on failure.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use terraphim_agent::mcp_tool_index::McpToolIndex;
    /// use terraphim_types::McpToolEntry;
    /// use std::path::PathBuf;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut index = McpToolIndex::new(PathBuf::from("/tmp/mcp-tools.json"));
    /// index.add_tool(McpToolEntry::new("search_files", "Search for files", "filesystem"));
    /// index.save()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn save(&self) -> Result<(), std::io::Error> {
        if let Some(parent) = self.index_path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let json = serde_json::to_string_pretty(self)?;
        std::fs::write(&self.index_path, json)?;
        Ok(())
    }

    /// Load an index from disk.
    ///
    /// # Arguments
    ///
    /// * `index_path` - Path to the saved index file
    ///
    /// # Returns
    ///
    /// The loaded `McpToolIndex` on success, or an IO error on failure.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use terraphim_agent::mcp_tool_index::McpToolIndex;
    /// use std::path::PathBuf;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let index = McpToolIndex::load(PathBuf::from("/tmp/mcp-tools.json"))?;
    /// println!("Loaded {} tools", index.tool_count());
    /// # Ok(())
    /// # }
    /// ```
    pub fn load(index_path: PathBuf) -> Result<Self, std::io::Error> {
        let json = std::fs::read_to_string(&index_path)?;
        let index: Self = serde_json::from_str(&json)?;
        Ok(index)
    }

    /// Get the count of tools in the index.
    ///
    /// # Examples
    ///
    /// ```
    /// use terraphim_agent::mcp_tool_index::McpToolIndex;
    /// use terraphim_types::McpToolEntry;
    /// use std::path::PathBuf;
    ///
    /// let mut index = McpToolIndex::new(PathBuf::from("/tmp/mcp-tools.json"));
    /// assert_eq!(index.tool_count(), 0);
    ///
    /// index.add_tool(McpToolEntry::new("search_files", "Search for files", "filesystem"));
    /// assert_eq!(index.tool_count(), 1);
    /// ```
    pub fn tool_count(&self) -> usize {
        self.tools.len()
    }

    /// Get all tools in the index.
    pub fn tools(&self) -> &[McpToolEntry] {
        &self.tools
    }

    /// Get the index path.
    pub fn index_path(&self) -> &Path {
        &self.index_path
    }
}

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

    fn create_test_tool(name: &str, description: &str, server: &str) -> McpToolEntry {
        McpToolEntry::new(name, description, server)
    }

    #[test]
    fn test_tool_index_add_and_search() {
        let mut index = McpToolIndex::new(PathBuf::from("/tmp/test-mcp-tools.json"));

        let tool1 = create_test_tool(
            "search_files",
            "Search for files matching a pattern",
            "filesystem",
        );
        let tool2 = create_test_tool("read_file", "Read file contents", "filesystem");
        let tool3 = create_test_tool("grep_search", "Search text using grep", "search");

        index.add_tool(tool1);
        index.add_tool(tool2);
        index.add_tool(tool3);

        // Search for "file" should match tool1 and tool2
        let results = index.search("file");
        assert!(!results.is_empty());
        assert!(results.iter().any(|t| t.name == "search_files"));
        assert!(results.iter().any(|t| t.name == "read_file"));
    }

    #[test]
    fn test_tool_index_save_and_load() {
        let temp_dir = std::env::temp_dir();
        let index_path = temp_dir.join("test-mcp-index.json");

        // Create and save
        {
            let mut index = McpToolIndex::new(index_path.clone());
            let tool = create_test_tool("search_files", "Search for files", "filesystem")
                .with_tags(vec!["search".to_string(), "filesystem".to_string()]);
            index.add_tool(tool);
            index.save().expect("Failed to save index");
        }

        // Load and verify
        {
            let index = McpToolIndex::load(index_path.clone()).expect("Failed to load index");
            assert_eq!(index.tool_count(), 1);
            assert_eq!(index.tools[0].name, "search_files");
            assert_eq!(index.tools[0].tags, vec!["search", "filesystem"]);
        }

        // Cleanup
        let _ = std::fs::remove_file(&index_path);
    }

    #[test]
    fn test_tool_index_empty_search() {
        let index = McpToolIndex::new(PathBuf::from("/tmp/test-empty.json"));

        // Empty index should return empty results
        let results = index.search("anything");
        assert!(results.is_empty());
    }

    #[test]
    fn test_tool_index_count() {
        let mut index = McpToolIndex::new(PathBuf::from("/tmp/test-count.json"));
        assert_eq!(index.tool_count(), 0);

        index.add_tool(create_test_tool("tool1", "First tool", "server1"));
        assert_eq!(index.tool_count(), 1);

        index.add_tool(create_test_tool("tool2", "Second tool", "server1"));
        assert_eq!(index.tool_count(), 2);
    }

    #[test]
    fn test_search_partial_match() {
        let mut index = McpToolIndex::new(PathBuf::from("/tmp/test-partial.json"));

        index.add_tool(create_test_tool(
            "search_files",
            "Search for files",
            "filesystem",
        ));
        index.add_tool(create_test_tool(
            "search_code",
            "Search code repositories",
            "code",
        ));
        index.add_tool(create_test_tool(
            "read_file",
            "Read file contents",
            "filesystem",
        ));

        // Search for partial match
        let results = index.search("search");
        assert!(results.iter().any(|t| t.name == "search_files"));
        assert!(results.iter().any(|t| t.name == "search_code"));
        assert!(!results.iter().any(|t| t.name == "read_file"));
    }

    #[test]
    fn test_search_description_match() {
        let mut index = McpToolIndex::new(PathBuf::from("/tmp/test-desc.json"));

        index.add_tool(create_test_tool(
            "tool_a",
            "This tool reads data from files",
            "server",
        ));
        index.add_tool(create_test_tool(
            "tool_b",
            "This tool writes data to database",
            "server",
        ));

        // Search should match description
        let results = index.search("reads");
        assert!(results.iter().any(|t| t.name == "tool_a"));
        assert!(!results.iter().any(|t| t.name == "tool_b"));
    }

    #[test]
    fn test_discovery_latency_benchmark() {
        let mut index = McpToolIndex::new(PathBuf::from("/tmp/test-benchmark.json"));

        // Add 100 tools
        for i in 0..100 {
            let tool = create_test_tool(
                &format!("tool_{}", i),
                &format!("Tool number {} does something useful", i),
                &format!("server_{}", i % 10),
            );
            index.add_tool(tool);
        }

        // Measure search latency for partial name match
        let start = Instant::now();
        let results = index.search("tool_50");
        let elapsed = start.elapsed();

        assert!(!results.is_empty(), "Should find at least one tool");
        assert!(
            elapsed.as_millis() < 70,
            "Search should complete in under 70ms, took {:?}",
            elapsed
        );
    }

    #[test]
    fn test_search_with_tags() {
        let mut index = McpToolIndex::new(PathBuf::from("/tmp/test-tags.json"));

        let tool1 = create_test_tool("search_files", "Search for files", "filesystem")
            .with_tags(vec!["search".to_string(), "files".to_string()]);
        let tool2 = create_test_tool("grep_search", "Search with grep", "search")
            .with_tags(vec!["search".to_string(), "text".to_string()]);

        index.add_tool(tool1);
        index.add_tool(tool2);

        // Search by tag
        let results = index.search("text");
        assert!(results.iter().any(|t| t.name == "grep_search"));
    }

    #[test]
    fn test_empty_query_returns_empty() {
        let mut index = McpToolIndex::new(PathBuf::from("/tmp/test-empty-query.json"));
        index.add_tool(create_test_tool("tool1", "Description", "server"));

        let results = index.search("");
        assert!(results.is_empty());
    }

    #[test]
    fn test_new_creates_empty_index() {
        let index = McpToolIndex::new(PathBuf::from("/tmp/test-new.json"));
        assert_eq!(index.tool_count(), 0);
        assert!(index.tools().is_empty());
    }
}