shellvibe 0.3.5

Observable MCP shell access with process lifecycle controls and modern Tasks support
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use std::sync::Arc;

use rmcp::model::{JsonObject, Tool, ToolAnnotations};
use serde_json::{Value, json};

use crate::{
    model::{
        MAX_ARG_BYTES, MAX_ARGV_ITEMS, MAX_COMMAND_BYTES, MAX_ENV_ITEMS, MAX_PATH_BYTES,
        MAX_STDIN_BYTES, MIN_READ_PAGE_BYTES,
    },
    policy::ExecPolicy,
};

pub fn tools(policy: &ExecPolicy) -> Vec<Tool> {
    vec![
        shell_tool(policy),
        read_tool(),
        write_tool(),
        signal_tool(),
        resize_tool(),
    ]
}

pub fn get_tool(policy: &ExecPolicy, name: &str) -> Option<Tool> {
    match name {
        "shell" => Some(shell_tool(policy)),
        "shell_read" => Some(read_tool()),
        "shell_write" => Some(write_tool()),
        "shell_signal" => Some(signal_tool()),
        "shell_resize" => Some(resize_tool()),
        _ => None,
    }
}

fn shell_tool(policy: &ExecPolicy) -> Tool {
    let (description, primary) = if policy.is_restricted() {
        (
            "Execute one permitted top-level program directly without a shell interpreter. Pass argv with the executable as argv[0]. Shell operators (|, &&, redirects, globbing, command substitution) are unavailable. foreground is the default and asks for the final result; a long foreground command transparently becomes an MCP Task on MCP 2026-07-28 when the client declares Tasks, otherwise it yields a processId fallback. background returns a processId immediately. interactive allocates a PTY and returns a processId. The executable policy is a top-level guardrail, not an OS sandbox.",
            json!({
                "argv": {
                    "type": "array",
                    "minItems": 1,
                    "maxItems": MAX_ARGV_ITEMS,
                    "items": {"type": "string", "maxLength": MAX_ARG_BYTES},
                    "description": "Executable and arguments. argv[0] is resolved and checked against the startup policy."
                }
            }),
        )
    } else {
        (
            "Execute an unrestricted shell command with the permissions of the user running shellvibe. foreground is the default and asks for the final result; a long foreground command transparently becomes an MCP Task when the client supports Tasks, otherwise it yields a processId fallback. background returns a processId immediately. interactive allocates a PTY and returns a processId for shell_read/shell_write/shell_signal/shell_resize.",
            json!({
                "command": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": MAX_COMMAND_BYTES,
                    "description": "Command passed to the configured shell using -c (or cmd.exe /C on Windows)."
                }
            }),
        )
    };

    let mut properties = primary
        .as_object()
        .expect("primary schema must be object")
        .clone();
    properties.extend(common_shell_properties());
    let required = if policy.is_restricted() {
        json!(["argv"])
    } else {
        json!(["command"])
    };

    let schema = json!({
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "type": "object",
        "additionalProperties": false,
        "required": required,
        "properties": properties
    });

    Tool::new("shell", description, arc_object(schema))
        .with_title("Run shell process")
        .with_raw_output_schema(arc_object(tool_output_schema(shell_result_schema())))
        .with_annotations(
            ToolAnnotations::new()
                .read_only(false)
                .destructive(true)
                .idempotent(false)
                .open_world(true),
        )
}

fn common_shell_properties() -> JsonObject {
    serde_json::from_value(json!({
        "cwd": {"type": "string", "maxLength": MAX_PATH_BYTES, "description": "Working directory for this process."},
        "env": {
            "type": "object",
            "maxProperties": MAX_ENV_ITEMS,
            "propertyNames": {"minLength": 1, "pattern": "^[^=]+$"},
            "additionalProperties": {"type": "string", "maxLength": MAX_STDIN_BYTES},
            "description": "Environment variables added or overridden for the child process. Top-level executable policy resolution uses shellvibe's own startup PATH."
        },
        "stdin": {"type": "string", "maxLength": MAX_STDIN_BYTES, "description": "Optional UTF-8 input written immediately after spawn."},
        "execution": {
            "type": "string",
            "enum": ["foreground", "background", "interactive"],
            "default": "foreground",
            "description": "foreground asks for the final result and may transparently use MCP Tasks; background returns a process handle; interactive forces a PTY and returns a process handle."
        },
        "pty": {"type": "boolean", "default": false, "description": "Allocate a pseudo-terminal. interactive mode always enables PTY."},
        "yieldMs": {"type": "integer", "minimum": 1, "description": "Override the foreground grace period before a long command becomes a Task or process-handle fallback."},
        "timeoutMs": {"type": "integer", "minimum": 1, "description": "Optional per-call runtime limit. Values above the server --max-runtime ceiling are clamped."},
        "rows": {"type": "integer", "minimum": 1, "maximum": 1000, "default": 24, "description": "Initial PTY rows."},
        "cols": {"type": "integer", "minimum": 1, "maximum": 2000, "default": 80, "description": "Initial PTY columns."}
    })).expect("common schema is an object")
}

fn read_tool() -> Tool {
    Tool::new(
        "shell_read",
        "Canonical paginated output retrieval for a managed process. Returns retained output events after cursor. waitMs is only the maximum long-poll duration: a read returns on new output, terminal process state, or timeout. nextCursor is an output-event cursor for the next read, not a byte or line offset. hasMore=false means no additional buffered page is currently available; it does not imply that the process exited. cursorLost=true means output history requested by cursor was evicted.",
        arc_object(json!({
            "$schema": "https://json-schema.org/draft/2020-12/schema",
            "type": "object",
            "additionalProperties": false,
            "required": ["processId"],
            "properties": {
                "processId": {"type": "string", "pattern": "^p_[0-9a-f]{32}$"},
                "cursor": {"type": "integer", "minimum": 0, "default": 0, "description": "Output-event cursor returned as nextCursor by the previous read; not a byte or line offset."},
                "waitMs": {"type": "integer", "minimum": 0, "default": 0, "description": "Maximum long-poll duration only. The read returns earlier for new output or terminal process state; the server caps this value."},
                "maxBytes": {"type": "integer", "minimum": MIN_READ_PAGE_BYTES, "description": "Maximum output event-data bytes requested for this page. The server caps this value at its configured response maximum."}
            }
        })),
    )
    .with_title("Read process output")
    .with_raw_output_schema(arc_object(tool_output_schema(read_result_schema())))
    .with_annotations(
        ToolAnnotations::new()
            .read_only(true)
            .destructive(false)
            .idempotent(true)
            .open_world(false),
    )
}

fn write_tool() -> Tool {
    Tool::new(
        "shell_write",
        "Send UTF-8 input to a running process stdin/PTY and optionally close stdin to deliver EOF. This returns an acknowledgement and does not wait for resulting output; use shell_read to observe output.",
        arc_object(json!({
            "$schema": "https://json-schema.org/draft/2020-12/schema",
            "type": "object",
            "additionalProperties": false,
            "required": ["processId"],
            "properties": {
                "processId": {"type": "string", "pattern": "^p_[0-9a-f]{32}$"},
                "data": {"type": "string", "maxLength": MAX_STDIN_BYTES},
                "appendNewline": {"type": "boolean", "default": false},
                "closeStdin": {"type": "boolean", "default": false, "description": "Drop the stdin/PTY writer after any data is written, delivering EOF where supported."}
            },
            "anyOf": [
                {"required": ["data"]},
                {"properties": {"closeStdin": {"const": true}}, "required": ["closeStdin"]}
            ]
        })),
    )
    .with_title("Write process input")
    .with_raw_output_schema(arc_object(tool_output_schema(write_result_schema())))
    .with_annotations(
        ToolAnnotations::new()
            .read_only(false)
            .destructive(true)
            .idempotent(false)
            .open_world(false),
    )
}

fn signal_tool() -> Tool {
    Tool::new(
        "shell_signal",
        "Send INT, TERM, or KILL to the managed process tree. The acknowledgement confirms the request was sent, not that the process has exited; use shell_read with waitMs to observe terminal state.",
        arc_object(json!({
            "$schema": "https://json-schema.org/draft/2020-12/schema",
            "type": "object",
            "additionalProperties": false,
            "required": ["processId", "signal"],
            "properties": {
                "processId": {"type": "string", "pattern": "^p_[0-9a-f]{32}$"},
                "signal": {"type": "string", "enum": ["int", "term", "kill"]}
            }
        })),
    )
    .with_title("Signal process")
    .with_raw_output_schema(arc_object(tool_output_schema(signal_result_schema())))
    .with_annotations(
        ToolAnnotations::new()
            .read_only(false)
            .destructive(true)
            .idempotent(false)
            .open_world(false),
    )
}

fn resize_tool() -> Tool {
    Tool::new(
        "shell_resize",
        "Resize a running PTY. This returns only the new dimensions and current status; accumulated terminal output remains available through shell_read.",
        arc_object(json!({
            "$schema": "https://json-schema.org/draft/2020-12/schema",
            "type": "object",
            "additionalProperties": false,
            "required": ["processId", "rows", "cols"],
            "properties": {
                "processId": {"type": "string", "pattern": "^p_[0-9a-f]{32}$"},
                "rows": {"type": "integer", "minimum": 1, "maximum": 1000},
                "cols": {"type": "integer", "minimum": 1, "maximum": 2000}
            }
        })),
    )
    .with_title("Resize PTY")
    .with_raw_output_schema(arc_object(tool_output_schema(resize_result_schema())))
    .with_annotations(
        ToolAnnotations::new()
            .read_only(false)
            .destructive(false)
            .idempotent(true)
            .open_world(false),
    )
}

fn tool_output_schema(success: Value) -> Value {
    json!({
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "oneOf": [
            success,
            {
                "type": "object",
                "additionalProperties": false,
                "required": ["error", "message"],
                "properties": {
                    "error": {"const": "shellvibe_error"},
                    "message": {"type": "string"}
                }
            }
        ]
    })
}

fn shell_result_schema() -> Value {
    json!({
        "type": "object",
        "additionalProperties": false,
        "required": ["processId", "status", "pty", "nextCursor"],
        "properties": {
            "processId": {"type": "string", "pattern": "^p_[0-9a-f]{32}$"},
            "status": {"type": "string", "enum": ["running", "exited", "signaled", "timed_out", "cancelled", "failed"]},
            "pid": {"type": "integer", "minimum": 0},
            "pty": {"type": "boolean"},
            "rows": {"type": "integer", "minimum": 1},
            "cols": {"type": "integer", "minimum": 1},
            "exitCode": {"type": "integer"},
            "signal": {"type": "string"},
            "elapsedMs": {"type": "integer", "minimum": 0, "description": "Total process lifetime in milliseconds, not tool-call or read latency."},
            "output": {"type": "string", "description": "Bounded ordered output tail present for completed foreground execution."},
            "outputTruncated": {"type": "boolean", "description": "Whether foreground output was truncated. When true, retained output can be retrieved with shell_read(cursor=0) while the process handle remains available."},
            "nextCursor": {"type": "integer", "minimum": 0, "description": "Latest output-event cursor, not a byte or line offset."}
        }
    })
}

fn read_result_schema() -> Value {
    json!({
        "type": "object",
        "additionalProperties": false,
        "required": ["processId", "status", "events", "nextCursor", "hasMore", "cursorLost"],
        "properties": {
            "processId": {"type": "string", "pattern": "^p_[0-9a-f]{32}$"},
            "status": {"type": "string", "enum": ["running", "exited", "signaled", "timed_out", "cancelled", "failed"]},
            "events": {
                "type": "array",
                "items": {
                    "type": "object",
                    "additionalProperties": false,
                    "required": ["cursor", "stream", "data"],
                    "properties": {
                        "cursor": {"type": "integer", "minimum": 1},
                        "stream": {"type": "string", "enum": ["stdout", "stderr", "pty"]},
                        "data": {"type": "string"}
                    }
                }
            },
            "nextCursor": {"type": "integer", "minimum": 0, "description": "Output-event cursor to pass to the next read; not a byte or line offset."},
            "hasMore": {"type": "boolean", "description": "Whether another buffered page is immediately available. false does not imply that the process exited."},
            "cursorLost": {"type": "boolean", "description": "true when output history requested by cursor was evicted from retention."},
            "exitCode": {"type": "integer"},
            "signal": {"type": "string"},
            "elapsedMs": {"type": "integer", "minimum": 0, "description": "Total process lifetime in milliseconds, not shell_read latency."}
        }
    })
}

fn write_result_schema() -> Value {
    json!({
        "type": "object",
        "additionalProperties": false,
        "required": ["processId", "acceptedBytes", "stdinClosed", "status"],
        "properties": {
            "processId": {"type": "string", "pattern": "^p_[0-9a-f]{32}$"},
            "acceptedBytes": {"type": "integer", "minimum": 0},
            "stdinClosed": {"type": "boolean"},
            "status": {"type": "string", "enum": ["running", "exited", "signaled", "timed_out", "cancelled", "failed"]}
        }
    })
}

fn signal_result_schema() -> Value {
    json!({
        "type": "object",
        "additionalProperties": false,
        "required": ["processId", "signal", "accepted", "status"],
        "properties": {
            "processId": {"type": "string", "pattern": "^p_[0-9a-f]{32}$"},
            "signal": {"type": "string", "enum": ["int", "term", "kill"]},
            "accepted": {"type": "boolean"},
            "status": {"type": "string", "enum": ["running", "exited", "signaled", "timed_out", "cancelled", "failed"]}
        }
    })
}

fn resize_result_schema() -> Value {
    json!({
        "type": "object",
        "additionalProperties": false,
        "required": ["processId", "rows", "cols", "status"],
        "properties": {
            "processId": {"type": "string", "pattern": "^p_[0-9a-f]{32}$"},
            "rows": {"type": "integer", "minimum": 1, "maximum": 1000},
            "cols": {"type": "integer", "minimum": 1, "maximum": 2000},
            "status": {"type": "string", "enum": ["running", "exited", "signaled", "timed_out", "cancelled", "failed"]}
        }
    })
}

fn arc_object(value: Value) -> Arc<JsonObject> {
    Arc::new(
        value
            .as_object()
            .expect("schema must be a JSON object")
            .clone(),
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn unrestricted_schema_exposes_command_not_argv() {
        let tool = shell_tool(&ExecPolicy::Unrestricted);
        let properties = tool
            .input_schema
            .get("properties")
            .unwrap()
            .as_object()
            .unwrap();
        assert!(properties.contains_key("command"));
        assert!(!properties.contains_key("argv"));
    }

    #[test]
    fn restricted_schema_exposes_argv_not_command() {
        let policy = ExecPolicy::Deny {
            names: Default::default(),
            paths: Default::default(),
        };
        let tool = shell_tool(&policy);
        let properties = tool
            .input_schema
            .get("properties")
            .unwrap()
            .as_object()
            .unwrap();
        assert!(properties.contains_key("argv"));
        assert!(!properties.contains_key("command"));
    }

    #[test]
    fn read_schema_exposes_bounded_page_controls_and_cursor_semantics() {
        let tool = read_tool();
        let properties = tool
            .input_schema
            .get("properties")
            .unwrap()
            .as_object()
            .unwrap();
        assert_eq!(
            properties["maxBytes"]["minimum"],
            json!(MIN_READ_PAGE_BYTES)
        );
        assert!(
            properties["waitMs"]["description"]
                .as_str()
                .unwrap()
                .contains("Maximum long-poll duration only")
        );

        let output = tool.output_schema.unwrap();
        let success = &output["oneOf"][0];
        assert!(
            success["properties"]["hasMore"]["description"]
                .as_str()
                .unwrap()
                .contains("does not imply")
        );
        assert!(
            success["properties"]["cursorLost"]["description"]
                .as_str()
                .unwrap()
                .contains("evicted")
        );
    }
}