lean-ctx-server 3.12.13

The LeanCTX MCP server and its tools on top of LeanCTX | OSS.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// SPDX-License-Identifier: Apache-2.0
use rmcp::ErrorData;
use rmcp::model::Tool;
use serde_json::{Map, Value, json};

use crate::server::tool_trait::{
    McpTool, ShellOutcome, ToolContext, ToolOutput, get_int, get_str, require_resolved_path,
};
use lean_ctx::tool_defs::tool_def;

pub struct CtxExecuteTool;

impl McpTool for CtxExecuteTool {
    fn name(&self) -> &'static str {
        "ctx_execute"
    }

    fn tool_def(&self) -> Tool {
        tool_def(
            "ctx_execute",
            "Run code in sandbox (11 languages) — use when conditionals, multi-line or cross-language transforms.\n\
             ANTIPATTERN: for simple one-liners, prefer ctx_shell (lower overhead, auto-compressed).\n\
             language=shell supports multi-line scripts but shares ctx_shell's security policy;\n\
             other languages run only after the user approves them.\n\
             action=code (default) for one-shot; action=batch for parallel multi-language;\n\
             action=file to summarize admitted project text as data, without executing it.\n\
             For code/batch, intent annotates large output; file records the intent alongside its preview. Languages: javascript,\n\
             typescript, python, shell, ruby, go, rust, php, perl, r, elixir.",
            json!({
                "type": "object",
                "properties": {
                    "language": {
                        "type": "string",
                        "description": "javascript|typescript|python|shell|ruby|go|rust|php|perl|r|elixir (for action=code)"
                    },
                    "code": {
                        "type": "string",
                        "description": "Source code for action=code. Set intent to filter large output."
                    },
                    "intent": {
                        "type": "string",
                        "description": "Focus intent; triggers filtering when output is large."
                    },
                    "timeout": {
                        "type": "integer",
                        "description": "Timeout in seconds (default: 30)"
                    },
                    "action": {
                        "type": "string",
                        "description": "code (default, run script) | batch (parallel) | file (project file)"
                    },
                    "items": {
                        "type": "string",
                        "description": "JSON array of [{language, code}] for batch action."
                    },
                    "path": {
                        "type": "string",
                        "description": "Project text file for action=file; content is inspected, never executed."
                    }
                },
                "oneOf": [
                    {
                        "properties": { "action": { "enum": ["code"] } },
                        "required": ["language", "code"]
                    },
                    {
                        "properties": { "action": { "const": "batch" } },
                        "required": ["action", "items"]
                    },
                    {
                        "properties": { "action": { "const": "file" } },
                        "required": ["action", "path"]
                    }
                ]
            }),
        )
    }

    fn handle(
        &self,
        args: &Map<String, Value>,
        ctx: &ToolContext,
    ) -> Result<ToolOutput, ErrorData> {
        let action = get_str(args, "action").unwrap_or_default();

        // action=file inspects text and never executes it; code and batch run
        // code, so restricted clients are refused like ctx_shell refuses them.
        if action != "file"
            && let Some(message) = super::ctx_shell::shell_access_denial(ctx)
        {
            return Ok(ToolOutput {
                shell_outcome: Some(crate::server::tool_trait::ShellOutcome::Blocked),
                content_blocks: None,
                ..ToolOutput::simple(message)
            });
        }
        let (result, outcome) = if action == "batch" {
            let items_str = get_str(args, "items")
                .ok_or_else(|| ErrorData::invalid_params("items is required for batch", None))?;
            let items: Vec<serde_json::Value> = serde_json::from_str(&items_str)
                .map_err(|e| ErrorData::invalid_params(format!("Invalid items JSON: {e}"), None))?;
            let batch: Vec<(String, String)> = items
                .iter()
                .filter_map(|item| {
                    let lang = item.get("language")?.as_str()?.to_string();
                    let code = item.get("code")?.as_str()?.to_string();
                    Some((lang, code))
                })
                .collect();
            match admit_code(ctx, &batch) {
                Ok(()) => {
                    crate::tools::ctx_execute::handle_batch_in(&batch, rights_cwd(ctx).as_deref())
                }
                Err(refusal) => (refusal.into_message(), ShellOutcome::Blocked),
            }
        } else if action == "file" {
            let path = require_resolved_path(ctx, args, "path")?;
            let project_root = if ctx.project_root.is_empty() {
                None
            } else {
                Some(ctx.project_root.as_str())
            };
            let intent = get_str(args, "intent");
            crate::tools::ctx_execute::handle_file(&path, intent.as_deref(), project_root)
        } else {
            let language = get_str(args, "language")
                .ok_or_else(|| ErrorData::invalid_params("language is required", None))?;
            let code = get_str(args, "code")
                .ok_or_else(|| ErrorData::invalid_params("code is required", None))?;
            let intent = get_str(args, "intent");
            let timeout = get_int(args, "timeout").and_then(|t| u64::try_from(t).ok());
            match admit_code(ctx, &[(language.as_str(), code.as_str())]) {
                Ok(()) => crate::tools::ctx_execute::handle_in(
                    &language,
                    &code,
                    intent.as_deref(),
                    timeout,
                    rights_cwd(ctx).as_deref(),
                ),
                Err(refusal) => (refusal.into_message(), ShellOutcome::Blocked),
            }
        };

        let result = lean_ctx::core::redaction::redact_text_if_enabled(&result);
        Ok(ToolOutput {
            text: result,
            original_tokens: 0,
            saved_tokens: 0,
            mode: Some(action),
            path: None,
            changed: false,
            shell_outcome: Some(outcome),
            content_blocks: None,
        })
    }

    /// `action=file` reads exactly one source, through the admitted rooted
    /// reader on the handler thread (`tools::ctx_execute::handle_file_in_view`)
    /// and derives its summary from that text alone. `code` and `batch` run
    /// arbitrary programs whose reads nothing here observes: not declared.
    fn publishes_every_source(&self, args: Option<&Map<String, Value>>) -> bool {
        args.and_then(|args| args.get("action"))
            .and_then(Value::as_str)
            == Some("file")
    }
}

/// Why [`admit_code`] refused code before anything ran.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum CodeRefusal {
    /// The session's rights profile refuses it.
    Rights(String),
    /// A language that needs the user's approval has none in this dispatch.
    Unapproved(String),
}

impl CodeRefusal {
    pub(crate) fn into_message(self) -> String {
        match self {
            Self::Rights(message) | Self::Unapproved(message) => message,
        }
    }
}

/// The gates `ctx_execute` code passes before anything runs: the session's
/// rights for every item, then the user's approval of every language that
/// needs one (GHSA-rxv5-4c3x-g4q6). Every path that runs code through
/// `tools::ctx_execute` — this tool's code and batch actions and the
/// interpreter heredoc `ctx_shell` reroutes here (GHSA-hqgc-cjh7-q3w4) —
/// goes through this one function, so the paths cannot drift apart.
pub(crate) fn admit_code<S: AsRef<str>>(
    ctx: &ToolContext,
    items: &[(S, S)],
) -> Result<(), CodeRefusal> {
    items
        .iter()
        .try_for_each(|(language, code)| {
            session_rights_check(ctx, language.as_ref(), code.as_ref())
        })
        .map_err(CodeRefusal::Rights)?;
    crate::tools::ctx_execute::refuse_unapproved(
        items.iter().map(|(language, _)| language.as_ref()),
    )
    .map_err(CodeRefusal::Unapproved)
}

/// The session's rights profile (process-v1 §9) for code this tool runs:
/// shell code is analysed like a `ctx_shell` command line, other languages
/// are refused under a profile.
fn session_rights_check(ctx: &ToolContext, language: &str, code: &str) -> Result<(), String> {
    lean_ctx::core::session_rights::current().check_code(language, code, &checked_cwd(ctx))
}

/// The directory the rights check judges relative paths against.
fn checked_cwd(ctx: &ToolContext) -> std::path::PathBuf {
    if ctx.project_root.is_empty() {
        std::env::current_dir().unwrap_or_default()
    } else {
        std::path::PathBuf::from(&ctx.project_root)
    }
}

/// Under a profile the code runs where it was checked (the shared daemon's
/// own directory is not the session's); without one, as before.
fn rights_cwd(ctx: &ToolContext) -> Option<std::path::PathBuf> {
    lean_ctx::core::session_rights::current()
        .is_active()
        .then(|| checked_cwd(ctx))
}

#[cfg(test)]
pub(crate) mod session_rights_fixture {
    //! A Developer rights file over a temporary worktree whose
    //! `tests/locked_test.rs` is locked (spec_commit set).

    pub(crate) struct Fixture {
        pub _dir: tempfile::TempDir,
        pub worktree: std::path::PathBuf,
        pub rights: std::path::PathBuf,
    }

    pub(crate) fn developer(denied_tools: &[&str]) -> Fixture {
        let dir = tempfile::tempdir().expect("tempdir");
        let worktree = dir.path().join("wt");
        std::fs::create_dir_all(worktree.join("tests")).expect("tests dir");
        std::fs::create_dir_all(worktree.join("specs/42")).expect("spec dir");
        std::fs::write(worktree.join("tests/locked_test.rs"), "#[test] fn t() {}\n").expect("test");
        let list = serde_json::json!({
            "schema": "leanctx.cloud.process.feature-list/v1", "process": "1.0",
            "task": {"vault": "#acme/payments", "number": 42},
            "spec_commit": "1a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d",
            "test_command": "cargo test",
            "locked_paths": ["tests/locked_test.rs", "specs/42/features.json"],
            "features": [{"id": "F1", "description": "d", "steps": ["s"],
                          "tests": ["tests/locked_test.rs::t"], "passes": false}]
        });
        std::fs::write(worktree.join("specs/42/features.json"), list.to_string()).expect("list");
        let record = serde_json::json!({
            "schema": "leanctx.session-rights/v1", "process": "1.0",
            "profile": {"id": "developer", "name": "Developer", "stages": ["build"],
                        "delivers": "commits in its own worktree", "read": ["repo", "vault"],
                        "write": ["own_worktree"], "deny_write": ["locked", "spec", "tests"],
                        "may_stop": false, "availability": "now"},
            "bindings": {"own_worktree": [worktree.to_string_lossy()]},
            "feature_list": "specs/42/features.json",
            "denied_tools": denied_tools,
        });
        let rights = dir.path().join("rights.json");
        std::fs::write(&rights, record.to_string()).expect("rights");
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&rights, std::fs::Permissions::from_mode(0o600))
                .expect("0600");
        }
        Fixture {
            _dir: dir,
            worktree,
            rights,
        }
    }

    impl Fixture {
        pub(crate) fn state(&self) -> lean_ctx::core::session_rights::State {
            let state =
                lean_ctx::core::session_rights::from_file_arg(Some(&self.rights.to_string_lossy()));
            assert!(state.is_active(), "{state:?}");
            state
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::server::tool_trait::ShellOutcome;

    /// F4: ctx_execute runs code under the session rights: shell code that
    /// writes a locked test and code in another language are refused before
    /// anything runs.
    #[test]
    fn session_rights_ctx_execute_refuses_locked_writes_and_other_languages() {
        let fx = super::session_rights_fixture::developer(&[]);
        let ctx = ToolContext {
            project_root: fx.worktree.to_string_lossy().into_owned(),
            ..ToolContext::default()
        };
        lean_ctx::core::session_rights::sync_scope(fx.state(), || {
            for (language, code) in [
                ("shell", "echo x > tests/locked_test.rs"),
                ("bash", "bash -c 'echo x > tests/locked_test.rs'"),
                ("python", "open('tests/locked_test.rs','w').write('x')"),
            ] {
                let output = CtxExecuteTool
                    .handle(&args(&[("language", language), ("code", code)]), &ctx)
                    .expect("answer");
                assert_eq!(
                    output.shell_outcome,
                    Some(ShellOutcome::Blocked),
                    "{language}"
                );
                assert!(output.text.contains("rule "), "{language}: {}", output.text);
            }
            let batch = args(&[
                ("action", "batch"),
                (
                    "items",
                    r#"[{"language":"shell","code":"echo x >> tests/locked_test.rs"}]"#,
                ),
            ]);
            let output = CtxExecuteTool.handle(&batch, &ctx).expect("answer");
            assert_eq!(output.shell_outcome, Some(ShellOutcome::Blocked));
            assert!(session_rights_check(&ctx, "shell", "echo ok > notes.txt").is_ok());
            // R2-4: the code runs in the directory it was checked against,
            // not in the server's own working directory.
            let output = CtxExecuteTool
                .handle(
                    &args(&[("language", "shell"), ("code", "echo ok > made_here.txt")]),
                    &ctx,
                )
                .expect("answer");
            assert_eq!(
                output.shell_outcome,
                Some(ShellOutcome::Exit(0)),
                "{}",
                output.text
            );
        });
        assert!(
            fx.worktree.join("made_here.txt").is_file(),
            "the checked directory is where the code ran"
        );
        assert_eq!(
            std::fs::read_to_string(fx.worktree.join("tests/locked_test.rs")).expect("read"),
            "#[test] fn t() {}\n",
            "the locked test is unchanged"
        );
    }

    fn args(pairs: &[(&str, &str)]) -> Map<String, Value> {
        pairs
            .iter()
            .map(|(k, v)| ((*k).to_string(), Value::String((*v).to_string())))
            .collect()
    }

    /// GHSA-rxv5-4c3x-g4q6 B: clients that may not run ctx_shell may not run
    /// code through ctx_execute either.
    #[test]
    fn restricted_clients_cannot_execute_code() {
        let code = args(&[("language", "python"), ("code", "print(1)")]);
        let batch = args(&[
            ("action", "batch"),
            ("items", r#"[{"language":"python","code":"print(1)"}]"#),
        ]);
        let contexts = [
            ToolContext {
                client_role: Some("untrusted".to_string()),
                ..ToolContext::default()
            },
            ToolContext {
                client_role: Some("readonly".to_string()),
                ..ToolContext::default()
            },
            ToolContext {
                shell_access: Some(false),
                ..ToolContext::default()
            },
        ];
        for ctx in &contexts {
            for request in [&code, &batch] {
                let output = CtxExecuteTool
                    .handle(request, ctx)
                    .expect("denial is a tool result");
                assert_eq!(output.shell_outcome, Some(ShellOutcome::Blocked));
                assert!(
                    output.text.contains("SHELL ACCESS DENIED"),
                    "{}",
                    output.text
                );
            }
        }
    }
}