claude-code-acp-rs 0.1.22

Use Claude Code from any ACP client - A Rust implementation of Claude Code ACP Agent
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! NotebookEdit tool for editing Jupyter notebooks
//!
//! Edits Jupyter notebook (.ipynb) files by replacing, inserting, or deleting cells.

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::fs;

use super::base::Tool;
use crate::mcp::registry::{ToolContext, ToolResult};

/// Input parameters for NotebookEdit
#[derive(Debug, Deserialize)]
struct NotebookEditInput {
    /// The absolute path to the notebook file
    notebook_path: String,
    /// The new source for the cell
    new_source: String,
    /// The cell number (0-indexed) or cell ID to edit
    #[serde(default)]
    cell_number: Option<usize>,
    /// The cell ID to edit (alternative to cell_number)
    #[serde(default)]
    cell_id: Option<String>,
    /// The type of the cell (code or markdown)
    #[serde(default)]
    cell_type: Option<String>,
    /// The edit mode (replace, insert, delete)
    #[serde(default)]
    edit_mode: Option<String>,
}

/// Jupyter notebook structure
#[derive(Debug, Deserialize, Serialize)]
struct Notebook {
    cells: Vec<NotebookCell>,
    metadata: Value,
    nbformat: u32,
    nbformat_minor: u32,
}

/// Notebook cell structure
#[derive(Debug, Deserialize, Serialize, Clone)]
struct NotebookCell {
    cell_type: String,
    source: Value, // Can be string or array of strings
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    outputs: Vec<Value>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    execution_count: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    id: Option<String>,
    #[serde(default)]
    metadata: Value,
}

/// NotebookEdit tool for editing Jupyter notebooks
#[derive(Debug, Default)]
pub struct NotebookEditTool;

impl NotebookEditTool {
    /// Create a new NotebookEdit tool
    pub fn new() -> Self {
        Self
    }

    /// Create a new cell with the given source and type
    fn create_cell(source: &str, cell_type: &str, cell_id: Option<String>) -> NotebookCell {
        let lines: Vec<String> = source.lines().map(|l| format!("{}\n", l)).collect();
        let source_value = if lines.len() == 1 {
            Value::String(lines[0].clone())
        } else {
            Value::Array(lines.into_iter().map(Value::String).collect())
        };

        NotebookCell {
            cell_type: cell_type.to_string(),
            source: source_value,
            outputs: Vec::new(),
            execution_count: if cell_type == "code" { Some(0) } else { None },
            id: cell_id.or_else(|| Some(uuid::Uuid::new_v4().to_string())),
            metadata: json!({}),
        }
    }
}

#[async_trait]
impl Tool for NotebookEditTool {
    fn name(&self) -> &str {
        "NotebookEdit"
    }

    fn description(&self) -> &str {
        "Completely replaces the contents of a specific cell in a Jupyter notebook (.ipynb file) \
         with new source. The notebook_path parameter must be an absolute path. The cell_number \
         is 0-indexed. Use edit_mode=insert to add a new cell at the index specified by \
         cell_number. Use edit_mode=delete to delete the cell at the index specified by cell_number."
    }

    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "required": ["notebook_path", "new_source"],
            "properties": {
                "notebook_path": {
                    "type": "string",
                    "description": "The absolute path to the Jupyter notebook file to edit"
                },
                "new_source": {
                    "type": "string",
                    "description": "The new source for the cell"
                },
                "cell_number": {
                    "type": "number",
                    "description": "The 0-indexed cell number to edit"
                },
                "cell_id": {
                    "type": "string",
                    "description": "The ID of the cell to edit. When inserting a new cell, the new cell will be inserted after the cell with this ID."
                },
                "cell_type": {
                    "type": "string",
                    "enum": ["code", "markdown"],
                    "description": "The type of the cell. Required when using edit_mode=insert."
                },
                "edit_mode": {
                    "type": "string",
                    "enum": ["replace", "insert", "delete"],
                    "description": "The type of edit to make. Defaults to replace."
                }
            }
        })
    }

    async fn execute(&self, input: Value, _context: &ToolContext) -> ToolResult {
        // Parse input
        let params: NotebookEditInput = match serde_json::from_value(input) {
            Ok(p) => p,
            Err(e) => return ToolResult::error(format!("Invalid input: {}", e)),
        };

        // Validate path
        if !std::path::Path::new(&params.notebook_path)
            .extension()
            .is_some_and(|ext| ext.eq_ignore_ascii_case("ipynb"))
        {
            return ToolResult::error("File must have .ipynb extension");
        }

        // Read the existing notebook
        let content = match fs::read_to_string(&params.notebook_path) {
            Ok(c) => c,
            Err(e) => {
                return ToolResult::error(format!(
                    "Failed to read notebook '{}': {}",
                    params.notebook_path, e
                ));
            }
        };

        // Parse as notebook
        let mut notebook: Notebook = match serde_json::from_str(&content) {
            Ok(n) => n,
            Err(e) => return ToolResult::error(format!("Failed to parse notebook: {}", e)),
        };

        let edit_mode = params.edit_mode.as_deref().unwrap_or("replace");

        // Find the cell index
        let cell_index = if let Some(idx) = params.cell_number {
            idx
        } else if let Some(ref id) = params.cell_id {
            // Find cell by ID
            notebook
                .cells
                .iter()
                .position(|c| c.id.as_deref() == Some(id))
                .unwrap_or(notebook.cells.len())
        } else {
            // Default to first cell for replace, end for insert
            if edit_mode == "insert" {
                notebook.cells.len()
            } else {
                0
            }
        };

        let cell_type = params.cell_type.as_deref().unwrap_or("code");

        match edit_mode {
            "insert" => {
                // Insert a new cell
                if cell_index > notebook.cells.len() {
                    return ToolResult::error(format!(
                        "Cell index {} is out of bounds (notebook has {} cells)",
                        cell_index,
                        notebook.cells.len()
                    ));
                }

                let new_cell = Self::create_cell(&params.new_source, cell_type, None);
                notebook.cells.insert(cell_index, new_cell);

                // Write back
                let output_json = serde_json::to_string_pretty(&notebook)
                    .map_err(|e| format!("Failed to serialize notebook: {}", e));

                match output_json {
                    Ok(json) => {
                        if let Err(e) = fs::write(&params.notebook_path, json) {
                            return ToolResult::error(format!("Failed to write notebook: {}", e));
                        }
                    }
                    Err(e) => return ToolResult::error(e),
                }

                ToolResult::success(format!(
                    "Inserted new {} cell at index {} in {}",
                    cell_type, cell_index, params.notebook_path
                ))
            }
            "delete" => {
                // Delete a cell
                if cell_index >= notebook.cells.len() {
                    return ToolResult::error(format!(
                        "Cell index {} is out of bounds (notebook has {} cells)",
                        cell_index,
                        notebook.cells.len()
                    ));
                }

                let removed = notebook.cells.remove(cell_index);

                // Write back
                let output_json = serde_json::to_string_pretty(&notebook)
                    .map_err(|e| format!("Failed to serialize notebook: {}", e));

                match output_json {
                    Ok(json) => {
                        if let Err(e) = fs::write(&params.notebook_path, json) {
                            return ToolResult::error(format!("Failed to write notebook: {}", e));
                        }
                    }
                    Err(e) => return ToolResult::error(e),
                }

                ToolResult::success(format!(
                    "Deleted {} cell at index {} from {}",
                    removed.cell_type, cell_index, params.notebook_path
                ))
            }
            _ => {
                // Replace (default)
                if cell_index >= notebook.cells.len() {
                    return ToolResult::error(format!(
                        "Cell index {} is out of bounds (notebook has {} cells)",
                        cell_index,
                        notebook.cells.len()
                    ));
                }

                // Update the cell
                {
                    let cell = &mut notebook.cells[cell_index];

                    // Update cell type if specified
                    if params.cell_type.is_some() {
                        cell.cell_type = cell_type.to_string();
                    }

                    // Update source
                    let lines: Vec<String> = params
                        .new_source
                        .lines()
                        .map(|l| format!("{}\n", l))
                        .collect();
                    cell.source = if lines.len() == 1 {
                        Value::String(lines[0].clone())
                    } else {
                        Value::Array(lines.into_iter().map(Value::String).collect())
                    };

                    // Clear outputs for code cells
                    if cell.cell_type == "code" {
                        cell.outputs.clear();
                        cell.execution_count = None;
                    }
                }

                // Get cell type for the success message
                let cell_type_str = notebook.cells[cell_index].cell_type.clone();

                // Write back
                let output_json = serde_json::to_string_pretty(&notebook)
                    .map_err(|e| format!("Failed to serialize notebook: {}", e));

                match output_json {
                    Ok(json) => {
                        if let Err(e) = fs::write(&params.notebook_path, json) {
                            return ToolResult::error(format!("Failed to write notebook: {}", e));
                        }
                    }
                    Err(e) => return ToolResult::error(e),
                }

                ToolResult::success(format!(
                    "Replaced cell {} ({}) in {}",
                    cell_index, cell_type_str, params.notebook_path
                ))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write as IoWrite;
    use tempfile::TempDir;

    fn sample_notebook() -> &'static str {
        r##"{
            "cells": [
                {
                    "cell_type": "markdown",
                    "id": "cell-1",
                    "metadata": {},
                    "source": ["# Test Notebook\n"]
                },
                {
                    "cell_type": "code",
                    "execution_count": 1,
                    "id": "cell-2",
                    "metadata": {},
                    "source": ["print('Hello')"],
                    "outputs": []
                }
            ],
            "metadata": {},
            "nbformat": 4,
            "nbformat_minor": 5
        }"##
    }

    #[test]
    fn test_notebook_edit_properties() {
        let tool = NotebookEditTool::new();
        assert_eq!(tool.name(), "NotebookEdit");
        assert!(tool.description().contains("Jupyter"));
        assert!(tool.description().contains("cell"));
    }

    #[test]
    fn test_notebook_edit_input_schema() {
        let tool = NotebookEditTool::new();
        let schema = tool.input_schema();

        assert_eq!(schema["type"], "object");
        assert!(schema["properties"]["notebook_path"].is_object());
        assert!(schema["properties"]["new_source"].is_object());
        assert!(schema["properties"]["cell_number"].is_object());
        assert!(schema["properties"]["edit_mode"].is_object());
    }

    #[tokio::test]
    async fn test_notebook_edit_replace() {
        let temp_dir = TempDir::new().unwrap();
        let notebook_path = temp_dir.path().join("test.ipynb");

        let mut file = fs::File::create(&notebook_path).unwrap();
        write!(file, "{}", sample_notebook()).unwrap();

        let tool = NotebookEditTool::new();
        let context = ToolContext::new("test-session", temp_dir.path());

        let result = tool
            .execute(
                json!({
                    "notebook_path": notebook_path.to_str().unwrap(),
                    "new_source": "# Updated Title",
                    "cell_number": 0
                }),
                &context,
            )
            .await;

        assert!(!result.is_error);
        assert!(result.content.contains("Replaced"));

        // Verify the change
        let content = fs::read_to_string(&notebook_path).unwrap();
        assert!(content.contains("Updated Title"));
    }

    #[tokio::test]
    async fn test_notebook_edit_insert() {
        let temp_dir = TempDir::new().unwrap();
        let notebook_path = temp_dir.path().join("test.ipynb");

        let mut file = fs::File::create(&notebook_path).unwrap();
        write!(file, "{}", sample_notebook()).unwrap();

        let tool = NotebookEditTool::new();
        let context = ToolContext::new("test-session", temp_dir.path());

        let result = tool
            .execute(
                json!({
                    "notebook_path": notebook_path.to_str().unwrap(),
                    "new_source": "# New Cell",
                    "cell_number": 1,
                    "cell_type": "markdown",
                    "edit_mode": "insert"
                }),
                &context,
            )
            .await;

        assert!(!result.is_error);
        assert!(result.content.contains("Inserted"));

        // Verify the notebook now has 3 cells
        let content = fs::read_to_string(&notebook_path).unwrap();
        let notebook: Notebook = serde_json::from_str(&content).unwrap();
        assert_eq!(notebook.cells.len(), 3);
    }

    #[tokio::test]
    async fn test_notebook_edit_delete() {
        let temp_dir = TempDir::new().unwrap();
        let notebook_path = temp_dir.path().join("test.ipynb");

        let mut file = fs::File::create(&notebook_path).unwrap();
        write!(file, "{}", sample_notebook()).unwrap();

        let tool = NotebookEditTool::new();
        let context = ToolContext::new("test-session", temp_dir.path());

        let result = tool
            .execute(
                json!({
                    "notebook_path": notebook_path.to_str().unwrap(),
                    "new_source": "",
                    "cell_number": 0,
                    "edit_mode": "delete"
                }),
                &context,
            )
            .await;

        assert!(!result.is_error);
        assert!(result.content.contains("Deleted"));

        // Verify the notebook now has 1 cell
        let content = fs::read_to_string(&notebook_path).unwrap();
        let notebook: Notebook = serde_json::from_str(&content).unwrap();
        assert_eq!(notebook.cells.len(), 1);
    }

    #[tokio::test]
    async fn test_notebook_edit_invalid_index() {
        let temp_dir = TempDir::new().unwrap();
        let notebook_path = temp_dir.path().join("test.ipynb");

        let mut file = fs::File::create(&notebook_path).unwrap();
        write!(file, "{}", sample_notebook()).unwrap();

        let tool = NotebookEditTool::new();
        let context = ToolContext::new("test-session", temp_dir.path());

        let result = tool
            .execute(
                json!({
                    "notebook_path": notebook_path.to_str().unwrap(),
                    "new_source": "test",
                    "cell_number": 99
                }),
                &context,
            )
            .await;

        assert!(result.is_error);
        assert!(result.content.contains("out of bounds"));
    }
}