Skip to main content

agentic_evolve_mcp/tools/
evolve_pattern_list.rs

1//! Tool: evolve_pattern_list — List all stored patterns.
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 ListParams {
14    #[serde(default)]
15    domain: Option<String>,
16    #[serde(default)]
17    language: Option<String>,
18    #[serde(default = "default_limit")]
19    limit: usize,
20}
21
22fn default_limit() -> usize {
23    50
24}
25
26/// Return the tool definition for evolve_pattern_list.
27pub fn definition() -> ToolDefinition {
28    ToolDefinition {
29        name: "evolve_pattern_list".to_string(),
30        description: Some("List all stored patterns with optional filtering".to_string()),
31        input_schema: json!({
32            "type": "object",
33            "properties": {
34                "domain": {
35                    "type": "string",
36                    "description": "Filter by domain"
37                },
38                "language": {
39                    "type": "string",
40                    "description": "Filter by programming language"
41                },
42                "limit": {
43                    "type": "integer",
44                    "default": 50,
45                    "description": "Maximum number of results"
46                },
47                "include_content": {
48                    "type": "boolean",
49                    "default": false,
50                    "description": "Include full template content in response"
51                },
52                "intent": {
53                    "type": "string",
54                    "enum": ["exists", "ids", "summary", "full"],
55                    "description": "Response detail level"
56                },
57                "since": {
58                    "type": "integer",
59                    "description": "Only return data changed after this Unix timestamp"
60                },
61                "token_budget": {
62                    "type": "integer",
63                    "description": "Maximum token budget for the response"
64                },
65                "max_results": {
66                    "type": "integer",
67                    "default": 10,
68                    "description": "Maximum number of results to return"
69                },
70                "cursor": {
71                    "type": "string",
72                    "description": "Pagination cursor from a previous response"
73                }
74            }
75        }),
76    }
77}
78
79/// Execute the evolve_pattern_list tool.
80pub async fn execute(
81    args: Value,
82    session: &Arc<Mutex<SessionManager>>,
83) -> McpResult<ToolCallResult> {
84    let params: ListParams =
85        serde_json::from_value(args).map_err(|e| McpError::InvalidParams(e.to_string()))?;
86
87    let session = session.lock().await;
88    let all = session.list_patterns();
89
90    let filtered: Vec<_> = all
91        .into_iter()
92        .filter(|p| {
93            if let Some(domain) = &params.domain {
94                if p.domain.to_lowercase() != domain.to_lowercase() {
95                    return false;
96                }
97            }
98            if let Some(language) = &params.language {
99                if p.language.as_str() != language.to_lowercase() {
100                    return false;
101                }
102            }
103            true
104        })
105        .take(params.limit)
106        .collect();
107
108    let patterns: Vec<Value> = filtered
109        .iter()
110        .map(|p| {
111            json!({
112                "pattern_id": p.id.as_str(),
113                "name": p.name,
114                "domain": p.domain,
115                "language": p.language.as_str(),
116                "confidence": p.confidence,
117                "usage_count": p.usage_count,
118                "tags": p.tags
119            })
120        })
121        .collect();
122
123    let total = session.pattern_count();
124
125    Ok(ToolCallResult::json(&json!({
126        "total_in_library": total,
127        "returned": patterns.len(),
128        "patterns": patterns
129    })))
130}