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
//! Tool registry for managing MCP tools

use std::collections::HashMap;
use std::sync::Arc;

use sacp::JrConnectionCx;
use sacp::link::AgentToClient;
use sacp::schema::{
    SessionId, SessionNotification, SessionUpdate, Terminal, ToolCallContent, ToolCallId,
    ToolCallStatus, ToolCallUpdate, ToolCallUpdateFields,
};
use serde::{Deserialize, Serialize};

use super::tools::Tool;
use crate::session::BackgroundProcessManager;
use crate::settings::PermissionChecker;
use crate::terminal::TerminalClient;

/// Tool execution result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolResult {
    /// Result status
    pub status: ToolStatus,
    /// Output content
    pub content: String,
    /// Whether this is an error
    pub is_error: bool,
    /// Additional metadata
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<serde_json::Value>,
}

impl ToolResult {
    /// Create a successful result
    pub fn success(content: impl Into<String>) -> Self {
        Self {
            status: ToolStatus::Success,
            content: content.into(),
            is_error: false,
            metadata: None,
        }
    }

    /// Create an error result
    pub fn error(message: impl Into<String>) -> Self {
        Self {
            status: ToolStatus::Error,
            content: message.into(),
            is_error: true,
            metadata: None,
        }
    }

    /// Create a result with metadata
    pub fn with_metadata(mut self, metadata: serde_json::Value) -> Self {
        self.metadata = Some(metadata);
        self
    }
}

/// Tool execution status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ToolStatus {
    /// Tool executed successfully
    Success,
    /// Tool execution failed
    Error,
    /// Tool execution was cancelled
    Cancelled,
    /// Tool is still running (for async operations)
    Running,
}

/// Tool execution context
#[derive(Debug, Clone)]
pub struct ToolContext {
    /// Session ID
    pub session_id: String,
    /// Working directory
    pub cwd: std::path::PathBuf,
    /// Whether to allow dangerous operations
    pub allow_dangerous: bool,
    /// Background process manager
    background_processes: Option<Arc<BackgroundProcessManager>>,
    /// Terminal client for executing commands via Client PTY
    terminal_client: Option<Arc<TerminalClient>>,
    /// Current tool use ID (for sending mid-execution updates)
    tool_use_id: Option<String>,
    /// Connection context for sending notifications
    connection_cx: Option<JrConnectionCx<AgentToClient>>,
    /// Permission checker for tool-level permission checks
    pub permission_checker: Option<Arc<tokio::sync::RwLock<PermissionChecker>>>,
}

impl ToolContext {
    /// Create a new tool context
    pub fn new(session_id: impl Into<String>, cwd: impl Into<std::path::PathBuf>) -> Self {
        Self {
            session_id: session_id.into(),
            cwd: cwd.into(),
            allow_dangerous: false,
            background_processes: None,
            terminal_client: None,
            tool_use_id: None,
            connection_cx: None,
            permission_checker: None,
        }
    }

    /// Set whether dangerous operations are allowed
    pub fn with_dangerous(mut self, allow: bool) -> Self {
        self.allow_dangerous = allow;
        self
    }

    /// Set the background process manager
    pub fn with_background_processes(mut self, manager: Arc<BackgroundProcessManager>) -> Self {
        self.background_processes = Some(manager);
        self
    }

    /// Set the terminal client
    pub fn with_terminal_client(mut self, client: Arc<TerminalClient>) -> Self {
        self.terminal_client = Some(client);
        self
    }

    /// Set the current tool use ID
    pub fn with_tool_use_id(mut self, id: impl Into<String>) -> Self {
        self.tool_use_id = Some(id.into());
        self
    }

    /// Set the connection context for sending notifications
    pub fn with_connection_cx(mut self, cx: JrConnectionCx<AgentToClient>) -> Self {
        self.connection_cx = Some(cx);
        self
    }

    /// Set the permission checker for tool-level permission checks
    pub fn with_permission_checker(
        mut self,
        checker: Arc<tokio::sync::RwLock<PermissionChecker>>,
    ) -> Self {
        self.permission_checker = Some(checker);
        self
    }

    /// Get the background process manager
    pub fn background_processes(&self) -> Option<&Arc<BackgroundProcessManager>> {
        self.background_processes.as_ref()
    }

    /// Get the terminal client
    ///
    /// When available, tools can use this to execute commands via the Client's PTY
    /// instead of directly spawning processes.
    pub fn terminal_client(&self) -> Option<&Arc<TerminalClient>> {
        self.terminal_client.as_ref()
    }

    /// Get the current tool use ID
    pub fn tool_use_id(&self) -> Option<&str> {
        self.tool_use_id.as_deref()
    }

    /// Send a ToolCallUpdate notification with Terminal content
    ///
    /// This is used by tools like Bash to send terminal ID immediately after
    /// creating a terminal, so the client can start showing terminal output.
    ///
    /// # Arguments
    ///
    /// * `terminal_id` - The terminal ID from CreateTerminalResponse
    /// * `status` - The tool call status (usually InProgress)
    /// * `title` - Optional title/description for the tool call
    ///
    /// # Returns
    ///
    /// `Ok(())` if notification was sent, `Err` if context doesn't have connection
    pub fn send_terminal_update(
        &self,
        terminal_id: impl Into<String>,
        status: ToolCallStatus,
        title: Option<&str>,
    ) -> Result<(), String> {
        let Some(connection_cx) = &self.connection_cx else {
            return Err("No connection context available".to_string());
        };

        let Some(tool_use_id) = &self.tool_use_id else {
            return Err("No tool use ID available".to_string());
        };

        // Build terminal content
        let terminal = Terminal::new(terminal_id.into());
        let content = vec![ToolCallContent::Terminal(terminal)];

        // Build update fields
        let mut update_fields = ToolCallUpdateFields::new().status(status).content(content);

        if let Some(title) = title {
            update_fields = update_fields.title(title);
        }

        // Build and send notification
        let tool_call_id = ToolCallId::new(tool_use_id.clone());
        let update = ToolCallUpdate::new(tool_call_id, update_fields);
        let notification = SessionNotification::new(
            SessionId::new(self.session_id.as_str()),
            SessionUpdate::ToolCallUpdate(update),
        );

        connection_cx
            .send_notification(notification)
            .map_err(|e| format!("Failed to send notification: {}", e))
    }
}

/// ACP tool prefix for compatibility with TypeScript implementation
pub const ACP_TOOL_PREFIX: &str = "mcp__acp__";

/// Tool registry for managing available tools
#[derive(Debug, Default)]
pub struct ToolRegistry {
    /// Registered tools by name
    tools: HashMap<String, Arc<dyn Tool>>,
}

impl ToolRegistry {
    /// Create a new empty registry
    pub fn new() -> Self {
        Self {
            tools: HashMap::new(),
        }
    }

    /// Register a tool
    pub fn register<T: Tool + 'static>(&mut self, tool: T) {
        let name = tool.name().to_string();
        self.tools.insert(name, Arc::new(tool));
    }

    /// Register a tool as Arc
    pub fn register_arc(&mut self, tool: Arc<dyn Tool>) {
        let name = tool.name().to_string();
        self.tools.insert(name, tool);
    }

    /// Get a tool by name, supporting ACP prefix
    ///
    /// If the tool name starts with `mcp__acp__`, it will try to find
    /// the tool with the prefix stripped.
    pub fn get(&self, name: &str) -> Option<Arc<dyn Tool>> {
        // Try direct lookup first
        if let Some(tool) = self.tools.get(name) {
            return Some(tool.clone());
        }

        // Try stripping ACP prefix
        if let Some(stripped) = name.strip_prefix(ACP_TOOL_PREFIX) {
            if let Some(tool) = self.tools.get(stripped) {
                return Some(tool.clone());
            }
        }

        None
    }

    /// Check if a tool exists, supporting ACP prefix
    pub fn contains(&self, name: &str) -> bool {
        if self.tools.contains_key(name) {
            return true;
        }

        // Try stripping ACP prefix
        if let Some(stripped) = name.strip_prefix(ACP_TOOL_PREFIX) {
            return self.tools.contains_key(stripped);
        }

        false
    }

    /// Normalize a tool name by stripping ACP prefix if present
    pub fn normalize_name(name: &str) -> &str {
        name.strip_prefix(ACP_TOOL_PREFIX).unwrap_or(name)
    }

    /// Get all tool names
    pub fn names(&self) -> Vec<&str> {
        self.tools.keys().map(String::as_str).collect()
    }

    /// Get the number of registered tools
    pub fn len(&self) -> usize {
        self.tools.len()
    }

    /// Check if the registry is empty
    pub fn is_empty(&self) -> bool {
        self.tools.is_empty()
    }

    /// Execute a tool by name
    pub async fn execute(
        &self,
        name: &str,
        input: serde_json::Value,
        context: &ToolContext,
    ) -> ToolResult {
        match self.get(name) {
            Some(tool) => tool.execute(input, context).await,
            None => ToolResult::error(format!("Tool not found: {}", name)),
        }
    }

    /// Get tool schemas for all registered tools
    pub fn schemas(&self) -> Vec<ToolSchema> {
        self.tools
            .values()
            .map(|tool| ToolSchema {
                name: tool.name().to_string(),
                description: tool.description().to_string(),
                input_schema: tool.input_schema(),
            })
            .collect()
    }
}

/// Tool schema for registration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolSchema {
    /// Tool name
    pub name: String,
    /// Tool description
    pub description: String,
    /// JSON Schema for input
    pub input_schema: serde_json::Value,
}

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

    #[test]
    fn test_tool_result_success() {
        let result = ToolResult::success("Hello, World!");
        assert_eq!(result.status, ToolStatus::Success);
        assert_eq!(result.content, "Hello, World!");
        assert!(!result.is_error);
    }

    #[test]
    fn test_tool_result_error() {
        let result = ToolResult::error("Something went wrong");
        assert_eq!(result.status, ToolStatus::Error);
        assert_eq!(result.content, "Something went wrong");
        assert!(result.is_error);
    }

    #[test]
    fn test_tool_result_with_metadata() {
        let result = ToolResult::success("data").with_metadata(json!({"lines": 10}));
        assert!(result.metadata.is_some());
    }

    #[test]
    fn test_tool_context() {
        let ctx = ToolContext::new("session-1", "/tmp").with_dangerous(true);
        assert_eq!(ctx.session_id, "session-1");
        assert_eq!(ctx.cwd, std::path::PathBuf::from("/tmp"));
        assert!(ctx.allow_dangerous);
    }

    #[test]
    fn test_registry_new() {
        let registry = ToolRegistry::new();
        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);
    }

    #[test]
    fn test_acp_prefix_normalize() {
        // Without prefix
        assert_eq!(ToolRegistry::normalize_name("Read"), "Read");
        assert_eq!(ToolRegistry::normalize_name("Bash"), "Bash");

        // With prefix
        assert_eq!(ToolRegistry::normalize_name("mcp__acp__Read"), "Read");
        assert_eq!(ToolRegistry::normalize_name("mcp__acp__Bash"), "Bash");
        assert_eq!(
            ToolRegistry::normalize_name("mcp__acp__TodoWrite"),
            "TodoWrite"
        );
    }

    #[test]
    fn test_acp_prefix_constant() {
        assert_eq!(ACP_TOOL_PREFIX, "mcp__acp__");
    }
}