|
| 1 | +# terraphim_mcp_search |
| 2 | + |
| 3 | +MCP (Model Context Protocol) tool and Terraphim skill indexing & discovery, |
| 4 | +using `terraphim_automata`'s Aho-Corasick pattern matching for sub-100-entry |
| 5 | +searches under 70ms. |
| 6 | + |
| 7 | +## What this crate gives you |
| 8 | + |
| 9 | +| Item | When to use | |
| 10 | +|------|-------------| |
| 11 | +| [`McpToolIndex`] | You want a persistent, mutable, large-corpus tool index with save/load. | |
| 12 | +| [`mcp_search_tools`] | You have a slice of tools and want a one-shot search without managing state. | |
| 13 | +| [`SkillEntry`] + [`mcp_search_skills`] | You want to search Terraphim skills (TinyClaw JSON, etc.) with the same engine. | |
| 14 | + |
| 15 | +## Why this crate exists |
| 16 | + |
| 17 | +Extracted from `terraphim_agent::mcp_tool_index` (sibling `terraphim-agents` |
| 18 | +polyrepo, version 1.20.x) on **2026-06-28**, as part of the Mega-MCP / Tool |
| 19 | +Search adaptation plan for terraphim-ai. `mcp_search_tools` and |
| 20 | +`mcp_search_skills` were added the same day as the first consumers of the |
| 21 | +extracted index. |
| 22 | + |
| 23 | +The extraction of `McpToolIndex` is **source-verbatim** to preserve the |
| 24 | +existing API contract and all 9 unit tests + 7 doctests unchanged. |
| 25 | + |
| 26 | +The new search helpers are stateless conveniences over `McpToolIndex` — |
| 27 | +they build an ephemeral index per call. For repeated searches over a stable |
| 28 | +corpus, build a `McpToolIndex` directly. |
| 29 | + |
| 30 | +## Status |
| 31 | + |
| 32 | +- **Here in `terraphim-ai`:** functional, tested, clippy-clean, ready for new |
| 33 | + consumers in this workspace. |
| 34 | +- **In `terraphim_agent` (sibling repo):** the original `mcp_tool_index` module |
| 35 | + is **still present and unchanged** — see Gitea issue |
| 36 | + [`terraphim-agents#64`](https://git.terraphim.cloud/terraphim/terraphim-agents/issues/64) |
| 37 | + for the dependency-swap migration plan. |
| 38 | + |
| 39 | +## Usage |
| 40 | + |
| 41 | +```rust |
| 42 | +use terraphim_mcp_search::{ |
| 43 | + McpToolIndex, mcp_search_tools, mcp_search_skills, SkillEntry, |
| 44 | +}; |
| 45 | +use terraphim_types::McpToolEntry; |
| 46 | +use std::path::PathBuf; |
| 47 | + |
| 48 | +// State-managed: persistent, mutable index |
| 49 | +let mut index = McpToolIndex::new(PathBuf::from("/tmp/mcp-tools.json")); |
| 50 | +index.add_tool(McpToolEntry::new("search_files", "Search files", "filesystem")); |
| 51 | +index.add_tool(McpToolEntry::new("read_file", "Read file contents", "filesystem")); |
| 52 | +let hits = index.search("search"); |
| 53 | +// hits[0].name == "search_files" |
| 54 | + |
| 55 | +// Stateless: one-shot slice search |
| 56 | +let tools = index.tools().to_vec(); |
| 57 | +let hits = mcp_search_tools("search", &tools); |
| 58 | +// hits is Vec<McpToolEntry> (owned) |
| 59 | + |
| 60 | +// Skill search via the same engine |
| 61 | +let skills = vec![ |
| 62 | + SkillEntry::new("code-review", "Automated code review") |
| 63 | + .with_tags(vec!["review".into()]), |
| 64 | + SkillEntry::new("deploy", "Deploy to staging"), |
| 65 | +]; |
| 66 | +let skill_hits = mcp_search_skills("review", &skills); |
| 67 | +// skill_hits[0].name == "code-review" |
| 68 | +``` |
| 69 | + |
| 70 | +## Performance NFR |
| 71 | + |
| 72 | +- **Search latency**: < 70 ms for 100 tools (`McpToolIndex::test_discovery_latency_benchmark`, |
| 73 | + release-mode only). |
| 74 | +- **Stateless helpers**: ephemeral index in `/tmp/terraphim-mcp-search-ephemeral.json`, |
| 75 | + never persisted. |
| 76 | +- **Memory**: proportional to corpus size. |
| 77 | + |
| 78 | +## Migration plan (the "C" of "A/B/C") |
| 79 | + |
| 80 | +| Step | Where | Who | Status | |
| 81 | +|------|-------|-----|--------| |
| 82 | +| 1. Create standalone crate | `terraphim-ai` workspace | done 2026-06-28 | ✅ | |
| 83 | +| 2. Add stateless search helpers | `terraphim-ai` workspace | done 2026-06-28 | ✅ | |
| 84 | +| 3. File Gitea issue in `terraphim-agents` | `terraphim-agents` repo | done 2026-06-28 | ✅ | |
| 85 | +| 4. Update `terraphim_agent` Cargo.toml to depend on `terraphim_mcp_search` | `terraphim-agents` polyrepo | TBD | ⏳ | |
| 86 | +| 5. Re-export `terraphim_agent::mcp_tool_index` as a deprecation shim pointing at this crate | `terraphim-agents` polyrepo | TBD | ⏳ | |
| 87 | +| 6. Eventually remove the legacy module | `terraphim-agents` polyrepo | TBD | ⏳ | |
| 88 | + |
| 89 | +Steps 4-6 belong in the `terraphim-agents` polyrepo — **do not** execute them |
| 90 | +from this workspace. They're out of scope per the surgeon pre-cut check. |
| 91 | + |
| 92 | +## Dependencies |
| 93 | + |
| 94 | +- `terraphim_automata = "1.20.2"` (from terraphim registry) |
| 95 | +- `terraphim_types = "1.20.2"` (from terraphim registry, for `McpToolEntry`) |
| 96 | +- `serde`, `serde_json`, `tempfile` (dev) |
0 commit comments