Skip to main content

agentic_evolve_mcp/tools/
evolve_compose.rs

1//! Tool: evolve_compose — Compose multiple patterns together.
2
3use std::collections::HashMap;
4use std::sync::Arc;
5use tokio::sync::Mutex;
6
7use serde::Deserialize;
8use serde_json::{json, Value};
9
10use crate::session::SessionManager;
11use crate::types::{McpError, McpResult, ToolCallResult, ToolDefinition};
12
13#[derive(Debug, Deserialize)]
14struct ComposeParams {
15    pattern_ids: Vec<String>,
16    #[serde(default)]
17    bindings: HashMap<String, String>,
18}
19
20/// Return the tool definition for evolve_compose.
21pub fn definition() -> ToolDefinition {
22    ToolDefinition {
23        name: "evolve_compose".to_string(),
24        description: Some("Compose multiple patterns together with variable bindings".to_string()),
25        input_schema: json!({
26            "type": "object",
27            "properties": {
28                "pattern_ids": {
29                    "type": "array",
30                    "items": { "type": "string" },
31                    "description": "List of pattern IDs to compose together"
32                },
33                "bindings": {
34                    "type": "object",
35                    "additionalProperties": { "type": "string" },
36                    "description": "Variable bindings (variable_name -> value)"
37                }
38            },
39            "required": ["pattern_ids"]
40        }),
41    }
42}
43
44/// Execute the evolve_compose tool.
45pub async fn execute(
46    args: Value,
47    session: &Arc<Mutex<SessionManager>>,
48) -> McpResult<ToolCallResult> {
49    let params: ComposeParams =
50        serde_json::from_value(args).map_err(|e| McpError::InvalidParams(e.to_string()))?;
51
52    if params.pattern_ids.is_empty() {
53        return Err(McpError::InvalidParams(
54            "pattern_ids must contain at least one pattern ID".to_string(),
55        ));
56    }
57
58    let session = session.lock().await;
59    let result = session
60        .compose(&params.pattern_ids, &params.bindings)
61        .map_err(|e| McpError::AgenticEvolve(e.to_string()))?;
62
63    Ok(ToolCallResult::json(&json!({
64        "code": result.code,
65        "patterns_used": result.patterns_used,
66        "coverage": result.coverage,
67        "gaps": result.gaps
68    })))
69}