Skip to main content

agentic_evolve_mcp/tools/
evolve_pattern_search.rs

1//! Tool: evolve_pattern_search — Search patterns by query.
2
3use std::sync::Arc;
4use tokio::sync::Mutex;
5
6use serde::Deserialize;
7use serde_json::{json, Value};
8
9use crate::session::SessionManager;
10use crate::types::{McpError, McpResult, ToolCallResult, ToolDefinition};
11
12#[derive(Debug, Deserialize)]
13struct SearchParams {
14    query: String,
15    #[serde(default = "default_limit")]
16    limit: usize,
17}
18
19fn default_limit() -> usize {
20    20
21}
22
23/// Return the tool definition for evolve_pattern_search.
24pub fn definition() -> ToolDefinition {
25    ToolDefinition {
26        name: "evolve_pattern_search".to_string(),
27        description: Some("Search patterns by query string".to_string()),
28        input_schema: json!({
29            "type": "object",
30            "properties": {
31                "query": {
32                    "type": "string",
33                    "description": "Search query (matches name, domain, template, tags)"
34                },
35                "limit": {
36                    "type": "integer",
37                    "default": 20,
38                    "description": "Maximum number of results to return"
39                },
40                "include_content": {
41                    "type": "boolean",
42                    "default": false,
43                    "description": "Include full template content in response"
44                },
45                "intent": {
46                    "type": "string",
47                    "enum": ["exists", "ids", "summary", "full"],
48                    "description": "Response detail level"
49                },
50                "since": {
51                    "type": "integer",
52                    "description": "Only return data changed after this Unix timestamp"
53                },
54                "token_budget": {
55                    "type": "integer",
56                    "description": "Maximum token budget for the response"
57                },
58                "max_results": {
59                    "type": "integer",
60                    "default": 10,
61                    "description": "Maximum number of results to return"
62                },
63                "cursor": {
64                    "type": "string",
65                    "description": "Pagination cursor from a previous response"
66                }
67            },
68            "required": ["query"]
69        }),
70    }
71}
72
73/// Execute the evolve_pattern_search tool.
74pub async fn execute(
75    args: Value,
76    session: &Arc<Mutex<SessionManager>>,
77) -> McpResult<ToolCallResult> {
78    let params: SearchParams =
79        serde_json::from_value(args).map_err(|e| McpError::InvalidParams(e.to_string()))?;
80
81    let session = session.lock().await;
82    let results = session.search_patterns(&params.query);
83
84    let limited: Vec<_> = results.into_iter().take(params.limit).collect();
85    let patterns: Vec<Value> = limited
86        .iter()
87        .map(|p| {
88            json!({
89                "pattern_id": p.id.as_str(),
90                "name": p.name,
91                "domain": p.domain,
92                "language": p.language.as_str(),
93                "confidence": p.confidence,
94                "usage_count": p.usage_count,
95                "tags": p.tags
96            })
97        })
98        .collect();
99
100    Ok(ToolCallResult::json(&json!({
101        "count": patterns.len(),
102        "patterns": patterns
103    })))
104}