Skip to main content

lean_ctx/tools/registered/
ctx_edit.rs

1use rmcp::model::Tool;
2use rmcp::ErrorData;
3use serde_json::{json, Map, Value};
4
5use crate::server::tool_trait::{
6    get_bool, get_int, get_str, require_resolved_path, McpTool, ToolContext, ToolOutput,
7};
8use crate::tool_defs::tool_def;
9
10pub struct CtxEditTool;
11
12impl McpTool for CtxEditTool {
13    fn name(&self) -> &'static str {
14        "ctx_edit"
15    }
16
17    fn tool_def(&self) -> Tool {
18        tool_def(
19            "ctx_edit",
20            "Edit a file via search-and-replace. Works without native Read/Edit tools. Use this when the IDE's Edit tool requires Read but Read is unavailable.",
21            json!({
22                "type": "object",
23                "properties": {
24                    "path": { "type": "string", "description": "Absolute file path" },
25                    "old_string": { "type": "string", "description": "Exact text to find and replace (must be unique unless replace_all=true)" },
26                    "new_string": { "type": "string", "description": "Replacement text" },
27                    "replace_all": { "type": "boolean", "description": "Replace all occurrences (default: false)", "default": false },
28                    "create": { "type": "boolean", "description": "Create a new file with new_string as content (ignores old_string)", "default": false }
29                },
30                "required": ["path", "new_string"]
31            }),
32        )
33    }
34
35    fn handle(
36        &self,
37        args: &Map<String, Value>,
38        ctx: &ToolContext,
39    ) -> Result<ToolOutput, ErrorData> {
40        let path = require_resolved_path(ctx, args, "path")?;
41
42        let old_string = get_str(args, "old_string").unwrap_or_default();
43        let new_string = get_str(args, "new_string")
44            .ok_or_else(|| ErrorData::invalid_params("new_string is required", None))?;
45        let replace_all = get_bool(args, "replace_all").unwrap_or(false);
46        let create = get_bool(args, "create").unwrap_or(false);
47        let expected_md5 = get_str(args, "expected_md5");
48        let expected_size = get_int(args, "expected_size").and_then(|v| u64::try_from(v).ok());
49        let expected_mtime_ms =
50            get_int(args, "expected_mtime_ms").and_then(|v| u64::try_from(v).ok());
51        let backup = get_bool(args, "backup").unwrap_or(false);
52        let backup_path = get_str(args, "backup_path")
53            .map(|p| ctx.resolved_paths.get("backup_path").cloned().unwrap_or(p));
54        let evidence = get_bool(args, "evidence").unwrap_or(true);
55        let diff_max_lines = get_int(args, "diff_max_lines")
56            .and_then(|v| usize::try_from(v.max(0)).ok())
57            .unwrap_or(200);
58        let allow_lossy_utf8 = get_bool(args, "allow_lossy_utf8").unwrap_or(false);
59
60        tokio::task::block_in_place(|| {
61            let cache_lock = ctx
62                .cache
63                .as_ref()
64                .ok_or_else(|| ErrorData::internal_error("cache not available", None))?;
65            let cache_guard = {
66                let rt = tokio::runtime::Handle::current();
67                rt.block_on(tokio::time::timeout(
68                    std::time::Duration::from_secs(10),
69                    cache_lock.write(),
70                ))
71            };
72            let Ok(mut cache) = cache_guard else {
73                return Err(ErrorData::internal_error(
74                    "cache write-lock timeout (10s) in ctx_edit — retry in a moment",
75                    None,
76                ));
77            };
78            let output = crate::tools::ctx_edit::handle(
79                &mut cache,
80                &crate::tools::ctx_edit::EditParams {
81                    path: path.clone(),
82                    old_string,
83                    new_string,
84                    replace_all,
85                    create,
86                    expected_md5,
87                    expected_size,
88                    expected_mtime_ms,
89                    backup,
90                    backup_path,
91                    evidence,
92                    diff_max_lines,
93                    allow_lossy_utf8,
94                },
95            );
96            drop(cache);
97
98            if let Some(session_lock) = ctx.session.as_ref() {
99                let guard = {
100                    let rt = tokio::runtime::Handle::current();
101                    rt.block_on(tokio::time::timeout(
102                        std::time::Duration::from_secs(5),
103                        session_lock.write(),
104                    ))
105                };
106                if let Ok(mut session) = guard {
107                    session.mark_modified(&path);
108                }
109            }
110
111            Ok(ToolOutput {
112                text: output,
113                original_tokens: 0,
114                saved_tokens: 0,
115                mode: None,
116                path: Some(path),
117                changed: false,
118            })
119        })
120    }
121}