Skip to main content

lean_ctx/tools/registered/
ctx_workflow.rs

1use rmcp::model::Tool;
2use rmcp::ErrorData;
3use serde_json::{json, Map, Value};
4
5use crate::server::tool_trait::{get_str, McpTool, ToolContext, ToolOutput};
6use crate::tool_defs::tool_def;
7
8pub struct CtxWorkflowTool;
9
10impl McpTool for CtxWorkflowTool {
11    fn name(&self) -> &'static str {
12        "ctx_workflow"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_workflow",
18            "Workflow rails (state machine + evidence). Actions: start|status|transition|complete|evidence_add|evidence_list|stop.",
19            json!({
20                "type": "object",
21                "properties": {
22                    "action": {
23                        "type": "string",
24                        "enum": ["start", "status", "transition", "complete", "evidence_add", "evidence_list", "stop"],
25                        "description": "Workflow operation (default: status)"
26                    },
27                    "name": { "type": "string", "description": "Optional workflow name override (action=start)" },
28                    "spec": { "type": "string", "description": "WorkflowSpec JSON (action=start). If omitted, uses builtin plan_code_test." },
29                    "to": { "type": "string", "description": "Target state (action=transition)" },
30                    "key": { "type": "string", "description": "Evidence key (action=evidence_add)" },
31                    "value": { "type": "string", "description": "Optional evidence value / transition note" }
32                }
33            }),
34        )
35    }
36
37    fn handle(
38        &self,
39        args: &Map<String, Value>,
40        ctx: &ToolContext,
41    ) -> Result<ToolOutput, ErrorData> {
42        let action = get_str(args, "action").unwrap_or_else(|| "status".to_string());
43
44        let agent_id_str = ctx
45            .agent_id
46            .as_ref()
47            .and_then(|h| h.blocking_read().clone());
48
49        let result = {
50            let session_handle = ctx
51                .session
52                .as_ref()
53                .ok_or_else(|| ErrorData::internal_error("session not available", None))?;
54            let mut session = session_handle.blocking_write();
55            crate::tools::ctx_workflow::handle_with_session_agent(
56                Some(args),
57                &mut session,
58                agent_id_str.as_deref(),
59            )
60        };
61
62        if let Some(workflow_handle) = ctx.workflow.as_ref() {
63            let mut wf = workflow_handle.blocking_write();
64            *wf = crate::core::workflow::load_active_for_agent(agent_id_str.as_deref())
65                .ok()
66                .flatten();
67        }
68
69        Ok(ToolOutput {
70            text: result,
71            original_tokens: 0,
72            saved_tokens: 0,
73            mode: Some(action),
74            path: None,
75            changed: false,
76        })
77    }
78}