npcterm 1.1.0

Headless terminal emulator for AI agents with full PTY access over MCP
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use serde_json::{Value, json};

use crate::input::keys::Key;
use crate::input::mouse::MouseAction;
use crate::manager::instance::TerminalInstance;
use crate::manager::registry::TerminalRegistry;

use super::types::{ToolCallResult, ToolDef};

fn get_id<'a>(args: &'a Value) -> Result<&'a str, ToolCallResult> {
    args.get("id")
        .and_then(|v| v.as_str())
        .ok_or_else(|| ToolCallResult::error("Missing 'id' parameter".into()))
}

fn get_instance<'a>(
    registry: &'a mut TerminalRegistry,
    args: &Value,
) -> Result<&'a mut TerminalInstance, ToolCallResult> {
    let id = get_id(args)?;
    registry
        .get_mut(id)
        .ok_or_else(|| ToolCallResult::error(format!("Terminal '{}' not found", id)))
}

/// Return all tool definitions
pub fn tool_definitions() -> Vec<ToolDef> {
    vec![
        ToolDef {
            name: "terminal_create".into(),
            description: "Create a new terminal instance. Returns {id, cols, rows}. The id is required for all subsequent terminal operations. Available sizes: 80x24 (default), 120x40, 160x40, 200x50.".into(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "size": { "type": "string", "enum": ["80x24", "120x40", "160x40", "200x50"], "default": "80x24" },
                    "shell": { "type": "string", "description": "Shell path (optional)" }
                }
            }),
        },
        ToolDef {
            name: "terminal_destroy".into(),
            description: "Destroy a terminal instance and kill its PTY process. Returns {success: bool}.".into(),
            input_schema: json!({
                "type": "object",
                "properties": { "id": { "type": "string" } },
                "required": ["id"]
            }),
        },
        ToolDef {
            name: "terminal_list".into(),
            description: "List all terminal instances with id, size, state, and running command.".into(),
            input_schema: json!({ "type": "object", "properties": {} }),
        },
        ToolDef {
            name: "terminal_send_key".into(),
            description: "Send a single keystroke. Supports: a-z, Enter, Tab, Escape, Backspace, Delete, arrows, Home, End, PageUp, PageDown, F1-F12, Ctrl+key, Alt+key, space. For multiple keystrokes or text input, use terminal_send_keys instead.".into(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "id": { "type": "string" },
                    "key": { "type": "string", "description": "Key name (e.g. 'Enter', 'Ctrl+c', 'a')" }
                },
                "required": ["id", "key"]
            }),
        },
        ToolDef {
            name: "terminal_send_keys".into(),
            description: "Send a batch of text and special keys in one call (preferred over terminal_send_key for multi-step input). Each element is either {\"text\":\"string\"} for raw text or {\"key\":\"Enter\"} for special keys (Enter, Tab, Escape, Ctrl+c, etc). Example: [{\"text\":\"echo hello\"},{\"key\":\"Enter\"}]".into(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "id": { "type": "string" },
                    "input": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "text": { "type": "string", "description": "Raw text to type" },
                                "key": { "type": "string", "description": "Special key name (Enter, Tab, Ctrl+c, etc)" }
                            }
                        }
                    }
                },
                "required": ["id", "input"]
            }),
        },
        ToolDef {
            name: "terminal_mouse".into(),
            description: "Perform a mouse action. col and row required for left_click, right_click, double_click, move, set_position. get_position returns current cursor coords.".into(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "id": { "type": "string" },
                    "action": { "type": "string", "enum": ["left_click", "right_click", "double_click", "move", "get_position", "set_position"] },
                    "col": { "type": "integer" },
                    "row": { "type": "integer" }
                },
                "required": ["id", "action"]
            }),
        },
        ToolDef {
            name: "terminal_read_screen".into(),
            description: "Read terminal screen with coordinate overlay. Use mode 'changes' to see only new output since your last read (default 50 lines, max 200). Default mode 'full' returns the complete screen.".into(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "id": { "type": "string" },
                    "mode": { "type": "string", "enum": ["full", "changes"], "default": "full" },
                    "max_lines": { "type": "integer", "default": 50, "description": "Max lines to return in 'changes' mode (1-200)" }
                },
                "required": ["id"]
            }),
        },
        ToolDef {
            name: "terminal_show_screen".into(),
            description: "Return terminal screen as plain text without coordinates. Lighter output, best for reading content or passing to other tools.".into(),
            input_schema: json!({
                "type": "object",
                "properties": { "id": { "type": "string" } },
                "required": ["id"]
            }),
        },
        ToolDef {
            name: "terminal_read_rows".into(),
            description: "Read specific rows from terminal screen with coordinate overlay (column headers + row numbers).".into(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "id": { "type": "string" },
                    "start_row": { "type": "integer" },
                    "end_row": { "type": "integer" }
                },
                "required": ["id", "start_row", "end_row"]
            }),
        },
        ToolDef {
            name: "terminal_read_region".into(),
            description: "Read a rectangular region from terminal screen with row numbers.".into(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "id": { "type": "string" },
                    "col1": { "type": "integer" }, "row1": { "type": "integer" },
                    "col2": { "type": "integer" }, "row2": { "type": "integer" }
                },
                "required": ["id", "col1", "row1", "col2", "row2"]
            }),
        },
        ToolDef {
            name: "terminal_status".into(),
            description: "Get terminal status: process state, running command, cursor position, dirty rows, last N screen lines, pending event count, and scrollback depth. Token-efficient alternative to reading the full screen.".into(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "id": { "type": "string" },
                    "last_n_lines": { "type": "integer", "default": 5 }
                },
                "required": ["id"]
            }),
        },
        ToolDef {
            name: "terminal_poll_events".into(),
            description: "Drain pending terminal events (destructive: events are removed after reading). Event types: CommandFinished, WaitingForInput, Bell, ProcessStateChanged, ScreenChanged.".into(),
            input_schema: json!({
                "type": "object",
                "properties": { "id": { "type": "string" } },
                "required": ["id"]
            }),
        },
        ToolDef {
            name: "terminal_select".into(),
            description: "Select text by coordinate range (like click-drag) and return it. Unlike read_region, this is a logical text selection that can span across lines.".into(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "id": { "type": "string" },
                    "start_col": { "type": "integer" }, "start_row": { "type": "integer" },
                    "end_col": { "type": "integer" }, "end_row": { "type": "integer" }
                },
                "required": ["id", "start_col", "start_row", "end_col", "end_row"]
            }),
        },
        ToolDef {
            name: "terminal_scroll".into(),
            description: "Navigate scrollback history: page_up, page_down, or search (requires 'text' param). Returns {scroll_offset} (0 = bottom). Use terminal_read_screen after scrolling to see the result.".into(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "id": { "type": "string" },
                    "action": { "type": "string", "enum": ["page_up", "page_down", "search"] },
                    "text": { "type": "string" }
                },
                "required": ["id", "action"]
            }),
        },
    ]
}

/// Handle a tool call
pub fn handle_tool_call(
    registry: &mut TerminalRegistry,
    tool_name: &str,
    args: &Value,
) -> ToolCallResult {
    match tool_name {
        "terminal_create" => {
            let size = args.get("size").and_then(|v| v.as_str()).unwrap_or("80x24");
            let (cols, rows) = match size {
                "120x40" => (120, 40),
                "160x40" => (160, 40),
                "200x50" => (200, 50),
                _ => (80, 24),
            };
            let shell = args.get("shell").and_then(|v| v.as_str());
            match registry.create(cols, rows, shell) {
                Ok(id) => ToolCallResult::text(json!({ "id": id, "cols": cols, "rows": rows }).to_string()),
                Err(e) => ToolCallResult::error(e),
            }
        }

        "terminal_destroy" => {
            let id = match get_id(args) {
                Ok(id) => id,
                Err(e) => return e,
            };
            ToolCallResult::text(json!({ "success": registry.destroy(id) }).to_string())
        }

        "terminal_list" => {
            ToolCallResult::text(json!({ "terminals": registry.list() }).to_string())
        }

        "terminal_send_key" => {
            let key_str = match args.get("key").and_then(|v| v.as_str()) {
                Some(k) => k,
                None => return ToolCallResult::error("Missing 'key' parameter".into()),
            };
            let key = match Key::from_str(key_str) {
                Ok(k) => k,
                Err(e) => return ToolCallResult::error(e),
            };
            let instance = match get_instance(registry, args) {
                Ok(i) => i,
                Err(e) => return e,
            };
            match instance.send_key(key) {
                Ok(_) => ToolCallResult::text(json!({ "success": true }).to_string()),
                Err(e) => ToolCallResult::error(format!("Failed to send key: {}", e)),
            }
        }

        "terminal_send_keys" => {
            let instance = match get_instance(registry, args) {
                Ok(i) => i,
                Err(e) => return e,
            };
            let input_arr = match args.get("input").and_then(|v| v.as_array()) {
                Some(k) => k,
                None => return ToolCallResult::error("Missing 'input' parameter".into()),
            };
            for item in input_arr {
                if let Some(text) = item.get("text").and_then(|v| v.as_str()) {
                    if let Err(e) = instance.write_raw(text.as_bytes()) {
                        return ToolCallResult::error(format!("Failed to send text: {}", e));
                    }
                } else if let Some(key_str) = item.get("key").and_then(|v| v.as_str()) {
                    let key = match Key::from_str(key_str) {
                        Ok(k) => k,
                        Err(e) => return ToolCallResult::error(e),
                    };
                    if let Err(e) = instance.send_key_no_flush(key) {
                        return ToolCallResult::error(format!("Failed to send key: {}", e));
                    }
                } else {
                    return ToolCallResult::error("Each input element must have 'text' or 'key'".into());
                }
            }
            let _ = instance.flush_input();
            ToolCallResult::text(json!({ "success": true }).to_string())
        }

        "terminal_mouse" => {
            let action_str = match args.get("action").and_then(|v| v.as_str()) {
                Some(a) => a,
                None => return ToolCallResult::error("Missing 'action' parameter".into()),
            };
            let col = args.get("col").and_then(|v| v.as_u64()).unwrap_or(0) as u16;
            let row = args.get("row").and_then(|v| v.as_u64()).unwrap_or(0) as u16;

            let action = match action_str {
                "left_click" => MouseAction::LeftClick { col, row },
                "right_click" => MouseAction::RightClick { col, row },
                "double_click" => MouseAction::DoubleClick { col, row },
                "move" => MouseAction::MoveTo { col, row },
                "get_position" => MouseAction::GetPosition,
                "set_position" => MouseAction::SetPosition { col, row },
                _ => return ToolCallResult::error(format!("Unknown action: {}", action_str)),
            };

            let instance = match get_instance(registry, args) {
                Ok(i) => i,
                Err(e) => return e,
            };
            let result = instance.send_mouse(action);
            ToolCallResult::text(serde_json::to_string(&result).unwrap())
        }

        "terminal_read_screen" => {
            let mode = args.get("mode").and_then(|v| v.as_str()).unwrap_or("full");
            let max_lines = args
                .get("max_lines")
                .and_then(|v| v.as_u64())
                .map(|v| (v as usize).clamp(1, 200))
                .unwrap_or(50);
            let instance = match get_instance(registry, args) {
                Ok(i) => i,
                Err(e) => return e,
            };
            match mode {
                "changes" => ToolCallResult::text(instance.read_changes(max_lines)),
                _ => ToolCallResult::text(instance.read_screen()),
            }
        }

        "terminal_show_screen" => {
            let instance = match get_instance(registry, args) {
                Ok(i) => i,
                Err(e) => return e,
            };
            ToolCallResult::text(instance.show_screen())
        }

        "terminal_read_rows" => {
            let start = args.get("start_row").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
            let end = args.get("end_row").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
            let instance = match get_instance(registry, args) {
                Ok(i) => i,
                Err(e) => return e,
            };
            ToolCallResult::text(instance.read_rows(start, end))
        }

        "terminal_read_region" => {
            let col1 = args.get("col1").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
            let row1 = args.get("row1").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
            let col2 = args.get("col2").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
            let row2 = args.get("row2").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
            let instance = match get_instance(registry, args) {
                Ok(i) => i,
                Err(e) => return e,
            };
            ToolCallResult::text(instance.read_region(col1, row1, col2, row2))
        }

        "terminal_status" => {
            let last_n = args.get("last_n_lines").and_then(|v| v.as_u64()).unwrap_or(5) as usize;
            let instance = match get_instance(registry, args) {
                Ok(i) => i,
                Err(e) => return e,
            };
            ToolCallResult::text(serde_json::to_string(&instance.get_status(last_n)).unwrap())
        }

        "terminal_poll_events" => {
            let instance = match get_instance(registry, args) {
                Ok(i) => i,
                Err(e) => return e,
            };
            let events = instance.poll_events();
            ToolCallResult::text(json!({ "events": events }).to_string())
        }

        "terminal_select" => {
            let sc = args.get("start_col").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
            let sr = args.get("start_row").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
            let ec = args.get("end_col").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
            let er = args.get("end_row").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
            let instance = match get_instance(registry, args) {
                Ok(i) => i,
                Err(e) => return e,
            };
            let text = instance.select_range(sc, sr, ec, er);
            ToolCallResult::text(json!({ "selected_text": text }).to_string())
        }

        "terminal_scroll" => {
            let action = match args.get("action").and_then(|v| v.as_str()) {
                Some(a) => a,
                None => return ToolCallResult::error("Missing 'action' parameter".into()),
            };
            let search_text = args.get("text").and_then(|v| v.as_str()).unwrap_or("");
            let instance = match get_instance(registry, args) {
                Ok(i) => i,
                Err(e) => return e,
            };
            match action {
                "page_up" => {
                    let offset = instance.scroll_page_up();
                    ToolCallResult::text(json!({ "scroll_offset": offset }).to_string())
                }
                "page_down" => {
                    let offset = instance.scroll_page_down();
                    ToolCallResult::text(json!({ "scroll_offset": offset }).to_string())
                }
                "search" => {
                    if search_text.is_empty() {
                        return ToolCallResult::error("Missing 'text' for search".into());
                    }
                    let (offset, found) = instance.scroll_to_text(search_text);
                    ToolCallResult::text(json!({ "scroll_offset": offset, "found": found }).to_string())
                }
                _ => ToolCallResult::error(format!("Unknown scroll action: {}", action)),
            }
        }

        _ => ToolCallResult::error(format!("Unknown tool: {}", tool_name)),
    }
}